python实现Zabbix-API监控

yipeiwu_com5年前Python基础

  做运维的朋友应该知道,公司IDC机房经常有上架、下架、报修和报废的服务器。如果服务器数量很多的时候很容易造成监控遗漏。


       大的互联网公司把监控系统和CMDB(资产管理系统|配置管理数据库系统)集成在一起,当上架一台新机器的时候CMDB里面会记录相关的信息,Zabbix根据CMDB里面信息自动Link相关的模块,添加|删除监控。很多小的公司没有资产管理系统,但作为监控的负责人应该每天知道上架了哪些新的机器,确保能添加到Zabbix监控里面。

      首先给大家说一下脚本思路:

1)通过Nmap工具扫描网段,扫描出已经使用的IP地址。
2)通过Nmap检测已经扫描IP的3389或者22端口是否开放,可以判断那些事windows机器,那些是Linux机器。
3)Linux下面通过ssh + hostname命令找出Linux主机名。
4)Windows下面通过nmblookup -A 命令找出Windows主机名。
5)用Python脚本读扫描结果文件,把主机名写到列表里面。
6)用Zabbix python API 调用已经监控的主机名,写到列表里面。
7)两个列表取交集,用for循环判断哪些主机名没有监控。
8)发邮件通知监控负责人。

    下面我分享一下我写的Python写的脚本,其中scan_machine.sh是我调用的用Shell写的关于Nmap扫描的脚本,scan_hostname.log是Nmap扫描的结果,里面内容是IP 主机名。

#!/usr/bin/env python#create by:sfzhang 20140820#coding=utf-8import os,sysimport jsonimport urllib2import datetime,timefrom urllib2 import URLError
nmap_cmd = "/shell/machine/scan_machine.sh"def runCmd(command):
 global mail_cmd
 mail_cmd = '''mail -s "Report on not monitor Hosts of Zabbix" shifeng_zhang88 < /shell/machine/result/result.txt'''
 return os.system(command)runCmd(nmap_cmd)def nmap_host():
 hostiplst = []
 hostnamelst = []
 f = file('/shell/machine/result/scan_hostname.log')
 for line in f.readlines():
  hostip = line.split()[0]
  hostname = line.split()[1]
  hostiplst.append(hostip)
  hostnamelst.append(hostname)
 hostnamelst.sort()
 #print hostiplst
 return hostnamelst
 f.close()def zabbix_host():
 zabbixhostlst= []
 #based url and required header
 url = "http://192.168.161.128/api_jsonrpc.php"
 header = {"Content-Type": "application/json"}
 #request json
 data = json.dumps(
 { 
 "jsonrpc": "2.0",
  "method": "host.get",
  "params":{
   "output":["hostid","name"],
   "filter":{"host":""}
 }, 
 #auth id
 "auth":"Zabbix Auth ID",
 "id": 1,
 })
 #create request object
 request = urllib2.Request(url,data)
 for key in header: 
 request.add_header(key,header[key])
 #get host list
 try:
 result = urllib2.urlopen(request)
 except URLError as e:
 print "The server could not fulfill the request.",e.reason else:
  reponse = json.loads(result.read())
 result.close()
 #print "Number of Hosts:",len(reponse['result'])
 for host in reponse['result']:
   #print "Host ID:",host['hostid'],"Host Name:",host['name']
   zbxhosts=host['name']
   zabbixhostlst.append(zbxhosts)
  zabbixhostlst.sort()
  return zabbixhostlst def main():
 nmaphostlst = nmap_host() 
 zbxhostlst = zabbix_host() 
 diff = list(set(nmaphostlst) ^ set(zbxhostlst)) 
 content = "\n"
 nomonitorlst = [] 
 if len(diff) != 0: 
  for host in diff: 
   if host in nmaphostlst: 
    nomonitorlst.append(host)
    
 else: 
  sys.exit()
 #print zbxhostlst
 string = '\n'.join(nomonitorlst)
 f = file('/shell/machine/result/result.txt','w')
 f.write(string)
 f.flush()
 f.close()
 runCmd(mail_cmd)if __name__ == "__main__": 
 main()


把脚本添加到crontab,每台会收到关于那些主机没有添加监控的信息。

总结:

     1)Zabbix API相关信息可以查看官方详细资料。

     2)通过该脚本可以知道那些主机没有添加监控,希望对大家有帮助,如果有更好的解决方法欢迎多多交流。    

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

微信跳一跳python辅助脚本(总结)

微信跳一跳python辅助脚本(总结)

这段时间微信跳一跳这个游戏非常火爆,但是上分又非常的难,对于程序员来说第一个念头就是通过写一个辅助脚本外挂让上分变的容易,python现在比较火,我们一起来以python语言为基础总结以...

如何修复使用 Python ORM 工具 SQLAlchemy 时的常见陷阱

在使用 SQLAlchemy 时,那些看似很小的选择可能对这种对象关系映射工具包的性能产生重要影响。 对象关系映射Object-relational mapping(ORM)使应用程序开...

Django开发的简易留言板案例详解

Django开发的简易留言板案例详解

本文实例讲述了Django开发的简易留言板。分享给大家供大家参考,具体如下: Django在线留言板小练习 环境 ubuntu16.04 + python3 + django1.11 1...

python3.6+django2.0+mysql搭建网站过程详解

python3.6+django2.0+mysql搭建网站过程详解

之前用过python2.7版本,改用3.6版本发现很多语法发生了变化。 在templates里新建一个html文件,命名为index.html作为要测试的界面, 新建一个应用,Tools...

Python while 循环使用的简单实例

while循环是在Python中的循环结构之一。 while循环继续,直到表达式变为假。表达的是一个逻辑表达式,必须返回一个true或false值,本文章向码农介绍Python whil...