Zabbix实战-简易教程--业务类
作者:@skyflask
转载本文请注明出处:https://www.cnblogs.com/skyflask/p/8439115.html
目录
一、需求
二、数据提取
三、测试和自定义key
四、制作模板
五、效果图
六、参考文献
一、需求
项目要求对线上服务器进行监控,包括服务器本身状态、进程相关数据、业务相关数据。
服务器本身状态可以通过基础模板即可获取数据(CPU、内存、网络、磁盘);
进程相关数据,前面也有相关文章专门监控http://www.cnblogs.com/skyflask/articles/8007162.html
所以只剩下业务相关数据了。而业务数据需要紧贴业务,所以需要程序那边提供接口,把数据吐出来,然后我这边接受获取,进行监控。
于是,向程序提出需求,将你要监控的业务数据以json格式吐出来,剩下的交给我就行了。
二、数据提取
1、数据结构
拿到json字段后,就可以开工获取数据进行相关处理了。
我这边主要是游戏的数据,大家都知道,游戏是分区服的,所以数据的提取可以根据区服关系来,比如一个大区,下面有N个服,那么,这N个服就可以很方便的使用LLD方式,因为各个服的监控字段是一样的。
而大区一般有全局(Global字段)、N服(服字段)。以下是我简化最简单的json字段,中间省略了很多角色。比如Gloabl里面还有其他进程,group里面还有其他类型的进程。
2、提取数据
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | #!/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目录,定义配置文件:
区配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [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 |
服配置:
1 2 3 4 5 | [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
五、效果图
监控项:
六、参考文献
模板和脚本已上传至github:https://github.com/loveqx/zabbix-doc/tree/master/zabbix-scripts/zabbix-template-project
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」