zabbix告警事件归档与提取篇中对zabbix的事件表设置了触发器转存机制,存到了另外一个库的表中。这里针对该表的内容写了几个统计页面,暂未成系统性的东西,后面有时间可以理成一个简单的查询系统,或者可以直接集成在zabbix前端页面上,做一个查询按钮 。

一、磁盘查询

查询页面效果如下:

zabbix-disk-query
zabbix-disk-query

页面比较较单,未使用前端框架,自写了一点前端内容。代码如下:

 1<!-- code from www.361way.com -->
 2<html xmlns="http://www.w3.org/1999/xhtml">
 3<head>
 4<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5<title>zabbix未处理理事件</title>
 6<style>
 7table {
 8    border-collapse: collapse;
 9    width: 70%;
10}
11th, td {
12    text-align: left;
13    padding: 8px;
14}
15tr:nth-child(even){background-color: #f2f2f2}
16th {
17    background-color: #4CAF50;
18    color: white;
19}
20</style>
21</head>
22<body>
23<center>
24<h2>zabbix磁盘未清理主机</h2>
25<table>
26<div style="overflow-x:auto;">
27<tr><th>主机名</th><th>告警内容</th><th>告警级别</th><th>是否恢复</th><th>告警时间</th></body></tr>
28<?php
29$link=mysql_connect('10.211.137.173','username','password')or die("数据库连接失败");
30//连接数据库
31mysql_select_db('databasename',$link);//选择数据库
32mysql_query("set names utf8");//设置编码格式
33$q="select host,description,priority,value,time from newevent where value=1
34and  id not in (select id from newevent where value=0)
35and  description like '%磁盘%'";//设置查询指令
36$result=mysql_query($q);//执行查询
37while($row=mysql_fetch_assoc($result))//将result结果集中查询结果取出一条
38{
39 echo"<tr><td>".$row["reportip"]."</td><td>".$row["alarmname"]."</td><td>".$row["alarmlevel"]."</td><td>".$row["alarmstat"]."</td><td>".$row["alarmtime"]."</td></tr>\n";
40  }
41?>
42</table>
43</div>
44<center>
45</body>
46<br/>
47<br/>
48<div>注:告警级别5代表严重,2为警告;是否恢复,值为1,代表未恢复!</div>
49<div id="copyright"> 版权所有 © 2015-2017 运维之路</div>
50</html>

二、CPU告警次数统计

zabbix-cpu-count
zabbix-cpu-count

由于cpu指标比较多,这里为了尽快实现功能,写了四个单页面。对应的都是修改下sql 语句就行了,这里使用了前端框架layui 做一了个简单的导航栏。

主页面:

<pre data-language="HTML">```markup

<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>

**其中一个页面内容如下:**

```php
<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语名如下:

1$q="select host,triggerid,description,value,time from newevent
2where description like '%磁盘%' and value=1
3and eventid in (select max(eventid) from newevent  group by triggerid )";