• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

Script to detect the space % percentage used on the filesystems to send an email with this result

I need a screipt that I can detect the space on the filesystems and then send an email of this log which will be the output.

This in Solaris.
0
LindaC
Asked:
LindaC
  • 9
  • 5
1 Solution
 
ghostdog74Commented:

here's an awk script. Use nawk in Solaris

awk ' BEGIN {
    "hostname" | getline host
    recipient = "root"
    threshold = "90%"
    df = "/bin/df -k"
    while ( ( df |getline line ) > 0 ) {
      split(line,array," ")
      if (array[5] > threshold && array[5] !~ /[a-zA-Z]/) {      
        email="/usr/bin/mail "recipient
        print "Filesystem " array[1] " hits the threshold " threshold ".Value: " array[5] | email
        close(email)
      }    
    }
    close(df)    
}'
0
 
LindaCAuthor Commented:
Sorry, I have an error and I don't now how to fix it.  
Do I substitute the recipient with my email?  or only where it says email?
see the rrors below

nawk ' BEGIN {
    "hostname" | getline host
    recipient = "root"
    threshold = "50%"
    df = "/bin/df -k"
    while ( ( df |getline line ) > 0 ) {
      split(line,array," ")
      if (array[5] > threshold && array[5] !~ /[a-zA-Z]/) {
        email="/usr/bin/mail "recipient
        print "Filesystem " array[1] " hits the threshold " threshold ".Value: "
 array[5] | <my email>
        close(email)
      }
    }
    close(df)

VZWPR_USF_03:/export/home/oracle/dba/scripts> ./test.sh
nawk: syntax error at source line 10
 context is
        system " array[1] " hits the threshold " threshold ".Value: " array[5]
nawk: illegal statement at source line 10
VZWPR_USF_03:/export/home/oracle/dba/scripts>
0
 
ghostdog74Commented:
no ...don't change the "email" variable

>>>  email="/usr/bin/mail "recipient
>>>  print "Filesystem " array[1] " hits the threshold " threshold ".Value: "
 array[5] | email

the "email" you see after the pipe "| is the "email" variable , which is the mail program. the variable "recipient" is who you want to send to... you can change that. currently, i defined it as root.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LindaCAuthor Commented:
I ran the script and now id like on a "limbo state"

see;

VZWPR_USF_03:/export/home/oracle/dba/scripts> ./test.sh



It does nothing and does not exit.
0
 
LindaCAuthor Commented:
Ok, it sent an email which is good  but no subject , no title and the threshold is 50% but the disk usage percent is 7,see:

see:

oracle@prtc.net
10/12/2007 09:42 PM      
To

cc

Subject

Filesystem /dev/md/dsk/d102 hits the threshold 50%.Value: 7%

      

      
Filesystem /dev/md/dsk/d102 hits the threshold 50%.Value: 7%
0
 
ghostdog74Commented:
>>> but no subject , no title
you can construct the mail command , using -s for subject and others , pls look at the man page for mail/mailx for more options. You can try doing emailing on the shell first , make sure everything works, then code it into the script. eg
# mail -s "subject" <some recipients> <some more options>
when its done, construct the email variable: email = "/usr/bin/mail -s mysubject " .....

>>> the threshold is 50% but the disk usage percent is 7,see:

can you show the output of your df  command? array[5] should be the "Use" column.
0
 
LindaCAuthor Commented:
df -k

Filesystem            kbytes    used   avail capacity  Mounted on
/dev/md/dsk/d10      16136042 1952467 14022215    13%    /
/proc                      0       0       0     0%    /proc
mnttab                     0       0       0     0%    /etc/mnttab
fd                         0       0       0     0%    /dev/fd
swap                 24386632     104 24386528     1%    /var/run
swap                 24400040   13512 24386528     1%    /tmp
/dev/md/dsk/d30      16136042  306338 15668344     2%    /opt
/dev/md/dsk/d105     20646121 5683185 14756475    28%    /VZWPR_USF_03/ora4
/dev/md/dsk/d102     5161518  318921 4790982     7%    /VZWPR_USF_03/ora1
/dev/md/dsk/d106     7228653   83152 7073215     2%    /VZWPR_USF_03/ora5
/dev/md/dsk/d103     20855017 2816757 17829710    14%    /VZWPR_USF_03/ora2
/dev/md/dsk/d104     5161518  830045 4279858    17%    /VZWPR_USF_03/ora3
/dev/md/dsk/d50      20174761   20026 19952988     1%    /export/home
/dev/md/dsk/d100     5161518 3278736 1831167    65%    /home/app/oracle
/dev/md/dsk/d101     5161518  754551 4355352    15%    /export/home/oracle
/dev/md/dsk/d122     15490539 1476420 13859214    10%    /vzwpr_usf_03/etasprd/o
ra7
/dev/md/dsk/d124     15490539 1286892 14048742     9%    /vzwpr_usf_03/etasprd/o
ra9
/dev/md/dsk/d121     5163254  317772 4793850     7%    /vzwpr_usf_03/etasprd/ora
6
/dev/md/dsk/d123     8261365  833116 7345636    11%    /vzwpr_usf_03/etasprd/ora
8
/dev/md/dsk/d125     8261365  961604 7217148    12%    /vzwpr_usf_03/etasprd/ora
10
/dev/md/dsk/d126     9294317  301309 8900065     4%    /vzwpr_usf_03/etasprd/ora
11
VZWPR_USF_03:/export/home/oracle/dba/scripts>
0
 
LindaCAuthor Commented:
The script should be emailing only this filesystem based on the threshold of 50%

/dev/md/dsk/d100     5161518 3278736 1831167    65%    /home/app/oracle
0
 
ghostdog74Commented:
it works fine on my GNU awk. I do not have a Solaris machine with me, so can't try with nawk. I suspect its the percent sign (%).try this: I removed the % sign from threshold as well as the Use column.

awk ' BEGIN {
    "hostname" | getline host
    recipient = "root"
    threshold = "50"
    df = "/bin/df -k"
    while ( ( df |getline line ) > 0 ) {
      split(line,array," ")      
      sub("%","",array[5])
      print "array[5] is " array[5]
      if (array[5] > threshold ) {
        email="/usr/bin/mail -s mysubject"recipient
        print "Filesystem " array[1] " hits the threshold " threshold ".Value: " array[5]  | email
        close(email)
      }
    }
    close(df)
    }'
0
 
LindaCAuthor Commented:
It's sending the following :

Filesystem Filesystem hits the threshold 50.Value: capacity

Filesystem /dev/md/dsk/d102 hits the threshold 50.Value: 7

Filesystem /dev/md/dsk/d100 hits the threshold 50.Value: 65
Filesystem /dev/md/dsk/d124 hits the threshold 50.Value: 9
Filesystem /dev/md/dsk/d121 hits the threshold 50.Value: 7
0
 
ghostdog74Commented:
ok change these:
>>> threshold = "50"

to

threshold=50

and

>>> if (array[5] > threshold ) {

to

if ( int(array[5] )> threshold ) {

and see how it goes.
0
 
LindaCAuthor Commented:
Very nice, and compact script.

This is the result:

Filesystem /dev/md/dsk/d100 hits the threshold 50.Value: 65
0
 
LindaCAuthor Commented:
In case someone wants to copy, this is final script for Solaris:

nawk ' BEGIN {
    "hostname" | getline host
    recipient = "<put your email here>"
    threshold = 50
    df = "/bin/df -k"
    while ( ( df |getline line ) > 0 ) {
      split(line,array," ")
      sub("%","",array[5])
      print "array[5] is " array[5]
     if ( int(array[5] )> threshold ) {
        email="/usr/bin/mail "recipient
        print "Filesystem " array[1] " hits the threshold " threshold ".Value: "
 array[5]  | email
        close(email)
      }
    }
    close(df)
    }'
~
0
 
TintinCommented:
I'd do it like (tested on a Solaris box):

#!/bin/sh
threshold=50    # Percent free threshold to email on.

df -Fufs -k|awk '/%/ {print $5,$6}'|sed 's/%//' | while read percent fs
do
  [ $percent -ge $threshold  ] && echo "$fs freespace is less than $threshold" | mailx -s "$fs freespace alert" user
done


0
 
LindaCAuthor Commented:
I tested and it worked too!

Thank you.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 9
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now