Zabbix Python API Application Combat

Source: Internet
Author: User
Tags diff

Do the monitoring students should know that the company IDC room often has shelves, shelves, repair and scrap server. If the number of servers is very large, it is easy to cause monitoring omission.
Big Internet companies integrate surveillance systems and CMDB (Asset Management System | Configuration Management database systems) together, and when a new machine is listed, the CMDB will record relevant information, Zabbix according to the information in the CMDB automatically link related modules, add | Delete monitoring. Many small companies do not have an asset management system, but the person responsible for monitoring should know what new machines are on the shelves every day, ensuring that they can be added to the Zabbix Monitor.
First of all, let's talk about scripting ideas:
1) scan the network segment with the Nmap tool to scan the IP address that has been used.
2) through Nmap detection has been scanned IP 3389 or 22 port is open, can judge those things Windows machine, those are Linux machines.
3) under Linux, find the Linux hostname via the SSH + hostname command.
4) under Windows, locate the Windows host name by using the NMBLOOKUP-A command.
5) Read the scan result file with the Python script and write the hostname to the list.
6) Use the Zabbix Python API to invoke the monitored host name and write it to the list.
7) Two lists Intersect, using a for loop to determine which host names are not monitored.
8) Email Notification supervisor.

Below I share the Python script I wrote, where scan_machine.sh is the script I wrote about Nmap scanning with the shell, Scan_hostname.log is the result of Nmap scan, the content is the IP host name.

#!/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 =  "\ 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 ()    &nbsP;runcmd (mail_cmd) if __name__ ==  "__main__":       main () 

Add the script to Crontab, and each will receive information about those hosts that are not adding monitoring.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/49/9C/wKiom1QWnH2TqA4TAACr-PgcC_U866.jpg "title=" 1.jpg " alt= "Wkiom1qwnh2tqa4taacr-pgcc_u866.jpg"/>

Summarize:

1) Zabbix API related information can view the official details, can not read English http://paperplane.ruhoh.com/zabbix/intro-to-zabbix-api/this article.

2) through the script can know that those hosts do not add monitoring, hope to help you, if there is a better solution to welcome a lot of communication.







This article is from the "Simple Dreamer" blog, please be sure to keep this source http://sfzhang88.blog.51cto.com/4995876/1552916

Zabbix Python API app actual combat

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.