我接到的任务是使用 top(htop 不是我们系统上的选项)来监控一段时间内的 CPU 使用情况。如果用户在服务器上使用超过 100% 的时间超过一个小时,则会向管理员发送电子邮件警报。
我很容易编写了下面的代码来为我提供我需要的信息。您可以在下面看到代码和结果。我面临的问题是我不知道如何使服务器名称与每个进程内联?有了它,我剩下的任务就会大大简化。
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+
答案1
尝试这个:
ssh $i top -b -n 1 | tail -n +7 | awk '$9 >= 100.0' | awk -v "host=$i" '{print host, $2, $9, $12}' >> temp1