Zabbix实战-简易教程--业务类

时间:2022-07-03 11:10:45

一、需求

项目要求对线上服务器进行监控,包括服务器本身状态、进程相关数据、业务相关数据。

服务器本身状态可以通过基础模板即可获取数据(CPU、内存、网络、磁盘);

进程相关数据,前面也有相关文章专门监控http://www.cnblogs.com/skyflask/articles/8007162.html

所以只剩下业务相关数据了。而业务数据需要紧贴业务,所以需要程序那边提供接口,把数据吐出来,然后我这边接受获取,进行监控。

于是,向程序提出需求,将你要监控的业务数据以json格式吐出来,剩下的交给我就行了。

二、数据提取

1、数据结构

拿到json字段后,就可以开工获取数据进行相关处理了。

我这边主要是游戏的数据,大家都知道,游戏是分区服的,所以数据的提取可以根据区服关系来,比如一个大区,下面有N个服,那么,这N个服就可以很方便的使用LLD方式,因为各个服的监控字段是一样的。

而大区一般有全局(Global字段)、N服(服字段)。以下是我简化最简单的json字段,中间省略了很多角色。比如Gloabl里面还有其他进程,group里面还有其他类型的进程。

Zabbix实战-简易教程--业务类

 2、提取数据

#!/usr/bin/env python
#coding:utf-8 import simplejson as json
import argparse
import urllib2
import os class LjStatics(object): def __init__(self,api_url): html = urllib2.urlopen(api_url)
strJson = "".join([ html.read().strip().rsplit("}" , 1)[0] , "}"] )
with open('lj.json','w+') as f:
f.write(strJson)
#jsondata = json.loads(strJson)
global hjson
hjson = self.get_json_data() def get_json_data(self):
jsondata = json.load(file("lj.json"))
return jsondata def get_role_list(self):
return hjson.keys() def get_role_uptime(self,arg):
self.arg = args.uptime return hjson[arg]['update_time'] def get_global_role_list(self):
g_list = [] g_list = hjson['global']['status'].keys()
return g_list def get_global_role_status(self,arg,item):
self.arg = args.glbstatus
self.item = args.gitem for key in hjson['global']['status'].keys():
if key == arg:
return hjson['global']['status'].get(key).get(item) def get_region_role_id(self):
_id_list = []
for role in hjson['region']['status']:
_id_list.append(role['id'])
return _id_list def get_regin_role_list(self):
r_list = []
for role in hjson['region']['status']:
for r_role in role['status']:
if r_role['type_name'] == 'location':continue
r_list.append(r_role['type_name']+'-'+str(r_role['instance_id']))
return r_list def get_region_location_lld(self):
r_list = []
for role in hjson['region']['status']:
for r_role in role['status']:
if r_role['type_name'] != 'location':continue
r_list.append(r_role['type_name']+'-'+str(r_role['instance_id']))
_rts = []
for _rt in r_list:
r = os.path.basename(_rt.strip())
_rts += [{'{#SERVERID}':r}]
return json.dumps({'data':_rts},sort_keys=True,indent=4,separators=(',',':')) def get_regin_role_status(self,arg,item):
self.arg = args.regstatus
self.item = args.item
_id = int(arg.split('-')[1])
_arg = arg.split('-')[0] for role in hjson['region']['status']:
for r_role in role['status']:
if r_role['type_name'] == _arg and r_role['instance_id'] == _id:
return r_role['status'][item] @staticmethod
def parse_args():
parser = argparse.ArgumentParser() help = 'Get role list'
parser.add_argument('-rl','--getrole', help=help) help = 'Get global role list '
parser.add_argument('-gbl','--getglist', help=help) help = 'The global role status'
parser.add_argument('-grs','--glbstatus', help=help) help = 'The global role status item'
parser.add_argument('-gi','--gitem', help=help) help = 'Get regin role list'
parser.add_argument('-grl','--getrlist', help=help) help = 'Get regin location role list'
parser.add_argument('-grll','--getrllist', help=help) help = 'The regin role status'
parser.add_argument('-rs','--regstatus', help=help) help = 'The regin role status item'
parser.add_argument('-i','--item', help=help) help = 'Get the role uptime'
parser.add_argument('-u','--uptime', help=help) args = parser.parse_args()
return args if __name__ == '__main__':
'''
python ljstatics.py -rl 1 #获取所有角色列表
python ljstatics.py -gbl 1 #获取所有global列表
python ljstatics.py -grl 1 #获取所有region列表
python ljstatics.py -gbll 1 #获取所有location的LLD值
python ljstatics.py -grs feedback -gi http_total_req #获取global里面的某个角色的监控项
python ljstatics.py -rs queue -i total_send_message #获取region里面queue的监控项total_send_message 全局和除location以外的,只能通过具体监控项参数进行添加
location1-15,可以通过LLD功能进行监控项添加
LLD适用场景:对于同一对象,有相同的指标值。例如:采集每个磁盘的IO参数。
'''
api_url = 'http://10.20.122.7:10200/GetMonitorData'
lj = LjStatics(api_url) args = lj.parse_args()
#获取整体角色列表[region、global、ret]
if args.getrole:
print lj.get_role_list() #获取global角色列表gbl
elif args.getglist:
print lj.get_global_role_list() #获取region角色列表grl
elif args.getrlist:
print lj.get_regin_role_list() #获取region里面的location自动发现项 grll
elif args.getrllist:
print lj.get_region_location_lld() #获取全局角色及监控项值 grs gi
elif args.glbstatus and args.gitem:
print lj.get_global_role_status(args.glbstatus,args.gitem) #获取region角色及监控项值 rs i
elif args.regstatus and args.item:
print lj.get_regin_role_status(args.regstatus,args.item) #获取角色uptime
elif args.uptime:
print lj.get_role_uptime(args.uptime) else:
print 'null'

  

三、测试和自定义key

通过上面的脚本,我们可以获取region(区)和group(服)的数据。所以可以按照这个思路通过LLD进行监控项设置:

由于监控项比较多,所以我们可以通过写成配置文件的方式,进入/etc/zabbix/zabbix_agentd.d目录,定义配置文件:

区配置:

[root@172-31-0-35-pub zabbix_agentd.d]# cat userparameter_global.conf
#lld for global statics(gc)
UserParameter=lj.global.activation.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs activation -gi $1
UserParameter=lj.global.app.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs app -gi $1
UserParameter=lj.global.authentication.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs authentication -gi $1
UserParameter=lj.global.config.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs config -gi $1
UserParameter=lj.global.feedback.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs feedback -gi $1
UserParameter=lj.global.file.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs file -gi $1
UserParameter=lj.global.gm.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs gm -gi $1
UserParameter=lj.global.im.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs im -gi $1
UserParameter=lj.global.logger.status[*],python /etc/zabbix/externalscripts/ljstatics.py -grs logger -gi $1 #UserParameter=lj.region.process.discovery[*],python /etc/zabbix/externalscripts/region_list.py
#UserParameter=lj.region.process.status[*],python /etc/zabbix/externalscripts/ljstatics.py -rs $1 -i $2

  

服配置:

[root@172-31-0-35-pub zabbix_agentd.d]# cat userparameter_region.conf
UserParameter=lj.region.process.status[*],python /etc/zabbix/externalscripts/ljstatics.py -rs $1 -i $2 UserParameter=custom.location.discovery,python /etc/zabbix/externalscripts/ljstatics.py -grll true
UserParameter=custom.location.status[*],python /etc/zabbix/externalscripts/ljstatics.py -rs $1 -i $2

  

四、制作模板

模板已上传至github:https://github.com/loveqx/zabbix-doc/tree/master/zabbix-scripts/zabbix-template-project

五、效果图

监控项:

Zabbix实战-简易教程--业务类

Zabbix实战-简易教程--业务类

Zabbix实战-简易教程--业务类

六、参考文献

模板和脚本已上传至github:https://github.com/loveqx/zabbix-doc/tree/master/zabbix-scripts/zabbix-template-project