【今更】PythonでZabbixAPI投げてみた…

何番煎じになるのだろう(笑)
PythonでZabbixAPI投げてみた。

ただ投げるだけはつまらないので、xxx.jsonと言うJson形式のテンプレートファイルを用意してauthとパラメータのみ設定して投げるという方法を考えてみた。

 

下記は時間指定でevent.getを実行します。

```py3

# -*- coding: utf-8 -*- 
import sys, json, urllib2, time, math

result = ""
url = "http://xxx.xxx.xxx.xxx/zabbix/api_jsonrpc.php"
headers = {"Content-Type":"application/json-rpc"}

# get unixtime
def get_unixtime(s):
	format = '%Y-%m-%d %H:%M:%S'

	return str(math.floor(time.mktime(time.strptime(s, format))))

# file read
def get_json(flnm):
	f = open(flnm)
	jdata = json.load(f)
	f.close()

	return jdata

# Zabbix API
def ZabbixAPI(url, post, headers):
	global result

	req = urllib2.Request(url, json.dumps(post), headers)
	res = urllib2.urlopen(req)
	result = json.loads(res.read())

	if "error" in result:
		ret = False
	else:
		ret = True

	return ret

# user.login
def user_login(url, headers, user, password):
	post = get_json("user.login.json")

	params = post["params"]
	params["user"] = user
	params["password"] = password
	post["params"] = params

	return ZabbixAPI(url, post,  headers)

# event.get
def event_get(url, headers, auth, time_from, time_till):
	post = get_json("event.get.json")
	post["auth"] = auth
	params = post["params"]
	params["time_from"] = time_from
	params["time_till"] = time_till

	return ZabbixAPI(url, post,  headers)

# main ------------------------------------------------------
if not user_login(url, headers, "xxxxx", "xxxxx"):
	exit()

auth = result["result"]
time_from = get_unixtime('2015-03-15 17:00:00')
time_till = get_unixtime('2015-03-16 17:00:00')

if not event_get(url, headers, auth, time_from, time_till):
	exit()

print json.dumps(result, sort_keys=False, indent=4)
```

下記の第３引数にユーザ名、第４引数にパスワードを設定してください。

```py3
if not user_login(url, headers, "xxxxx", "xxxxx"):
```

下記がevent.getの開始時刻と終了時刻の設定になります。

```py3
time_from = get_unixtime('2015-03-15 17:00:00')
time_till = get_unixtime('2015-03-16 17:00:00')
```

user.login.json

```json
{
  "method": "user.login",
  "params": {
    "password": "password",
    "user": "user"
  },
  "auth": null,
  "id": 1,
  "jsonrpc": "2.0"
}
```

event.get.json

```json
{
  "method": "event.get",
  "params": {
    "output": "extend",
    "time_from": "time_from",
    "time_till": "time_till"
  },
  "auth": "auth",
  "id": 1,
  "jsonrpc": "2.0"
}
```

実行結果

```json
{
    "jsonrpc": "2.0", 
    "result": [
        {
            "eventid": "11148", 
            "objectid": "13607", 
            "clock": "1426443239", 
            "object": "0", 
            "acknowledged": "0", 
            "value": "1", 
            "source": "0", 
            "ns": "896968674"
        }, 
        {
            "eventid": "11149", 
            "objectid": "13607", 
            "clock": "1426443359", 
            "object": "0", 
            "acknowledged": "0", 
            "value": "0", 
            "source": "0", 
            "ns": "921961452"
        }, 
        {
            "eventid": "11152", 
            "objectid": "13607", 
            "clock": "1426445999", 
            "object": "0", 
            "acknowledged": "0", 
            "value": "1", 
            "source": "0", 
            "ns": "641393521"
        }
    ], 
    "id": 1
}
```