python 一键添加 zabbix 监控脚本

释放双眼,带上耳机,听听看~!

由于时间关系没有在脚本里把创建主机组和模板的功能,只是单单实现了添加主机的功能

zabbix_tools.py

  1. #!/usr/bin/python

 

  1. #coding:utf-8

 

  1.  
  2. import

 json 

  1. import

 urllib2 

  1. from

 urllib2 
import
 URLError 

  1. import

 sys 

  1.  
  2. class

 ZabbixTools: 

  1.     

def
 init(
self
): 

  1.         

self
.url = 
'http://lihuipeng.blog.51cto.com/zabbix/api_jsonrpc.php'
 

  1.         

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

  1.          
  2.          
  3.          
  4.     

def
 user_login(
self
): 

  1.         data = json.dumps({ 
  2.                            

"jsonrpc"

"2.0"

  1.                            

"method"

"user.login"

  1.                            

"params"
: { 

  1.                                       

"user"

"Admin"

  1.                                       

"password"

"lihuipeng"
 

  1.                                       }, 
  2.                            

"id"

0
 

  1.                            }) 
  2.          
  3.         request = urllib2.Request(

self
.url, data) 

  1.         

for
 key 
in
 
self
.header: 

  1.             request.add_header(key, 

self
.header[key]) 

  1.      
  2.         

try

  1.             result = urllib2.urlopen(request) 
  2.         

except
 URLError as e: 

  1.             

print
 
"Auth Failed, please Check your name and password:"
, e.code 

  1.         

else

  1.             response = json.loads(result.read()) 
  2.             result.close() 
  3.             

#print response['result']
 

  1.             

self
.authID = response[
'result'

  1.             

return
 
self
.authID 

  1.          
  2.     

def
 host_get(
self
): 

  1.         data = json.dumps({ 
  2.                            

"jsonrpc"
:
"2.0"

  1.                            

"method"
:
"host.get"

  1.                            

"params"
:{ 

  1.                                      

"output"
:[
"hostid"
,
"name"
], 

  1.                                      

"filter"
:{
"host"
:""} 

  1.                                      }, 
  2.                            

"auth"
:
self
.user_login(), 

  1.                            

"id"
:
1

  1.                            }) 
  2.          
  3.         request = urllib2.Request(

self
.url, data) 

  1.         

for
 key 
in
 
self
.header: 

  1.             request.add_header(key, 

self
.header[key]) 

  1.              
  2.      
  3.         

try

  1.             result = urllib2.urlopen(request) 
  2.         

except
 URLError as e: 

  1.             

if
 hasattr(e, 
'reason'
): 

  1.                 

print
 
'We failed to reach a server.'
 

  1.                 

print
 
'Reason: '
, e.reason 

  1.             

elif
 hasattr(e, 
'code'
): 

  1.                 

print
 
'The server could not fulfill the request.'
 

  1.                 

print
 
'Error code: '
, e.code 

  1.         

else

  1.             response = json.loads(result.read()) 
  2.             result.close() 
  3.             

print
 
"Number Of Hosts: "
, len(response[
'result'
]) 

  1.             

for
 host 
in
 response[
'result'
]: 

  1.                 

print
 
"Host ID:"
,host[
'hostid'
],
"Host Name:"
,host[
'name'

  1.                  
  2.     

def
 hostgroup_get(
self
, hostgroupName): 

  1.         data = json.dumps({ 
  2.                            

"jsonrpc"
:
"2.0"

  1.                            

"method"
:
"hostgroup.get"

  1.                            

"params"
:{ 

  1.                                      

"output"

"extend"

  1.                                      

"filter"
: { 

  1.                                                 

"name"
: [ 

  1.                                                          hostgroupName, 
  2.                                                          ] 
  3.                                                 } 
  4.                                      }, 
  5.                            

"auth"
:
self
.user_login(), 

  1.                            

"id"
:
1

  1.                            }) 
  2.          
  3.         request = urllib2.Request(

self
.url, data) 

  1.         

for
 key 
in
 
self
.header: 

  1.             request.add_header(key, 

self
.header[key]) 

  1.               
  2.         

try

  1.             result = urllib2.urlopen(request) 
  2.         

except
 URLError as e: 

  1.             

print
 
"Error as "
, e 

  1.         

else

  1.             response = json.loads(result.read()) 
  2.             result.close() 
  3.             

print
 
"hostgroup : %s —— id : %s"
 % (response[
'result'
][
0
][
'name'
], response[
'result'
][
0
][
'groupid'
]) 

  1.             

self
.hostgroupID = response[
'result'
][
0
][
'groupid'

  1.             

return
 response[
'result'
][
0
][
'groupid'

  1.              
  2.     

def
 template_get(
self
, templateName): 

  1.         data = json.dumps({ 
  2.                            

"jsonrpc"
:
"2.0"

  1.                            

"method"

"template.get"

  1.                            

"params"
: { 

  1.                                       

"output"

"extend"

  1.                                       

"filter"
: { 

  1.                                                  

"host"
: [ 

  1.                                                           templateName, 
  2.                                                           ] 
  3.                                                  } 
  4.                                       }, 
  5.                            

"auth"
:
self
.user_login(), 

  1.                            

"id"
:
1

  1.                            }) 
  2.          
  3.         request = urllib2.Request(

self
.url, data) 

  1.         

for
 key 
in
 
self
.header: 

  1.             request.add_header(key, 

self
.header[key]) 

  1.               
  2.         

try

  1.             result = urllib2.urlopen(request) 
  2.         

except
 URLError as e: 

  1.             

print
 
"Error as "
, e 

  1.         

else

  1.             response = json.loads(result.read()) 
  2.             result.close() 
  3.             

print
 
"template : %s —— id : %s"
 % (response[
'result'
][
0
][
'name'
], response[
'result'
][
0
][
'templateid'
]) 

  1.             

self
.templateID = response[
'result'
][
0
][
'templateid'

  1.             

return
 response[
'result'
][
0
][
'templateid'

  1.                  
  2.     

def
 host_create(
self
, hostip, hostgroupName, templateName): 

  1.         data = json.dumps({ 
  2.                            

"jsonrpc"
:
"2.0"

  1.                            

"method"
:
"host.create"

  1.                            

"params"
:{ 

  1.                                      

"host"
: hostip, 

  1.                                      

"interfaces"
: [ 

  1.                                                         { 
  2.                                                             

"type"

1

  1.                                                             

"main"

1

  1.                                                             

"useip"

1

  1.                                                             

"ip"
: hostip, 

  1.                                                             

"dns"
: "", 

  1.                                                             

"port"

"10050"
 

  1.                                                         } 
  2.                                                     ], 
  3.                                     

"groups"
: [ 

  1.                                                     { 
  2.                                                         

"groupid"

self
.hostgroup_get(hostgroupName) 

  1.                                                     } 
  2.                                                ], 
  3.                                     

"templates"
: [ 

  1.                                                     { 
  2.                                                         

"templateid"

self
.template_get(templateName) 

  1.                                                     } 
  2.                                                   ], 
  3.                                      }, 
  4.                            

"auth"

self
.user_login(), 

  1.                            

"id"
:
1
                   

  1.         }) 
  2.         request = urllib2.Request(

self
.url, data) 

  1.         

for
 key 
in
 
self
.header: 

  1.             request.add_header(key, 

self
.header[key]) 

  1.               
  2.         

try

  1.             result = urllib2.urlopen(request) 
  2.         

except
 URLError as e: 

  1.             

print
 
"Error as "
, e 

  1.         

else

  1.             response = json.loads(result.read()) 
  2.             result.close() 
  3.             

print
 
"host : %s —— id : %s"
 % (hostip, response[
'result'
][
'hostids'
]) 

  1.             

self
.hostid = response[
'result'
][
'hostids'

  1.             

return
 response[
'result'
][
'hostids'

  1.          
  2.                  
  3.                  
  4. if

 name == 
"main"

  1.     

if
 len(sys.argv) != 
4

  1.         

print
 
"Usage: %s ip hostgroupName templateName"
 % sys.argv[
0

  1.         sys.exit(

1

  1.          
  2.     test = ZabbixTools() 
  3.     test.host_create(sys.argv[

1
], sys.argv[
2
], sys.argv[
2
]) 

  1.                  
  2.                  
  3.                  
  4.                  
  5.                  

user_login  是帐号密码验证

host_get    是列出监控机  

hostgroup_get  是获取主机组的ID

template_get   是获取模板的ID

host_create    添加主机

运行方式:(前提是test-group这个主机组和test-template这个模板必须存在)

./zabbix_tools.py  192.168.3.100  test-group  test-template   

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++ lambda表达式

2022-1-11 12:36:11

安全运维

JDBC连接Mysql数据库

2021-12-11 11:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索