目的:实现对远端服务器的状态进行监控

首先贴贴出github地址:https://github.com/BotoX/ServerStatus/

通过主服务器监控远端的服务器的状态.

服务器监控温度怎么看 服务器的监控_服务器

安装和使用:

主服务器

注意事项:

切换到非特权用户或创建一个。端口35601需要空闲并转发/打开才能运行服务器。



git clone https://github.com/BotoX/ServerStatus.git
cd ServerStatus/server
make
./sergate



如果安装完成,修改配置文件,只需要编辑config.json就可以了.



{"servers":
    [
        {
            "username": "s01",
            "name": "Mainserver 1",
            "type": "Dedicated Server",
            "host": "GenericServerHost123",
            "location": "Austria",
            "password": "some-hard-to-guess-copy-paste-password"
        },
    ]
}



如果你暂时不需要启动服务,在配置文件中添加即可.



"disabled": true



相关的命令参数的查看,只需要在运行二进制文件时天剑-h参数.



-h, --help            Show this help message and exit
    -v, --verbose         Verbose output
    -c, --config=<str>    Config file to use
    -d, --web-dir=<str>   Location of the web directory
    -b, --bind=<str>      Bind to address
    -p, --port=<int>      Listen on port



然后:不要忘记复制或者移动web目录(ServerStatus/status)到你的webserver能找到的地方.

运行:

可以通过启动脚本来运行主服务器.

Debian和其他基于init.d的系统.

复制the init.d script to /etc/init.d



cp ServerStatus/other/sergate.initd /etc/init.d/sergate



使用编辑器添加下列行



# Change this according to your setup!
DAEMON_PATH="/usr/local/share/ServerStatus/server"
WEB_PATH="/var/www/botox.bz/status"
DAEMON="sergate"
OPTS="-d $WEB_PATH"
RUNAS="www-data"



以root方式运行



service sergate start



设置自启动



update-rc.d sergate defaults



Arch Linux and other systemd based systems

复制systemd service to /etc/systemd/system



cp ServerStatus/other/sergate.service /etc/systemd/system/sergate.service



使用编辑器添加下列行



WorkingDirectory=/usr/local/share/ServerStatus/server
User=botox.bz
Group=http
ExecStart=/usr/local/share/ServerStatus/server/sergate -d /home/botox.bz/status



启动:



systemctl start sergate



设置自启动



systemctl enable sergate



注意:确保运行主服务器的用户可以访问Web路径并可以写入$web_path/json。

客户端

三种实现方式

  • Python2 [preferred]
  • Python2-psutil
  • Bash

三种方式都非常容易实现.

Python2和Bash客户端只支持GNU/Linux.Python2-psutil支持很多的操作系统,比如BSD or Window.

自动安装

自动安装,你需要获得证书,可以这样运行



wget https://raw.github.com/BotoX/ServerStatus/master/other/client-setup.sh
bash client-setup.sh



常规的安装

再启动文件中添加下列行

Python Client



SERVER = "status.botox.bz"
PORT = 35601
USER = "s01"
PASSWORD = "some-hard-to-guess-copy-paste-password"
INTERVAL = 1 # Update interval



Python2-psutil Client



SERVER = "status.botox.bz"
PORT = 35601
USER = "s01"
PASSWORD = "some-hard-to-guess-copy-paste-password"
INTERVAL = 1 # Update interval



客户端的操作系统必须支持psutil project.

Windows系统

要么使用预编译的自包含版本,要么安装python2和psutil。预编译版本已使用py2exe生成。如果您想自己构建安装脚本,则会包含它。安装完所有依赖项后,

运行“python setup.py py2exe”

Windows配置的工作方式不同,使预编译版本和自包含版本都可以。根据您的设置编辑client.cfg文件


[client]
SERVER = status.botox.bz
PORT = 35601
USER = s01
PASSWORD = some-hard-to-guess-copy-paste-password
INTERVAL = 1



Bash客户端



SERVER="status.botox.bz"
PORT=35601
USER="s01"
PASSWORD="some-hard-to-guess-copy-paste-password"
INTERVAL=1 # Update interval



运行

确保你的客户端已经配置正确,运行.可以通过启动脚本或cronjob使其自动启动,也可以手动启动.



nohup ./client.py &> /dev/null &



Debian 和其他基于init.d的系统

添加下列文件到/etc/rc.local



su -l $USERNAME -c "/path/to/client.py &> /dev/null &"



Arch Linux 和其他基于systems的系统

创建文件/etc/systemd/system/serverstatus.service来创建新的systemd服务:



[Unit]
Description=ServerStatus Client
After=network.target

[Service]
Type=simple
IgnoreSIGPIPE=no
User=$USERNAME
ExecStart=/path/to/client.py

[Install]
WantedBy=multi-user.target



注意:

ignoresigpipe=no对于bash客户机很重要,当缺少此行时,它将不会重新连接,而且会损坏管道错误淹没日志.

如果主服务器出现故障,也不用担心客户机,他们将继续尝试重新连接,直到他们能够再次访问主服务器。