#!/usr/bin/env python # # Authors: # rafael@postgresql.org.es / http://www.postgresql.org.es/ # # Copyright (c) 2014 USIT-University of Oslo import sys import os import time import json import urllib2 import xml.etree.ElementTree as ET # ############################################ # Method: get_temperature_box_info() # # Parameters: Text = temperature box hostname, deviceID # Returns: List data estructure with temperature # box information # # device_info[0] = TempC actual value # device_info[1] = TempC max value # device_info[2] = TempC min value # device_info[3] = TempC alarm max (-255 if not defined) # device_info[4] = TempC alarm min (-255 if not defined) # # ############################################ def get_device_info(temperature_box,device_id): device_info = [] try: minutes_since_last_update = 0 temp_file = '/tmp/' + temperature_box + '_temperature_data.xml' if os.path.exists(temp_file) == True: minutes_since_last_update = (time.time() - os.path.getmtime(temp_file)) / 60 # # Get the XML data from # http:///data.xml only if the local # temp file with the XML code does not exist or if the file is # older that 5 minutes. # if os.path.exists(temp_file) == False or minutes_since_last_update > 5: req = urllib2.Request('http://' + temperature_box + '/data.xml') response = urllib2.urlopen(req,timeout=5) the_page = response.read() with open (temp_file,'w') as xmlfile: xmlfile.write(the_page) tree = ET.parse(temp_file) root = tree.getroot() except Exception as e: print e sys.exit(1) try: # # Generate a list with the information we get from a device # # # Device information # for child in root.findall("./devices/device"): if child.attrib['id'] == device_id: for field in child.findall("field"): if field.attrib['key'] == 'TempC': device_info.append(field.attrib['value']) device_info.append(field.attrib['max']) device_info.append(field.attrib['min']) if len(device_info) > 0: # # Alarm information # if len(root.findall("./alarms/alarm")) == 0: device_info.append('-255') device_info.append('-255') else: for child in root.findall("./alarms/alarm"): if child.attrib['device-id'] == device_id and child.attrib['field'] == 'TempC': device_info.append(child.attrib['max']) device_info.append(child.attrib['min']) else: device_info = [] return device_info except Exception as e: print e sys.exit(1) # ################################################### # Method: get_filsystem_item # # Parameters: Text = temperature box hostname, deviceID, item # Returns: item value # # device_info[0] = TempC actual value # device_info[1] = TempC max value # device_info[2] = TempC min value # device_info[3] = TempC alarm max (-255 if not defined) # device_info[4] = TempC alarm min (-255 if not defined) # # ################################################### def get_device_item(temperature_box,device_id,item): device_info_list = get_device_info(temperature_box,device_id) try: if len(device_info_list) > 0: if item == 'tempc': return float(device_info_list[0]) elif item == 'tempc_max': return float(device_info_list[1]) elif item == 'tempc_min': return float(device_info_list[2]) elif item == 'tempc_alarm_max': return float(device_info_list[3]) elif item == 'tempc_alarm_min': return float(device_info_list[4]) else: return -255 else: sys.exit(1) except Exception as e: print e sys.exit(1) # ############################################ # MAIN # ############################################ if __name__ == '__main__': try: if len(sys.argv) == 4: temperature_box = sys.argv[1].lower() device_id = sys.argv[2] item = sys.argv[3].lower() print get_device_item(temperature_box,device_id,item) else: print "Error: Wrong number of parameters" print 'Format: ' + sys.argv[0] + ' ' sys.exit(1) except Exception as e: print e sys.exit(1)