zabbix企业应用之定时获取监控数据做报表

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

最近某项目突然提出一个新需求,需要每周五14点,获取他们监控项目每天20-24点监控平均数据,以小时为单位的,输出文件是excel的,要求以每天为单位单独一个sheet,汇总邮件转给业务。

他们主要是做业务使用量报表,每周周报使用,虽然需求困难,但作为运维也得解决,下面是邮件的效果图。

wKiom1g7pDPDRY4MAAFLMmvvl7c034.png-wh_50

可以看到邮件标题是带有项目名称与时间,收集人是业务与我。

下面是excel的格式

wKiom1g7pVSzzFrqAAWzlpq65v4427.png-wh_50

每天一个sheet,获取这些项目自己每天20-24点的监控平均数据,以小时为单位。

主要是使用sql查看上面的监控数据,并通过python把数据汇总到excel里并使用crontab定时发送。

重要:我这里对默认的linux监控模板,添加了一个监控主机cpu数量的item,key名称是system.cpu.num,请大家也加入到模板里,否则导出的excel里是空的。

wKiom1mAmaDRxGOhAANbOKyx-Rg636.png

下面是脚本内容,大家改改自己需要获取项目组与发送邮箱信息就行(我是使用163邮箱)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
#coding=utf-8
#Author: Denglei
#Email: dl528888@gmail.com
#QQ: 244979152
import  MySQLdb
import  datetime
import  xlwt
import  sys
from email.mime.multipart  import  MIMEMultipart
from email.mime.base  import  MIMEBase
from email.mime.text  import  MIMEText
 
from email.utils  import  COMMASPACE,formatdate
from email  import  encoders
 
import  os
 
def send_mail(server, fro, to, subject, text, files=[]):
     assert  type (server) == dict
     assert  type (to) == list
     assert  type (files) == list
 
     msg = MIMEMultipart()
     msg[ 'From' ] = fro
     msg[ 'Subject' ] = subject
     msg[ 'To' ] = COMMASPACE. join (to)  #COMMASPACE==', '
     msg[ 'Date' ] = formatdate(localtime=True)
     msg.attach(MIMEText(text))
 
     for  in  files:
         part = MIMEBase( 'application' 'octet-stream' #'octet-stream': binary data
         part.set_payload( open (f,  'rb' ). read ())
         encoders.encode_base64(part)
         part.add_header( 'Content-Disposition' 'attachment; filename="%s"'  % os.path. basename (f))
         msg.attach(part)
 
     import  smtplib
     smtp = smtplib.SMTP(server[ 'name' ], server[ 'port' ])
     smtp.ehlo()
     smtp.starttls()
     smtp.ehlo()
     smtp.login(server[ 'user' ], server[ 'passwd' ])
     smtp.sendmail(fro, to, msg.as_string())
     smtp.close()
 
 
def get_mysql_data(sql):
     cur.execute(sql)
     results=cur.fetchall()
     return  results
def cover_excel(msg,start_time):
     #wb = xlwt.Workbook()
     ws = wb.add_sheet(start_time,cell_overwrite_ok=True)
     count=len(msg)
     x=msg
     title=[ '时间' .encode( 'utf8' ), '所属组' .encode( 'utf8' ), '主机IP' .encode( 'utf8' ), 'CPU逻辑核数(单位:个)' .encode( 'utf8' ), 'CPU空闲值(单位:%)' .encode( 'utf8' ), '可用内存值(单位:GB)' .encode( 'utf8' ), '总内存值(单位:GB)' .encode( 'utf8' ), '公网进入流量(单位:kbps)' .encode( 'utf8' ), '公网流出流量(单位:kbps)' .encode( 'utf8' )]
     x.insert(0,title)
     for  in  range(0,9):
         for  in  range(0,count):
             if  i == 0:
     #ws.write(i,j,title[j].decode('utf8'))
                 value=x[0]
             else :
                 value=x[i]
       if  isinstance(value[j],long) or isinstance(value[j],int) or isinstance(value[j],float):
     ws.write(i,j,value[j])
       else :
                 ws.write(i,j,value[j].decode( 'utf8' ))
     #wb.save('/tmp/zabbix_log/chance_zabbix_monitor_test.xls')
def run_select(start_time,end_time):
     get_cpu_idle_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg),2) as Cpu_Idle  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends hi on  i.itemid = hi.itemid  where  i.key_='system.cpu.util[,idle]' and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     cpu_idle_result=get_mysql_data(get_cpu_idle_sql)
     get_cpu_num_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,avg(hi.value_avg) as Cpu_Number  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='system.cpu.num' and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     cpu_num_result=get_mysql_data(get_cpu_num_sql)
     get_mem_avai_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg/1024/1024/1024),2) as Memory_Avaiable  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='vm.memory.size[available]'  and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     mem_avai_result=get_mysql_data(get_mem_avai_sql)
     #get_mem_free_sql="select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,hi.value_avg/1024/1024/1024 as Memory_Avaiable  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='vm.memory.size[free]'  and  hi.clock >= UNIX_TIMESTAMP('%s') and  hi.clock < UNIX_TIMESTAMP('%s') and g.name like '%%广告%%';"%(start_time,end_time)
     #mem_free_result=get_mysql_data(get_mem_free_sql)
     get_mem_total_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg/1024/1024/1024),2) as Memory_Total  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='vm.memory.size[total]' and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     mem_total_result=get_mysql_data(get_mem_total_sql)
     get_em2_in_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg/1000),2) as Network_Eth0_In  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='net.if.in[em2]' and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     em2_in_result=get_mysql_data(get_em2_in_sql)
     get_em2_out_sql= "select from_unixtime(hi.clock,'%%Y-%%m-%%d %%T') as Date,g.name as Group_Name,h.host as Host,round(avg(hi.value_avg/1000),2) as Network_Eth0_In  from hosts_groups hg join groups g on g.groupid = hg.groupid join items i on hg.hostid = i.hostid join hosts h on h.hostid=i.hostid join trends_uint hi on  i.itemid = hi.itemid  where  i.key_='net.if.out[em2]' and  hi.clock >= UNIX_TIMESTAMP('%s 20:00:00') and  hi.clock < UNIX_TIMESTAMP('%s 00:00:00') and g.name like '%%广告%%' group by h.host;" %(start_time,end_time)
     em2_out_result=get_mysql_data(get_em2_out_sql)
     msg=[list(i)  for  in  cpu_num_result]
     for  in  msg:
         for  ii  in  cpu_idle_result:
       if  i[0] ==ii[0] and i[1] == ii[1] and i[2] == ii[2]:
     i[3]=int(i[3])
     #msg.append([i[0],i[1],i[2],int(i[3]),ii[3]])
     i.append(int(ii[3]))
   for  iii  in  mem_avai_result:
       if  i[0] ==iii[0] and i[1] == iii[1] and i[2] == iii[2]:
     i.append(round(float(iii[3]),2))
   for  iiii  in  mem_total_result:
       if  i[0] ==iiii[0] and i[1] == iiii[1] and i[2] == iiii[2]:
           i.append(int(iiii[3]))
   for  in  em2_in_result:
       if  i[0] == a[0] and i[1] == a[1] and i[2] == a[2]:
     i.append(int(a[3]))
         if  len(i) == 7:
       i.append(0)
   for  in  em2_out_result:
       if  i[0] == b[0] and i[1] == b[1] and i[2] == b[2]:
     i.append(int(b[3]))
   if  len(i) == 8:
       i.append(0)
     cover_excel(msg,start_time)
def main():
     for  in  range(7,0,-1):
         start_time=((datetime.datetime.now() - datetime.timedelta(days = i))).strftime( "%Y-%m-%d" )
         end_time=((datetime.datetime.now() - datetime.timedelta(days = i-1))).strftime( "%Y-%m-%d" )
   run_select(start_time,end_time)
if  __name__ ==  "__main__" :
     default_encoding =  'utf-8'
     if  sys.getdefaultencoding() != default_encoding:
         reload(sys)
         sys.setdefaultencoding(default_encoding)
     if  os.path.exists( "/tmp/zabbix_log/" ) is False:
         os. mkdir ( "/tmp/zabbix_log/" )
     conn=MySQLdb.connect(host= '10.10.14.19' ,user= 'zabbix' , passwd = 'zabbix' ,port=3306,charset= "utf8" )
     cur=conn.cursor()
     conn.select_db( 'zabbix' )
     wb = xlwt.Workbook()
     main()
     wb.save( '/tmp/zabbix_log/chance_zabbix_monitor_hour_avg.xls' )
     cur.close()
     conn.close()
     #follow is send mail
     server = { 'name' : 'smtp.163.com' 'user' : 'ops_monitor' 'passwd' : 'xxxx' 'port' :25}
     fro =  'xxx@163.com'
     to = [ 'xx@xx.com' , '244979152@qq.com' ]
     now_time=((datetime.datetime.now() - datetime.timedelta(days = 1))).strftime( "%Y/%m/%d" )
     last_time=((datetime.datetime.now() - datetime.timedelta(days = 7))).strftime( "%Y/%m/%d" )
     subject =  'xx平台监控数据【%s-%s】' %(last_time,now_time)
     text =  'xx你好,附件是畅思平台最近一周每天20至24点平均值监控数据,请查收!\n有问题请联系邓磊.'
     files = [ '/tmp/zabbix_log/chance_zabbix_monitor_hour_avg.xls' ]
     send_mail(server, fro, to, subject, text, files=files)

想修改获取监控组的话,就把上面%%广告%%里广告改为你要求的组就行,其他的自动修改。

脚本我放入github里(博客里脚本格式难调整,大家直接去github吧),地址是https://github.com/dl528888/public_script/blob/master/zabbix_hour_avg_monitor.py

想定时发送就把这个脚本放入crontab里,设置好时间允许即可。

下面是我的crontab

1
00 14 * * 5  /usr/bin/python  /data/software/zabbix_hour_avg_monitor .py

BTW:我公司现在提供IT与运维方面技术外包,主要提供物理机租赁、云主机租赁与管理、一站式运维外包支持(包含程序部署与维护、主机维护与管理、数据库管理与维护、CDN管理与维护、监控管理与维护等);

有需要运维外包可以登录官网




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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
3月前
|
存储 SQL 监控
修改Zabbix源码实现监控数据双写,满足业务需求!
虽然对接Elasticsearch后有诸多好处,但是它不往数据库写历史数据了,同时还不再计算趋势数据了。有这么一个场景...
修改Zabbix源码实现监控数据双写,满足业务需求!
|
4月前
|
数据采集 监控 数据库
OceanBase社区版可以通过Zabbix监控
OceanBase社区版可以通过Zabbix监控
77 4
|
4月前
|
监控 关系型数据库 机器人
小白带你学习linux的监控平台zabbix
小白带你学习linux的监控平台zabbix
137 0
|
7月前
|
监控 前端开发 JavaScript
Zabbix分布式监控Web监控
Zabbix分布式监控Web监控
82 0
|
6月前
|
监控 关系型数据库 MySQL
企业实战(8)CentOS 6.8安装Zabbix-agent 5.0监控主机性能与Mysql数据库
企业实战(8)CentOS 6.8安装Zabbix-agent 5.0监控主机性能与Mysql数据库
|
1月前
|
数据采集 监控 数据库
请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
【2月更文挑战第25天】请问OceanBase社区版能否通过zabbix监控,然后将报错信息展现到grafana?
25 2
|
6月前
|
监控
zabbix如何添加自定义监控项
zabbix如何添加自定义监控项
263 0
|
2月前
|
监控 Cloud Native 关系型数据库
使用 Grafana 统一监控展示 - 对接 Zabbix
使用 Grafana 统一监控展示 - 对接 Zabbix
|
4月前
|
监控 Docker 容器
Zabbix【部署 03】zabbix-agent2安装配置使用(zabbix-agent2监控docker实例分享)
Zabbix【部署 03】zabbix-agent2安装配置使用(zabbix-agent2监控docker实例分享)
241 0
|
4月前
|
监控 Java
Zabbix【部署 02】Zabbix-Java-Gateway安装配置使用(使用Zabbix-Java-Gateway通过JMX监控Java应用程序实例分享)
Zabbix【部署 02】Zabbix-Java-Gateway安装配置使用(使用Zabbix-Java-Gateway通过JMX监控Java应用程序实例分享)
90 0

推荐镜像

更多