Raghu
Raghu

Reputation: 51

How to set evaluation time before sending an mail alert

#!/bin/bash

 CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //')
 CPU=$( printf "%.0f" $CPU )

if [ "$CPU" -lt 20 ]
 then
    echo "CPU usage is high!" | sendmail [email protected]
fi

I need to evaluate the condition for certain time of 5min before sending an alert. ex: cpu space < 20 condition should evaluate for 5min if state is same then i need to send a mail.
like prometheus evaluates the expression with for clause,it will check that the alert continues to be active during each evaluation for 5 minutes before firing the alert

Upvotes: 0

Views: 33

Answers (1)

Try the following:

#!/bin/bash

CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //')
CPU=$( printf "%.0f" $CPU )
timer=0

while [ "$CPU" -lt 20 ]
do
    # WAIT 1 MINUTE
    sleep 60

    # INCREMENT TIMER
    ((timer++))

    # CHECK TIMER
    if [ $timer = 5 ]
    then
        echo "CPU usage is high!" | sendmail [email protected]
        break
    fi
done

Upvotes: 0

Related Questions