LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   need script to query tape drive (https://www.linuxquestions.org/questions/linux-software-2/need-script-to-query-tape-drive-207914/)

Finlay 07-21-2004 11:42 AM

need script to query tape drive
 
I want to run a script each day to query a tape drive to see if there is a tape in it.

If there isn't a tape in it i want an email sent to notify me. (users aren't putting the tapes in everyday.

If i run this when there isn't a tape i get:
mt -f /dev/st0 status
SCSI 2 tape drive:
File number=-1, block number=-1, partition=0.
Tape block size 0 bytes. Density code 0x0 (default).
Soft error count since last status=0
General status bits on (50000):
DR_OPEN IM_REP_EN


If i run this when there is a tape in the drive i get:
mt -f /dev/st0 status
SCSI 2 tape drive:
File number=0, block number=0, partition=0.
Tape block size 512 bytes. Density code 0x48 (Quantum SDLT220).
Soft error count since last status=0
General status bits on (41010000):
BOT ONLINE IM_REP_EN

If i run this with a tape in i get nothing back:
mt -f /dev/st0 rewind

If i run this when there isn't a tape in the drive i get:
mt -f /dev/st0 rewind
/dev/st0: No medium found
mt: The device is offline (not powered on, no tape ?).

Is there a way to take any of these outputs and use them to send an email when there isn't a tape in?

cmfarley19 07-21-2004 01:39 PM

I wouldn't do the
Code:

mt -f /dev/st0 rewind
becuase it will rewind the tape if it is in there (unless that's what you want to do.
Is that what you want to do? (That will make things easier if it is).

SheldonPlankton 07-21-2004 01:52 PM

Sounds like you could easily write a Perl script to do this ...
Code:

...
my $mtCmd = "/bin/mt -f /dev/st0 status";
open( MT, "|$mtCmd" ) or die "Can't do $mtCmd:$!\n";

# if last line of output is  DR_OPEN IM_REP_EN send me email
...

You could even write a shell script to do this.
Code:

grep DR_OPEN `/bin/mt -f /dev/st0 status|tail -1`

if [ $? != 0 ]
then
    mail -S "No Tape" ....
fi


Finlay 07-21-2004 04:27 PM

since i don't know anything about perl the shell script would be best, but i get the following error when i run:

grep DR_OPEN `/bin/mt -f /dev/st0 status|tail -1`

if [ $? != 0 ]
then
mail -s 'No Tape' finlay@finlay.homelinux.org
fi



grep: BOT: No such file or directory
grep: ONLINE: No such file or directory
grep: IM_REP_EN: No such file or directory

then it halts on the mail part and i have to ctrl-c it twice

mossy 07-21-2004 04:37 PM



<EDIT>


Oops - nevermind I see you already got past that - double check your locations and run as root?

</EDIT>

Finlay 07-21-2004 04:42 PM

ok here is what i got:
#!/bin/bash
grep DR_OPEN `/bin/mt -f /dev/st0 status|tail -1`

if [ $? != 0 ]
then
mail -s 'No Tape' finlay@finlay.homelinux.org
fi

and this is what i get:
grep: BOT: No such file or directory
grep: ONLINE: No such file or directory
grep: IM_REP_EN: No such file or directory

mossy 07-21-2004 04:47 PM

where's it getting the reference for ' BOT ONLINE IM_REP_EN' in that script?

Finlay 07-21-2004 05:04 PM

BOT ONLINE IM_REP_EN is the result of
mt -f /dev/st0 status|tail -1
when there is a tape in the drive

SheldonPlankton 07-21-2004 10:34 PM

Oops! I meant

/bin/mt -f /dev/st0 status|tail -1| grep DR_OPEN

I think ... my point was you can write a shell script to do what you want.
I didn't bother testing any code I posted. Sorry :D

Heck you could probably even do something like this ...

/bin/mt -f /dev/st0 status > /dev/null 2>&1
if [ $? != 0 ]
then
mail
fi


Finlay 07-22-2004 01:57 PM

Ok this is what i have now:
#!/bin/bash
/bin/mt -f /dev/st0 status|tail -1| grep DR_OPEN
if [ $? != 0 ]
then
mail -s 'No Tape' finlay@finlay.homelinux.org
fi

The problem is it is trying to send mail when the tape is in, not when it is out.
And it is still halting at the mail command

mossy 07-22-2004 04:03 PM

replace
Code:

if [ $? != 0 ]
with
Code:

if [ $? = 0 ]
and giver a whirl

Finlay 07-22-2004 05:02 PM

thanks mossy, the script works to the point of the mail command.
It keeps wanting user intervention to actually send the email.
Do you know of any switches that will send the email, or if you can > it to an attachment?

SheldonPlankton 07-22-2004 05:09 PM

Use a here doc ...
Code:

$ mail -s "test test" dogface <<EOT
> Hi
> there how are things
> this is call a "Here document" in shell scripting
> EOT
bash-2.05b$ mail
Mail version 8.1 6/6/93.  Type ? for help.
"/var/spool/mail/dogface": 1 message 1 new
>N  1 dogface@localh  Thu Jul 22 15:04  18/794  "test test"
& 1
Message 1:
From dogface@localhost.localdomain  Thu Jul 22 15:04:30 2004
Date: Thu, 22 Jul 2004 15:04:29 -0700
From: Dogface Monkey <dogface@localhost.localdomain>
To: dogface@localhost.localdomain
Subject: test test
 
Hi
there how are things
this is call a "Here document" in shell scripting
 
& q
Saved 1 message in mbox

To do the above in your script your script should have lines in it that look like this ...
Code:

mail -s "test test" Finlay <<EOT
Hi there,
how are things?

this is call a "Here document" in shell scripting
EOT


Finlay 07-22-2004 07:02 PM

that worked to send to a local user.
when i tried to send it to an external address it got rejected because it came from root@localhost.localdomain

is there a way to set a from or reply to address?

mossy 07-22-2004 07:43 PM

try adding the -u parameter to mail:
Code:

mail -su uid_finley "test test" Finlay <<EOT
not sure if this is correct usage it may be:
Code:

mail -s "test test" -u uid_finley Finlay <<EOT
you may also need to setup smtp server settings etc - but I am not sure.


All times are GMT -5. The time now is 05:23 PM.