1. 程式人生 > >02: zabbix API 基本操作

02: zabbix API 基本操作

clock == ons spa user msi splay mes json-rpc

1.1 zabbix api常用操作

技術分享圖片
#! -*-  coding:utf8 -*-
import urllib2
import json
#定義URL賬戶密碼
url = http://1.1.1.5/zabbix/api_jsonrpc.php
username = Admin
password = 1
header = {"Content-Type": "application/json"}

################################ 一:登陸腳本 login.py  ###########################

#1、定義通過HTTP方式訪問API地址的函數,後面每次請求API的各個方法都會調用這個函數
def requestJson(url,values): data = json.dumps(values) req = urllib2.Request(url, data, {Content-Type: application/json-rpc}) response = urllib2.urlopen(req, data) output = json.loads(response.read()) try: message = output[result] except: message = output[
error][data] print message quit() return output[result] #2、API接口認證的函數,登錄成功會返回一個Token def authenticate(url, username, password): values = {jsonrpc: 2.0, method: user.login, params: { user: username,
password: password }, id: 0 } idvalue = requestJson(url,values) return idvalue # 結果是一個token值:cc75ed2a314906a835ac0786266468ac
1、和2、zabbix認證和發送請求獲取數據 技術分享圖片
# 3、獲取所有報警信息
def trigger_get():
    values = (
        {
            "jsonrpc": "2.0",
            "method": "trigger.get",
            "params": {
                "output": [
                    "host",
                    "description",
                    "triggerid",
                    "eventid",
                    "templateids"
                ],
                "selectGroups": [
                    "name"
                ],
                "selectHosts": [
                    "name",
                    "host"
                ],
                "selectItems": [
                    "name",
                    "lastvalue",
                    "units"
                ],
                "filter": {
                    "value": 1
                },
                "monitored": 1,
                "selectLastEvent": "extend",
                "expandComment": 1,
                "expandDescription": 1,
                "sortfield": "priority",
                "sortorder": "DESC",
                "withLastEventUnacknowledged":1
            },
            "auth":auth,  # 獲取認證session值
            "id": 1
        }
    )
    result = requestJson(url,values)
    return result
3、獲取所有報警信息 技術分享圖片
# 4、獲取所有報警信息(簡單信息)
def triggerget():
    values = {jsonrpc: 2.0,
           "method":"trigger.get",
               "params": {
                        "output": [
                        "triggerid",
                        "description",
                        "priority"
                        ],
              "filter": {
                         "value": 1
                         },
              "expandData":"hostname",
              "sortfield": "priority",
              "sortorder": "DESC"
            },
              auth: auth,
              id: 1,
              }
    output = requestJson(url,values)
    return output
‘‘‘ 獲取簡單的報警信息
[{
    u ‘priority‘: u ‘3‘,
    u ‘triggerid‘: u ‘15579‘,
    u ‘description‘: u ‘登錄用戶超過2人報警測試‘
}, {
    u ‘priority‘: u ‘3‘,
    u ‘triggerid‘: u ‘13491‘,
    u ‘description‘: u ‘Zabbix agent on {HOST.NAME} is unreachable for 5 minutes‘
}]
‘‘‘
4、獲取所有報警信息(簡單信息) 技術分享圖片
# 5、定義通過ip獲取主機id的函數
def ipgetHostsid(ip=1.1.1.3):
    values = {jsonrpc: 2.0,
              method: host.get,
              params: {
                  output: [ "host" ],
                  filter: {
                      ip: ip
                  },
              },
              auth: auth,
              id: 3
              }
    output = requestJson(url,values)
    return output  # [{u‘host‘: u‘1.1.1.3‘, u‘hostid‘: u‘10255‘}]
5、定義通過ip獲取主機id的函數 技術分享圖片
# 6、定義通過主機id獲取開啟關閉agent監控
def idupdatehost(status=0,hostid=None):
    # status=0 啟用agent,status=1 禁用agent
    hostid = ipgetHostsid(ip=1.1.1.3)[0][hostid]
    values = {jsonrpc: 2.0,
              method: host.update,
              params: {
                  "hostid": hostid,
                  "status": status
              },
              auth: auth,
              id: 4
              }
    output = requestJson(url,values)
    return output
6、定義通過主機id獲取開啟關閉agent監控 技術分享圖片
# 7、定義通過項目hostid獲取itemid函數
def getHostsitemsid(hostid=None, itemsname=None):  # 查詢items表
    hostid = ipgetHostsid(ip=1.1.1.3)[0][hostid]
    itemsname = log_user
    values = {jsonrpc: 2.0,
              method: "item.get",
              "params": {
                  "output": ["itemids","name","key_"],  # 輸出什麽值可以在這裏填寫
                  "hostids": hostid,
                  "filter": {   # 如果沒有給 filter 參數那麽就會過濾出這個主機關聯的所有item
                      "key_": itemsname,
                  },
              },

              auth: auth,
              id: 5
              }
    output = requestJson(url, values)
    if len(output) == 0:
        return output
    else:
        return output[0][itemid]  # 28337 獲取
        # return output  # [{u‘itemid‘: u‘28337‘, u‘key_‘: u‘log_user‘, u‘name‘: u‘login_user‘}]
7、定義通過項目hostid獲取itemid函數 技術分享圖片
# 8、定義通過itemid獲取監控項目最近n次,值信息的函數
def getHostsitemsvalue(itemid=None):
    itemid = getHostsitemsid()
    values = {jsonrpc: 2.0,
              method: "history.get",
              "params": {
                  "output": "extend",
                  "history": 3,
                  "itemids": itemid,
                  "sortfield": "clock",
                  "sortorder": "DESC",
                  "limit": 1,  # 限制顯示多少條歷史記錄
              },

              auth: auth,
              id: 6
              }
    output = requestJson(url, values)
    if len(output) == 0:
        return output
    else:
        # return output[0]["value"]  # 3    這個監控項獲取的最近一次值為 3
        return output  # [{u‘itemid‘: u‘28337‘, u‘ns‘: u‘205638409‘, u‘value‘: u‘3‘, u‘clock‘: u‘1527743117‘}]
8、定義通過itemid獲取監控項目最近n次,值信息的函數 技術分享圖片
# 9、定義通過 組名 獲取groupid 等信息
def groupnameGroupid(groupname=None):
    groupname = Nginx
    values = {jsonrpc: 2.0,
              method: hostgroup.get,
              "params": {
                    "output": "extend",
                    "filter": {
                        "name": [
                            groupname
                        ]
                    }
                },
              auth: auth,
              id: 11
              }
    output = requestJson(url,values)
    return output  # [{u‘internal‘: u‘0‘, u‘flags‘: u‘0‘, u‘groupid‘: u‘15‘, u‘name‘: u‘Nginx‘}]
9、定義通過 組名 獲取groupid 等信息 技術分享圖片
# 10、定義通過hostid 和 圖形名稱 獲取圖形id graphid的函數
def getgraphid(hostid=None,graphname=None):
    graphname = loguser
    values = {jsonrpc: 2.0,
                      method: graph.get,
                      params: {
                              "output": [graphid,name],
                              # "output": "extend",
                              "hostids": hostid,
                              "sortfield": "name",
                      filter: {
                                    "name": graphname
                              },

                      },
                      auth: auth,
                      id: 14
                      }
    output = requestJson(url,values)
    return output  # [{u‘graphid‘: u‘811‘, u‘name‘: u‘loguser‘}]
10、定義通過hostid 和 圖形名稱 獲取圖形id graphid的函數 技術分享圖片
# 11、獲取這個zabbix server監控的所有主機信息
def getallhost():
    values = {
            "jsonrpc": "2.0",
            "method": "host.get",
            "params": {
                "output": [
                    "hostid",
                    "host"
                ],
                "selectInterfaces": [
                    "interfaceid",
                    "ip"
                ]
            },
            "id": 2,
            "auth": auth
        }
    output = requestJson(url,values)
    return output
‘‘‘
[{
    u ‘host‘: u ‘Zabbix server‘,
    u ‘hostid‘: u ‘10084‘,
    u ‘interfaces‘: [{
        u ‘interfaceid‘: u ‘1‘,
        u ‘ip‘: u ‘1.1.1.5‘
    }]
}, {
    u ‘host‘: u ‘1.1.1.3‘,
    u ‘hostid‘: u ‘10255‘,
    u ‘interfaces‘: [{
        u ‘interfaceid‘: u ‘3‘,
        u ‘ip‘: u ‘1.1.1.3‘
    }]
}]
‘‘‘
11、獲取這個zabbix server監控的所有主機信息

02: zabbix API 基本操作