Enh: (Grégory Starck) still code clean for pyro wrapper in satellitelinks.
[shinken.git] / shinken / util.py
blobf71a87821ca99a8e07ab66275773be06899f6386
1 #!/usr/bin/env python
2 #Copyright (C) 2009-2010 :
3 # Gabes Jean, naparuba@gmail.com
4 # Gerhard Lausser, Gerhard.Lausser@consol.de
6 #This file is part of Shinken.
8 #Shinken is free software: you can redistribute it and/or modify
9 #it under the terms of the GNU Affero General Public License as published by
10 #the Free Software Foundation, either version 3 of the License, or
11 #(at your option) any later version.
13 #Shinken is distributed in the hope that it will be useful,
14 #but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 #GNU Affero General Public License for more details.
18 #You should have received a copy of the GNU Affero General Public License
19 #along with Shinken. If not, see <http://www.gnu.org/licenses/>.
21 import time, calendar, re
22 try:
23 from ClusterShell.NodeSet import NodeSet
24 except ImportError:
25 NodeSet = None
27 from shinken.macroresolver import MacroResolver
28 #from memoized import memoized
31 ################################### TIME ##################################
32 #@memoized
33 def get_end_of_day(year, month_id, day):
34 end_time = (year, month_id, day, 23, 59, 59, 0, 0, -1)
35 end_time_epoch = time.mktime(end_time)
36 return end_time_epoch
39 #@memoized
40 def print_date(t):
41 return time.asctime(time.localtime(t))
44 #@memoized
45 def get_day(t):
46 return int(t - get_sec_from_morning(t))
49 #@memoized
50 def get_sec_from_morning(t):
51 t_lt = time.localtime(t)
52 h = t_lt.tm_hour
53 m = t_lt.tm_min
54 s = t_lt.tm_sec
55 return h * 3600 + m * 60 + s
58 #@memoized
59 def get_start_of_day(year, month_id, day):
60 start_time = (year, month_id, day, 00, 00, 00, 0, 0, -1)
61 start_time_epoch = time.mktime(start_time)
62 return start_time_epoch
65 #change a time in seconds like 3600 into a format : 0d 1h 0m 0s
66 def format_t_into_dhms_format(t):
67 s = t
68 m,s=divmod(s,60)
69 h,m=divmod(m,60)
70 d,h=divmod(h,24)
71 return '%sd %sh %sm %ss' % (d, h, m, s)
74 ################################# Pythonization ###########################
75 #first change to foat so manage for example 25.0 to 25
76 def to_int(val):
77 return int(float(val))
79 def to_float(val):
80 return float(val)
82 def to_char(val):
83 return val[0]
85 def to_split(val):
86 val = val.split(',')
87 if val == ['']:
88 val = []
89 return val
91 #bool('0') = true, so...
92 def to_bool(val):
93 if val == '1':
94 return True
95 else:
96 return False
98 def from_bool_to_string(b):
99 if b :
100 return '1'
101 else:
102 return '0'
104 def from_bool_to_int(b):
105 if b :
106 return 1
107 else:
108 return 0
110 def from_list_to_split(val):
111 val = ','.join(['%s' % v for v in val])
112 return val
114 def from_float_to_int(val):
115 val = int(val)
116 return val
119 ### Functions for brok_transformations
120 ### They take 2 parameters : ref, and a value
121 ### ref is the item like a service, and value
122 ### if the value to preprocess
124 # Just a string list of all names, with ,
125 def to_list_string_of_names(ref, tab):
126 return ",".join([e.get_name() for e in tab])
128 # take a list of hosts and return a list
129 # of all host_names
130 def to_hostnames_list(ref, tab):
131 r = []
132 for h in tab:
133 if hasattr(h, 'host_name'):
134 r.append(h.host_name)
135 return r
137 # Will create a dict with 2 lists:
138 # *services : all services of the tab
139 # *hosts : all hosts of the tab
140 def to_svc_hst_distinct_lists(ref, tab):
141 r = {'hosts' : [], 'services' : []}
142 for e in tab:
143 cls = e.__class__
144 if cls.my_type == 'service':
145 name = e.get_dbg_name()
146 r['services'].append(name)
147 else:
148 name = e.get_dbg_name()
149 r['hosts'].append(name)
150 return r
153 # Will expaand the value with macros from the
154 # host/service ref before brok it
155 def expand_with_macros(ref, value):
156 return MacroResolver().resolve_simple_macros_in_string(value, ref.get_data_for_checks())
159 # Just get the string name of the object
160 # (like for realm)
161 def get_obj_name(obj):
162 return obj.get_name()
165 ###################### Sorting ################
166 def scheduler_no_spare_first(x, y):
167 if x.spare and not y.spare:
168 return 1
169 elif x.spare and y.spare:
170 return 0
171 else:
172 return -1
175 #-1 is x first, 0 equal, 1 is y first
176 def alive_then_spare_then_deads(x, y):
177 #First are alive
178 if x.alive and not y.alive:
179 return -1
180 if y.alive and not x.alive:
181 return 0
182 #if not alive both, I really don't care...
183 if not x.alive and not y.alive:
184 return -1
185 #Ok, both are alive... now spare after no spare
186 if not x.spare:
187 return -1
188 #x is a spare, so y must be before, even if
189 #y is a spare
190 if not y.spare:
191 return 1
192 return 0
194 #-1 is x first, 0 equal, 1 is y first
195 def sort_by_ids(x, y):
196 if x.id < y.id:
197 return -1
198 if x.id > y.id:
199 return 1
200 #So is equal
201 return 0
205 ##################### Cleaning ##############
206 def strip_and_uniq(tab):
207 new_tab = set()
208 for elt in tab:
209 new_tab.add(elt.strip())
210 return list(new_tab)
214 #################### Patern change application (mainly for host) #######
216 def expand_xy_patern(pattern):
217 ns = NodeSet(pattern)
218 if len(ns) > 1:
219 for elem in ns:
220 for a in expand_xy_patern(elem):
221 yield a
222 else:
223 yield pattern
228 #This function is used to generate all patern change as
229 #recursive list.
230 #for example, for a [(1,3),(1,4),(1,5)] xy_couples,
231 #it will generate a 60 item list with:
232 #Rule: [1, '[1-5]', [1, '[1-4]', [1, '[1-3]', []]]]
233 #Rule: [1, '[1-5]', [1, '[1-4]', [2, '[1-3]', []]]]
234 #...
235 def got_generation_rule_patern_change(xy_couples):
236 res = []
237 xy_cpl = xy_couples
238 if xy_couples == []:
239 return []
240 (x, y) = xy_cpl[0]
241 for i in xrange(x, y+1):
242 n = got_generation_rule_patern_change(xy_cpl[1:])
243 if n != []:
244 for e in n:
245 res.append( [i, '[%d-%d]'%(x,y), e])
246 else:
247 res.append( [i, '[%d-%d]'%(x,y), []])
248 return res
251 #this fuction apply a recursive patern change
252 #generate by the got_generation_rule_patern_change
253 #function.
254 #It take one entry of this list, and apply
255 #recursivly the change to s like :
256 #s = "Unit [1-3] Port [1-4] Admin [1-5]"
257 #rule = [1, '[1-5]', [2, '[1-4]', [3, '[1-3]', []]]]
258 #output = Unit 3 Port 2 Admin 1
259 def apply_change_recursive_patern_change(s, rule):
260 #print "Try to change %s" % s, 'with', rule
261 new_s = s
262 (i, m, t) = rule
263 #print "replace %s by %s" % (r'%s' % m, str(i)), 'in', s
264 s = s.replace(r'%s' % m, str(i))
265 #print "And got", s
266 if t == []:
267 return s
268 return apply_change_recursive_patern_change(s, t)
271 #For service generator, get dict from a _custom properties
272 #as _disks C$(80%!90%),D$(80%!90%)$,E$(80%!90%)$
273 #return {'C' : '80%!90%', 'D' : '80%!90%', 'E' : '80%!90%'}
274 #And if we have a key that look like [X-Y] we will expand it
275 #into Y-X+1 keys
276 GET_KEY_VALUE_SEQUENCE_ERROR_NOERROR = 0
277 GET_KEY_VALUE_SEQUENCE_ERROR_SYNTAX = 1
278 GET_KEY_VALUE_SEQUENCE_ERROR_NODEFAULT = 2
279 GET_KEY_VALUE_SEQUENCE_ERROR_NODE= 3
280 def get_key_value_sequence(entry, default_value=None):
281 array1 = []
282 array2 = []
283 conf_entry = entry
285 # match a key$(value1..n)$
286 keyval_pattern_txt = r"""
287 \s*(?P<key>[^,]+?)(?P<values>(\$\(.*?\)\$)*)(?:[,]|$)
289 keyval_pattern = re.compile('(?x)' + keyval_pattern_txt)
290 # match a whole sequence of key$(value1..n)$
291 all_keyval_pattern = re.compile('(?x)^(' + keyval_pattern_txt + ')+$')
292 # match a single value
293 value_pattern = re.compile('(?:\$\((?P<val>.*?)\)\$)')
294 # match a sequence of values
295 all_value_pattern = re.compile('^(?:\$\(.*?\)\$)+$')
297 if all_keyval_pattern.match(conf_entry):
298 for mat in re.finditer(keyval_pattern, conf_entry):
299 r = { 'KEY' : mat.group('key') }
300 # The key is in mat.group('key')
301 # If there are also value(s)...
302 if mat.group('values'):
303 if all_value_pattern.match(mat.group('values')):
304 # If there are multiple values, loop over them
305 valnum = 1
306 for val in re.finditer(value_pattern, mat.group('values')):
307 r['VALUE' + str(valnum)] = val.group('val')
308 valnum += 1
309 else:
310 # Value syntax error
311 return (None, GET_KEY_VALUE_SEQUENCE_ERROR_SYNTAX)
312 else:
313 r['VALUE1'] = None
314 array1.append(r)
315 else:
316 # Something is wrong with the values. (Maybe unbalanced '$(')
317 # TODO: count opening and closing brackets in the pattern
318 return (None, GET_KEY_VALUE_SEQUENCE_ERROR_SYNTAX)
320 # now fill the empty values with the default value
321 for r in array1:
322 if r['VALUE1'] == None:
323 if default_value == None:
324 return (None, GET_KEY_VALUE_SEQUENCE_ERROR_NODEFAULT)
325 else:
326 r['VALUE1'] = default_value
327 r['VALUE'] = r['VALUE1']
329 #Now create new one but for [X-Y] matchs
330 # array1 holds the original entries. Some of the keys may contain wildcards
331 # array2 is filled with originals and inflated wildcards
332 #import time
333 #t0 = time.time()
334 #NodeSet = None
335 if NodeSet == None:
336 #The patern that will say if we have a [X-Y] key.
337 pat = re.compile('\[(\d*)-(\d*)\]')
339 for r in array1:
341 key = r['KEY']
342 orig_key = r['KEY']
344 #We have no choice, we cannot use NodeSet, so we use the
345 #simple regexp
346 if NodeSet == None:
347 m = pat.search(key)
348 got_xy = (m != None)
349 else: # Try to look with a nodeset check directly
350 try:
351 ns = NodeSet(key)
352 #If we have more than 1 element, we have a xy thing
353 got_xy = (len(ns) != 1)
354 except NodeSetParseRangeError:
355 return (None, GET_KEY_VALUE_SEQUENCE_ERROR_NODE)
356 pass # go in the next key
358 #Now we've got our couples of X-Y. If no void,
359 #we were with a "key generator"
361 if got_xy:
362 #Ok 2 cases : we have the NodeSet lib or not.
363 #if not, we use the dumb algo (quick, but manage less
364 #cases like /N or , in paterns)
365 if NodeSet == None: #us the old algo
366 still_loop = True
367 xy_couples = [] # will get all X-Y couples
368 while still_loop:
369 m = pat.search(key)
370 if m != None: # we've find one X-Y
371 (x,y) = m.groups()
372 (x,y) = (int(x), int(y))
373 xy_couples.append((x,y))
374 #We must search if we've gotother X-Y, so
375 #we delete this one, and loop
376 key = key.replace('[%d-%d]' % (x,y), 'Z'*10)
377 else:#no more X-Y in it
378 still_loop = False
380 #Now we have our xy_couples, we can manage them
382 #We search all patern change rules
383 rules = got_generation_rule_patern_change(xy_couples)
385 #Then we apply them all to get ours final keys
386 for rule in rules:
387 res = apply_change_recursive_patern_change(orig_key, rule)
388 new_r = {}
389 for key in r:
390 new_r[key] = r[key]
391 new_r['KEY'] = res
392 array2.append(new_r)
394 else:
395 #The key was just a generator, we can remove it
396 #keys_to_del.append(orig_key)
398 #We search all patern change rules
399 #rules = got_generation_rule_patern_change(xy_couples)
400 nodes_set = expand_xy_patern(orig_key)
401 new_keys = list(nodes_set)
403 #Then we apply them all to get ours final keys
404 for new_key in new_keys:
405 #res = apply_change_recursive_patern_change(orig_key, rule)
406 new_r = {}
407 for key in r:
408 new_r[key] = r[key]
409 new_r['KEY'] = new_key
410 array2.append(new_r)
411 else:
412 # There were no wildcards
413 array2.append(r)
416 #t1 = time.time()
417 #print "***********Diff", t1 -t0
419 return (array2, GET_KEY_VALUE_SEQUENCE_ERROR_NOERROR)