forked from aigarskadikis/externalscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-host-info-from-item.py
executable file
·70 lines (53 loc) · 1.91 KB
/
update-host-info-from-item.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
#!/usr/bin/python
"""
Shows a list of all current issues (AKA tripped triggers)
"""
#for argument support
import sys
#pip install pyzabbix
from pyzabbix import ZabbixAPI
#import api credentials from different file
sys.path.insert(0,'/var/lib/zabbix')
import config
# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = config.url
zapi = ZabbixAPI(ZABBIX_SERVER)
# Login to the Zabbix API
zapi.login(config.username, config.password)
#query host id by parsing hostname as argument 1
hosts = zapi.host.get (
output=['hostid'],
filter={'host': sys.argv[1]})
#extract hostid from json string. host['hostid'] is array element with string identifier
for host in hosts:
hostid = host['hostid']
print host
#look for last value. key_ is the name of column. sysName is name of 'item key' in zabbix
items = zapi.item.get(
hostids=hostid,
output=['lastvalue'],
filter={'key_':'system.name'})
#assign item lastvalue to dev_name
for item in items:
print item['lastvalue']
dev_name = item['lastvalue']
#if nothing has assigned. if nothing kas placed in variable then python assigns 0 to it
if dev_name == '0':
sys.exit("Sytem name not found !")
#get decription
items = zapi.item.get(
hostids=hostid,
output=['lastvalue'],
filter={'key_':'system.descr'})
#assign last value
for item in items:
dev_desc = item['lastvalue']
items = zapi.item.get(
hostids=hostid,
output=['lastvalue'],
filter={'key_':'system.location'})
#generate full description sentence
for item in items:
dev_desc = 'Location: ' + item['lastvalue'] + '\n\n' + dev_desc
#update host 'Visible name' and 'Description'
zapi.do_request('host.update', {'hostid': hostid,'name': dev_name,'description': dev_desc})