PHP Server Monitor 在外網要監控內網有些困難 ...只能用 agent 自動會報方式處裡
windows 可寫成 agent 服務或登入背景執行 , linux 放在 crontab 或 背景執行
upstream-nz.rhcloud.com 就是server , up.php 就是接收用的 ....
windows 背景執行的 VBS upstream.vbs ==> :
Do
Dim o
Set o = CreateObject("Microsoft.XMLDOM")
o.async = False
o.load "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.19&id=ULcgRFofDCZsKo3jAKds"
result = o.XML
WScript.Sleep 20*1000
Loop
windows 服務執行的 (我放於 c:\VBS 下面 ) :
upstream.vbs ==>
Do
Dim o
Set o = CreateObject("Microsoft.XMLDOM")
o.async = False
o.load "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.19&id=ULcgRFofDCZsKo3jAKds"
result = o.XML
WScript.Sleep 20*1000
Loop
---------------------------------------------------------------------
install.bat ==>
@echo off
rem Install Upstream Machine Agent Service using srvany.exe from the Windows Resource Kit
echo Installing AppDynamics Machine Agent Service into the Service Manager
sc create "Upstream Machine Agent Service" binPath= c:\vbs\srvany.exe start=auto depend=TCPIP
echo done.
echo Setting Registry settings for Upstream Machine Agent Service
regedit /S c:\vbs\MachineAgent.reg
echo done.
echo Starting Service
sc start "Upstream Machine Agent Service"
pause
--------------------------------------------------------------------
MachineAgent.reg ==>
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Upstream Machine Agent Service\Parameters]
"Application"="C:\\windows\\system32\\WScript.exe"
"AppParameters"="C:\\vbs\\upstream.vbs"
"AppDirectory"="C:\\vbs\\"
---------------------------------------------------------------------
srvany.exe ==> 參考 https://support.microsoft.com/zh-tw/kb/137890 ....請自行到官網下載 ......
---------------------------------------------------------------------
V2 ....後來用 golang 改寫 ....這樣比較快 參考一下 ! ....還是用 C 寫更快 ....哈
用 golang 當然 linux 下編譯一下也可用 .........
package main
import (
"net/http"
"time"
)
func main() {
for {
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
client.Get("http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.19&id=ULcgRFofDCZsKo3jAKds")
time.Sleep(20 * time.Second)
}
}
-------------------------------------------------------------------
linux 我來監控 qnap nas 是否關機 .....
[~] # cat /etc/config/crontab
..............
0-59/1 * * * * /share/homes/chiochou/uplive.sh >/dev/null 2>&1
---------------------------------------------------------------------
[~] # more /share/homes/chiochou/uplive.sh
#!/bin/sh
curl -m 60 "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.25&id=WkzqQMa4uZ2nwwkKGXbA"
sleep 20
curl -m 60 "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.25&id=WkzqQMa4uZ2nwwkKGXbA"
[~] #
ESXI 是否關機
more /var/spool/cron/crontabs/root
#min hour day mon dow command
.........
*/1 * * * * /vmfs/volumes/nfs/uplive-201.sh
-----------------------------------------------------------------------
~ # cat /vmfs/volumes/nfs/uplive-201.sh
wget -O /dev/null "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.20&id=ujevkQ6hWrqSb"
sleep 20
wget -O /dev/null "http://upstream-nz.rhcloud.com/up.php?Info=192.168.0.20&id=ujevkQ6hWrqSb"
----------------------------------------------------------------------
Server 端的程式 (有改db時間加 12 小時 ....哈)
[upstream-nz.rhcloud.com ]\> cat app-root/repo/www/up.php
<?php
$dbhost = "127.8.180.10";
$dbname = "upstream";
$dbuser = "adminas";
$dbpswd = "LSU_g233lfb";
try {
$db = new PDO("mysql:host=".$dbhost.";dbname=".$dbname,$dbuser,$dbpswd);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND,'SET NAMES UTF8');
} catch (PDOException $e) {
throw new PDOException("Error : " .$e->getMessage());
}
$id_key=$_GET['id'];
$sql = "SELECT id,name FROM psm_id where id_key='".$id_key."'";
$sth = $db->prepare($sql);
$sth->execute();
$row_count = $sth->rowCount();
if ($row_count == "") {
echo " Error !!";
} else {
$sth->setFetchMode(PDO::FETCH_ASSOC);
$result = $sth->fetchAll();
// print_r ($result);
$id = $result[0]['id'];
$name = $result[0]['name'];
$llabel= $name." ".$_GET['Info'];
$sql = "UPDATE psm_servers SET error = '',label = '".$llabel."' ,rtime=1 ,warning_threshold_counter = 0,status = true ,last_online = NOW() + INTERVAL 12 hour WHERE server_id = ".$id ;
$sth2 = $db->prepare($sql);
$sth2 ->execute();
echo "OK !!";
}
unset ($db);
---------------------------------------------------------------------------
要建立資料
其中 ID 是 由 psm_servers 內的 server_id 取得
網頁 show 出大概就是