天天看点

运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(五)

4、监控视图

主要是通过shell脚本进行收集mysql的数据,然后同gnuplot软件进行数据视图化,然后php加载这些图片到web里显示,用到的php文件为view.php

day.php、month.php、year.php、gansu_day_view.php、gansu_day_web.php(各省安装服务器类型有各自的,比如甘肃当天的备用的程序为gansu_day_beiyong.php)、gansu_month_view.php 、gansu_month_web.php、gansu_year_view.php 、gansu_year_web.php.

详细的php程序描述

A、add.php

<?php 

include("conn.php");  

 include("head.php");  

 include("head_device.php");  

 ?> 

主要有3个表,分别的作用为:

conn.php是连接数据库的

head.php是php的head文件

head_device.php是设备的head文件

B、add_device.php

 if($_POST['submit']){  

  $sql="insert into device values ('','$_POST[name]','$_POST[ip]','$_POST[mac]','$_POST[type]','$_POST[jifang_name]','$_POST[raw_name]','$_POST[raw_location]','$_POST[assets_code]','$_POST[state]','$_POST[price]')";  

  mysql_query($sql);  

  echo "<script language=\"javascript\">alert('添加成功');history.go(-1)</script>";  

 }  

##点击完提交后,就进行添加并返回添加成功按键  

?> 

<SCRIPT language=javascript> 

function CheckPost()  

{  

    if (myform.name.value=="")  

    {  

        alert("请填写设备名称");  

        myform.name.focus();  

        return false;  

    }  

    if (myform.ip.value.length<5)  

        alert("设备ip不能少于5个字符");  

        myform.ip.focus();  

    if (myform.mac.value.length<23)  

        alert("设备mac不能少于23位");  

        myform.mac.focus();  

    if (myform.type.value=="")  

        alert("请填写设备类型");  

        myform.type.focus();  

        if (myform.jifang_name.value=="")  

        alert("请填写机房名称");  

        myform.jifang_name.focus();  

        if (myform.raw_name.value=="")  

        alert("请填写机架名称");  

        myform.raw_name.focus();  

        if (myform.raw_location.value=="")  

        alert("请填写设备位置");  

        myform.raw_location.focus();  

        if (myform.assets_code.value=="")  

        alert("请填写财产编号");  

        myform.assets_code.focus();  

        if (myform.state.value=="")  

        alert("请填写设备状态");  

        myform.state.focus();  

        if (myform.price.value=="")  

        alert("请填写设备价格");  

        myform.price.focus();  

}  

</SCRIPT> 

##使用javascript方式进行约束各自的值  

 <form action="add_device.php" method="post" name="myform" onsubmit="return CheckPost();"> 

 设备名称:<input type="text" size="10" name="name" /><br> 

 设备I P: <input type="text" size="10" name="ip" /><br/> 

 设备MAC: <input type="text" size="10" name="mac" /><br/> 

 设备类型:<input type="text" size="10" name="type" /><br/> 

  机房名称:<input type="text" size="10" name="jifang_name" /><br/> 

  机架名称:<input type="text" size="10" name="raw_name" /><br/> 

  机架位置:<input type="text" size="10" name="raw_location" /><br/> 

 财产编号:<input type="text" size="10" name="assets_code" /><br/> 

 设备状态:<input type="text" size="10"name="state" /><br/> 

 设备价格:<input type="text" size="10" name="price" /><br/> 

 <input type="submit" name="submit" value="新增"/> 

 <input name="reset" type="reset" value="重置" /> 

 </form> 

##使用html表单  

C、conn.php

$conn = @ mysql_connect("1.1.1.1", "root", "****") or die("数据库链接错误");  

mysql_select_db("monitor", $conn);  

mysql_query("set names 'GBK'"); //使用GBK中文编码;  

##连接的数据库ip、用户名与密码,如果连接不成功,就返回数据库连接错误  

#function htmtocode($content) {  

#   $content = str_replace("\n", "<br>", str_replace(" ", " ", $content));  

#   return $content;  

#}  

function htmtocode($status) {  

str_replace("working", "test", $status);  

    return $status;  

#function  

//$content=str_replace("'","‘",$content);  

 //htmlspecialchars();  

D、gansu.php

<?php  

include("head.php");  

include("province.php");  

?>  

 <font size="3" color="red"><B>应用服务</B></font>  

 <table border="1" cellspacing="0" >  

    <tr>  

    <th width="55">编号</th>  

    <th width="55">省份</th>  

    <th width="160">被监控服务器</th>  

    <th width="100">IP</th>  

    <th width="100">监控服务器</th>  

    <th width="130">监控服务</th>  

    <th width="70">当前状态</th>  

    <th width="100">监控时间</th>  

  </tr>  

  </table>  

<?  

 $SQL="SELECT * FROM `gansu_service` order by date desc limit 0,13";  

  $query=mysql_query($SQL);  

  while($row=mysql_fetch_array($query)){  

<table border="1" cellspacing="0" cellpadding="0">  

  <tr>  

    <td width="55"><?=$row[id]?> </td>  

    <td width="55"><?=$row[province]?></td>  

    <td width="160"><?=$row[server]?> </td>  

    <td width="100"><?=$row[ip]?></td>  

    <td width="100"><?=$row[monitor_server]?></td>  

    <td width="130"><?=$row[service]?></td>  

    <td width="70"><?=$row[status]?></td>  

    <td width="100"><?=$row[date]?></td>  

</table>  

  }  

<font size="3" color="red"><B>硬盘使用率</B></font>  

    <th width="70">阀值</th>  

    <th width="90">当期分区</th>  

    <th width="70">当前值</th>  

 $SQL="SELECT * FROM `gansu_disk` order by date desc limit 0,5";  

  <table border="1" cellspacing="0" cellpadding="0">  

    <td width="70"><?=$row[alert]?></td>  

    <td width="90"><?=$row[partition]?></td>  

    <td width="70"><?=$row[value]?></td>  

<font size="3" color="red"><B>CPU使用率</B></font>  

 $SQL="SELECT * FROM `gansu_cpu` order by date desc limit 0,5";  

<font size="3" color="red"><B>硬件错误信息</B></font>  

 $SQL="SELECT * FROM `gansu_hardware` order by date desc limit 0,5";  

<font size="3" color="red"><B>I/O使用率</B></font>  

 $SQL="SELECT * FROM `gansu_io` order by date desc limit 0,5";  

<font size="3" color="red"><B>15分钟内的负载</B></font>  

 $SQL="SELECT * FROM `gansu_load` order by date desc limit 0,5";  

<font size="3" color="red"><B>内存使用率</B></font>  

 $SQL="SELECT * FROM `gansu_memory` order by date desc limit 0,10";  

<font size="3" color="red"><B>日志错误信息</B></font>  

 $SQL="SELECT * FROM `gansu_message` order by date desc limit 0,5";  

 <table border="1" cellspacing="0" cellpadding="0">  

<font size="3" color="red"><B>用户登录数</B></font>  

 $SQL="SELECT * FROM `gansu_user` order by date desc limit 0,5";  

此页面主要是各省展示自己的监控服务与资源详情的页面。

主要是通过从mysql里获取各自表的数据,插入到我设定好的表单里,在web里显示,这些表单的格式都是通过css进行控制的。

E、css.css

td {  

    line-height: 16pt;  

    font-size: 10pt;  

    font-family: "Verdana", "Arial", "Helvetica", "sans-serif";  

a:link {  

    text-decoration: none;  

    color: #000000;  

a:visited {  

a:hover {  

    text-decoration: underline;  

    color: #FF3333;  

body {  

    line-height: 13pt;  

    background-color: #ECF5FF;  

.border {  

    border: 1px solid #1D5892;  

textarea {  

    font-size: 8pt;  

    border: 1px solid #999999;  

    padding: 5px;  

select {  

    padding: 1px;  

    font-family: "Tahoma";  

.a1:link {  

    color: #FFFFFF;  

.a1:visited {  

.a1:hover {  

    color: #FF9900;  

.font14 {  

    font-size: 14px;  

form {  

    margin: 0px;  

    padding: 0px;  

.alpha {  

    filter: Alpha(Opacity=20);  

.filearea {  

    font-size: 9pt;  

.textdrow {  

    color:#666666;  

    filter: DropShadow(Color=white, OffX=1, OffY=1, Positive=1);  

.font18 {  

    font-size: 19px;  

.p {  

    text-indent: 24px;  

.font16 {  

    font-size: 16px;  

.border2 {  

    border: 1px solid #D5E4F4;  

.xborder {  

    border: 2px dotted #EBF5FE;  

table {  

width: 1100px;  

text-align:center;  

主要是观察最后2项,是控制表的宽度与表里单元格的内容显示方法。

F、gansu_warn.php

include ("conn.php");  

include ("head.php");  

include("warn_province.php");  

<? 

/*检测服务报警  

 *  

 */  

 $SQL="select * from gansu_service where 1=1 AND DATE_FORMAT(gansu_service.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='downing'";  

 $query=mysql_query($SQL);  

$result=mysql_fetch_row($query);  

if($result==''){  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现服务报警</B></font><br>';  

else {  

echo '<font size="3" color="red"><B>甘肃省份今天的服务报警为:</B></font><br>';  

include ("service_warning.php");  

/*检测磁盘报警  

 $SQL="select * from gansu_disk where 1=1 AND DATE_FORMAT(gansu_disk.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现磁盘报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的磁盘报警为:</B></font><br>';  

include ("disk_warning.php");  

/*检测CPU报警  

 $SQL="select * from gansu_cpu where 1=1 AND DATE_FORMAT(gansu_cpu.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现CPU报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的CPU报警为:</B></font><br>';  

include ("resource_warning.php");  

/*检测硬件信息错误报警  

 $SQL="select * from gansu_hardware where 1=1 AND DATE_FORMAT(gansu_hardware.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现硬件信息错误(hardware)报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的硬件信息错误(hardware)报警为:</B></font><br>';  

/*检测I/O报警  

 $SQL="select * from gansu_io where 1=1 AND DATE_FORMAT(gansu_io.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现I/O报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的I/O报警为:</B></font><br>';  

/*检测load报警  

 $SQL="select * from gansu_load where 1=1 AND DATE_FORMAT(gansu_load.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal';";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现负载(load)报警</B></font><br>';  

else{  

echo '<font size="3" color="red"><B>甘肃省份今天的负载(load)报警为:</B></font><br>';  

/*检测内存报警  

 $SQL="select * from gansu_memory where 1=1 AND DATE_FORMAT(gansu_memory.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现内存(memory)报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的内存(memory)报警为:</B></font><br>';  

/*检测日志信息报警  

 $SQL="select * from gansu_message where 1=1 AND DATE_FORMAT(gansu_message.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现日志信息(message)报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的日志信息(message)报警为:</B></font><br>';  

/*检测用户报警  

 $SQL="select * from gansu_user where 1=1 AND DATE_FORMAT(gansu_user.date,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') AND status='abnormal'";  

echo '<font size="3" color="red"><B>甘肃省份今天没有发现用户登录数(user)报警</B></font><br>';  

echo '<font size="3" color="red"><B>甘肃省份今天的用户登录数(user)报警为:</B></font><br>';  

此页面主要是展现甘肃省当日的服务与资源的报警。

下一篇文章地址:

运维自动化之使用PHP+MYSQL+SHELL打造私有监控系统(六)

<a href="http://dl528888.blog.51cto.com/2382721/1035297">http://dl528888.blog.51cto.com/2382721/1035297</a>

 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1035252,如需转载请自行联系原作者