1
2
3
4
5
6
7
8
9
10
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
31
33
34
35 dps = {}
36 for dp in cmd.points:
37 dps.setdefault(dp.data['processName'], []).append(dp)
38
39
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
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
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
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