From 686b574d3c1e55a778088b58a1a2fc75ce72d280 Mon Sep 17 00:00:00 2001 From: ndarshan Date: Mon, 17 Mar 2014 12:21:42 +0530 Subject: plugins:Fix to handle sadf not accepting time range, test case addition This patch handles the issue of sadf not accepting time range when used with -x (xml output) option(seen in version 9.0.4). Added unit-test for memory, cpu, swap, network plugins and refactored them. Change-Id: Ie7c2ecfbb38060f236a6faed606bce0aedd27d7a Signed-off-by: ndarshan Reviewed-on: https://cuckoo.blr.redhat.com:8443/14 Reviewed-by: Bala FA Tested-by: Bala FA --- plugins/cpu.py | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100755 plugins/cpu.py (limited to 'plugins/cpu.py') diff --git a/plugins/cpu.py b/plugins/cpu.py new file mode 100755 index 0000000..89e8b57 --- /dev/null +++ b/plugins/cpu.py @@ -0,0 +1,108 @@ +#!/usr/bin/python +# Copyright (C) 2014 Red Hat Inc +# +# 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 2 +# 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, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA +# + +import sys +import argparse +from glusternagios import utils +import sadf + +_sadfCpuCommand = ["sadf", "-x", "--", "-P", "ALL"] + + +def parse_input(): + parser = argparse.ArgumentParser() + parser.add_argument("-w", "--warning", action="store", + required=True, type=int, + help="Warning threshold in percentage") + parser.add_argument("-c", "--critical", action="store", + required=True, type=int, + help="Critical threshold in percentage") + sadf.add_common_args(parser) + args = parser.parse_args() + return args + + +def showCpuStat(warnLevel, critLevel, s): + pl_op = {} + if not s: + pl_op["message"] = ("CPU STATUS UNKNOWN") + pl_op['exit_status'] = utils.PluginStatusCode.UNKNOWN + return pl_op + perfLines = [] + idleCpu = 0 + try: + for cpu in s['cpu-load']['cpu']: + if cpu['number'] == 'all': + idleCpu = cpu['idle'] + perfLines.append( + ("cpu_%s_total=%s%%;%s;%s cpu_%s_system=%s%% " + "cpu_%s_user=%s%% cpu_%s_idle=%s%%" % ( + cpu['number'], 100 - float(cpu['idle']), + warnLevel, critLevel, + cpu['number'], cpu['system'], + cpu['number'], cpu['user'], + cpu['number'], cpu['idle']))) + if len(s['cpu-load']['cpu']) - 1 == 1: + break + except (KeyError, ValueError, TypeError) as e: + pl_op["message"] = "key: %s not found" % str(e) + pl_op["exit_status"] = utils.PluginStatusCode.UNKNOWN + return pl_op + + totalCpuUsage = 100 - float(idleCpu) + if totalCpuUsage > critLevel: + pl_op["message"] = ("CPU Status CRITICAL: Total CPU:" + "%s%% Idle CPU:%s%% " + "| num_of_cpu=%s %s" % ( + totalCpuUsage, idleCpu, + len(s['cpu-load']['cpu']) - 1, + " ".join(perfLines))) + pl_op['exit_status'] = utils.PluginStatusCode.CRITICAL + elif totalCpuUsage > warnLevel: + pl_op["message"] = ("CPU Status WARNING: Total CPU" + ":%s%% Idle CPU:%s%% " + "| num_of_cpu=%s %s" % ( + totalCpuUsage, idleCpu, + len(s['cpu-load']['cpu']) - 1, + " ".join(perfLines))) + pl_op['exit_status'] = utils.PluginStatusCode.WARNING + else: + pl_op["message"] = ("CPU Status OK: Total CPU:%s%% Idle CPU:%s%% " + "| num_of_cpu=%s %s" % ( + totalCpuUsage, idleCpu, + len(s['cpu-load']['cpu']) - 1, + " ".join(perfLines))) + pl_op['exit_status'] = utils.PluginStatusCode.OK + return pl_op + + +if __name__ == '__main__': + args = parse_input() + if args.critical <= args.warning: + print "UNKNOWN:Critical must be greater than Warning." + sys.exit(utils.PluginStatusCode.UNKNOWN) + try: + st = sadf.getLatestStat(sadf.sadfExecCmd(_sadfCpuCommand), + args.interval if args.interval else 1) + except (sadf.SadfCmdExecFailedException, + sadf.SadfXmlErrorException) as e: + print str(e) + exit(utils.PluginStatusCode.UNKNOWN) + d = showCpuStat(args.warning, args.critical, st) + print d["message"] + exit(d['exit_status']) -- cgit