Anonymous Anonymous - 2 months ago
220 0

Script to check cpu load

Bash

Bash script to see cpu load with email and threshold

#!/bin/bash

CPU_USAGE=$(top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1 | awk -F'id,' -v prefix="$prefix" '{ split($1, vs, ","); v=vs[length(vs)]; sub("%", "", v); printf "%s%.1f%%\n", prefix, 100 - v }')

DATE=$(date "+%Y-%m-%d %H:%M:")

CPU_USAGE="$CPU_USAGE"
CPU_USAGE=${CPU_USAGE:0:-1}

echo $CPU_USAGE
int=${CPU_USAGE%.*}
echo $int

if (( $int > 70 ))
then
mail -s "CPU LOAD server is high!" email@domain.com <<< "Current load: $CPU_USAGE"
fi