Package ZenRRD :: Package parsers :: Module ps
[hide private]
[frames] | no frames]

Source Code for Module ZenRRD.parsers.ps

  1  ########################################################################### 
  2  # 
  3  # This program is part of Zenoss Core, an open source monitoring platform. 
  4  # Copyright (C) 2009, Zenoss Inc. 
  5  # 
  6  # This program is free software; you can redistribute it and/or modify it 
  7  # under the terms of the GNU General Public License version 2 as published by 
  8  # the Free Software Foundation. 
  9  # 
 10  # For complete information please visit: http://www.zenoss.com/oss/ 
 11  # 
 12  ########################################################################### 
 13   
 14  import Globals 
 15  from Products.ZenRRD.CommandParser import CommandParser 
 16  from Products.ZenEvents.ZenEventClasses import Status_OSProcess 
 17   
 18  AllPids = {} 
 19   
 20  emptySet = set() 
 21   
22 -class ps(CommandParser):
23
24 - def dataForParser(self, context, datapoint):
25 id, name, ignoreParams, alertOnRestart, failSeverity = \ 26 context.getOSProcessConf() 27 return dict(processName=name, 28 ignoreParams=ignoreParams, 29 alertOnRestart=alertOnRestart, 30 failSeverity=failSeverity)
31
32 - def processResults(self, cmd, results):
33 34 # map data points by procesName 35 dps = {} 36 for dp in cmd.points: 37 dps.setdefault(dp.data['processName'], []).append(dp) 38 39 # group running processes by name (with and without args) 40 running = {} 41 for line in cmd.result.output.split('\n')[1:]: 42 if not line: continue 43 try: 44 pid, rss, cpu, args = line.split(None, 3) 45 days = 0 46 if cpu.find('-') > -1: 47 days, cpu = cpu.split('-') 48 days = int(days) 49 cpu = map(int, cpu.split(':')) 50 cpu = (days * 24 * 60 * 60 + 51 cpu[0] * 60 * 60 + 52 cpu[1] * 60 + 53 cpu[2]) 54 rss = int(rss) 55 pid = int(pid) 56 lst = running.setdefault(args, []) 57 lst.append( (pid, rss, cpu) ) 58 lst = running.setdefault(args.split()[0], []) 59 lst.append( (pid, rss, cpu) ) 60 except ValueError: 61 continue 62 # report any processes that are missing, and post perf data 63 for process, points in dps.items(): 64 match = running.get(process, []) 65 if not match: 66 results.events.append(dict( 67 summary='Process not running: ' + process, 68 eventClass=Status_OSProcess, 69 eventGroup='Process', 70 component=process, 71 severity=points[0].data['failSeverity'] 72 )) 73 else: 74 totalRss = sum([rss for pid, rss, cpu in match]) 75 totalCpu = sum([cpu for pid, rss, cpu in match]) 76 for dp in points: 77 if 'cpu' in dp.id: 78 results.values.append( (dp, totalCpu) ) 79 if 'mem' in dp.id: 80 results.values.append( (dp, totalRss) ) 81 if 'count' in dp.id: 82 results.values.append( (dp, len(match)) ) 83 84 # report process changes 85 device = cmd.deviceConfig.device 86 before = AllPids.get( (device, process), emptySet) 87 after = set([pid for pid, rss, cpu in match]) 88 alertOnRestart = points[0].data['alertOnRestart'] 89 if before != after: 90 if len(before) > len(after) and alertOnRestart: 91 pids = ', '.join(map(str, before - after)) 92 results.events.append(dict( 93 summary='Pid(s) %s stopped: %s' % (pids, process), 94 eventClass=Status_OSProcess, 95 eventGroup='Process', 96 component=process, 97 severity=points[0].data['failSeverity'] 98 )) 99 if len(before) == len(after) and alertOnRestart: 100 # process restarted 101 pids = ', '.join(map(str, before - after)) 102 results.events.append(dict( 103 summary='Pid(s) %s restarted: %s' % (pids, process), 104 eventClass=Status_OSProcess, 105 eventGroup='Process', 106 component=process, 107 severity=points[0].data['failSeverity'] 108 )) 109 if len(before) < len(after): 110 if len(before) == 0: 111 results.events.append(dict( 112 summary='Process running: %s' % process, 113 eventClass=Status_OSProcess, 114 eventGroup='Process', 115 component=process, 116 severity=0 117 )) 118 119 AllPids[device, process] = after
120