I am monitoring CPU usage and based on that I want to display message. I have written following script:
#!/bin/bash
typeset -F limit1=1.0 # Declare variables as floats.
typeset -F limit2=2.0
echo "-------------------------------------------"
echo "Server_Name CPU(%)"
echo "-------------------------------------------"
for server in `more /opt/scripts/server-list1.txt`
do
scpu1=$( ssh $server cat /proc/stat | awk '/cpu/{printf("%.1f\n"),
($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
echo "$server $scpu1"
done | column -t
echo "-------------------------------------------"
for server in `more /opt/scripts/server-list2.txt`
do
scpu2=$( ssh $server cat /proc/stat | awk '/cpu/{printf("%.2f\n"),
($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
echo "$server $scpu2"
done | column -t
echo "-------------------------------------------"
if [[ $scpu1 -ge $limit2 && $scpu2 -le $limit1 ]] ; then
echo " CPU utilization of node1 is above 2% and node2 is below 1%"
elif [[ $scpu1 -le $limit1 && $scpu2 -ge $limit2 ]] ; then
echo " CPU utilization of node2 is above 2% and node1 is below 1%"
elif [[ $scpu1 -ge $limit2 && $scpu2 -ge $limit2 ]] ; then
echo "CPU utilization of node1 and node2 is above 2% "
else
echo " Nothing to do"
fi
Though my node1 CPU is less than 2% and node2 CPU is greater than 1% it is giving output as: CPU utilization of node1 is above 2% and node2 is below 1%. Irrespective of condition it is giving the same output as above.