2013-08-29 15:21:13 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# (c)2011-2012 the Boeing Company
|
|
|
|
#
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
perflogserver.py - CORE server and node performace metrics logger and alarmer
|
|
|
|
server metrics: loadave1, 5, 15, mem, used cpu% of total, cpu1, cpu2, ..., cpun
|
|
|
|
node metrics: throughput, mem, cpu total, usr, sys, wait
|
|
|
|
"""
|
|
|
|
|
|
|
|
import commands
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import pdb
|
|
|
|
import signal
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def readfile(fname):
|
2018-10-12 17:38:14 +01:00
|
|
|
lines = []
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
|
|
|
f = open(fname, "r")
|
2018-10-12 17:38:14 +01:00
|
|
|
except IOError:
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.timestamp == True:
|
2018-10-12 17:38:14 +01:00
|
|
|
print str(time.time()),
|
|
|
|
print "ERROR: failed to open file %s\n" % fname
|
|
|
|
else:
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
2013-08-29 15:21:13 +01:00
|
|
|
return lines
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def numcpus():
|
|
|
|
lines = readfile("/proc/stat")
|
|
|
|
n = 0
|
|
|
|
for l in lines[1:]:
|
|
|
|
if l[:3] != "cpu":
|
|
|
|
break
|
|
|
|
n += 1
|
|
|
|
return n
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def handler(signum, frame):
|
2018-10-12 17:38:14 +01:00
|
|
|
print "stop timestamp:", str(
|
|
|
|
time.time()) + ", cyclecount=", cyclecount, ", caught signal", signum
|
2013-08-29 15:21:13 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
class ServerMetrics(object):
|
|
|
|
def __init__(self):
|
2018-10-12 17:38:14 +01:00
|
|
|
self.smetrics = {"serverloadavg1": 0.0,
|
|
|
|
"serverloadavg5": 0.0,
|
|
|
|
"serverloadavg15": 0.0,
|
|
|
|
"serverusedmemory": 0.0,
|
|
|
|
"serverusedcputime": 0.0,
|
|
|
|
"processorusedcputime": []}
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def setvalues(self, val):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Set values from val = (nump, ldavg1, ldavg5, adavg15, mem, cpu, p1cpu, p2cpu...).
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.smetrics["serverloadavg1"] = val[0]
|
|
|
|
self.smetrics["serverloadavg5"] = val[1]
|
|
|
|
self.smetrics["serverloadavg15"] = val[2]
|
|
|
|
self.smetrics["serverusedmemory"] = val[4]
|
|
|
|
self.smetrics["serverusedcputime"] = val[5]
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
pcpu = []
|
|
|
|
for ind in range(5, len(val)):
|
|
|
|
pcpu.append(val[ind])
|
2013-08-29 15:21:13 +01:00
|
|
|
self.smetrics["processorusedcputime"] = pcpu
|
|
|
|
|
|
|
|
def setvalue(self, key, val):
|
|
|
|
self.smetrics[key] = val
|
|
|
|
|
|
|
|
def getvalue(self, key):
|
|
|
|
return self.smetrics[key]
|
|
|
|
|
|
|
|
def getkeys(self):
|
|
|
|
return self.smetrics.keys()
|
|
|
|
|
|
|
|
def tocsv(self):
|
2018-10-12 17:38:14 +01:00
|
|
|
rv = "Server"
|
2013-08-29 15:21:13 +01:00
|
|
|
for k in self.smetrics:
|
|
|
|
if isinstance(self.smetrics[k], float):
|
|
|
|
rv += ", %.2f" % self.smetrics[k]
|
2018-10-12 17:38:14 +01:00
|
|
|
else:
|
2013-08-29 15:21:13 +01:00
|
|
|
if isinstance(self.smetrics[k], list):
|
2018-10-12 17:38:14 +01:00
|
|
|
values = ", ".join(str(round(x, 2)) for x in self.smetrics[k])
|
|
|
|
rv += ", [%s]" % values
|
|
|
|
else:
|
|
|
|
rv += ", " + str(self.smetrics[k])
|
|
|
|
return rv
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def readserverthresholds(filename):
|
|
|
|
if filename is None:
|
2018-10-12 17:38:14 +01:00
|
|
|
return
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
lines = readfile(filename)
|
|
|
|
for l in lines:
|
2018-10-12 17:38:14 +01:00
|
|
|
mval = l.strip().split('=')
|
|
|
|
if len(mval) > 1:
|
|
|
|
thekey = mval[0].strip()
|
|
|
|
theval = mval[1].strip()
|
|
|
|
if thekey in serverthresholds.getkeys():
|
|
|
|
serverthresholds.setvalue(thekey, float(theval))
|
|
|
|
|
|
|
|
|
|
|
|
def checkserverthreshold(metricval):
|
|
|
|
"""
|
|
|
|
Print out an alarm if a ServerMetrics value crosses threshold.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
for key in serverthresholds.getkeys():
|
2018-10-12 17:38:14 +01:00
|
|
|
if key == "processorusedcputime":
|
|
|
|
pcpus = metricval.getvalue(key)
|
|
|
|
for ind in range(0, len(pcpus)):
|
|
|
|
if pcpus[ind] > serverthresholds.getvalue(key):
|
2013-08-29 15:21:13 +01:00
|
|
|
alarm = ["server", os.uname()[1], str(ind) + key,
|
2018-10-12 17:38:14 +01:00
|
|
|
"%.2f" % pcpus[ind], ">", serverthresholds.getvalue(key)]
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.timestamp:
|
2018-10-12 17:38:14 +01:00
|
|
|
print str(time.time()) + ",",
|
|
|
|
print ", ".join(str(x) for x in alarm)
|
|
|
|
else:
|
|
|
|
if metricval.getvalue(key) > serverthresholds.getvalue(key):
|
2013-08-29 15:21:13 +01:00
|
|
|
alarm = ["server", os.uname()[1], key,
|
2018-10-12 17:38:14 +01:00
|
|
|
"%.2f" % metricval.getvalue(key), ">", serverthresholds.getvalue(key)]
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.timestamp:
|
2018-10-12 17:38:14 +01:00
|
|
|
print str(time.time()) + ",",
|
|
|
|
print ", ".join(str(x) for x in alarm)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def collectservercputimes():
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return cpu times in ticks of this server total and each processor 3*(1+#cpu) columns
|
|
|
|
(user+nice, sys, idle) from each /proc/stat cpu lines assume columns are:
|
|
|
|
cpu# user nice sys idle iowait irq softirq steal guest (man 5 proc)
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rval = {}
|
|
|
|
lines = readfile("/proc/stat")
|
|
|
|
for i in range(ncpus + 1):
|
|
|
|
items = lines[i].split()
|
2018-10-12 17:38:14 +01:00
|
|
|
user, nice, sys, idle = [int(x) for x in items[1:5]]
|
|
|
|
rval[i] = [user+nice, sys, idle]
|
2013-08-29 15:21:13 +01:00
|
|
|
return rval
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def csvservercputimes(cputimes):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return a csv string of this server total and each processor's cpu times
|
|
|
|
(usr, sys, idle) in ticks.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rval = ''
|
|
|
|
for i in range(len(cputimes)):
|
2018-10-12 17:38:14 +01:00
|
|
|
rval += ", ".join(str(x) for x in cputimes[i])
|
2013-08-29 15:21:13 +01:00
|
|
|
return rval
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def calcservercputimes(cputimea, cputimeb):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return cpu used/total % of this server total and each processor (1+#cpu columns).
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
p = {}
|
|
|
|
for n in range(ncpus + 1):
|
|
|
|
p[n] = []
|
|
|
|
for i in range(len(cputimea[n])):
|
|
|
|
p[n].append(cputimeb[n][i] - cputimea[n][i])
|
2018-10-12 17:38:14 +01:00
|
|
|
# cpu times total delta
|
|
|
|
total = sum(p[n])
|
2013-08-29 15:21:13 +01:00
|
|
|
if total == 0:
|
|
|
|
p[n] = 0.0
|
|
|
|
else:
|
|
|
|
p[n] = 100 - ((100.0 * p[n][-1]) / total)
|
|
|
|
return p
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
def collectservermems():
|
|
|
|
"""
|
|
|
|
Return memory (total, free) in KB from proc/meminfo.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
lines = readfile("/proc/meminfo")
|
2018-10-12 17:38:14 +01:00
|
|
|
mem = [x.plit() for x in lines[0:2]]
|
|
|
|
return [int(x) for x in zip(*mem)[1]]
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
def csvservermems(mems):
|
|
|
|
"""
|
|
|
|
Return a csv string of this server memory (total, free).
|
|
|
|
"""
|
|
|
|
return ", ".join(str(x) for x in mems)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
def calcserverusedmem(mems):
|
|
|
|
"""
|
|
|
|
Return int(100*(MemTotal-MemFree)/MemTotal) from /proc/meminfo.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return 100 * (mems[0] - mems[1]) / mems[0]
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def collectservermetrics(cputimes, mems, thresholdcheck):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return ServerMetrics object with a dictionary of
|
|
|
|
loadavg1,loadavg5,loadavg15, usedmem%, usedcpu% for total, cpu1, cpu2, ...
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
metricval = []
|
2018-10-12 17:38:14 +01:00
|
|
|
ldavgs = os.getloadavg()
|
2013-08-29 15:21:13 +01:00
|
|
|
for v in ldavgs:
|
|
|
|
metricval.append(v)
|
|
|
|
metricval.append(calcserverusedmem(mems))
|
|
|
|
|
|
|
|
for i in range(ncpus + 1):
|
2018-10-12 17:38:14 +01:00
|
|
|
metricval.append(cputimes[i])
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
srvmetrics = ServerMetrics()
|
|
|
|
srvmetrics.setvalues(metricval)
|
|
|
|
|
|
|
|
if thresholdcheck:
|
2018-10-12 17:38:14 +01:00
|
|
|
checkserverthreshold(srvmetrics)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
return srvmetrics
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def csvservermetrics(srvmetrics):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return a csv string of ServerMetrics.tocsv()
|
|
|
|
loadavg1,loadavg5,loadavg15, usedmem%, usedcpu% for total, cpu1, cpu2, ...
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rv = ""
|
|
|
|
if options.timestamp:
|
2018-10-12 17:38:14 +01:00
|
|
|
rv = str(time.time()) + ", "
|
2013-08-29 15:21:13 +01:00
|
|
|
rv += srvmetrics.tocsv()
|
|
|
|
return rv
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def csvserverbaseline():
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return a csv string of raw server metrics data: memfree, memtotal, cpuused, cpusystem, cpuidle.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return "memory (total, free) = " + csvservermems(collectservermems()) + "\ncputime (used, sys, idl) = " + csvservercputimes(collectservercputimes())
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
class NodeMetrics(object):
|
|
|
|
def __init__(self):
|
2018-10-12 17:38:14 +01:00
|
|
|
self.nmetrics = {"nodethroughput": 0.0,
|
|
|
|
"nodeusedmemory": 0.0,
|
|
|
|
"nodetotalcpu": 0.0,
|
|
|
|
"nodeusercpu": 0.0,
|
|
|
|
"nodesystemcpu": 0.0,
|
|
|
|
"nodewaitcpu": 0.0}
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def setvalues(self, val):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Set values from val = (throughput, mem, tcpu, ucpu, scpu, wcpu).
|
|
|
|
"""
|
|
|
|
self.nmetrics["nodethroughput"] = val[0]
|
|
|
|
self.nmetrics["nodeusedmemory"] = val[1]
|
|
|
|
self.nmetrics["nodetotalcpu"] = val[2]
|
|
|
|
self.nmetrics["nodeusercpu"] = val[3]
|
|
|
|
self.nmetrics["nodesystemcpu"] = val[4]
|
|
|
|
self.nmetrics["nodewaitcpu"] = val[5]
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def setvalue(self, key, val):
|
2018-10-12 17:38:14 +01:00
|
|
|
self.nmetrics[key] = val
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getvalue(self, key):
|
2018-10-12 17:38:14 +01:00
|
|
|
return self.nmetrics[key]
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getkeys(self):
|
2018-10-12 17:38:14 +01:00
|
|
|
return self.nmetrics.keys()
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def tocsv(self):
|
2018-10-12 17:38:14 +01:00
|
|
|
return ", ".join(str(x) for x in self.nmetrics.values())
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LogSession(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.nodethresholds = NodeMetrics()
|
2018-10-12 17:38:14 +01:00
|
|
|
# set node threshold default values:
|
|
|
|
# nodethroughput=20.0, nodeusedmemory=15.0, nodetotalcpu=90.0,
|
|
|
|
# nodeusercpu=30.0, nodewaitcpu=50.0, nodesystemcpu=20.0}
|
|
|
|
self.nodethresholds.setvalues([20.0, 15.0, 90.0, 30.0, 50.0, 20.0])
|
|
|
|
if options.configfile is not None:
|
|
|
|
self.readnodethresholds(options.configfile)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.pids = {}
|
|
|
|
self.nodemetricsA = {}
|
|
|
|
self.nodemetricsB = {}
|
|
|
|
self.nodemetricsC = {}
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
def getpids(self):
|
|
|
|
"""
|
|
|
|
Return dict of all CORE session pids in a dict using node name as the keys
|
|
|
|
parent pid (vnoded) is the first value.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.pids = {}
|
2018-10-12 17:38:14 +01:00
|
|
|
nodes = commands.getstatusoutput(
|
|
|
|
"ls /tmp/pycore.%s/*pid" % options.session)
|
2013-08-29 15:21:13 +01:00
|
|
|
if nodes[0] != 0:
|
2018-10-12 17:38:14 +01:00
|
|
|
return
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
nodes = nodes[1].split('\n')
|
|
|
|
for nod in nodes:
|
2018-10-12 17:38:14 +01:00
|
|
|
nodename = nod.split('/')[-1].strip(".pid")
|
|
|
|
self.pids[nodename] = commands.getoutput("cat %s" % nod)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# do not expect failure of this command
|
|
|
|
procs = commands.getoutput('ps -eo ppid,pid,comm').split('\n')
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# build self.pids dict with key=nodename and val="ppid,pid,cmd"
|
|
|
|
for nname in self.pids:
|
|
|
|
if self.pids[nname] == "":
|
2018-10-12 17:38:14 +01:00
|
|
|
if options.timestamp == True:
|
|
|
|
print str(time.time()),
|
|
|
|
print "ERROR: null vnoded pid of node: %s" % nname
|
|
|
|
else:
|
|
|
|
childprocs = []
|
|
|
|
ppid = self.pids[nname]
|
2013-08-29 15:21:13 +01:00
|
|
|
for proc in procs:
|
2018-10-12 17:38:14 +01:00
|
|
|
val = proc.split()
|
|
|
|
if ppid == val[1]:
|
|
|
|
childprocs.append([val[1], val[2]])
|
|
|
|
if ppid == val[0]:
|
|
|
|
childprocs.append([val[1], val[2]])
|
2013-08-29 15:21:13 +01:00
|
|
|
self.pids[nname] = childprocs
|
|
|
|
return self.pids
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def printsesspids(self):
|
|
|
|
if self.pids == {}:
|
|
|
|
return {}
|
|
|
|
for pp in self.pids:
|
2018-10-12 17:38:14 +01:00
|
|
|
if self.pids[pp] != []:
|
2013-08-29 15:21:13 +01:00
|
|
|
for ap in range(len(self.pids[pp]) - 1):
|
2018-10-12 17:38:14 +01:00
|
|
|
# ap pid
|
|
|
|
print ", " + self.pids[pp][ap][0],
|
|
|
|
# ap cmd
|
|
|
|
print ", " + self.pids[pp][ap][1],
|
|
|
|
procmetrics = [str(x) for x in self.pids[pp][ap][-1]]
|
|
|
|
print ", " + ", ".join(procmetrics),
|
|
|
|
nodemetrics = [str(x) for x in self.pids[pp][-1]]
|
|
|
|
print ", " + ", ".join(nodemetrics)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def getprocessmetrics(self, pid):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return [cpu#, vsize(kb), ttime, utime, stime, wtime]
|
|
|
|
from a /proc/pid/stat (a single line file) assume columns are:
|
|
|
|
pid(0) comm(1) state ppid pgrp sess tty_nr tpgid flags
|
|
|
|
minflt cmiflt majflt cmajflt # utime(12) stime cutime cstime
|
|
|
|
priority nice num_threads itrealvalue starttime vsize(22) rss rsslim
|
|
|
|
startcode endcode startstack kstkesp signal blocked sigignore sigcatch
|
|
|
|
wchan nswap cnswap exit_signal processor(38) rt_priority
|
|
|
|
policy ioblock guest_time cguest_time (man 5 proc)
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
rval = {}
|
|
|
|
lines = readfile("/proc/" + pid + "/stat")
|
|
|
|
if lines == []:
|
2018-10-12 17:38:14 +01:00
|
|
|
return rval
|
2013-08-29 15:21:13 +01:00
|
|
|
items = lines[0].split()
|
2018-10-12 17:38:14 +01:00
|
|
|
utime, stime, cutime, cstime = [int(x) for x in items[13:17]]
|
2013-08-29 15:21:13 +01:00
|
|
|
rval = (items[38], # last run processor
|
2018-10-12 17:38:14 +01:00
|
|
|
int(items[22])/1000, # process virtual mem in kb
|
|
|
|
utime + stime + cutime + cstime, # totoal time
|
2013-08-29 15:21:13 +01:00
|
|
|
utime, # user time
|
|
|
|
stime, # system time
|
|
|
|
cutime + cstime) # wait time
|
|
|
|
return rval
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def getnodethroughput(self, pid):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return node throughput of total receive and transmit packets in kb.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
lines = readfile("/proc/" + pid + "/net/dev")
|
|
|
|
if lines == []:
|
2018-10-12 17:38:14 +01:00
|
|
|
return -0.00
|
|
|
|
ifs = [x.split() for x in lines[2:]]
|
2013-08-29 15:21:13 +01:00
|
|
|
ifm = zip(*ifs)
|
2018-10-12 17:38:14 +01:00
|
|
|
rv = sum(int(x) for x in ifm[1]) # received bytes
|
|
|
|
tr = sum(int(x) for x in ifm[9]) # transmited bytes
|
2013-08-29 15:21:13 +01:00
|
|
|
return (rv + tr)/1000
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def getnodemetrics(self, mindex):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return NodeMetrics with indexed by nodename, values are rows of
|
|
|
|
[ [ppid, vnoded, [cpu#, vmem(kb), ttime, utime, stime, wtime]],
|
|
|
|
[cpid, cmd, [cpu#, vmem(kb), ttime, utime, stime, wtime]], ... ,
|
|
|
|
[thrput, vmem(kb), ttime, utime, stime, wtime]]
|
|
|
|
"""
|
|
|
|
if mindex == 'a':
|
|
|
|
metricref = self.nodemetricsA
|
|
|
|
else:
|
|
|
|
metricref = self.nodemetricsB
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
self.getpids()
|
|
|
|
if self.pids == {}:
|
2018-10-12 17:38:14 +01:00
|
|
|
return {}
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
for nod in self.pids:
|
|
|
|
nmetric = NodeMetrics()
|
|
|
|
nmetric.__init__()
|
|
|
|
nodeapps = {}
|
2018-10-12 17:38:14 +01:00
|
|
|
for ap in range(len(self.pids[nod])): # get each process metrics
|
|
|
|
procm = self.getprocessmetrics(self.pids[nod][ap][0])
|
|
|
|
if procm == []:
|
|
|
|
if options.timestamp == True:
|
|
|
|
print str(time.time()),
|
|
|
|
print "WARNING: transient process", self.pids[nod][ap][1], \
|
|
|
|
"/", self.pids[nod][ap][0], "on node %s" % nod
|
|
|
|
else:
|
|
|
|
nodeapps[ap] = procm
|
|
|
|
self.pids[nod][ap].append(nodeapps[ap])
|
|
|
|
processm = zip(*nodeapps.values()) # get overall node metrics
|
|
|
|
if len(processm) > 0:
|
|
|
|
nmetric.setvalues((self.getnodethroughput(self.pids[nod][0][0]),
|
|
|
|
# vsize(kb)
|
|
|
|
sum(int(x) for x in processm[1]),
|
|
|
|
# ttime
|
|
|
|
sum(int(x) for x in processm[2]),
|
|
|
|
# utime
|
|
|
|
sum(int(x) for x in processm[3]),
|
|
|
|
# stime
|
|
|
|
sum(int(x) for x in processm[4]),
|
|
|
|
sum(int(x) for x in processm[5]))) # wtime
|
2013-08-29 15:21:13 +01:00
|
|
|
metricref[nod] = nmetric
|
|
|
|
return metricref
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
def setnodemetricsC(self, key, val):
|
|
|
|
self.nodemetricsC[key] = val
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
def printnodemetrics(self, mindex):
|
2013-08-29 15:21:13 +01:00
|
|
|
if mindex == 'c':
|
2018-10-12 17:38:14 +01:00
|
|
|
mm = self.nodemetricsC
|
|
|
|
else:
|
|
|
|
if mindex == 'a':
|
|
|
|
mm = self.nodemetricsA
|
|
|
|
else:
|
|
|
|
mm = self.nodemetricsB
|
|
|
|
|
|
|
|
for k in self.nodemetricsC:
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.timestamp:
|
|
|
|
print str(time.time()) + ",",
|
2018-10-12 17:38:14 +01:00
|
|
|
print k, ",", mm[k].tocsv()
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
def readnodethresholds(self, filename):
|
|
|
|
if filename is None:
|
|
|
|
return
|
|
|
|
lines = readfile(filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
for l in lines:
|
2018-10-12 17:38:14 +01:00
|
|
|
mval = l.strip().split('=')
|
|
|
|
if len(mval) > 1:
|
|
|
|
thekey = mval[0].strip()
|
|
|
|
theval = mval[1].strip()
|
|
|
|
if thekey in self.nodethresholds.getkeys():
|
|
|
|
self.nodethresholds.setvalue(thekey, float(theval))
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def checknodethresholds(self, nname):
|
2018-10-12 17:38:14 +01:00
|
|
|
calcm = self.nodemetricsC[nname]
|
2013-08-29 15:21:13 +01:00
|
|
|
for keyname in self.nodethresholds.getkeys():
|
2018-10-12 17:38:14 +01:00
|
|
|
if float(calcm.getvalue(keyname)) > float(self.nodethresholds.getvalue(keyname)):
|
|
|
|
alarm = ["node", nname + "/" + self.pids[nname][0][0], keyname,
|
|
|
|
calcm.getvalue(keyname), ">", self.nodethresholds.getvalue(keyname)]
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.timestamp:
|
|
|
|
print str(time.time()) + ",",
|
2018-10-12 17:38:14 +01:00
|
|
|
print ", ".join(str(x) for x in alarm)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def calcnodemetrics(self, cputimea, cputimeb, mems):
|
2018-10-12 17:38:14 +01:00
|
|
|
"""
|
|
|
|
Return a dict of nodemetrics indexed by node name
|
|
|
|
nodemetrics[nodename][-1] = node/host%.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
p = []
|
|
|
|
for i in range(len(cputimeb[0])):
|
|
|
|
p.append(cputimeb[0][i] - cputimea[0][i])
|
|
|
|
hostusedcpu = p[0] + p[1]
|
|
|
|
hostusedmem = mems[0] - mems[1]
|
|
|
|
if hostusedcpu == 0:
|
2018-10-12 17:38:14 +01:00
|
|
|
print "WARNING: host used cpu = 0, ", p[0], p[1]
|
|
|
|
hostusedcpu = 1
|
2013-08-29 15:21:13 +01:00
|
|
|
if hostusedmem == 0:
|
2018-10-12 17:38:14 +01:00
|
|
|
print "WARNING: host used mem = 0, ", mems[0], mems[1]
|
|
|
|
hostusedmem = 1
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
nodesa = self.nodemetricsA
|
|
|
|
nodesb = self.nodemetricsB
|
2013-08-29 15:21:13 +01:00
|
|
|
for nod in nodesb:
|
2018-10-12 17:38:14 +01:00
|
|
|
calcm = self.nodemetricsC
|
2013-08-29 15:21:13 +01:00
|
|
|
calcm = NodeMetrics()
|
2018-10-12 17:38:14 +01:00
|
|
|
calcm.__init__()
|
|
|
|
if (nod in nodesa):
|
|
|
|
try:
|
|
|
|
if (nodesb[nod] == []) | (nodesa[nod] == []) | \
|
|
|
|
(False == isinstance(nodesb[nod], NodeMetrics)) | \
|
|
|
|
(False == isinstance(nodesa[nod], NodeMetrics)):
|
|
|
|
if options.timestamp == True:
|
|
|
|
print str(time.time()),
|
|
|
|
print "Warning: nodes %s is not fully instanciated" % nod
|
|
|
|
else:
|
|
|
|
# calc throughput kbps
|
|
|
|
calcm.setvalue("nodethroughput", "%.2f" % (8 * (nodesb[nod].getvalue("nodethroughput")
|
|
|
|
- nodesa[nod].getvalue("nodethroughput")) / options.interval))
|
|
|
|
# calc mem node used / host used
|
|
|
|
calcm.setvalue("nodeusedmemory", "%.2f" % (
|
|
|
|
100.0 * (nodesb[nod].getvalue("nodeusedmemory") / hostusedmem)))
|
|
|
|
|
|
|
|
# calc total cpu time node / host
|
|
|
|
calcm.setvalue("nodetotalcpu", "%.2f" % (100.0 * (nodesb[nod].getvalue("nodetotalcpu")
|
|
|
|
- nodesa[nod].getvalue("nodetotalcpu")) / hostusedcpu))
|
|
|
|
# calc user cpu time node / host
|
|
|
|
calcm.setvalue("nodeusercpu", "%.2f" % (100.0 * (nodesb[nod].getvalue("nodeusercpu")
|
|
|
|
- nodesa[nod].getvalue("nodeusercpu")) / hostusedcpu))
|
|
|
|
# calc system cpu time node / host
|
|
|
|
calcm.setvalue("nodesystemcpu", "%.2f" % (100.0 * (nodesb[nod].getvalue("nodesystemcpu")
|
|
|
|
- nodesa[nod].getvalue("nodesystemcpu")) / hostusedcpu))
|
|
|
|
# calc waitcpu time node / host
|
|
|
|
calcm.setvalue("nodewaitcpu", "%.2f" % (100.0 * (nodesb[nod].getvalue("nodewaitcpu")
|
|
|
|
- nodesa[nod].getvalue("nodewaitcpu")) / hostusedcpu))
|
|
|
|
logsession.nodemetricsC[nod] = calcm
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
if options.alarm is not None:
|
2018-10-12 17:38:14 +01:00
|
|
|
logsession.checknodethresholds(nod)
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
print "Warning: transient node %s " % nod
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
return nodesb
|
2018-10-12 17:38:14 +01:00
|
|
|
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def main():
|
|
|
|
usagestr = "%prog [-h] [options] [args]\n\nLog server and optional CORE session metrics to stdout."
|
2018-10-12 17:38:14 +01:00
|
|
|
parser = optparse.OptionParser(usage=usagestr)
|
|
|
|
parser.set_defaults(interval=2, timestamp=False,
|
|
|
|
configfile="/etc/core/perflogserver.conf",
|
|
|
|
alarm=True, session=None)
|
|
|
|
parser.add_option("-i", "--interval", dest="interval", type=int,
|
|
|
|
help="seconds to wait between samples; default=%s" %
|
2013-08-29 15:21:13 +01:00
|
|
|
parser.defaults["interval"])
|
2018-10-12 17:38:14 +01:00
|
|
|
parser.add_option("-t", "--timestamp", action="store_true",
|
|
|
|
dest="timestamp",
|
|
|
|
help="include timestamp on each line")
|
|
|
|
parser.add_option("-c", "--configfile", dest="configfile",
|
|
|
|
type="string",
|
|
|
|
help="read threshold values from the specified file;\
|
2013-08-29 15:21:13 +01:00
|
|
|
default=%s" % parser.defaults["configfile"])
|
2018-10-12 17:38:14 +01:00
|
|
|
parser.add_option("-a", "--alarm", action="store_true",
|
|
|
|
dest="alarm",
|
|
|
|
help="generate alarms based threshold check on each cycle")
|
|
|
|
parser.add_option("-s", "--session", dest="session", type=int,
|
|
|
|
help="CORE session id; default=%s" %
|
2013-08-29 15:21:13 +01:00
|
|
|
parser.defaults["session"])
|
|
|
|
global options
|
|
|
|
global ncpus
|
|
|
|
global serverthresholds
|
|
|
|
global logsession
|
|
|
|
global cyclecount
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
options, _args = parser.parse_args()
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, handler)
|
|
|
|
signal.signal(signal.SIGTERM, handler)
|
|
|
|
|
|
|
|
ncpus = numcpus()
|
|
|
|
|
|
|
|
# server threshold dictionary - a ServerMetrics instant with default values
|
|
|
|
serverthresholds = ServerMetrics()
|
2018-10-12 17:38:14 +01:00
|
|
|
# set to server threshold default values: serverloadavg1=3.5,
|
|
|
|
# serverloadavg5=3.5, serverloadavg15=3.5, serverusedmemory=80.0,
|
2013-08-29 15:21:13 +01:00
|
|
|
# serverusedcputime=80.0, processorusedcputime=90.0
|
|
|
|
serverthresholds.setvalues([3.5, 3.5, 3.5, 80.0, 80.0, 90.0])
|
|
|
|
if options.alarm is True:
|
|
|
|
# read server threshold values from configuration file
|
2018-10-12 17:38:14 +01:00
|
|
|
readserverthresholds(options.configfile)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.session is not None:
|
|
|
|
logsession = LogSession()
|
|
|
|
|
|
|
|
# mark host log baseline
|
2018-10-12 17:38:14 +01:00
|
|
|
print "server: ", ", ".join(str(x) for x in os.uname()), ",", ncpus, "CPU cores"
|
2013-08-29 15:21:13 +01:00
|
|
|
print "start timestamp:", time.time(), ", baseline data: "
|
|
|
|
print csvserverbaseline()
|
2018-10-12 17:38:14 +01:00
|
|
|
print "server metrics: ", ", ".join(str(x) for x in serverthresholds.getkeys())
|
2013-08-29 15:21:13 +01:00
|
|
|
if options.session is not None:
|
2018-10-12 17:38:14 +01:00
|
|
|
print "node metrics: nodename, ", ", ".join(str(x) for x in logsession.nodethresholds.getkeys())
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
cyclecount = 0
|
|
|
|
while True:
|
|
|
|
cputimea = collectservercputimes()
|
|
|
|
time.sleep(options.interval)
|
|
|
|
cputimeb = collectservercputimes()
|
2018-10-12 17:38:14 +01:00
|
|
|
mems = collectservermems()
|
2013-08-29 15:21:13 +01:00
|
|
|
calccputime = calcservercputimes(cputimea, cputimeb)
|
2018-10-12 17:38:14 +01:00
|
|
|
m = csvservermetrics(collectservermetrics(
|
|
|
|
calccputime, mems, options.alarm))
|
|
|
|
print m
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
if options.session is not None:
|
2018-10-12 17:38:14 +01:00
|
|
|
nodesb = logsession.getnodemetrics('b')
|
|
|
|
if nodesb != {}:
|
|
|
|
logsession.calcnodemetrics(cputimea, cputimeb, mems)
|
|
|
|
logsession.printnodemetrics('c')
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
sys.stdout.flush()
|
2013-08-29 15:21:13 +01:00
|
|
|
cyclecount = cyclecount + 1
|
|
|
|
|
2018-10-12 17:38:14 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|