基于zabbix API添加监控主机

  1. 因为zabbix监控的主机虽为同一个业务,可是因为其跨机房而且网络为为16位,两个机房致使zabbix的自动添加扫描的主机数量就差很少有12w多个,严重影响其效率和性能.php

  2. 使用zabbix API的基本步骤以下:python

    1. 链接http://x.x.x.x/api_jsonrpc.php,(在zabbix网页文件的目录下为api_jsonrpc.php),提供用户名和密码,并标识HTTP头部"Content-Type":"application/json",HTTP方法为post.json

    2. 获取SESSIONapi

    3. 经过SESSION创建后续链接.服务器

    4. 提交POST数据,格式为JSON,其中存放对应的API方法,获取(提交)须要的数据网络

  3. 添加HOST示例(用python写的示例,python>=2.6)app

  4. #!/usr/bin/env python
    #coding=utf-8
    
    import json
    import urllib2
    import sys
    from urllib2 import Request,urlopen,URLError,HTTPError
    #zabbix的地址,用户名,密码
    zabbix_url="http://x.x.x.x/api_jsonrpc.php"
    zabbix_header={"Content-Type":"application/json"}
    zabbix_user="admin"
    zabbix_pass="zabbix"
    auth_code=""
    #用户认证信息,最终目的是须要获得一个SESSIONID
    #下面是生成一个JSON格式的数据:用户名和密码
    auth_data=json.dumps(
    {
      "jsonrpc":"2.0",
      "method":"user.login",
      "params":
          {
            "user":zabbix_user,
            "password":zabbix_pass
          },
      "id":0
    })
    request = urllib2.Request(zabbix_url,auth_data)
    for key in zabbix_header:
      request.add_header(key,zabbix_header[key])
    try:
      result = urllib2.urlopen(request)
    except HTTPError,e:
      print 'The server couldn\'t fulfill the request,Error code: ' ,e.code
    except URLError,e:
      print 'We failed to reach a server.Reason:',e.reason
    else:
      response=json.loads(result.read())
      result.close()
    
    if 'result' in response:
      auth_code=response['result']
    else:
      print response['error']['data']
    #下面是API的请求,方法是host.create建立一个主机,具体API的方法能够参考官网的,上面很全
    json_data={
      "method":"host.create",
      "params":{'groups':[{'groupid':'8'}],
                'host':'192.168.2.208',
                'proxy_hostid':'10107',  #代理服务器
                'interfaces':[{'dns':'',
                               'ip':'192.168.2.208',
                               'main':1,
                               'port':'10050',
                               'type':1,
                               'useip':1
                             }],
                'templates':[{'templateid':'10429'},{'templateid':'10129'}] #用到的模板
          
         }
    }
    
    json_base={
      "jsonrpc":"2.0",
      "auth":auth_code,
      "id":1
    }
    
    json_data.update(json_base)
    
    if len(auth_code) == 0:
      sys.exit(1)
    if len(auth_code) != 0:
      get_host_data = json.dumps(json_data)
      request = urllib2.Request(zabbix_url,get_host_data)
      for key in zabbix_header:
        request.add_header(key,zabbix_header[key])
      try:
        result = urllib2.urlopen(request)
      except URLError as e:
        if hasattr(e,'reason'):
          print 'We failed to reach a server'
          print 'Reason:',e.reason
        elif hasattr(e,'code'):
          print 'The server could not fulfill the request'
          print 'Error code:',e.code
      else:
        response = json.loads(result.read())
        result.close()
        print response
        print "Number of hosts",len(response['result'])

5.其实主要仍是python和API的使用方法.提供一个思路,至于如何批量操做,只须要从这里扩展就好了,文章参考吴兆松的<<Zabbix企业级分布式监控系统>>,这书仍是挺不错的.嘿嘿....分布式

相关文章
相关标签/搜索