-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzabbix_api.py
62 lines (51 loc) · 2.58 KB
/
zabbix_api.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
from pyzabbix import ZabbixAPI
import time
# Проверка подключения
# answer = z.do_request('apiinfo.version')
# print("Version:",answer['result'])
def time_unix(): #Функция получение настроящего времени - 122 секунды в формате unix (Нас интересует только актуальные значения)
time_unic = int(time.mktime(time.localtime())) - 122
return time_unic
#time_unix = int(time.mktime(time.localtime())) - 122 #Настроящее время - 122 секунды
#print(time_unix)
#time_unix_3m = int(time_unix) - 122
zabbix_server = "http://192.168.1.100/zabbix"
zabbix_user = "user"
zabbix_password = "password"
z = ZabbixAPI(zabbix_server, user=zabbix_user, password=zabbix_password)
def return_last_history(itemids, history):
try:
get = z.history.get(itemids=itemids, sortfield='clock', limit=1, sortorder='DESC', time_from=time_unix(), history=history)
print(get)
get_value = get[0]['value']
return get_value
except IndexError:
print('no data')
#информация по запросам zabbix api:
#https://www.zabbix.com/documentation/current/ru/manual/api/reference/history/get
#примеры:
#return_temp_1 = round(float(return_last_history(30671, 0)),2)
#return_temp_2 = round(float(return_last_history(30654, 0)),2)
# history=
# History object types to return.
# Possible values:
# 0 - numeric float;
# 1 - character;
# 2 - log;
# 3 - numeric unsigned;
# 4 - text
# itemids можно получить в веб морде Zabbix, к примеру навести курсор на "график" в меню "последние данные" нужного хоста, посмотрев ссылку
# так же получать itemids можно в автоматическом режиме через функцию и zabbix.api по запросу из telegram
# https://www.zabbix.com/documentation/current/ru/manual/api/reference/host/get
#Получить список групп:
# groups = z.hostgroup.get(output=['itemid','name'])
# for group in groups:
# print group['groupid'],group['name']
#Получить список хостов в заданной группе (groupids)
# hosts = z.host.get(groupids=4, output=['hostid','name'])
# for host in hosts:
# print(host['hostid'],host['name'])
#Получить список items выбранного хоста (hostids)
# items = z.item.get(hostids=10084, output=['itemid','name'])
# for item in items:
# print(item['itemid'],item['name'])