-2

So if you run this command locally on the box I'm trying to monitor:

df -Ph | awk '0+$5 >= 70 {print}'

It returns all file systems above a 70% threshold.

In my shell script, I'm trying:

set -x

SPACE70=$(df -Ph | awk '0+$5 >= 70 {print}')

if [ "$SPACE70" != "" ]; then

    msg_text="WARNING $HOST has a File system above threshold $SPACE70 "
    result=$STATE_WARNING

fi

And Space70 is not storing anything even though File Systems are over 70... Works locally but not remotely. For context I plan on running this on Nagios and remotely logging into the box via SSH.

Any thoughts? I tried another edit but it didn't help:

COMMAND="df -Ph "
SPACE_REPORT=`/usr/local/nagios/sshpass-1.05/sshpass  -p$PASS ssh -q -o StrictHostKeyChecking=no -o ConnectTimeout=310 $USER@$HOST "$COMMAND"`
# Set default to OK
result=$STATE_OK
SPACE85=0
SPACE95=0
#
set -x
#AWK FOR WARNING:
SPACE85=$($SPACE_REPORT | awk '0+$5 >= 70 {print}')
if [ "$SPACE85" != "" ]; then
msg_text="WARNING $HOST has a File System above threshold $SPACE85 "
result=$STATE_WARNING
fi
JHarder51
  • 11
  • 1
  • 1
  • 4
  • seems to work, i am able to see SPACE70 storing(checked it locally on my box). Please double check. – User9102d82 Feb 09 '17 at 06:30
  • I updated my post... sorry I'm trying to get this running remote on a shell script via sshpass 1.05 on our Nagios box. – JHarder51 Feb 09 '17 at 20:11

3 Answers3

1

You are just missing an echo and quotation marks in the 10th line of your code, the one that calls the awk:

#AWK FOR WARNING:
SPACE85=$($SPACE_REPORT | awk '0+$5 >= 70 {print}')

Should be:

#AWK FOR WARNING:
SPACE85=$(echo "$SPACE_REPORT" | awk '0+$5 >= 70 {print}')

I tested it and it works, remotely.

Hope that helps!

Ramiro
  • 948
  • 2
  • 9
  • 22
0

If I may suggest,

SPACE70=$(df -Ph | awk '0+$5 >= 70 {N++} END {print 0 + N}')

if [ $SPACE70 -gt  0 ]

I don't see anything particularly wrong with your first version, other than it's doing more work than necessary.

When executing remotely, I would suspect PATH issues. Because there's no interactive shell invoked, it's easy to assume initialization that in fact is not happening. Also, I don't know anything about sshpass, but are you sure you're seeing messages on standard error? If not, that might explain the mystery.

James K. Lowden
  • 7,574
  • 1
  • 16
  • 31
0

I saw your command and understood the issue. Now I am posting my version of the solution.

so as I understand that you would like to check the disk space on the remote host or multiple hosts, and if the disk space is above 70%, GENERATE alert. Correct me if my understanding is not correct.

Before we see the script, lets see the output:

%_Host@User:/home/Gaurava/study> ./fscheck.sh

==========[192.168.246.132 STARTS]==========
[ **ALERT** (192.168.246.132) has FS above THRESHOLD ---vmhgfs-fuse              224G  182G   42G  82% /mnt/hgfs--- ]
[ **ALERT** (192.168.246.132) has FS above THRESHOLD ---/dev/sda1                497M  376M  122M  76% /boot--- ]
==========[192.168.246.132 ENDS]==========

==========[192.168.246.137 STARTS]==========
[ **ALERT** (192.168.246.137) has FS above THRESHOLD ---.host:/         224G  182G   42G  82% /mnt/hgfs--- ]
==========[192.168.246.137 ENDS]==========

%_Host@User:/home/Gaurava/study>

In above output, the script logs to 2 remote hosts, one by one and then executes command to check disk space and it found 3 file systems above the specified limit. So it generate ALERT.

The script:

#!/bin/bash

# Define your command, host/s and user/s.
command='df -Ph'
host1=192.168.246.132
host2=192.168.246.137
user=gaurav

# Main loop STARTS
for h in $host1 $host2
do

        # This line can be removed.
        echo "==========[$h STARTS]=========="

        # Here you can replace this with your sshpass command.
        # I am feeding the command output to a while loop to read
        # the output line by line, for each of the host/s.

        ssh $user@$h "$command" | while read line

        do
                # Now we check, if the disk space output contains anything
                # which matches value greater than 70%, thats it! and
                # generate alert.

                if [[ $line =~ .*7[1-9]%.* || $line =~ .*[89][0-9]%.* ]]
                then
                        # If above 'if' statement is TRUE, It generates ALERT
                        # in the below format. '$line' is the variable holding
                        # info about the file system breaching the threshold.

                        echo "[ **ALERT** ($h) has FS above THRESHOLD ---$line--- ]"

                elif [[ $line =~ .*100% ]]
                then
                        echo "[ **ALERT** ($h) FS reached 100% ---$line--- ]"
                fi

        done
        # This line can be removed.
        echo "==========[$h ENDS]==========" ; echo
done
# Main loop ENDS.

My script is not using the exact same logic which you were trying to use but it is works on a similar logic, and utilizes a couple of loops, which simplify our problem and provide greater control

I hope this helps to resolve your problem. Please let me know if it was any good!

Edit: Added an else part to the if loop. Although when I checked, it is ignoring any FS if it has already reached 100%, but still, it poses no harm to add a warning/notification. It is a good idea indeed.

Edit2: updated the if loop with one more condition and added echo for 100%. I realized later that earlier loop wasn't matching numbers 71,81,91.

User9102d82
  • 1,172
  • 9
  • 19
  • I like it, my first issue was just part of the problem... Which I forgot an echo!!! I have file systems I need to ignore now as well like /hadoop for instance. They have hadoop1, 2, 3, 4, etc and if they go over threshold I need to ingore them and set up logic to easily add exceptions in the future. You solution doesn't see 100% full though does it? I'm just trying to understand it all. I'm an old C++ coder after 10 years of operations i'm getting thrown into Shell! Maybe an else in order for just 100% would work here? – JHarder51 Feb 10 '17 at 19:08
  • Hi, for the 100% thing, actually the script already ignores it as we are only matching 70%-99%, but not 100%. So inadvertently it was already present :), but thanks for pointing it out. with the else loop, we now cover all conditions (i am guessing..). Check it out. Btw.. you have been c++ for 10 years? wow.. i am just getting into it.. its nasty.. hehe.. I am more into shell and perl. We should exchange my friend.. what say.. Cheers, Gaurav – User9102d82 Feb 10 '17 at 20:58