def get_metric_and_value(self, perf_data): res = [] metrics = PerfDatas(perf_data) for e in metrics: #try: # logger.debug("[Graphite broker] Groking: %s" % str(e)) #except UnicodeEncodeError: # pass name = self.illegal_char.sub('_', e.name) name = self.multival.sub(r'.\1', name) # get metric value and its thresholds values if they exist name_value = {name: e.value} if e.warning and e.critical: name_value[name + '_warn'] = e.warning name_value[name + '_crit'] = e.critical # bailout if need if name_value[name] == '': continue #try: # logger.debug("[Graphite broker] End of grok: %s, %s" % (name, str(e.value))) #except UnicodeEncodeError: # pass for key, value in name_value.items(): res.append((key, value)) return res
def get_metric_and_value(self, service, perf_data): result = [] metrics = PerfDatas(perf_data) # Separate perfdata multiple values multival = re.compile(r'_(\d+)$') for e in metrics: name = multival.sub(r'.\1', e.name) # bailout if no value if name == '': continue # get metric value and its thresholds values if they exist metric = dict(name=name, uom=e.uom) # Get or ignore extra values depending upon module configuration for s in ('warning', 'critical', 'min', 'max'): if getattr(e, s) is not None and getattr(self, 'use_%s' % s): metric[s] = getattr(e, s) result.append(metric) logger.debug("[Graphite UI] get_metric_and_value: %s", result) return result
def get_printer(h): s = _findServiceByName(h, 'printer') if not s: return 'UNKNOWN',0 print "Service found", s.get_full_name() printed_pages = 0 printer_state = s.state # Now perfdata p = PerfDatas(s.perf_data) # p = PerfDatas("'CutPages'=12[c];;;; 'RetractedPages'=8[c];;;;") print "PERFDATA", p, p.__dict__ if 'CutPages' in p: m = p['CutPages'] if m.name and m.value is not None: printed_pages = m.value if 'Cut Pages' in p: m = p['Cut Pages'] if m.name and m.value is not None: printed_pages = m.value # if 'Retracted Pages' in p: # m = p['Retracted Pages'] # if m.name and m.value is not None: # retracted = m.value return printer_state, printed_pages
def manage_check_ping_command(elt): safe_print('Get check_ping perfdata of', elt.get_full_name()) p = PerfDatas(elt.perf_data) if not 'rta' in p: print "No rta in p" return None m = p['rta'] v = m.value crit = m.critical if not v or not crit: print "No value, I bailout" return None # Percent of ok should be the log of time versus max/2 pct = get_logarithmic(v, crit / 2) # Now get the color # OK: #6f2 (102,255,34) green # Warning: #f60 (255,102,0) orange # Crit: #ff0033 (255,0,51) base_color = {0: (102, 255, 34), 1: (255, 102, 0), 2: (255, 0, 51)} state_id = get_stateid(elt) color = base_color.get(state_id, (179, 196, 255)) s_color = 'RGB(%d,%d,%d)' % color lnk = '#' metrics = [(s_color, pct), ('white', 100-pct)] title = '%sms' % v print "HTTP: return", {'lnk': lnk, 'metrics': metrics, 'title': title} return {'lnk': lnk, 'metrics': metrics, 'title': title}
def process_perfdata(obj, s, prefix=''): p = PerfDatas(s) if prefix: prefix += '_' checks = [] for metric in p: # print 'm,M', metric.value, metric.min, metric.max if float(metric.value) < float(metric.min): return 'UNKNOWN', '%s=%s%s below min(%s%s)' % ( metric.name, metric.value, metric.uom, metric.min, metric.uom) if float(metric.value) > float(metric.max): return 'UNKNOWN', '%s=%s%s above max(%s%s)' % ( metric.name, metric.value, metric.uom, metric.max, metric.uom) thresholds_name = '%s%s_thresholds' % (prefix, metric.name) if hasattr(obj, thresholds_name): warning, critical = getattr(obj, thresholds_name).split(',') # print 'w,c', warning, critical checks.append(check_value(metric, metric.name, warning, critical)) if CRITICAL in [chk[0] for chk in checks]: return 'CRITICAL', 'CRITICAL - ' + ' '.join( [chk[1] for chk in checks if chk[0] == CRITICAL]) if WARNING in [chk[0] for chk in checks]: return 'WARNING', 'WARNING - ' + ' '.join( [chk[1] for chk in checks if chk[0] == WARNING]) return 'OK', 'OK - ' + ' '.join([chk[1] for chk in checks])
def get_memory(h): all = {} state = 'UNKNOWN' s = _findServiceByName(h, params['svc_mem_name']) if s: logger.debug("[WebUI-cvhost], found %s", s.get_full_name()) state = s.state try: p = PerfDatas(s.perf_data) for m in p: if m.name and m.value is not None: logger.debug("[WebUI-cvhost], metric '%s' = %s, uom: %s", m.name, m.value, m.uom) if re.search(params['svc_mem_used'], m.name) and \ re.match(params['svc_mem_uom'], m.uom): logger.debug("[WebUI-cvhost], got '%s' = %s", m.name, m.value) all[m.name] = m.value except Exception as exp: logger.warning("[WebUI-cvhost] get_memory, exception: %s", str(exp)) logger.debug("[WebUI-cvhost], get_memory %s", all) return state, all
def manage_check_http_command(elt, metric='time'): logger.debug("[WebUI] Get check_http perfdata of %s", elt.get_full_name()) p = PerfDatas(elt.perf_data) if not metric in p: return None m = p[metric] v = m.value if not v: return None # Percent of ok should be time/1s pct = get_logarithmic(v, 1) # Now get the color # OK: #6f2 (102,255,34) green # Warning: #f60 (255,102,0) orange # Crit: #ff0033 (255,0,51) base_color = {0: (102, 255, 34), 1: (255, 102, 0), 2: (255, 0, 51)} state_id = get_stateid(elt) color = base_color.get(state_id, (179, 196, 255)) s_color = 'RGB(%d,%d,%d)' % color lnk = '#' metrics = [(s_color, pct), ('white', 100 - pct)] title = '%ss' % v #print "HTTP: return", {'lnk': lnk, 'metrics': metrics, 'title': title} return {'lnk': lnk, 'metrics': metrics, 'title': title}
def get_memory(h): mem_state = swap_state = 'UNKNOWN' s = _findServiceByName(h, 'memory') if not s: return (mem_state,swap_state,0,0) print "Service found", s.get_full_name() mem_state = swap_state = s.state # Now grep perfdata in it p = PerfDatas(s.perf_data) mem = 0 swap = 0 if 'ram_used' in p: m = p['ram_used'] # Maybe it's an invalid metric? if m.name and m.value is not None and m.max is not None and m.max != 0: # Classic pct compute pct = 100*float(m.value)/m.max mem = int(pct) if 'swap_used' in p: m = p['swap_used'] # Maybe it's an invalid metric? if m.name and m.value is not None and m.max is not None and m.max != 0: # Classic pct compute pct = 100*float(m.value)/m.max swap = int(pct) return mem_state,swap_state,mem,swap
def manage_check_tcp_command(elt, metric='time'): logger.debug("[WebUI] Get check_tcp perfdata of %s", elt.get_full_name()) p = PerfDatas(elt.perf_data) if not metric in p: return None m = p[metric] v = m.value if not v or not m.max: return None # Percent of ok should be the log of time versus m.max / 2 pct = get_logarithmic(v, m.max / 2) # Now get the color # OK: #6f2 (102,255,34) green # Warning: #f60 (255,102,0) orange # Crit: #ff0033 (255,0,51) base_color = {0: (102, 255, 34), 1: (255, 102, 0), 2: (255, 0, 51)} state_id = get_stateid(elt) color = base_color.get(state_id, (179, 196, 255)) s_color = 'RGB(%d,%d,%d)' % color #pct = 100 * (v / m.max) # Convert to int #pct = int(pct) # Minimum 1%, maximum 100% #pct = min(max(1, pct), 100) lnk = '#' metrics = [(s_color, pct), ('white', 100 - pct)] title = '%ss' % v #print "HTTP: return", {'lnk': lnk, 'metrics': metrics, 'title': title} return {'lnk': lnk, 'metrics': metrics, 'title': title}
def find_disks(h): all_disks = [] disks_state = 'UNKNOWN' s = h.find_service_by_name('Disks') print "Service found", s.get_full_name() if not s: return disks_state, all_disks disks_state = s.state p = PerfDatas(s.perf_data) print "PERFDATA", p, p.__dict__ for m in p: print "KEY", m # Skip void disks? if not m.name or m.value is None or m.max is None or m.max == 0: continue # Skip device we don't care about if m.name == '/dev' or m.name.startswith('/sys/'): continue pct = 100 * float(m.value) / m.max pct = int(pct) print m.value, m.max, pct all_disks.append((m.name, pct)) return disks_state, all_disks
def get_memory(h): # Windows : 'physical memory %'=51%;80;90 'physical memory'=6.165G;9.54599;10.74;0;11.933 'virtual memory %'=0%;80;90 'virtual memory'=365.539M;6710886.3;7549747.087;0;8388607.875 'paged bytes %'=29%;80;90 'paged bytes'=7.04399G;19.092;21.478;0;23.864 'p mem_state = virtual_state = paged_state = 'UNKNOWN' s = _findServiceByName(h, 'memory') if not s: return (mem_state, virtual_state, paged_state, 0, 0, 0) print "Service found", s.get_full_name() mem_state = virtual_state = paged_state = s.state # Now grep perfdata in it p = PerfDatas(s.perf_data) mem = virtual = paged = 0 if 'physical memory' in p: m = p['physical memory'] if m.name and m.value is not None and m.max is not None and m.max != 0: pct = 100 * float(m.value) / m.max mem = int(pct) if 'virtual memory' in p: m = p['virtual memory'] if m.name and m.value is not None and m.max is not None and m.max != 0: pct = 100 * float(m.value) / m.max virtual = int(pct) if 'paged bytes' in p: m = p['paged bytes'] if m.name and m.value is not None and m.max is not None and m.max != 0: pct = 100 * float(m.value) / m.max paged = int(pct) return mem_state, virtual_state, paged_state, mem, virtual, paged
def get_disks(h): all_disks = [] s = _findServiceByName(h, 'disk') if not s: return 'UNKNOWN', all_disks print "Service found", s.get_full_name() disks_state = s.state p = PerfDatas(s.perf_data) print "PERFDATA", p, p.__dict__ for m in p: print "KEY", m # Skip void disks? if not m.name or m.value is None or m.max is None or m.max == 0: continue # Skip device we don't care about if m.name == 'C:\ %' or m.name.endswith('%'): continue pct = 100 * float(m.value) / m.max pct = int(pct) all_disks.append((m.name, pct)) return disks_state, all_disks
def get_memory(h): mem_state = swap_state = 'UNKNOWN' s = h.find_service_by_name('Memory') print "Service found", s.get_full_name() if not s: return (mem_state, swap_state, 0, 0) mem_state = swap_state = s.state # Now grep perfdata in it p = PerfDatas(s.perf_data) print "PERFDATA", p, p.__dict__ mem = 0 swap = 0 if 'ram_used' in p: m = p['ram_used'] # Maybe it's an invalid metric? if m.name and m.value is not None and m.max is not None and m.max != 0: # Classic pct compute pct = 100 * float(m.value) / m.max mem = int(pct) print "Mem", m.value, m.max, pct if 'swap_used' in p: m = p['swap_used'] # Maybe it's an invalid metric? if m.name and m.value is not None and m.max is not None and m.max != 0: # Classic pct compute pct = 100 * float(m.value) / m.max swap = int(pct) print "Swap", m.value, m.max, pct return mem_state, swap_state, mem, swap
def get_check_result_perfdata_points(self, perf_data, timestamp, tags={}): """ :param perf_data: Perf data of the brok :param timestamp: Timestamp of the check result :param tags: Tags for the point :return: List of perfdata points """ points = [] metrics = PerfDatas(perf_data).metrics for e in metrics.values(): fields = {} fields_mappings = [('value', 'value'), ('uom', 'unit'), ('warning', 'warning'), ('critical', 'critical'), ('min', 'min'), ('max', 'max')] for mapping in fields_mappings: value = getattr(e, mapping[0], None) if value is not None: if isinstance(value, (int, long)): value = float(value) fields[mapping[1]] = value if fields: point = { "measurement": 'metric_%s' % self.illegal_char.sub('_', e.name), "time": timestamp, "fields": fields, "tags": tags, } points.append(point) return points
def allperfs(obj_ref): """ Get all perfdatas from a service or a host """ obj = get_object(obj_ref) p = PerfDatas(obj.perf_data) logger.debug("[trigger] I get all perfdatas") return dict([(metric.name, p[metric.name]) for metric in p])
def perf(obj_ref, metric_name): obj = get_object(obj_ref) p = PerfDatas(obj.perf_data) if metric_name in p: logger.debug("[trigger] I found the perfdata") return p[metric_name].value logger.debug("[trigger] I am in perf command") return None
def get_check_result_perfdata_points(perf_data, timestamp, name): points = [] metrics = PerfDatas(perf_data).metrics for e in metrics.values(): points.append( {"points": [[timestamp, e.value, e.uom, e.warning, e.critical, e.min, e.max]], "name": "%s.%s" % (name, e.name), "columns": ["time", "value", "unit", "warning", "critical", "min", "max"] } ) return points
def get_network(h): all_nics = [] network_state = 'UNKNOWN' s = _findServiceByName(h, 'network') if not s: return 'UNKNOWN',all_nics print "Service found", s.get_full_name() # Host perfdata p = PerfDatas(h.perf_data) print "PERFDATA", p, p.__dict__ # all_nics.append(('perfdata', h.perf_data)) network_state = s.state p = PerfDatas(s.perf_data) if 'BytesTotalPersec' in p: all_nics.append(('BytesTotalPersec', p['BytesTotalPersec'].value)) if 'CurrentBandwidth' in p: all_nics.append(('CurrentBandwidth', p['CurrentBandwidth'].value)) return network_state, all_nics
def manage_unknown_command(elt): safe_print('Get an unmanaged command perfdata of', elt.get_full_name()) p = PerfDatas(elt.perf_data) if len(p) == 0: return None m = None # Got some override name we know to be ok for printing if 'time' in p: m = p['time'] else: for v in p: print "Look for", v if v.name is not None and v.value is not None: m = v break prop = m.name print "Got a property", prop, "and a value", m v = m.value if not v: print "No value, I bailout" return None # Now look if min/max are available or not pct = 0 if m.min and m.max and (m.max - m.min != 0): pct = 100 * (v / (m.max - m.min)) else: # ok, we will really guess this time... # Percent of ok should be time/10s pct = 100 * (v / 10) # go to int pct = int(pct) # But at least 1% pct = max(1, pct) # And max to 100% pct = min(pct, 100) lnk = '#' color = get_linear_color(elt, prop) s_color = 'RGB(%d,%d,%d)' % color metrics = [(s_color, pct), ('white', 100-pct)] uom = '' or m.uom title = '%s%s' % (v, uom) print "HTTP: return", {'lnk': lnk, 'metrics': metrics, 'title': title} return {'lnk': lnk, 'metrics': metrics, 'title': title}
def get_perfdata_table(self, elt): perfdatas = PerfDatas(elt.perf_data) display_min = any(p.min for p in perfdatas) display_max = any(p.max is not None for p in perfdatas) display_warning = any(p.warning is not None for p in perfdatas) display_critical = any(p.critical is not None for p in perfdatas) s = '<table class="table table-condensed table-w-condensed">' s += '<tr><th></th><th>Label</th><th>Value</th>' if display_min: s += '<th>Min</th>' if display_max: s += '<th>Max</th>' if display_warning: s += '<th>Warning</th>' if display_critical: s += '<th>Critical</th>' s += '</tr>' for p in perfdatas: s += '<tr><td>%s</td><td>%s</td><td>%s %s</td>' % ( self.get_perfdata_pie(p), p.name, p.value, p.uom) if display_min: if p.min is not None: s += '<td>%s %s</td>' % (p.min, p.uom) else: s += '<td></td>' if display_max: if p.max is not None: s += '<td>%s %s</td>' % (p.max, p.uom) else: s += '<td></td>' if display_warning: if p.warning is not None: s += '<td>%s %s</td>' % (p.warning, p.uom) else: s += '<td></td>' if display_critical: if p.critical is not None: s += '<td>%s %s</td>' % (p.critical, p.uom) else: s += '<td></td>' s += '</tr>' s += '</table>' return s
def test_parsing_perfdata(self): s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;0;3827 memused=1550MB;2973;3964;0;5810' s = 'ramused=1009MB;;;0;1982' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 1009) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 1982) s = 'ramused=90%;85;95;;' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == 85) self.assert_(m.critical == 95) self.assert_(m.min == 0) self.assert_(m.max == 100) s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;; memused=90%' p = PerfDatas(s) p.metrics m = p['swapused'] self.assert_(m.name == 'swapused') self.assert_(m.value == 540) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == None) self.assert_(m.max == None) m = p['memused'] self.assert_(m.name == 'memused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 100) self.assert_(len(p) == 3)
def get_cpu(h): cpu_state = 'UNKNOWN' s = _findServiceByName(h, 'cpu') if not s: return 'UNKNOWN', 0 print "Service found", s.get_full_name() cpu_state = s.state # Now perfdata p = PerfDatas(s.perf_data) print "PERFDATA", p, p.__dict__ cpu = 0 if '30s' in p: m = p['30s'] if m.name and m.value is not None: cpu = m.value print "Cpu", m.value return cpu_state, cpu
def get_cpu(h): cpu_state = 'UNKNOWN' s = h.find_service_by_name('Cpu') print "Service found", s.get_full_name() if not s: return cpu_state, 0 cpu_state = s.state # Now perfdata p = PerfDatas(s.perf_data) print "PERFDATA", p, p.__dict__ cpu = 0 if 'cpu_prct_used' in p: m = p['cpu_prct_used'] # Maybe it's an invalid metric? if m.name and m.value is not None: cpu = m.value print "Cpu", m.value return cpu_state, cpu
def get_perfdata_pies(self, elt): return " ".join( [self.get_perfdata_pie(p) for p in PerfDatas(elt.perf_data)])
def manage_unknown_command(elt, metric=None): logger.debug("[WebUI] perfometer, manage command for: %s, metric: %s", elt.get_full_name(), metric) p = PerfDatas(elt.perf_data) if len(p) == 0: return None # Got some override name we know to be ok for printing if metric is None and 'time' in p and p['time'].value is not None: metric = p['time'] else: if metric is None: for v in p: if v.name is not None and v.value is not None: metric = v else: for v in p: if v.name is not None and v.value is not None and v.name == metric: metric = v if metric is None: return None name = metric.name value = metric.value logger.debug("[WebUI] perfometer, manage command, metric: %s=%d", name, value) if not value: return None pct = 0 # If metric UOM is % if metric.uom and metric.uom == '%': # Return value pct = value # Look if min/max are defined elif metric.min and metric.max and (metric.max - metric.min != 0): pct = 100 * (value / (metric.max - metric.min)) else: # Assume value is 100% pct = 100 # Percentage is at least 1% and max 100% pct = max(1, pct) pct = min(pct, 100) # Title try: title = '%s%s' % (value, metric.uom) except: title = '%s' % (value) # Link lnk = '#' # Get the color base = {0: 'success', 1: 'warning', 2: 'danger', 3: 'info'} color = base.get(elt.state_id, 'info') logger.debug("[WebUI] perfometer, manage command, metric: %s=%d -> %d", name, value, pct) return {'lnk': lnk, 'metrics': [(color, pct)], 'title': title}
def test_parsing_perfdata(self): s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;0;3827 memused=1550MB;2973;3964;0;5810' s = 'ramused=1009MB;;;0;1982' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 1009) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 1982) s = 'ramused=90%;85;95;;' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == 85) self.assert_(m.critical == 95) self.assert_(m.min == 0) self.assert_(m.max == 100) s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;; memused=90%' p = PerfDatas(s) p.metrics m = p['swapused'] self.assert_(m.name == 'swapused') self.assert_(m.value == 540) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == None) self.assert_(m.max == None) m = p['memused'] self.assert_(m.name == 'memused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 100) self.assert_(len(p) == 3) s = "'Physical Memory Used'=12085620736Bytes; 'Physical Memory Utilisation'=94%;80;90;" p = PerfDatas(s) p.metrics m = p['Physical Memory Used'] self.assert_(m.name == 'Physical Memory Used') self.assert_(m.value == 12085620736) self.assert_(m.uom == 'Bytes') self.assert_(m.warning is None) self.assert_(m.critical is None) self.assert_(m.min is None) self.assert_(m.max is None) m = p['Physical Memory Utilisation'] self.assert_(m.name == 'Physical Memory Utilisation') self.assert_(m.value == 94) self.assert_(m.uom == '%') self.assert_(m.warning == 80) self.assert_(m.critical == 90) self.assert_(m.min == 0) self.assert_(m.max == 100) s = "'C: Space'=35.07GB; 'C: Utilisation'=87.7%;90;95;" p = PerfDatas(s) p.metrics m = p['C: Space'] self.assert_(m.name == 'C: Space') self.assert_(m.value == 35.07) self.assert_(m.uom == 'GB') self.assert_(m.warning is None) self.assert_(m.critical is None) self.assert_(m.min is None) self.assert_(m.max is None) m = p['C: Utilisation'] self.assert_(m.name == 'C: Utilisation') self.assert_(m.value == 87.7) self.assert_(m.uom == '%') self.assert_(m.warning == 90) self.assert_(m.critical == 95) self.assert_(m.min == 0) self.assert_(m.max == 100)
def test_parsing_perfdata(self): s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;0;3827 memused=1550MB;2973;3964;0;5810' s = 'ramused=1009MB;;;0;1982' m = Metric(s) self.assertEqual('ramused', m.name) self.assertEqual(1009, m.value) self.assertEqual('MB', m.uom) self.assertEqual(None, m.warning) self.assertEqual(None, m.critical) self.assertEqual(0, m.min) self.assertEqual(1982, m.max) s = 'ramused=90%;85;95;;' m = Metric(s) self.assertEqual('ramused', m.name) self.assertEqual(90, m.value) self.assertEqual('%', m.uom) self.assertEqual(85, m.warning) self.assertEqual(95, m.critical) self.assertEqual(0, m.min) self.assertEqual(100, m.max) s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;; memused=90%' p = PerfDatas(s) p.metrics m = p['swapused'] self.assertEqual('swapused', m.name) self.assertEqual(540, m.value) self.assertEqual('MB', m.uom) self.assertEqual(None, m.warning) self.assertEqual(None, m.critical) self.assertEqual(None, m.min) self.assertEqual(None, m.max) m = p['memused'] self.assertEqual('memused', m.name) self.assertEqual(90, m.value) self.assertEqual('%', m.uom) self.assertEqual(None, m.warning) self.assertEqual(None, m.critical) self.assertEqual(0, m.min) self.assertEqual(100, m.max) self.assertEqual(3, len(p)) s = "'Physical Memory Used'=12085620736Bytes; 'Physical Memory Utilisation'=94%;80;90;" p = PerfDatas(s) p.metrics m = p['Physical Memory Used'] self.assertEqual('Physical Memory Used', m.name) self.assertEqual(12085620736, m.value) self.assertEqual('Bytes', m.uom) self.assertIs(None, m.warning) self.assertIs(None, m.critical) self.assertIs(None, m.min) self.assertIs(None, m.max) m = p['Physical Memory Utilisation'] self.assertEqual('Physical Memory Utilisation', m.name) self.assertEqual(94, m.value) self.assertEqual('%', m.uom) self.assertEqual(80, m.warning) self.assertEqual(90, m.critical) self.assertEqual(0, m.min) self.assertEqual(100, m.max) s = "'C: Space'=35.07GB; 'C: Utilisation'=87.7%;90;95;" p = PerfDatas(s) p.metrics m = p['C: Space'] self.assertEqual('C: Space', m.name) self.assertEqual(35.07, m.value) self.assertEqual('GB', m.uom) self.assertIs(None, m.warning) self.assertIs(None, m.critical) self.assertIs(None, m.min) self.assertIs(None, m.max) m = p['C: Utilisation'] self.assertEqual('C: Utilisation', m.name) self.assertEqual(87.7, m.value) self.assertEqual('%', m.uom) self.assertEqual(90, m.warning) self.assertEqual(95, m.critical) self.assertEqual(0, m.min) self.assertEqual(100, m.max) s = "time_offset-192.168.0.1=-7.22636468709e-05s;1;2;0;;" p = PerfDatas(s) m = p['time_offset-192.168.0.1'] self.assertEqual('time_offset-192.168.0.1', m.name) self.assertEqual(-7.22636468709e-05, m.value) self.assertEqual('s', m.uom) self.assertEqual(1, m.warning) self.assertEqual(2, m.critical) self.assertEqual(0, m.min) self.assertIs(None, m.max) s = u"ééé-192.168.0.1=-7.22636468709e-05s;1;2;0;;" p = PerfDatas(s) m = p[u'ééé-192.168.0.1'] self.assertEqual(m.name, u'ééé-192.168.0.1') self.assertEqual(m.value, -7.22636468709e-05) self.assertEqual(m.uom, 's') self.assertEqual(m.warning, 1) self.assertEqual(m.critical, 2) self.assertEqual(m.min, 0) self.assertEqual(m.max, None) #Test that creating a perfdata with nothing dosen't fail s = None p = PerfDatas(s) self.assertEqual(len(p), 0)
def show_trivial_json(): data = { "nodes": [], "edges": [] } hosts = set() locs = set() user = app.request.environ['USER'] search = app.request.query.get('search', "type:host bp:>1") items = app.datamgr.search_hosts_and_services(search, user, get_impacts=False) hosts_items = [item for item in items if isinstance(item, Host)] # Firt Round: Host (need all host for locate orphan edge) for h in hosts_items: _host = h.get_name() if _host in locs: continue if len(h.parent_dependencies) > 0 or len(h.child_dependencies) > 0: hosts.add(_host) _node = {'data': { "id": _host, "name": _host, "address": getattr(h,'address'), "color": _host_state_to_color(h.state_id), "size": h.business_impact * 25, "tech": h.customs.get('_TECH',""), "model": h.customs.get('_MODEL',""), }} _loc = h.customs.get('_LOCATION') if _loc: _node['data']["parent"] = _loc if not _loc in locs: locs.add(_loc) data.get('nodes').append({'data': { "id": _loc, "color": '#ddd' }}) # Node boder color, max state vale # max_state = 0 # for service in h.services: # border_state = max(max_state, getattr(service,'state_id')) if h.services: _node['data']['border_color'] = _service_state_to_color(max(service.state_id for service in h.services)) else: _node['data']['border_color'] = _host_state_to_color(h.state_id) _node['data']['service0'] = 0 _node['data']['service1'] = 0 _node['data']['service2'] = 0 _node['data']['service3'] = 0 _node['data']['servicen'] = len(h.services) for s in h.services: _node['data']['service%d' % s.state_id] += 1 if len(h.hostgroups) > 0: _node['classes'] = " ".join([hg.get_name() for hg in h.hostgroups ]) data.get('nodes').append(_node) # Second Round: Detect ophan edges for h in hosts_items: try: # for help debug _host = h.get_name() if _host in locs: continue edge = {'data': {}} if len(h.parent_dependencies) > 0: for parent in h.parent_dependencies: #if len(h.child_dependencies) > 0: # for parent in h.child_dependencies: _parent = parent.host_name if (not _parent or _parent in locs or _parent not in hosts or _parent == _host): continue edge.get('data').update({ "id": "{}:{}".format(_host, _parent), "source": _host, "target": _parent, "label": '-' }) else: ### IF HOST IS CPE if hasattr(h, 'cpe_registration_host'): _parent = getattr(h, 'cpe_registration_host') edge.get('data').update({ "id": "{}:{}".format(_parent,_host), "source": _host, "target": _parent, "color": '#3f51b5', "label": '' }) dnbw, upbw = (None,None) for service in h.services: if hasattr(service,'perf_data'): perfdatas = PerfDatas(service.perf_data) for metric in perfdatas: if getattr(metric,'name') == 'dnbw': dnbw = _human_byte(getattr(metric,'value')) if getattr(metric,'name') == 'upbw': upbw = _human_byte(getattr(metric,'value')) perfdatas = PerfDatas(h.perf_data) for metric in perfdatas: if getattr(metric,'name') == 'dnbw': dnbw = _human_byte(getattr(metric,'value')) if getattr(metric,'name') == 'upbw': upbw = _human_byte(getattr(metric,'value')) if (dnbw and upbw): edge.get('data').update(label="{}/{}".format(dnbw,upbw)) # Cheat # if not _host: # pass # elif '-REM' in _host: # edge.get('data').update(color='#9c27b0') #ROSA # else: # edge.get('data').update(color='#999') # GRAY if len(h.hostgroups) > 0: edge['classes'] = " ".join([hg.get_name() for hg in h.hostgroups ]) except Exception as e: edge.get('data').update(label="") edge.update(error=str(e)) data.get('edges').append(edge) return data
def test_parsing_perfdata(self): s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;0;3827 memused=1550MB;2973;3964;0;5810' s = 'ramused=1009MB;;;0;1982' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 1009) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 1982) s = 'ramused=90%;85;95;;' m = Metric(s) self.assert_(m.name == 'ramused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == 85) self.assert_(m.critical == 95) self.assert_(m.min == 0) self.assert_(m.max == 100) s = 'ramused=1009MB;;;0;1982 swapused=540MB;;;; memused=90%' p = PerfDatas(s) p.metrics m = p['swapused'] self.assert_(m.name == 'swapused') self.assert_(m.value == 540) self.assert_(m.uom == 'MB') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == None) self.assert_(m.max == None) m = p['memused'] self.assert_(m.name == 'memused') self.assert_(m.value == 90) self.assert_(m.uom == '%') self.assert_(m.warning == None) self.assert_(m.critical == None) self.assert_(m.min == 0) self.assert_(m.max == 100) self.assert_(len(p) == 3) s = "'Physical Memory Used'=12085620736Bytes; 'Physical Memory Utilisation'=94%;80;90;" p = PerfDatas(s) p.metrics m = p['Physical Memory Used'] self.assert_(m.name == 'Physical Memory Used') self.assert_(m.value == 12085620736) self.assert_(m.uom == 'Bytes') self.assert_(m.warning is None) self.assert_(m.critical is None) self.assert_(m.min is None) self.assert_(m.max is None) m = p['Physical Memory Utilisation'] self.assert_(m.name == 'Physical Memory Utilisation') self.assert_(m.value == 94) self.assert_(m.uom == '%') self.assert_(m.warning == 80) self.assert_(m.critical == 90) self.assert_(m.min == 0) self.assert_(m.max == 100) s = "'C: Space'=35.07GB; 'C: Utilisation'=87.7%;90;95;" p = PerfDatas(s) p.metrics m = p['C: Space'] self.assert_(m.name == 'C: Space') self.assert_(m.value == 35.07) self.assert_(m.uom == 'GB') self.assert_(m.warning is None) self.assert_(m.critical is None) self.assert_(m.min is None) self.assert_(m.max is None) m = p['C: Utilisation'] self.assert_(m.name == 'C: Utilisation') self.assert_(m.value == 87.7) self.assert_(m.uom == '%') self.assert_(m.warning == 90) self.assert_(m.critical == 95) self.assert_(m.min == 0) self.assert_(m.max == 100) s = "time_offset-192.168.0.1=-7.22636468709e-05s;1;2;0;;" p = PerfDatas(s) m = p['time_offset-192.168.0.1'] self.assert_(m.name == 'time_offset-192.168.0.1') self.assert_(m.value == -7.22636468709e-05) self.assert_(m.uom == 's') self.assert_(m.warning == 1) self.assert_(m.critical == 2) self.assert_(m.min == 0) self.assert_(m.max is None) s = u"ééé-192.168.0.1=-7.22636468709e-05s;1;2;0;;" p = PerfDatas(s) m = p[u'ééé-192.168.0.1'] self.assertEqual(m.name, u'ééé-192.168.0.1') self.assertEqual(m.value, -7.22636468709e-05) self.assertEqual(m.uom, 's') self.assertEqual(m.warning, 1) self.assertEqual(m.critical, 2) self.assertEqual(m.min, 0) self.assertEqual(m.max, None) #Test that creating a perfdata with nothing dosen't fail s = None p = PerfDatas(s) self.assertEqual(len(p), 0)