在zabbix告警事件归档与提取篇中对zabbix的事件表设置了触发器转存机制,存到了另外一个库的表中。这里针对该表的内容写了几个统计页面,暂未成系统性的东西,后面有时间可以理成一个简单的查询系统,或者可以直接集成在zabbix前端页面上,做一个查询按钮 。
一、磁盘查询
查询页面效果如下:
页面比较较单,未使用前端框架,自写了一点前端内容。代码如下:
<!-- code from www.361way.com --> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>zabbix未处理理事件</title> <style> table { border-collapse: collapse; width: 70%; } th, td { text-align: left; padding: 8px; } tr:nth-child(even){background-color: #f2f2f2} th { background-color: #4CAF50; color: white; } </style> </head> <body> <center> <h2>zabbix磁盘未清理主机</h2> <table> <div style="overflow-x:auto;"> <tr><th>主机名</th><th>告警内容</th><th>告警级别</th><th>是否恢复</th><th>告警时间</th></body></tr> <?php $link=mysql_connect('10.211.137.173','username','password')or die("数据库连接失败"); //连接数据库 mysql_select_db('databasename',$link);//选择数据库 mysql_query("set names utf8");//设置编码格式 $q="select host,description,priority,value,time from newevent where value=1 and id not in (select id from newevent where value=0) and description like '%磁盘%'";//设置查询指令 $result=mysql_query($q);//执行查询 while($row=mysql_fetch_assoc($result))//将result结果集中查询结果取出一条 { echo"<tr><td>".$row["reportip"]."</td><td>".$row["alarmname"]."</td><td>".$row["alarmlevel"]."</td><td>".$row["alarmstat"]."</td><td>".$row["alarmtime"]."</td></tr>\n"; } ?> </table> </div> <center> </body> <br/> <br/> <div>注:告警级别5代表严重,2为警告;是否恢复,值为1,代表未恢复!</div> <div id="copyright"> 版权所有 © 2015-2017 运维之路</div> </html>
二、CPU告警次数统计
由于cpu指标比较多,这里为了尽快实现功能,写了四个单页面。对应的都是修改下sql 语句就行了,这里使用了前端框架layui 做一了个简单的导航栏。
主页面:
<!DOCTYPE html> <html> <!-- code from www.361way.com --> <head> <meta charset="UTF-8"> <title>Layui</title> <meta name="renderer" content="webkit"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" /> <link rel="stylesheet" href="css/begtable.css" /> </head> <body> <ul class="layui-nav"> <li class="layui-nav-item"><a href="cpu_load.php">Load Average</a></li> <li class="layui-nav-item"><a href="cpu_context.php">上下文件统计</a></li> <li class="layui-nav-item"><a href="cpu_iowait.php">CPU IOwait</a></li> <li class="layui-nav-item"><a href="cpu_idle.php">CPU idle不足</a></li> </ul> <p class="layui-elem-quote">cpu指标监控项,具体指标可以通过tab导航标签切换。</p> <script src="//res.layui.com/layui/build/layui.js" charset="utf-8"></script> <script> layui.use('element', function(){ var element = layui.element(); //导航的hover效果、二级菜单等功能,需要依赖element模块 //监听导航点击 element.on('nav(demo)', function(elem){ //console.log(elem) layer.msg(elem.text()); }); }); </script> </body> </html>
其中一个页面内容如下:
<html> <!-- code from www.361way.com --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>zabbix未处理理事件</title> <link rel="stylesheet" href="./plugins/layui/css/layui.css" media="all" /> </head> <body> <ul class="layui-nav"> <li class="layui-nav-item"><a href="cpu_load.php">Load Average</a></li> <li class="layui-nav-item"><a href="cpu_context.php">上下文件统计</a></li> <li class="layui-nav-item"><a href="cpu_iowait.php">CPU IOwait</a></li> <li class="layui-nav-item"><a href="cpu_idle.php">CPU idle不足</a></li> </ul> <center> <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"> <legend><b>最近一周load average告警次数统计</b></legend> </fieldset> <table class="layui-table width:50%;" style="table-layout:automatic;width:700px"> <tr><th>主机名</th><th>次数统计</th></tr> <?php require_once('conn.php'); $q="select DISTINCT host,count(reportip) count from newevent where time > DATE_SUB(CURDATE(), INTERVAL 1 WEEK) and value=0 and description like '%CPU核心数%' group by host order by count desc"; $result=mysql_query($q);//执行查询 while($row=mysql_fetch_assoc($result))//将result结果集中查询结果取出一条 { echo"<tr><td>".$row["reportip"]."</td><td>".$row["count"]."</td></tr>\n"; } mysql_close($conn); ?> </table> <center> <script src="//res.layui.com/layui/build/layui.js" charset="utf-8"></script> <script> layui.use('element', function(){ var element = layui.element(); //导航的hover效果、二级菜单等功能,需要依赖element模块 //监听导航点击 element.on('nav(demo)', function(elem){ //console.log(elem) layer.msg(elem.text()); }); }); </script> </body> <br/> <br/> <div id="copyright"> 版权所有 © 2015-2017 运维之路</div> </html>
conn连接功能单独分出来了,通过requice_once引入。
三、写在最后
这个功能页写的时候本来想通过python + flask + Jinja2 实现,不过感觉写web功能,php 还是有天然的优势,凭借着几年前自学的一点php知识很容易实现了几个页面。不过这里也有一些可以优化的地方:
1、mysql 连接这里使用的是mysql_connect 这种老的方式,后面可以通过 new mysqli 来实现;
2、这里每个页面都是通过单页面实现的,统计查询使用的sql 是基本模式相同的,只不过里面的某些字段发生了变化,可以通过写一个固定的函数,往里面传入固定的参数,实现不同的返回 。
3、这里先写了两个功能,一个是当前告警类的,一个是事件统计类的。后面可以做下页面布局,左边栏加一个锤直导航,通过选择不同的item进入不同的选项查看,当然再后面也可以再加上时间选择组件之类的。这都是后话了。
后记:
后续使用了几天,发现上面使用的SQL语名不准确,新更换的SQL语名如下:
$q="select host,triggerid,description,value,time from newevent where description like '%磁盘%' and value=1 and eventid in (select max(eventid) from newevent group by triggerid )";