forked from tianer1211/zabbix_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkTemplate.py
69 lines (61 loc) · 2.4 KB
/
linkTemplate.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
# -*- encoding: utf-8 -*-
import sys
import argparse
from zabbix.api import ZabbixAPI
from lib.configParse import configParse
config_file = './conf/zabbix_api.conf'
def getHostID(hostName=None):
try:
hostid = zapi.get_id('host', hostName)
return hostid
except:
print "Error: No hostid found on hostname %s" % hostName
sys.exit(1)
def getTemplateID(templateName=None):
try:
templateID = zapi.get_id('template', templateName)
return templateID
except:
print "Error: No templateid found on template %s" % templateName
sys.exit(1)
def linkTemplate(hostName=None, templateName=None):
templateID = getTemplateID(templateName)
hostID = getHostID(hostName)
if templateID is None or hostID is None:
print "Error: Can not link Template '%s' on Host '%s' because no tempalteID or HostID; Returned templateid is '%s', hostid is '%s" % (
templateName, hostName, templateID, hostID)
sys,exit(1)
data = {
'hosts': [{
'hostid': hostID
}],
'templates': [{
'templateid': templateID
}]
}
try:
result = zapi.do_request('template.massadd', data)
try:
templateids = result['result']['templateids']
print "Info: Successfully link template %s to host %s" % (hostName, templateName)
except:
print "Warning: uncorrected returned value, it may fail to link template '%s' to host '%s', returned data is '%s' please check your dashboard" % (templateName, hostName, result)
except Exception as e:
print "Error: Failed to link template %s to host %s; Error Message: %s" % (templateName, hostName, e)
def main():
global config
global zapi
config = configParse(config_file)
config.parse()
zapi = ZabbixAPI(url=config.zabbix_url, user=config.zabbix_user, password=config.zabbix_passwd)
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('-l','--link-template', help='Provide template name if you need link template')
parser.add_argument('-H','--host-name', help='Provides hostname to link template')
args = parser.parse_args()
if args.link_template:
linkTemplate(hostName=args.host_name, templateName=args.link_template)
else:
print "Warning: Check your parameters!"
sys.exit(1)
if __name__ == '__main__':
main()