远程服务器状态监控

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

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

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

安装和使用:

主服务器

注意事项:github

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

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

若是安装完成,修改配置文件,只须要编辑config.json就能够了.json

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

若是你暂时不须要启动服务,在配置文件中添加便可.bash

"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能找到的地方.编辑器

运行:this

能够经过启动脚原本运行主服务器.google

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客户机很重要,当缺乏此行时,它将不会从新链接,并且会损坏管道错误淹没日志.

若是主服务器出现故障,也不用担忧客户机,他们将继续尝试从新链接,直到他们可以再次访问主服务器。

相关文章
相关标签/搜索