5 zabbix 添加host_利用zabbix api 添加监控

本文介绍了如何使用Zabbix API来批量管理主机监控,包括添加、禁用、删除主机以及查询主机、主机组和模板等功能。通过Python脚本实现,适用于大量服务器监控场景。
摘要由CSDN通过智能技术生成

众所周知,zabbix是一款强大的分布式监控系统,集各家所长于一体,得到了广大SA的认可。其强大的管理界面也极其方便,但是美中不足的是,如果同时有大批量(50台+)的服务器需要添加监控时,这时,其图形界面反而显得有些臃肿了,好在zabbix 提供了一套强大的API管理接口,我们可以使用它快速地添加成千上万台服务器。

根据日常工作中常用到zabbix的功能,整理以下功能

1.基于zabbix 官方 api

2.提供查询单个或者多个host、hostgroup、template功能

3.提供增加host,hostgroup功能

4.提供disable host功能

5.增加删除host 功能

代码如下:

#!/usr/bin/python

#coding:utf-8

import json

import urllib2

from urllib2 import URLError

import sys,argparse

class zabbix_api:

def __init__(self):

self.url = 'http://localhost/api_jsonrpc.php' #修改URL

self.header = {"Content-Type":"application/json"}

def user_login(self):

data = json.dumps({

"jsonrpc": "2.0",

"method": "user.login",

"params": {

"user": "Admin", #修改用户名

"password": "zabbix" #修改密码

},

"id": 0

})

request = urllib2.Request(self.url, data)

for key in self.header:

request.add_header(key, self.header[key])

try:

result = urllib2.urlopen(request)

except URLError as e:

print " 33[041m 用户认证失败,请检查 ! 33[0m", e.code

else:

response = json.loads(result.read())

result.close()

#print response['result']

self.authID = response['result']

return self.authID

def host_get(self,hostName=''):

data=json.dumps({

"jsonrpc": "2.0",

"method": "host.get",

"params": {

"output": "extend",

"filter":{"host":hostName}

},

"auth": self.user_login(),

"id": 1

})

request = urllib2.Request(self.url,data)

for key in self.header:

request.add_header(key, self.header[key])

try:

result = urllib2.urlopen(request)

except URLError as e:

if hasattr(e, 'reason'):

print 'We failed to reach a server.'

print 'Reason: ', e.reason

elif hasattr(e, 'code'):

print 'The server could not fulfill the request.'

print 'Error code: ', e.code

else:

response = json.loads(result.read())

#print response

result.close()

print "主机数量: 33[31m%s 33[0m"%(len(response['result']))

for host in response['result']:

status={"0":"OK","1":"Disabled"}

available={"0":"Un

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值