Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Linux Filesystem monitor script

Posted on 2013-12-03
7
474 Views
Last Modified: 2013-12-04
I have a Filesystem monitor script which is not quite working I know there is a problem with the syntax but dont know where can someone help me fix this, or If anyone has a better script can you please share it with me

Here is the script

#!/bin/ksh
df -H | grep -vE '^Filesystem|dev|prd|tmpfs|cdrom|home' | while read output;
do
  echo $output
  value=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  fs=$(echo $output | awk '{ print $2 }' )
  if [ $value -ge 85 ]; then
    echo "Running out of space \"$fs ($value%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $value%" a6179p@att.net
  fi
done

Open in new window



Here is the output when I execute

# ./fs.sh
2.6G 1.9G 520M 79% /
./fs.sh: line 7: [: 2.6G: integer expression expected
976M 206M 720M 23% /opt
./fs.sh: line 7: [: 976M: integer expression expected
455M 37M 395M 9% /opt/netiq
./fs.sh: line 7: [: 455M: integer expression expected
1.6G 314M 1.2G 22% /var
./fs.sh: line 7: [: 1.6G: integer expression expected
976M 19M 907M 2% /tmp
./fs.sh: line 7: [: 976M: integer expression expected
520M 130M 364M 27% /opt/splunkforwarder
./fs.sh: line 7: [: 520M: integer expression expected


and my # df -H
Filesystem             Size   Used  Avail Use% Mounted on
/dev/mapper/rootvg-rootvol
                       2.6G   1.9G   520M  79% /
/dev/mapper/rootvg-optvol
                       976M   206M   720M  23% /opt
/dev/mapper/rootvg-netiq
                       455M    37M   395M   9% /opt/netiq
/dev/mapper/rootvg-varvol
                       1.6G   314M   1.2G  22% /var
/dev/mapper/rootvg-tmpvol
                       976M    19M   907M   2% /tmp
/dev/sda1              194M    28M   156M  16% /boot
tmpfs                  4.2G      0   4.2G   0% /dev/shm
/dev/mapper/rootvg-splunkforwarder
                       520M   130M   364M  27% /opt/splunkforwarder
prddmv03-20.corp.erac.com:/home02/e200mp
                       731G   610G   121G  84% /home/e200mp
prddmv03-20.corp.erac.com:/home02/e515kr
                       731G   610G   121G  84% /home/e515kr
0
Comment
Question by:aanya247
  • 5
7 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 39694712
try changing awk '{ print $1}' to  awk '{ print $4}'
0
 

Author Comment

by:aanya247
ID: 39694764
Sorry I gave you the wrong script... it is

#!/bin/ksh
df -H | grep -vE '^Filesystem|prd|tmpfs|cdrom|home' | awk '{ print $5 " " $1 }'| while read output;
do
  echo $output
  value=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  fs=$(echo $output | awk '{ print $2 }' )
  if [ $value -ge 85 ]; then
    echo "Running out of space \"$fs ($value%)\" on $(hostname) as on $(date)" |
     mail -s "Alert: Almost out of disk space $value%" a6179p@att.net
  fi
done

Open in new window




So I know where the problem is...When I run

# df -H | grep -vE '^Filesystem|prd|tmpfs|cdrom|home'


The output is displayed in 2 lines for example for root it is displayed in 2 lines like

/dev/mapper/rootvg-rootvol
                       2.6G   2.3G   117M  96% /


Instead my script will work if the out put is on the same line like


/dev/mapper/rootvg-rootvol         2.6G   2.3G   117M  96% /
/dev/mapper/rootvg-optvol         976M   206M   720M  23% /opt
/dev/mapper/rootvg-netiq           455M    37M   395M   9% /opt/netiq
/dev/mapper/rootvg-varvol           1.6G   605M   881M  41% /var
/dev/mapper/rootvg-tmpvol         976M    19M   907M   2% /tmp
/dev/sda1                                      194M    28M   156M  16% /boot
/dev/mapper/rootvg-splunkforwarder    520M   130M   364M  27% /opt/splunkforwarder


How do i format my script to get this desired output
0
 
LVL 19

Assisted Solution

by:simon3270
simon3270 earned 400 total points
ID: 39694964
I'd pipe the df -h output through something like:
df -H | awk 'NF==1{printf("%s",$1);next}{print}' | grep ...

Open in new window

with the rest of the line unchanged.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:aanya247
ID: 39695633
Thank you, now looks like the output is right now, thanks all
0
 

Author Comment

by:aanya247
ID: 39697603
I've requested that this question be closed as follows:

Accepted answer: 0 points for aanya247's comment #a39694764
Assisted answer: 100 points for ozo's comment #a39694712
Assisted answer: 400 points for simon3270's comment #a39694964

for the following reason:

script works perfectly fine
0
 

Author Comment

by:aanya247
ID: 39697602
simon3270 syntax helped me fix the issue with the script, ozo helped me with a typo
0
 

Author Closing Comment

by:aanya247
ID: 39697604
simon3270 syntax helped me fix the issue with the script, ozo helped me with a typo
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linux redhat 7.2 10 89
Linux mount of Windows Shared Now Fails 8 75
Linux FTP Error 5 33
The better OS Architecture 13 64
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question