-
Notifications
You must be signed in to change notification settings - Fork 0
Home
""" 标题:基于zabbix监控系统的报表系统设计 (zabbix-server+zabbix_api+mysql+grafana) 内容:通过使用ZABBIX API接口,获取主机组下的主机的特定的监控项数据, 并将数据存储到mysql数据库中,mysql作为数据源载grafana中进行数据分析。 说明:1.zabbix监控端配置:同一个群组下的主机应该使用相同的监控模板, 即监控项应该一致(至少我们想要的几个监控指标的监控项应该一致) 2.脚本使用前请将zabbix连接信息及mysql连接信息修改成自己的实际配置,更改主机群组及特定的监控项名称 默认配置: zabbix_web端连接地址:http://127.0.0.1 用户名:Admin 密码:zabbix mysql连接信息:主机:127.0.0.1:3306 用户名:root 密码:zabbix 默认存储指标为:windows群组下每个主机的如下指标: "ICMP ping","Uptime","CPU utilization","Memory utilization","Used memory","C:: Space utilization","C:: Used space", "Bits sent","Bits received"] 3.grafana数据展示应该绑定面板的时间选择器。 4.适用版本:zabbix5.0系列 zabbix6.0系列
# # """ # =====定义函数结构 # """ # # 1.函数1:获取token # def get_token(): # data = { # "jsonrpc": "2.0", # "method": "user.login", # "params": { # "user": """ # 标题:基于zabbix监控系统的报表系统设计 # (zabbix-server+zabbix_api+mysql+grafana) # 内容:通过使用ZABBIX API接口,获取主机组下的主机的特定的监控项数据, # 并将数据存储到mysql数据库中,mysql作为数据源载grafana中进行数据分析。 # 说明:1.zabbix监控端配置:同一个群组下的主机应该使用相同的监控模板, # 即监控项应该一致(至少我们想要的几个监控指标的监控项应该一致) # 2.脚本使用前请将zabbix连接信息及mysql连接信息修改成自己的实际配置,更改主机群组及特定的监控项名称 # 默认配置: # zabbix_web端连接地址:http://127.0.0.1 用户名:Admin 密码:zabbix # mysql连接信息:主机:127.0.0.1:3306 用户名:root 密码:zabbix # 默认存储指标为:windows群组下每个主机的如下指标: # "ICMP ping","Uptime","CPU utilization","Memory utilization","Used memory","C:: Space utilization","C:: Used space", # "Bits sent","Bits received"] # 3.grafana数据展示应该绑定面板的时间选择器。 # 4.适用版本:zabbix5.0系列 zabbix6.0系列 # # """ import requests, json,pymysql import time
""" =====定义函数结构 """ # 1.函数1:获取token def get_token(): data = { "jsonrpc": "2.0", "method": "user.login", "params": { "user": zabbix_web_user, #Zabbix web端用户名: "password": zabbix_web_password #Zabbix web端用户密码 }, "id": 0 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json()['result'] return result except Exception as e: print(str(e))
def get_groupid(group_name): data = { "jsonrpc": "2.0", "method": "hostgroup.get", "params": { "output": "extend", "filter": { "name": group_name } }, "auth": token, "id": 1 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json()['result'] return result except Exception as e: print(str(e))
def get_host_list(group_id): data = { "jsonrpc": "2.0", "method": "host.get", "params": { "groupids": group_id, "monitored_hosts": "", }, "auth": token, "id": 2 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json() return result['result'] except Exception as e: print(str(e))
def get_items(): data = { "jsonrpc": "2.0", "method": "item.get", "params": { "hostids": hostid, }, "auth": token, "id": 3 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json() return result['result'] except Exception as e: print(str(e))
def get_itemid(hostid, item_name): data = { "jsonrpc": "2.0", "method": "item.get", "params": { "output": ["itemid", "name", "key_"], "hostids": hostid, "search": { "name": item_name }, }, "auth": token, "id": 4 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json() return result['result'] except Exception as e: print(str(e))
def get_item_data(hostid, itemid): data = { "jsonrpc": "2.0", "method": "item.get", "params": { "output": "extend", "hostids": hostid, "itemids": itemid,
}, "auth": token, "id": 5 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json() return result['result'] except Exception as e: print(str(e))
def get_trend(): data = { "jsonrpc": "2.0", "method": "trend.get", "params": { "output": [ "itemid", "clock", "num", "value_min", "value_avg", "value_max", ], "search": { "name": "CPU" }, "hosts": hostid, "limit": "1" }, "auth": token, "id": 6 } try: result = requests.post(url=zabbix_api_url, headers=headers, data=json.dumps(data)).json() return result['result'] except Exception as e: print(str(e))
if name == 'main': #使用提示:
'''1.定义zabbix基本信息''' zabbix_web_url = "http://192.168.112.135" #zabbix_web地址(注意最后的‘/’不需要输入) zabbix_api_url = zabbix_web_url + "/api_jsonrpc.php" headers = { "Content-Type": "application/json-rpc" } zabbix_web_user="Admin" #Zabbix web端用户名: zabbix_web_password="zabbix" #Zabbix web端用户登录密码:
try: '''2.数据获取''' #获取token update_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) token = get_token() print(token) #打印指定主机群组id search_groups=["Templates"] #需要获取信息的主机所在的主机组,与zabbix服务端一致(如需获取多个群组的信息,请增加一个for循环进行遍历) groupid = get_groupid(search_groups[0]) print("群组清单如下:") for i in range(0, len(groupid)): group_info = groupid[i]['groupid'] + ":" + groupid[i]['name'] print("群组信息:" + group_info) group_id_ = groupid[i]['groupid'] # 主机下的主机清单 host_list = get_host_list(group_id_) for i in range(0, len(host_list)): host_info = host_list[i]['hostid'] + ":" + host_list[i]['host'] print(host_info) hostid = host_list[i]['hostid'] hostname = host_list[i]['host'] # 获取监控值 check_range = [ "ICMP ping", "Uptime", "CPU utilization", "Memory utilization", "Used memory", "C:: Space utilization", "C:: Used space", "Bits sent", "Bits received"] database_data = [hostid, hostname, update_time] # for i in range(0, len(check_range)): # item_id_ = (get_itemid(hostid, check_range[i]))[0]['itemid'] # last_value = (get_item_data(hostid, item_id_))[0]['lastvalue'] # database_data.append(last_value) print(database_data)
""" 3.数据存储 """ # 创建连接,定义主机、用户、密码等连接信息 conn = pymysql.connect( host='192.168.112.135', #mysql主机ip user='root', #允许登录mysql的用户,一般使用root passwd='Wsjy2018!' #mysql如上定义用户的登录密码 ) # 创建游标,传递命令 cursor = conn.cursor() # 定义sql语句、执行命令[注意每个mysql版本的语法差异性] # (1)创建数据库 sql_cretae_database = 'create database if not exists python_zabbix character set utf8 collate utf8_bin;' cursor.execute(sql_cretae_database) # (2)mysql用户授权 sql_grant="grant all on python_zabbix.* to 'root'@'%';" cursor.execute(sql_grant) print(search_groups[0]) # (3)数据库中创建表 sql_create_table1 = 'use python_zabbix;' # sql_create_table2 = 'create table if not exists python_zabbix_'+search_groups[0]+'(ID int(11) not null primary key AUTO_INCREMENT,' \ sql_create_table2 = 'create table if not exists python_zabbix_'+search_groups[0]+'(ID int(11) not null primary key AUTO_INCREMENT,' \ 'hostid int(250),' \ 'hostname varchar(200),' \ 'update_time datetime,' \ 'PING int(2),' \ 'Uptime int(250),' \ 'CPU_u DECIMAL(10,6),' \ 'memory_u DECIMAL(10,6),memory_used BIGINT(255),' \ 'file_c_u DECIMAL(10,6),' \ 'file_c_used BIGINT(255),'\ 'net_sent bigint(255),'\ 'net_re bigint(255)'\ ');' cursor.execute(sql_create_table1) cursor.execute(sql_create_table2)
# (3)插入数据 sql_insert = 'insert into python_zabbix_'+search_groups[0]+'(' \ 'hostid,' \ 'hostname,' \ 'update_time,' \ 'PING,' \ 'Uptime,' \ 'CPU_u,' \ 'memory_u,' \ 'memory_used,'\ 'file_c_u,' \ 'file_c_used,' \ 'net_sent,' \ 'net_re) ' \ 'values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s);' cursor.execute(sql_insert, database_data)
# 提交事务 conn.commit() # 关闭游标 cursor.close() # 关闭连接 conn.cursor() print("数据更新成功!") except Exception as e: print("脚本异常"+str(e))