【旧文补档】写个脚本监控你的VPS SolusVM面板适用
2020/3/29
本文最后修改于 2263 天前,请注意文章内容的时效性。
前言
前些天我买了一台 Virmach 的大盘鸡,1 核 512MB 内存,500G 硬盘,4T 流量。这个配置做 NextCloud 还行,但 LAMP 环境比较消耗内存,而且 Virmach 的资源监控很严格,VPS 不能在 5 分钟以上的时间内达到 95-100%的使用率,在 2 小时内的平均使用率不能超过 50%,一旦违反就会直接关机,让我经常看到 NextCloud 的“同步失败”提示。所以我要写个脚本来监控 VPS,并且在 VPS 下线时自动开机。
实现
监控
使用 nmap,扫描 80 端口是否开放,未开放即为服务器下线。
rt=`nmap $ip -p 80 | grep "open" | wc -l`
if [ $rt == 1 ]; then # Do something...
fi启动服务器
SolusVM 提供了 API,所以我们用 curl 发送 GET 请求调用 API 开机就好了。
API 地址http(s)://SolusVM面板地址/api/client/command.php?key=你的API Key&hash=你的API HASH&action=操作
| Action | 备注 |
|---|---|
| reboot | 重启 |
| boot | 启动 |
| status | 状态 |
| shutdown | 关机 |
| info | 信息 |
curl $boot_url; # boot_url 是 API 开机地址。完整脚本
#!/bin/bash
ip="你服务器 IP"
svm_url="你服务器 SolusVM 面板地址"
api_key="你的 API Key"
api_hash="你的 API HASH"
boot_url=""${svm_url}"/api/client/command.php?key="${api_key}"&hash="${api_hash}"&action=boot"
while true; do
rt=`nmap $ip -p 80 | grep "open" | wc -l`
echo "Checked.";
if [ $rt == 0 ]; then
echo "Booting."
curl $boot_url;
if [ $? == 1 ]; then
echo "Error:Cannot Boot Server";
exit;
else
echo "Successful booted server.";
fi
fi
sleep 5;
done使用方法
生成 API Key 和 API HASH
打开 SolusVM,找到你的服务器,然后点击 API-Generate。
SolusVM API
记好 API Key 和 API HASH。
修改脚本
把脚本里面的内容修改,填入服务器 IP/SolusVM 后台地址/API Key/API HASH,保存。
修复权限
chmod +x 脚本文件名注册服务
新建/etc/systemd/system/服务名称.service,在里面写入这些内容。
[Unit]
Description=Monitor the status of the SolusVM panel-Based VPS and enable it when the server is offline.
[Service]
ExecStart=/bin/bash 脚本文件路径
[Install]
WantedBy=multi-user.target保存,执行以下命令载入服务。
systemctl daemon-reload
systemctl enable 服务名
systemctl start 服务名最后,你就可以享受它了!


























