0

I am trying to get the status whether the file is modified or not in Linux. I am using this piece of code and it works well, but it has a problem that it exit after the first "if" statement.

It does not look for elif statement. I am trying to get "warning" if the file is not modified from the last 2 minutes, and it should give me "critical" if it still does not modified for 4 minutes. it always show warning or OK but does not take into account the elif statement. the code i am using

#!/bin/bash
# How to execute ./sensor.sh tem_sensor
HOUR=$(date +%H)
MIN=$(date +%M)

# Directory where they are sensor directorys
DIR=/home/robbin/Desktop/sensor_collection/
# Name of selected sensor
SENSOR=$1
# Name of sensor's directoris
SENSORS=(sensor1)

# Loop in every folder
for i in ${SENSORS[@]}
do
        # We only want the specified sensor so we will skip until we found it
        if [[ $SENSOR != $i ]]; then continue ; fi
        # You take the hour and minute value from last file
        LHOUR=$(ls -lrt $DIR/$i| tail -n1 | awk '{ print $8}' | awk -F ':' '{ print $1}')
        LMIN=$(ls -lrt $DIR/$i | tail -n1 | awk '{ print $8}' | awk -F ':' '{ print $2}')
        # We calculate the diferences
        let FHOUR=$(( HOUR - LHOUR ))
        let FMIN=$(( MIN - LMIN ))

        # if the diference is greater than 2
        if ([ $FMIN -gt 02 ] && [ $FMIN -lt 04 ]); then
            echo "WARNING - More than 2 minutes without recieving data"
            exit 1 # We put warning!
        # Else if it is not more than 2
        # We check if we have an hour of diference!
        elif [[ $FMIN -gt 04 ]]; then
            echo "CRITICAL - More than 4 minutes without recieving data"
            exit 2 # We put Red alert!
        else
            echo "OK - We recieve data"
            exit 0 # Green alert if we dont have problems
        fi
done
echo "UNKNOW - Sensor not found"
exit 3

If someone help me to solve this problem. cox it seems that it has no problem and it should work expectedly. I am new to bash scripts. I would be grateful. .

5
  • Have to be careful between 8am and 10am: 08 and 09 are invalid octal numbers: HOUR=15; LHOUR=08; FHOUR=$((HOUR-LHOUR)) throws an error. You might want to explicitly use base 10: FHOUR=$(( 10#$HOUR - 10#$LHOUR )). Also date +%k (or +%_H) returns the hour without the leading zero. Aug 9, 2017 at 15:20
  • Similarly, use %_M instead of %M to avoid invalid octal minutes 08 and 09, 18 and 19, etc Aug 9, 2017 at 15:25
  • is there are reason you're not using the stock check_file_age plugin?
    – Keith
    Aug 9, 2017 at 15:51
  • @Keith I don't know about check_file_age. I am new to bash and programming
    – robbin
    Aug 9, 2017 at 16:19
  • @glennjackman I will try your suggestion.
    – robbin
    Aug 9, 2017 at 16:19

2 Answers 2

2

You can re-arrange your if-else statements as follows:

if [[ $FMIN -gt 04 ]]; then
    echo "CRITICAL - More than 4 minutes without recieving data"
    exit 2 # We put Red alert!
elif [[ $FMIN -gt 02 ]]; then
    echo "WARNING - More than 2 minutes without recieving data"
    exit 1 # We put warning!
else
    echo "OK - We recieve data"
    exit 0 # Green alert if we dont have problems
fi

This is simpler to read and should do what you want.

4
  • i will give it a try and will come back soon
    – robbin
    Aug 9, 2017 at 14:55
  • it still does not enter into critical state. .don't know why. it shows only warning.
    – robbin
    Aug 9, 2017 at 15:06
  • are you sure FMIN got to 5?
    – chicks
    Aug 9, 2017 at 16:07
  • @chicks yes i am sure. it goes until 10
    – robbin
    Aug 9, 2017 at 16:16
1

You need to swap the conditions in the if and elif clauses. The first condition matches both cases, so the second one is never getting checked.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .