python 一键添加 zabbix 监控脚本

简介:

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

zabbix_tools.py

 
  1. #!/usr/bin/python 
  2. #coding:utf-8 
  3.  
  4. import json 
  5. import urllib2 
  6. from urllib2 import URLError 
  7. import sys 
  8.  
  9. class ZabbixTools: 
  10.     def __init__(self): 
  11.         self.url = 'http://lihuipeng.blog.51cto.com/zabbix/api_jsonrpc.php' 
  12.         self.header = {"Content-Type":"application/json"
  13.          
  14.          
  15.          
  16.     def user_login(self): 
  17.         data = json.dumps({ 
  18.                            "jsonrpc""2.0"
  19.                            "method""user.login"
  20.                            "params": { 
  21.                                       "user""Admin"
  22.                                       "password""lihuipeng" 
  23.                                       }, 
  24.                            "id"0 
  25.                            }) 
  26.          
  27.         request = urllib2.Request(self.url, data) 
  28.         for key in self.header: 
  29.             request.add_header(key, self.header[key]) 
  30.      
  31.         try
  32.             result = urllib2.urlopen(request) 
  33.         except URLError as e: 
  34.             print "Auth Failed, please Check your name and password:", e.code 
  35.         else
  36.             response = json.loads(result.read()) 
  37.             result.close() 
  38.             #print response['result'] 
  39.             self.authID = response['result'
  40.             return self.authID 
  41.          
  42.     def host_get(self): 
  43.         data = json.dumps({ 
  44.                            "jsonrpc":"2.0"
  45.                            "method":"host.get"
  46.                            "params":{ 
  47.                                      "output":["hostid","name"], 
  48.                                      "filter":{"host":""} 
  49.                                      }, 
  50.                            "auth":self.user_login(), 
  51.                            "id":1
  52.                            }) 
  53.          
  54.         request = urllib2.Request(self.url, data) 
  55.         for key in self.header: 
  56.             request.add_header(key, self.header[key]) 
  57.              
  58.      
  59.         try
  60.             result = urllib2.urlopen(request) 
  61.         except URLError as e: 
  62.             if hasattr(e, 'reason'): 
  63.                 print 'We failed to reach a server.' 
  64.                 print 'Reason: ', e.reason 
  65.             elif hasattr(e, 'code'): 
  66.                 print 'The server could not fulfill the request.' 
  67.                 print 'Error code: ', e.code 
  68.         else
  69.             response = json.loads(result.read()) 
  70.             result.close() 
  71.             print "Number Of Hosts: ", len(response['result']) 
  72.             for host in response['result']: 
  73.                 print "Host ID:",host['hostid'],"Host Name:",host['name'
  74.                  
  75.     def hostgroup_get(self, hostgroupName): 
  76.         data = json.dumps({ 
  77.                            "jsonrpc":"2.0"
  78.                            "method":"hostgroup.get"
  79.                            "params":{ 
  80.                                      "output""extend"
  81.                                      "filter": { 
  82.                                                 "name": [ 
  83.                                                          hostgroupName, 
  84.                                                          ] 
  85.                                                 } 
  86.                                      }, 
  87.                            "auth":self.user_login(), 
  88.                            "id":1
  89.                            }) 
  90.          
  91.         request = urllib2.Request(self.url, data) 
  92.         for key in self.header: 
  93.             request.add_header(key, self.header[key]) 
  94.               
  95.         try
  96.             result = urllib2.urlopen(request) 
  97.         except URLError as e: 
  98.             print "Error as ", e 
  99.         else
  100.             response = json.loads(result.read()) 
  101.             result.close() 
  102.             print "hostgroup : %s ------ id : %s" % (response['result'][0]['name'], response['result'][0]['groupid']) 
  103.             self.hostgroupID = response['result'][0]['groupid'
  104.             return response['result'][0]['groupid'
  105.              
  106.     def template_get(self, templateName): 
  107.         data = json.dumps({ 
  108.                            "jsonrpc":"2.0"
  109.                            "method""template.get"
  110.                            "params": { 
  111.                                       "output""extend"
  112.                                       "filter": { 
  113.                                                  "host": [ 
  114.                                                           templateName, 
  115.                                                           ] 
  116.                                                  } 
  117.                                       }, 
  118.                            "auth":self.user_login(), 
  119.                            "id":1
  120.                            }) 
  121.          
  122.         request = urllib2.Request(self.url, data) 
  123.         for key in self.header: 
  124.             request.add_header(key, self.header[key]) 
  125.               
  126.         try
  127.             result = urllib2.urlopen(request) 
  128.         except URLError as e: 
  129.             print "Error as ", e 
  130.         else
  131.             response = json.loads(result.read()) 
  132.             result.close() 
  133.             print "template : %s ------ id : %s" % (response['result'][0]['name'], response['result'][0]['templateid']) 
  134.             self.templateID = response['result'][0]['templateid'
  135.             return response['result'][0]['templateid'
  136.                  
  137.     def host_create(self, hostip, hostgroupName, templateName): 
  138.         data = json.dumps({ 
  139.                            "jsonrpc":"2.0"
  140.                            "method":"host.create"
  141.                            "params":{ 
  142.                                      "host": hostip, 
  143.                                      "interfaces": [ 
  144.                                                         { 
  145.                                                             "type"1
  146.                                                             "main"1
  147.                                                             "useip"1
  148.                                                             "ip": hostip, 
  149.                                                             "dns": "", 
  150.                                                             "port""10050" 
  151.                                                         } 
  152.                                                     ], 
  153.                                     "groups": [ 
  154.                                                     { 
  155.                                                         "groupid"self.hostgroup_get(hostgroupName) 
  156.                                                     } 
  157.                                                ], 
  158.                                     "templates": [ 
  159.                                                     { 
  160.                                                         "templateid"self.template_get(templateName) 
  161.                                                     } 
  162.                                                   ], 
  163.                                      }, 
  164.                            "auth"self.user_login(), 
  165.                            "id":1                   
  166.         }) 
  167.         request = urllib2.Request(self.url, data) 
  168.         for key in self.header: 
  169.             request.add_header(key, self.header[key]) 
  170.               
  171.         try
  172.             result = urllib2.urlopen(request) 
  173.         except URLError as e: 
  174.             print "Error as ", e 
  175.         else
  176.             response = json.loads(result.read()) 
  177.             result.close() 
  178.             print "host : %s ------ id : %s" % (hostip, response['result']['hostids']) 
  179.             self.hostid = response['result']['hostids'
  180.             return response['result']['hostids'
  181.          
  182.                  
  183.                  
  184. if __name__ == "__main__"
  185.     if len(sys.argv) != 4
  186.         print "Usage: %s ip hostgroupName templateName" % sys.argv[0
  187.         sys.exit(1
  188.          
  189.     test = ZabbixTools() 
  190.     test.host_create(sys.argv[1], sys.argv[2], sys.argv[2]) 
  191.                  
  192.                  
  193.                  
  194.                  
  195.                  

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   

 

参考文章:

https://www.zabbix.com/documentation/2.0/manual/appendix/api

本文转自运维笔记博客51CTO博客,原文链接http://blog.51cto.com/lihuipeng/1193026如需转载请自行联系原作者


lihuipeng

相关文章
|
2天前
|
Python Perl
LabVIEW调用Perl和Python脚本
LabVIEW调用Perl和Python脚本
|
4天前
|
缓存 人工智能 算法
编写高效的Python脚本:性能优化的策略与技巧
编写高效的Python脚本需要综合考虑多个方面,包括代码结构、数据结构和算法选择等。本文将探讨在Python编程中提高脚本性能的方法,包括优化数据结构、选择合适的算法、使用Python内置函数以及通过并行和异步编程提升效率。这些技巧旨在帮助开发者在不同应用场景中编写出高性能的Python代码。
|
7天前
|
运维 监控 Ubuntu
Python实现ubuntu系统进程内存监控
Python实现ubuntu系统进程内存监控
12 1
|
7天前
|
机器学习/深度学习 编解码 监控
利用Python实现监控视频的超分辨率提升
利用Python实现监控视频的超分辨率提升
19 2
|
15天前
|
存储 网络安全 数据安全/隐私保护
【专栏】Python 网络设备管理中,`ConnectHandler`(Paramiko库)和`telnetlib`模块常用于设备交互。
【4月更文挑战第28天】Python 网络设备管理中,`ConnectHandler`(Paramiko库)和`telnetlib`模块常用于设备交互。`ConnectHandler`简化SSH连接,便于与网络设备交互,而`telnetlib`是Python内置模块,支持Telnet协议的远程登录操作。两者都提供命令执行和响应接收功能。示例代码展示了如何使用它们获取防火墙设备的版本信息,降低了代码复杂度,提高了可读性和维护性。
|
15天前
|
网络安全 数据安全/隐私保护 Python
【专栏】如何使用 Python 编写脚本批量备份交换机配置
【4月更文挑战第28天】本文介绍如何使用 Python 编写脚本批量备份交换机配置。主要步骤包括了解交换机命令和接口,安装 `paramiko` 库,获取交换机登录信息。脚本实现分为建立 SSH 连接,执行备份命令并保存结果。示例脚本中,定义了 `backup_switch_config` 函数遍历交换机列表进行备份,每次备份后等待一段时间。此方法能有效提高网络管理效率。
|
20天前
|
监控 Python
Python监控主机是否存活,并发报警邮件
Python监控主机是否存活,并发报警邮件
|
24天前
|
小程序 Python
Python基础之简单的小程序和小脚本
这个Python教程介绍了两个小程序。首先是一个账户管理类小程序,支持存款、取款和查询余额功能,确保金额始终保留两位小数。用户可以输入初始金额创建账户,并进行存取款操作。其次,是一个检查作业提交的脚本,它遍历指定目录,找出未提交作业(即没有对应ID文件)的学生ID。用户输入目录路径后,脚本会显示未提交作业的学生ID,如果所有人都提交了,则显示相应消息。
|
25天前
|
监控 数据可视化 NoSQL
Python基于Flask的高校舆情分析,舆情监控可视化系统
Python基于Flask的高校舆情分析,舆情监控可视化系统
|
26天前
|
Python
Python脚本的两种实现
Python脚本的两种实现

推荐镜像

更多