]> git.uio.no Git - uio-zabbix.git/blame - zabbix_temperature_devices_info.py
Add zabbix autodiscovery script for rabbitmq
[uio-zabbix.git] / zabbix_temperature_devices_info.py
CommitLineData
9a702fb8
RMG
1#!/usr/bin/env python
2#
3# Authors:
4# rafael@postgresql.org.es / http://www.postgresql.org.es/
5#
6# Copyright (c) 2014 USIT-University of Oslo
7
8import sys
9import os
10import time
11import json
12import urllib2
13import xml.etree.ElementTree as ET
14
15# ############################################
16# Method: get_temperature_box_info()
17#
18# Parameters: Text = temperature box hostname, deviceID
19# Returns: List data estructure with temperature
20# box information
21#
22# device_info[0] = TempC actual value
23# device_info[1] = TempC max value
24# device_info[2] = TempC min value
25# device_info[3] = TempC alarm max (-255 if not defined)
26# device_info[4] = TempC alarm min (-255 if not defined)
27#
28# ############################################
29
30def get_device_info(temperature_box,device_id):
31
32 device_info = []
33
34 try:
35 minutes_since_last_update = 0
36 temp_file = '/tmp/' + temperature_box + '_temperature_data.xml'
37
38 if os.path.exists(temp_file) == True:
39 minutes_since_last_update = (time.time() - os.path.getmtime(temp_file)) / 60
40
41 #
42 # Get the XML data from
43 # http://<temperature_box_hostname>/data.xml only if the local
44 # temp file with the XML code does not exist or if the file is
45 # older that 5 minutes.
46 #
47
48 if os.path.exists(temp_file) == False or minutes_since_last_update > 5:
49
50 req = urllib2.Request('http://' + temperature_box + '/data.xml')
51 response = urllib2.urlopen(req,timeout=5)
52 the_page = response.read()
53
54 with open (temp_file,'w') as xmlfile:
55 xmlfile.write(the_page)
56
57 tree = ET.parse(temp_file)
58 root = tree.getroot()
59
60 except Exception as e:
61 print e
62 sys.exit(1)
63
64 try:
65
66 #
67 # Generate a list with the information we get from a device
68 #
69
70 #
71 # Device information
72 #
73 for child in root.findall("./devices/device"):
74
75 if child.attrib['id'] == device_id:
76 for field in child.findall("field"):
77 if field.attrib['key'] == 'TempC':
78 device_info.append(field.attrib['value'])
79 device_info.append(field.attrib['max'])
80 device_info.append(field.attrib['min'])
81
82 if len(device_info) > 0:
83
84 #
85 # Alarm information
86 #
87 if len(root.findall("./alarms/alarm")) == 0:
88 device_info.append('-255')
89 device_info.append('-255')
90
91 else:
92 for child in root.findall("./alarms/alarm"):
93 if child.attrib['device-id'] == device_id and child.attrib['field'] == 'TempC':
94 device_info.append(child.attrib['max'])
95 device_info.append(child.attrib['min'])
96
97 else:
98 device_info = []
99
100 return device_info
101
102 except Exception as e:
103 print e
104 sys.exit(1)
105
106# ###################################################
107# Method: get_filsystem_item
108#
109# Parameters: Text = temperature box hostname, deviceID, item
110# Returns: item value
111#
112# device_info[0] = TempC actual value
113# device_info[1] = TempC max value
114# device_info[2] = TempC min value
115# device_info[3] = TempC alarm max (-255 if not defined)
116# device_info[4] = TempC alarm min (-255 if not defined)
117#
118# ###################################################
119
120def get_device_item(temperature_box,device_id,item):
121
122 device_info_list = get_device_info(temperature_box,device_id)
123
124 try:
125
126 if len(device_info_list) > 0:
127 if item == 'tempc':
128 return float(device_info_list[0])
129
130 elif item == 'tempc_max':
131 return float(device_info_list[1])
132
133 elif item == 'tempc_min':
134 return float(device_info_list[2])
135
136 elif item == 'tempc_alarm_max':
137 return float(device_info_list[3])
138
139 elif item == 'tempc_alarm_min':
140 return float(device_info_list[4])
141
142 else:
143 return -255
144
145 else:
146 sys.exit(1)
147
148 except Exception as e:
149 print e
150 sys.exit(1)
151
152
153
154
155# ############################################
156# MAIN
157# ############################################
158
159if __name__ == '__main__':
160
161 try:
162 if len(sys.argv) == 4:
163 temperature_box = sys.argv[1].lower()
164 device_id = sys.argv[2]
165 item = sys.argv[3].lower()
166
167 print get_device_item(temperature_box,device_id,item)
168
169 else:
170 print "Error: Wrong number of parameters"
171 print 'Format: ' + sys.argv[0] + ' <temperature box hostname> <deviceID> <item>'
172 sys.exit(1)
173
174 except Exception as e:
175 print e
176 sys.exit(1)
177