15 # exception raised when timeout occurs
16 TimeoutException = "Timeout"
18 #################### comon-based acquisition
19 def check (hostname, timeout=None):
21 # print "Entering comon_sensor::check",hostname
27 # connect to comon and read data
28 page = read_data (hostname,timeout)
29 # parse lines and store in dict
30 dict = parse_data (page)
31 # keep only relevant stuff and refine parsing
34 return interpret (dict)
36 except TimeoutException:
37 print "While connecting to comon sensor : timeout expired %d s"%timeout
38 ### XXX - in some cases this is a KO, but in general
39 # maybe comon does not run on these nodes
42 ### implement timeout as an alarm signal
43 def alarm_handler(s,closure):
44 if s == signal.SIGALRM:
45 raise TimeoutException
47 print "unexpected signal s in alarm_handler"
50 # returns a list of lines
51 # dont use httplib nor urllib2
52 # the server side replies its data even before you send a GET request
53 # with urllib2 you basically get a 'Connection reset by peer' error
54 def read_data (hostname,timeout,port=None):
59 signal.signal(signal.SIGALRM,alarm_handler)
61 s =socket.socket(socket.AF_INET, socket.SOCK_STREAM)
62 s.connect((hostname,port))
78 ### pattern for interpreting sensor output
79 re_line="^([a-zA-Z0-9_]+): (.*)$"
80 ma_line=re.compile(re_line)
82 def parse_data (page):
85 matched=ma_line.match(line)
87 key,val=matched.groups()
97 'DfDot':'percent-floats',
105 for key in dict.keys():
106 if key in FIELDS_FOCUS.keys():
107 format=FIELDS_FOCUS[key]
108 value=dict[key].rstrip()
110 filtered[key]=map(int,value.split(' '))
111 elif format == 'floats':
112 filtered[key]=map(float,value.split(' '))
113 elif format == 'percent-floats':
115 pval=string.replace(fields[0],'%','')
116 filtered[key]=map(float,[pval]+fields[1:])
117 # simpler access to single-fields
118 value = filtered[key]
120 filtered[key]=value[0]
123 def interpret (dict):
128 ssh_delay = dict['Date']- dict['LastSsh']
136 print "Usage comon_sensor.py node timeout"
139 if __name__=='__main__':
140 if len(sys.argv) != 3:
142 check(sys.argv[1],int(sys.argv[2]))