本文主要通过一些监控示例来简单介绍zabbix的http agent类型的监控以及map图的简单用法,我们可以通过一些网站查询天气的api来获取天气情况,大部分的地图软件也都支持天气查询,这里我简单介绍几个,有高德地图,百度地图,心知天气,YY天气,个人用的是YY天气,因为我的需求不高,他能轻易地满足我
YY天气api地址:http://www.yytianqi.com/api.html
创建HTTP agent类型的监控项
以创建北京天气的监控项为例
获取数据如下
创建包含预处理的可依赖监控项
以监控天气信息为例
键值定义尽量规范,依赖项选择刚刚新建的http agent监控项
添加预处理步骤
根据刚才获取到的数据来获取tq的值
除此之外还需要定义气温、风力、风向、相对湿度,添加方法与上面相同
添加高温的触发器
编写脚本批量添加监控项和触发器
但是这样做要添加的监控项和触发器太多了,而且都是重复性的工作,这里我写了个脚本,来对各地城市的监控项和触发器进行批量添加,批量添加的好处是省时省力,而且减少出错概率。
实现思路是获取主机的接口id,并对城市进行循环添加监控项和触发器
#coding=utf-8
import requests,json
ApiUrl = 'http://192.168.1.100/api_jsonrpc.php'
header = {"Content-Type":"application/json"}
user="Admin"
password="zabbix"
city=[{"beijing":"CH010100"},{"shanghai":"CH020100"},{"tianjin":"CH030100"},{"chongqing":"CH040100"},{"haerbin":"CH050101"},{"changchun":"CH060101"},{"shenyang":"CH070101"},{"huhehaote":"CH080101"},{"shijiazhuang":"CH090101"},{"taiyuan":"CH100101"},{"xian":"CH110101"},{"jinan":"CH120101"},{"wulumuqi":"CH130101"},{"lasa":"CH140101"},{"xining":"CH150101"},{"lanzhou":"CH160101"},{"yinchuan":"CH170101"},{"zhengzhou":"CH180101"},{"nanjing":"CH190101"},{"wuhan":"CH200101"},{"hangzhou":"CH210101"},{"hefei":"CH220101"},{"fuzhou":"CH230101"},{"nanchang":"CH240101"},{"changsha":"CH250101"},{"guiyang":"CH260101"},{"chengdu":"CH270101"},{"guangzhou":"CH280101"},{"kunming":"CH290101"},{"nanning":"CH300101"},{"haikou":"CH310101"},{"hongkong":"CH320101"},{"aomen":"CH330101"},{"taibei":"CH340101"}]
def get_interface(auth):
data ={
"jsonrpc": "2.0",
"method": "hostinterface.get",
"params": {
"output": "extend",
"hostids": "10343"
},
"auth": auth,
"id": 1
}
gethost=requests.post(url=ApiUrl,headers=header,json=data)
return json.loads(gethost.content)["result"][0]["interfaceid"]
def gettoken():
data = {"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": user,
"password": password
},
"id": 1,
"auth": None
}
auth=requests.post(url=ApiUrl,headers=header,json=data)
return json.loads(auth.content)["result"]
def createitemandtrigger(auth,interface):
for dic in city:
for key,value in dic.items():
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"url":"http://api.yytianqi.com/observe",
"query_fields": [
{
"city":"%s" %value
},
{
"key": "iopesm5a0uhqwer"
}
],
"interfaceid": "%s" %interface,
"type":"19",
"hostid":"10343",
"delay":"5m",
"key_":"%s" %key,
"name":"%s" %key,
"value_type":"1",
"output_format":"0",
"timeout":"3s"
},
"auth": auth,
"id": 1
}
httpagent=requests.post(url=ApiUrl,headers=header,json=data)
httpagent=json.loads(httpagent.content)['result']['itemids'][0]
print(httpagent)
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"hostid": "10343",
"name": "%s-tq" %key,
"key_": "%s-tq" %key,
"type": "18",
"master_itemid": httpagent,
"value_type": "1",
"preprocessing": [
{
"type": "12",
"params": "$.data.tq",
"error_handler": "1",
"error_handler_params": ""
}
]
},
"auth": auth,
"id": 1
}
dependqw=requests.post(url=ApiUrl,headers=header,json=data)
print(dependqw.content)
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"hostid": "10343",
"name": "%s-fl" %key,
"key_": "%s-fl" %key,
"type": "18",
"master_itemid": httpagent,
"value_type": "1",
"preprocessing": [
{
"type": "12",
"params": "$.data.fl",
"error_handler": "1",
"error_handler_params": ""
}
]
},
"auth": auth,
"id": 1
}
dependfl=requests.post(url=ApiUrl,headers=header,json=data)
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"hostid": "10343",
"name": "%s-qw" %key,
"key_": "%s-qw" %key,
"type": "18",
"master_itemid": httpagent,
"value_type": "3",
"preprocessing": [
{
"type": "12",
"params": "$.data.qw",
"error_handler": "1",
"error_handler_params": ""
}
]
},
"auth": auth,
"id": 1
}
dependqw=requests.post(url=ApiUrl,headers=header,json=data)
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"hostid": "10343",
"name": "%s-fx" %key,
"key_": "%s-fx" %key,
"type": "18",
"master_itemid": httpagent,
"value_type": "1",
"preprocessing": [
{
"type": "12",
"params": "$.data.fx",
"error_handler": "1",
"error_handler_params": ""
}
]
},
"auth": auth,
"id": 1
}
dependfx=requests.post(url=ApiUrl,headers=header,json=data)
data={
"jsonrpc": "2.0",
"method": "item.create",
"params": {
"hostid": "10343",
"name": "%s-sd" %key,
"key_": "%s-sd" %key,
"type": "18",
"master_itemid": httpagent,
"value_type": "0",
"preprocessing": [
{
"type": "12",
"params": "$.data.sd",
"error_handler": "1",
"error_handler_params": ""
}
]
},
"auth": auth,
"id": 1
}
dependsd=requests.post(url=ApiUrl,headers=header,json=data)
data={
"jsonrpc": "2.0",
"method": "trigger.create",
"params": [
{
"description": "%s温度有点高" %key,
"expression": "{tianqi:%s-qw.last()}>30" %key,
"opdata": "当前气温:{ITEM.LASTVALUE}",
"priority": 3,
"manual_close": 1
}
],
"auth": auth,
"id": 1
}
triggercreate=requests.post(url=ApiUrl,headers=header,json=data)
return json.loads(dependqw.content)#["result"]
auth=gettoken()
interface=get_interface(auth)
createhttpitem(auth,interface)
实现效果如下
制作map图
首先创建背景图片
创建map图
对map图进行编辑
标签用于显示监控的数据,添加高温的触发器,当触发高温报警后,图标由绿点会变成红绿交替闪动,这里以北京气温为例
最终实现效果如下
报警效果如下