-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
126 lines (116 loc) · 4.59 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import datetime
import getpass
from zabbix_api import ZabbixApi
from optparse import OptionParser
# General function
def fail(c=""):
print "[Fail] %s" % c
sys.exit(1)
def parse():
parser = OptionParser()
parser.add_option(
'-d', type='string', help='Zabbix server (must)', dest='zabbix')
parser.add_option(
'-m', type='string', help='Method (must)', dest='method')
parser.add_option(
'--file', type='string',
help='Input json file name (option)', dest='file')
parser.add_option(
'--host', type='string', help='Host name (option)', dest='host')
parser.add_option(
'--mapid', type='string', help='Map id (option)', dest='mapid')
parser.add_option(
'--gid', type='int', help='Host group id (option)', dest='gid')
parser.add_option(
'--key', type='string', help='Item key (option)', dest='key')
parser.add_option(
'--itemid', type='int', help='Item ID (option)', dest='itemid')
parser.add_option(
'--time_from', type='string',
help='History time from (%Y/%m/%d %H:%M:%S) (option)',
dest='time_from')
(options, args) = parser.parse_args()
if options.zabbix is None or options.method is None:
parser.print_help()
fail("invalid argument")
return options
if __name__ == '__main__':
options = parse()
password = getpass.getpass()
# target zabbix FIXME
if options.zabbix == 'hoge':
zabbix = 'xxx.xxx.xxx.xxx'
user = 'user'
protocol = 'http or https'
else:
fail('no such zabbix server')
# create instance
api = ZabbixApi(zabbix, user, password, protocol)
# Special function or Input file
if options.file is None:
if options.method == 'host_get_search':
if options.host is None:
fail(
'if execute {0}, --host option is needed'
.format(options.method))
else:
api.host_get_search(options.host)
elif options.method == 'host_get_gid':
if options.gid is None:
fail(
'if execute {0}, --gid option is needed'
.format(options.method))
else:
api.host_get_gid(options.gid)
elif options.method == 'maintenance_create':
if options.host is None:
fail(
'if execute {0}, --host option is needed'
.format(options.method))
else:
api.maintenance_create(options.host)
elif options.method == 'map_create':
if options.gid is None:
fail(
'if execute {0}, --gid option is needed'
.format(options.method))
else:
api.map_create(options.gid)
elif options.method == 'map_delete':
if options.mapid is None:
fail(
'if execute {0}, --mapid option is needed'
.format(options.method))
else:
api.map_delete(options.mapid)
elif options.method == 'map_update':
if options.mapid is None or options.host is None:
fail(
'if execute {0}, --mapid and --host option is needed'
.format(options.method))
else:
api.map_update(options.mapid, options.host)
elif options.method == 'item_get_search':
if options.host is None or options.key is None:
fail(
'if execute {0}, --host and --key option is needed'
.format(options.method))
else:
api.item_get_search(options.host, options.key)
elif options.method == 'history_get':
if options.itemid is None or options.time_from is None:
fail(
'if execute {0}, --itemid and --time_from option is needed'
.format(options.method))
else:
dt = datetime.datetime.strptime(options.time_from, '%Y/%m/%d %H:%M:%S')
epoch = time.mktime(dt.timetuple())
api.history_get(options.itemid, epoch)
else:
fail('no such function')
else:
api.get_byfile(options.file, options.method)