天天看點

運維自動化之使用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,如需轉載請自行聯系原作者