分享一个服务器监控统计在线小程序
分类:linux日期:2016-12-03 - 18:39:33作者:老谢
git地址:http://git.oschina.net/supercell/service_count
具体安装说明请参考git地址,原抓取服务器信息的sh脚本,free使用-h的参数,现在的free好像都没有-h参数了,改成-m参数即可,如果爱折腾,可以把cpu和硬盘信息也抓取出来加上去,代码贴出来:
一、cron.sh(抓取服务器信息,在被控端部署)
POST_URL=127.0.0.1/service.php TOKEN=666 LOAD=`cat /proc/loadavg|awk '{print $1,$2,$3}'| tr ' ' ','` #ram=`cat /proc/meminfo | grep MemTotal | awk '{print $2,$3}'` RAM=`free -m | grep Mem | awk '{print $2}'` USED=`free -m | grep Mem | awk '{print $3}'` UPTIME=`uptime | awk '{print $3,$4,$5}' | sed 's/,$//'| tr ' ' ','` curl -s -d "key="$TOKEN"&load="$LOAD"&ram="$RAM"&used="$USED"&uptime="$UPTIME"" $POST_URL > /dev/null |
上传好cron.sh以后请添加到crontab,每分钟执行一次:
*/1 * * * * /你的路径/cron.sh |
二、service.php(监控端,收集记录受控端的信息,需填写数据库信息)
<?php define('USER', "root"); define('PASS', "passwd"); define('DB', "dbname"); define('KEY', "666"); define('PA', "");//如果设置了密码,只有输入密码后才能看到 $sql=mysql_connect("127.0.0.1",USER,PASS); $db=mysql_select_db(DB,$sql); $time=time(); if($_POST['key']&&(!$_POST['pa'])){ if(KEY==$_POST['key']){ $ip=trim($_SERVER['REMOTE_ADDR']); $ram=$_POST['ram']; $used=$_POST['used']; $uptime=$_POST['uptime']; $load=$_POST['load']; $valid=mysql_query("SELECT * FROM vps WHERE ip='".$ip."'"); $is_valid=mysql_num_rows($valid); if(!$is_valid){ mysql_query("INSERT INTO vps(ip,ram,used,uptime,aload,atime) VALUES('".$ip."','".$ram."','".$used."','".$uptime."','".$load."','".$time."') "); }else{ mysql_query("UPDATE vps SET ram='".$ram."',used='".$used."',uptime='".$uptime."',aload='".$load."',atime='".$time."' WHERE ip='".$ip."'"); } }else{ echo "error"; } }else{ ?> <!DOCTYPE html> <html> <head> <title>探针集合</title> <head> <meta charset="utf-8"> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css"> <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css"> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script> <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script> <style type="text/css"> .container{ margin-top: 50px; } </style> </head> </head> <body> <?php session_start(); if(strlen(PA)){ if($_SESSION['pa']!=PA){ if($_POST['pa']){ if(($_POST['pa']!=PA)){ header("Location:".$_SERVER['SCRIPT_NAME']); }else{ $_SESSION['pa']=PA; } }else{ ?> <div class="container"> <div class="col-md-4 col-offset-4"> <div class="row"> <h2>密码</h2> </div> <form action="" method="post"> <div class="input-group"> <input type="password" name="pa" class="form-control"> <span class="input-group-btn"> <input type="submit" name="提交" class="btn btn-success"> </span> </div> </form> </div> </div> <?php return false; } } } $query=mysql_query("SELECT * FROM vps order by id asc"); ?> <div class="container"> <table class="table table-striped"> <tr> <th>ID</th> <th>IP</th> <th>Location</th> <th>Memory</th> <th>USED</th> <th>UPTIME</th> <th>LOAD</th> <th>STATUS</th> </tr> <?php while($value=mysql_fetch_array($query)){ echo "<tr>"; echo "<td>".$value['id']."</td>"; echo "<td>".$value['ip']."</td>"; ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)'); $loc=@file_get_contents("http://freeapi.ipip.net/".$value['ip']); $location=json_decode($loc,true); echo "<td>".$location[0].$location[1].$location[2].$location[3].$location[4]."</td>"; echo "<td class=\"text-success\">".$value['ram']."</td>"; echo "<td class=\"text-danger\">".$value['used']."</td>"; echo "<td>".$value['uptime']."</td>"; echo "<td>".$value['aload']."</td>"; if ($time>$value['atime']+100) {//100s无计时,则不在线 echo "<td><span class=\"label label-danger\">OFFLINE</span><span class=\"text-danger\">Last:".date("Y-m-d H:i:s",$value['atime'])."</span></td>"; }else{ echo "<td><span class=\"label label-success\">ONLINE</span></td>"; } echo "</tr>"; } ?> </table> </div> </body> <?php } ?> </html> |
三、创建并导入数据库
CREATE TABLE IF NOT EXISTS `vps` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `ip` VARCHAR(128) NOT NULL, `ram` VARCHAR(128) NOT NULL, `used` VARCHAR(128) NOT NULL, `uptime` VARCHAR(128) NOT NULL, `aload` VARCHAR(128) NOT NULL, `atime` INT(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=16 ; |
咦?谢总不用之前自己写的统计数据库轮子了?
这个是监控服务器在线状态,挺好用的,部署也方便
看你说的这么好,那我就试试啦
又是个技术高手,博客能写这么多年,崇拜啊