RTOP – 通過 SSH 監控遠程主機

rtop 是一個簡單的、無代理的遠程服務器監控工具,可通過 SSH 運行。它不需要在遠程機器上安裝任何代理軟件。

它的工作原理是建立一個 SSH 會話,並在遠程服務器上運行命令來收集系統信息,例如 CPU、磁盤、內存、網絡。它每隔幾秒鐘就會刷新一次信息,就像 top 命令一樣。

系統環境

Centos8

安裝 golang

檢查一下系統是否安裝 golang:

[root@server1 ~]# rpm -qa|grep golang

發現沒有安裝,下面使用yum list golang命令查看一下遠程倉庫裏面是否有改安裝包:

[root@server1 ~]# yum list golang
Repository AppStream is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Available Packages
golang.x86_64                               1.14.12-1.module_el8.3.0+605+410c5674                               AppStream

[root@server1 ~]# yum -y install golang

爲了使用 Go 語言,請在用戶主目錄下創建一個工作空間。Go 語言會將所有文件保存在這裏:

[root@server1 ~]# mkdir -p ~/go_proj/bin

設置 GOPATH 和 GOBIN 環境變量。GOPATH 是項目工作區,將下面兩行內容添加到~/.bashrc配置文件中:

[root@server1 ~]# vim ~/.bashrc 

export GOPATH=$HOME/go_proj
export GOBIN=$GOPATH/bin

安裝 rtop

運行go get命令來構建 rtop。rtop 二進制文件自動保存在$GOPATH/bin下。運行 go get 之前,確保系統安裝了git工具:

[root@server1 ~]# yum -y install git
[root@server1 ~]# go get github.com/rapidloop/rtop
package git:/github.com/rapidloop/rtop: git:/github.com/rapidloop/rtop: invalid import path: malformed import path "git:/github.com/rapidloop/rtop": invalid char ':'
[root@server1 ~]# go get github.com/rapidloop/rtop
package golang.org/x/crypto/ssh: unrecognized import path "golang.org/x/crypto/ssh": https fetch: Get "https://golang.org/x/crypto/ssh?go-get=1": dial tcp 216.239.37.1:443: connect: connection refused
package golang.org/x/crypto/ssh/agent: unrecognized import path "golang.org/x/crypto/ssh/agent": https fetch: Get "https://golang.org/x/crypto/ssh/agent?go-get=1": dial tcp 216.239.37.1:443: connect: connection refused
package golang.org/x/crypto/ssh/terminal: unrecognized import path "golang.org/x/crypto/ssh/terminal": https fetch: Get "https://golang.org/x/crypto/ssh/terminal?go-get=1": dial tcp 216.239.37.1:443: connect: connection refused

運行 go get 之後,發現不能下載依賴包。

按照下面操作,創建目錄,並手動 git 下載相關依賴:

[root@server1 ~]# mkdir -p $GOPATH/src/golang.org/x/
[root@server1 x]# git clone https://github.com/golang/crypto.git
[root@server1 x]# git clone https://github.com/golang/term.git
[root@server1 x]# git clone https://github.com/golang/sys.git

下面再次運行go get就可以了:

[root@server1 x]# go get github.com/rapidloop/rtop

下面可以看到rtop可以正常運行了。

如何使用 rtop

rtop 二進制文件存在於 $GOPATH/bin 中,因此只需運行 $GOBIN/rtop就可以。只需添加遠程主機信息,然後使用 rtop 命令進行監控。刷新間隔默認爲 5 秒鐘。

[root@server1 ~]# $GOBIN/rtop root@192.168.43.131

手動設置刷新間隔以更好地監控。這裏設置了 2 秒刷新間隔。

[root@server1 ~]# $GOBIN/rtop root@192.168.43.131 2

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/7uPyp2IxNnTwNWJw9Bf_1g