I am trying to measure the cpu usage of my ubuntu machine and if the cpu usage is more than 60%, then I need to find out the process who has the highest cpu usage and then send out an email saying CPU usage is more than 60% and with the process name which has the highest cpu usage.
When I do top this is what I see.
Cpu0 : 20.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu1 : 34.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu2 : 17.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu3 : 20.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
I came up with the below script which only finds out the cpu load but not the cpu usage. How do I achieve above thing?
#!/bin/bash
top -bn1 | grep load | awk '{printf "CPU Load: %.2f\n", $(NF-2)}'
To send an email, I use below command and it works for me -
echo "Body" | mailx -r "[email protected]" -s "SUBJECT" "[email protected]"