Linux

如何使伺服器名稱與頂級輸出內聯?

  • August 6, 2019

我的任務是使用 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+

試試這個:

ssh $i top -b -n 1 | tail -n +7 | awk '$9 >= 100.0' | awk -v "host=$i" '{print host, $2, $9, $12}' >> temp1

引用自:https://unix.stackexchange.com/questions/534203