From ace9d78fe55042ee36daaae0a162ee4b24745ed9 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 3 Jun 2011 18:58:18 +0530 Subject: Added functionalities to get and update cpu details into rrd Added get_rrd_cpu_details.py, rrd_update_cpu_details.py --- .../src/get_rrd_cpu_details.py | 57 ++++++++++++++ .../src/rrd_update_cpu_details.py | 92 ++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100755 src/com.gluster.storage.management.server.scripts/src/get_rrd_cpu_details.py create mode 100755 src/com.gluster.storage.management.server.scripts/src/rrd_update_cpu_details.py (limited to 'src/com.gluster.storage.management.server.scripts') diff --git a/src/com.gluster.storage.management.server.scripts/src/get_rrd_cpu_details.py b/src/com.gluster.storage.management.server.scripts/src/get_rrd_cpu_details.py new file mode 100755 index 00000000..a525d531 --- /dev/null +++ b/src/com.gluster.storage.management.server.scripts/src/get_rrd_cpu_details.py @@ -0,0 +1,57 @@ +#!/usr/bin/python +# Copyright (C) 2010 Gluster, Inc. +# This file is part of Gluster Storage Platform. +# +# Gluster Storage Platform 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. +# +# Gluster Storage Platform 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 +# . +import os +import sys +import syslog +from XmlHandler import ResponseXml +import Utils +import Common + +def getCpuData(period): + cpuRrdFile = "/var/lib/rrd/cpu.rrd" + rs = ResponseXml() + command = ["rrdtool", "xport", "--start", "-1%s" % period, + "DEF:cpuuser=%s:user:AVERAGE" % cpuRrdFile, + "DEF:cpusystem=%s:system:AVERAGE" % cpuRrdFile, + "DEF:cpuidle=%s:idle:AVERAGE" % cpuRrdFile, + "XPORT:cpuuser:'user'", + "XPORT:cpusystem:'system'", + "XPORT:cpuidle:'idle'"] + + rv = Utils.runCommandFG(command, stdout=True, root=True) + message = Common.stripEmptyLines(rv["Stdout"]) + if rv["Stderr"]: + error = Common.stripEmptyLines(rv["Stderr"]) + message += "Error: [%s]" % (error) + Common.log(syslog.LOG_ERR, "failed to create RRD file for cpu usages %s" % file) + rs.appendTagRoute("status.code", rv["Status"]) + rs.appendTagRoute("status.message", message) + return rs.toxml() + return rv["Stdout"] + +def main(): + if len(sys.argv) != 2: + print >> sys.stderr, "usage: %s " % sys.argv[0] + sys.exit(-1) + + period = sys.argv[1] + print getCpuData(period) + sys.exit(0) + +if __name__ == "__main__": + main() diff --git a/src/com.gluster.storage.management.server.scripts/src/rrd_update_cpu_details.py b/src/com.gluster.storage.management.server.scripts/src/rrd_update_cpu_details.py new file mode 100755 index 00000000..81ca6add --- /dev/null +++ b/src/com.gluster.storage.management.server.scripts/src/rrd_update_cpu_details.py @@ -0,0 +1,92 @@ +#!/usr/bin/python +# Copyright (C) 2010 Gluster, Inc. +# This file is part of Gluster Storage Platform. +# +# Gluster Storage Platform 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. +# +# Gluster Storage Platform 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 +# . +import os +import sys +import syslog +from XmlHandler import ResponseXml +import Utils +import Common + +def createMemData(file, step): + rs = ResponseXml() + command = ["rrdtool", "create", file, "--step=%s" % step, + "DS:user:COUNTER:600:0:U", + "DS:system:COUNTER:600:0:U", + "DS:idle:COUNTER:600:0:U", + "RRA:AVERAGE:0.5:1:576", + "RRA:AVERAGE:0.5:6:672", + "RRA:AVERAGE:0.5:24:732", + "RRA:AVERAGE:0.5:144:1460"] + + rv = Utils.runCommandFG(command, stdout=True, root=True) + message = Common.stripEmptyLines(rv["Stdout"]) + if rv["Stderr"]: + error = Common.stripEmptyLines(rv["Stderr"]) + message += "Error: [%s]" % (error) + Common.log(syslog.LOG_ERR, "failed to create RRD file for cpu usages %s" % file) + rs.appendTagRoute("status.code", rv["Status"]) + rs.appendTagRoute("status.message", message) + return rs.toxml() + return None + +def updateMemData(file): + rs = ResponseXml() + user = None + system = None + idle = None + for line in open("/proc/stat").readlines(): + if line.startswith("cpu"): + cpudetails = line.split() + if "cpu" == cpudetails[0]: + user = cpudetails[1] + system = cpudetails[3] + idle = cpudetails[4] + break + + if None == user: + Common.log(syslog.LOG_ERR, "failed to fetch cpu details from /proc/stat") + rs.appendTagRoute("status.code", "-1") + rs.appendTagRoute("status.message", "failed to fetch cpu details") + return rs.toxml() + + command = ["rrdtool", "update", file, "-t", "user:system:idle", + "N:%s:%s:%s" % (user, system, idle)] + rv = Utils.runCommandFG(command, stdout=True, root=True) + if rv["Stderr"]: + error = Common.stripEmptyLines(rv["Stderr"]) + message = "Error: [%s]" % (error) + Common.log(syslog.LOG_ERR, "failed to update cpu usage into rrd file %s" % file) + rs.appendTagRoute("status.code", rv["Status"]) + rs.appendTagRoute("status.message", message) + return rs.toxml() + return None + + +def main(): + cpuRrdFile = "/var/lib/rrd/cpu.rrd" + if not os.path.exists(cpuRrdFile): + status = createMemData(cpuRrdFile, 100) + if status: + print status + status = updateMemData(cpuRrdFile) + if status: + print status + sys.exit(0) + +if __name__ == "__main__": + main() -- cgit