-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_runaway.py
executable file
·71 lines (59 loc) · 2.77 KB
/
local_runaway.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# Copyright 2011,2012 Max W. Schwarz
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import re
import subprocess
# The minimum PCPU value for the process to be considered "bad"
MINIMUM_PCPU = 10.0
# The minimum time the process can have been running for us to consider it a runaway (in minutes)
MINIMUM_TIME = 120
# The minimum pmem (%)
MINIMUM_PMEM = 10.0
# Minimum elapsed time (in minutes)
MINIMUM_ETIME = 60*2
# Parses the etime field. This is always in the form DAYS-HOURS:MINUTES:SECONDS. DAYS and HOURS may not be present
# Returns the elapsed time in minutes
def parseTime(etimeString):
regexTime = re.match(r"^((?P<days>\d*)-)*((?P<hours>\d*):)*(?P<mins>\d+):(?P<secs>\d+)$", etimeString).groupdict()
# print regexTime
days = hours = minutes = 0
if regexTime["days"]:
days = int(regexTime["days"])
if regexTime["hours"]:
hours = int(regexTime["hours"]) + 24*days
minutes = int(regexTime["mins"]) + 60*hours
return minutes
hostname = subprocess.Popen(["hostname", "-f"], stdout=subprocess.PIPE).stdout.read().strip()
print "Printing processes on <" + unicode(hostname) + ">" # with a minimum %CPU of", MINIMUM_PCPU
p = subprocess.Popen(['ps','--no-headers', '-eo','pcpu,pmem,pid,ruser,etime,ni,comm'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
stripped_line = line.strip()
# print stripped_line
psData = re.match(r"^(?P<pcpu>\S+)\s+(?P<pmem>\S+)\s+(?P<pid>\S+)\s+(?P<ruser>\S+)\s+(?P<etime>\S+)\s+(?P<ni>\S+)\s+(?P<comm>.+)$",stripped_line)
if psData:
# print psData.groupdict()
parsedData = {}
parsedData["pcpu"] = float(psData.group("pcpu"))
parsedData["pmem"] = float(psData.group("pmem"))
parsedData["pid "] = int(psData.group("pid"))
parsedData["ruser"] = psData.group("ruser")
parsedData["etime"] = int(parseTime(psData.group("etime")))
# print parsedData["etime"]
if ( parsedData["pcpu"] > MINIMUM_PCPU
or parsedData["pmem"] > MINIMUM_PMEM
# or parsedData["etime"] > MINIMUM_ETIME
):
print "\t"+stripped_line
else:
print "Error: No data matched"