zabbix批量添加items示例

本文介绍了一种使用Python脚本批量向Zabbix系统中创建监控项的方法。通过读取文件中的配置信息,并利用Zabbix API接口进行操作,实现监控项的自动化创建,提高运维效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1 #!/usr/bin/python
  2 #coding:utf-8
  3 
  4 import json
  5 import urllib2
  6 import re
  7 
  8 url = 'http://YOUR_IP/zabbix/api_jsonrpc.php'
  9 header = {'Content-Type':'application/json'}
 10 
 11 def Item_create():
 12     f = open('item.txt','r')
 13     line = f.readlines()
 14     for i in range(len(line)):
 15         data = json.dumps({
 16             'jsonrpc':'2.0',
 17             'method':'item.create',
 18             'params':{
 19                 'name':'Rsyslog_'+line[i].strip('\n'),
 20                 'key_':line[i].strip('\n'),
 21                 'hostid':'10107',
 22                 'type':2,
 23                 'value_type':3,
 24 #                'applications':['482'],
 25                 'delay':30,
 26                 'status':0
 27             },
 28             'auth':'13d1eed61d538f0a446b00a203f40503',  #此处为之前获取的token
 29             'id':2,
 30         })
 31 
 32         print i
 33         request = urllib2.Request(url,data)
 34         for key in header:
 35             request.add_header(key,header[key])
 36         try:
 37             result = urllib2.urlopen(request)
 38         except urllib2.URLError as e:
 39             print e
 40         else:
 41             response =  json.loads(result.read())
 42             print response
 43             print line[i].strip('\n')
 44     f.close()
 45 
 46 if __name__ == '__main__':
 47     Item_create()

返回如下:

0
{u'jsonrpc': u'2.0', u'result': {u'itemids': [u'24281']}, u'id': 2}
action_logs-dip_logstash_size
1
{u'jsonrpc': u'2.0', u'result': {u'itemids': [u'24282']}, u'id': 2}
action_logs-dip_logstash_enqueued


转载于:https://my.oschina.net/MrYx3en/blog/408458

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值