用Python实现Zabbix-API 监控!无处可逃!联网恢恢,疏而不漏!

用Python实现Zabbix-API 监控!无处可逃!联网恢恢,疏而不漏!,第1张

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

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

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

进群:548377875  即可获取数十套pdf哦!

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

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 URLErrornmap_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 = "" nomonitorlst = []  if len(diff) != 0:  for host in diff:  if host in nmaphostlst:  nomonitorlst.append(host)

else:
sys.exit()

print zbxhostlst

string = '
'.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相关信息可以查看官方详细资料,看不懂英文的可以参考下面这篇文档。http://paperplane.ruhoh.com/zabbix/intro-to-zabbix-API/

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

总结

以上是内存溢出为你收集整理的用Python实现Zabbix-API 监控!无处可逃!联网恢恢疏而不漏!全部内容,希望文章能够帮你解决用Python实现Zabbix-API 监控!无处可逃!联网恢恢,疏而不漏!所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1208736.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存