2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-08-16

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

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

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


# -*- 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)

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

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

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

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

user.login.json

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

event.get.json

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

実行結果

{
    "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
}
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?