I've been given the task to use top(htop is not an option on our system) to monitor CPU usage over time. If a user is on a server and using over 100% for longer than an hour, an email alert will be sent out to an admin.
Easily enough I written the code below to give me the information I need. You can see the code and results below. The issue I'm facing is I can't figure out how to get the server name inline with each process? Having that would greatly simplify the rest of my task.
1 #!/bin/bash
2
3 echo "Script was run at $(date)" >> temp1
4
5 HOSTS=("list of server names")
6
7 for i in ${HOSTS[@]} ; do
8 echo $i >> temp1
9 ssh $i top -b -n 1 | tail -n +7 | awk '$9 >= 100.0' | awk '{print $2, $9, $12}' >> temp1
10 done
11
Currently this is what my script is returning. Only thing I want differently is to have the server name follow each individual process.
serverName1
user 187.1 MATLAB
serverName2
user 144.4 plasma-de+
user 144.4 plasma-de+
user 141.7 plasma-de+
user 138.9 mate-sett+
serverName3
user 100.0 plasma-de+
user 100.0 plasma-de+