Discussion:
func/minion
Greg Swift
2011-03-20 18:21:41 UTC
Permalink
func/minion/modules/disk.py | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)

New commits:
commit 1a66cc90a60f9de9d3167cc70fecee0b87e50003
Author: Greg Swift <gregswift-***@public.gmane.org>
Date: Sun Mar 20 13:17:22 2011 -0500

Added file system time to the data gathered by disk modules' usage call

diff --git a/func/minion/modules/disk.py b/func/minion/modules/disk.py
index a2f0767..8cc9743 100644
--- a/func/minion/modules/disk.py
+++ b/func/minion/modules/disk.py
@@ -19,12 +19,12 @@ class DiskModule(func_module.FuncModule):

def usage(self, partition=None):
"""
- Returns the results of df -P
+ Returns the results of df -PT
"""
results = {}
# splitting the command variable out into a list does not seem to function
# in the tests I have run
- command = '/bin/df -P'
+ command = '/bin/df -PT'
if (partition):
command += ' %s' % (partition)
cmdref = sub_process.Popen(command, stdout=sub_process.PIPE,
@@ -34,11 +34,12 @@ class DiskModule(func_module.FuncModule):
for disk in stdout.split('\n'):
if (disk.startswith('Filesystem') or not disk):
continue
- (device, total, used, available, percentage, mount) = disk.split()
+ (device, fstype, total, used, available, percentage, mount) = disk.split()
results[mount] = {'device':device,
'total':str(total),
'used':str(used),
'available':str(available),
+ 'fstype':str(fstype),
'percentage':int(percentage[:-1])}
return results
Greg Swift
2011-03-21 03:28:02 UTC
Permalink
func/minion/modules/service.py | 36 +++++++++++++++++++++++++-----------
1 file changed, 25 insertions(+), 11 deletions(-)

New commits:
commit 676f1361bf01746bd5676acb7caefdb18360245f
Author: Greg Swift <gregswift-***@public.gmane.org>
Date: Sun Mar 20 21:50:38 2011 -0500

To better handle internationalized systems:
- chkconfig and /sbin/service now run with environment variable LANG=C
- Rewrote get_running(), now loops through get_enabled() grabbing exit code status from /etc/init.d/<service> status

diff --git a/func/minion/modules/service.py b/func/minion/modules/service.py
index 38de51c..1093797 100644
--- a/func/minion/modules/service.py
+++ b/func/minion/modules/service.py
@@ -28,9 +28,9 @@ class Service(func_module.FuncModule):

service_name = service_name.strip() # remove useless spaces

- filename = os.path.join("/etc/rc.d/init.d/",service_name)
+ filename = os.path.join("/etc/init.d/",service_name)
if os.path.exists(filename):
- return sub_process.call(["/sbin/service", service_name, command], close_fds=True)
+ return sub_process.call(["/sbin/service", service_name, command], close_fds=True, env={ 'LANG':'C' })
else:
raise codes.FuncException("Service not installed: %s" % service_name)

@@ -79,15 +79,13 @@ class Service(func_module.FuncModule):
grep = func_module.findout(grep)


-
-
def get_enabled(self):
"""
Get the list of services that are enabled at the various runlevels. Xinetd services
only provide whether or not they are running, not specific runlevel info.
"""

- chkconfig = sub_process.Popen(["/sbin/chkconfig", "--list"], stdout=sub_process.PIPE, close_fds=True)
+ chkconfig = sub_process.Popen(["/sbin/chkconfig", "--list"], stdout=sub_process.PIPE, close_fds=True, env={ "LANG": "C" })
data = chkconfig.communicate()[0]
results = []
for line in data.split("\n"):
@@ -106,13 +104,29 @@ class Service(func_module.FuncModule):
"""
Get a list of which services are running, stopped, or disabled.
"""
- chkconfig = sub_process.Popen(["/sbin/service", "--status-all"], stdout=sub_process.PIPE, close_fds=True)
- data = chkconfig.communicate()[0]
results = []
- for line in data.split("\n"):
- if line.find(" is ") != -1:
- tokens = line.split()
- results.append((tokens[0], tokens[-1].replace("...","")))
+
+ # Get services
+ services = self.get_enabled()
+
+ init_return_codes = { 0: 'running', 1: 'dead', 2:'locked', 3:'stopped' }
+
+ for service in services:
+ filename = os.path.join("/etc/init.d/",service[0])
+ # Run status for service
+ try:
+ init_exec = sub_process.Popen([filename, "status"], stdout=sub_process.PIPE, close_fds=True, env={ "LANG": "C" })
+ except Exception, e:
+ raise codes.FuncException("Service status error %s on initscript %s" % (e, filename))
+
+ # Get status output
+ data = init_exec.communicate()[0]
+
+ # Wait for command to complete
+ init_exec.wait()
+
+ # Append the result, service name, return status and status output
+ results.append((service[0], init_return_codes[init_exec.returncode], data))
return results

def register_method_args(self):
Greg Swift
2011-03-22 02:38:00 UTC
Permalink
func/minion/modules/cpu.py | 118 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 118 insertions(+)

New commits:
commit 528829f1982cbe34994b71cd826676316a748f08
Author: Greg Swift <gregswift-***@public.gmane.org>
Date: Mon Mar 21 21:35:11 2011 -0500

Added new cpu.py module to exposes cpu information. It has two methods:
- usage - gets the average cpu usage, returns per cpu percentage for user, nice, system, idle, iowait, etc.
- jiffies - fetches cpu statistics from /proc/stat and returns them

Adjusted to original submission:
- support additional CPU values if a system has them
- reduce some of the processing time by using a generator
- attempted to minimize repeated string and list operations

diff --git a/func/minion/modules/cpu.py b/func/minion/modules/cpu.py
new file mode 100644
index 0000000..56b8431
--- /dev/null
+++ b/func/minion/modules/cpu.py
@@ -0,0 +1,118 @@
+
+#
+# Copyright 2011
+# Tomas Edwardsson <tommi-***@public.gmane.org>
+#
+# This software may be freely redistributed under the terms of the GNU
+# general public license.
+#
+# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+import func_module
+from timemodule import sleep
+from func.minion.codes import FuncException
+
+SAMPLE_TIMER = 5
+MAX_SAMPLE_TIMER = 18
+
+class CpuModule(func_module.FuncModule):
+ version = "0.0.1"
+ api_version = "0.0.1"
+ description = "Gathering CPU related information"
+
+ def jiffies(self):
+
+ # Which fields we are parsing from /proc stat
+ fields = ['user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq', 'steal', 'guest']
+
+ # Returning struct
+ res = {}
+
+ # Open the /proc/stat
+ try:
+ stat_fd = open("/proc/stat", "r")
+ except Exception, e:
+ raise FuncException("Unable to open /proc/stat: %s" % (e))
+
+ # Run through the contents of /proc/stat
+ for statline in stat_fd.readlines():
+ # We don't care about non CPU stuff
+ if statline[0:3] != 'cpu':
+ break
+ statline = (statline.split())
+
+ # split cpu name and its stats
+ cpu, stat = statline[0], statline[1:]
+
+ # Total jiffies for this cpu
+ total = 0
+
+ # Create the dictionary
+ res[cpu] = {}
+
+ # Run through stats, matching with named fields
+ for i in xrange(1, (len(stat))):
+ try:
+ res[cpu][fields[i]] = int(stat[i])
+ except IndexError:
+ break
+ total += int(stat[i])
+
+ # Record total jiffies
+ res[cpu]['total'] = total
+
+ return res
+
+ def usage(self, sampletime=SAMPLE_TIMER):
+ """
+ Returns percentage CPU utilization in an given
+ timeperiod.
+ """
+ if int(sampletime) > MAX_SAMPLE_TIMER:
+ raise FuncException("sampletime maximum is %s" % MAX_SAMPLE_TIMER)
+
+ # Get CPU statistics
+ prestat = self.jiffies()
+
+ # Wait for some activity
+ sleep(int(sampletime))
+
+ # Re fetch CPU statistics
+ poststat = self.jiffies()
+
+ # Dict to store results
+ results = {}
+
+ # Run through each CPU entry
+ for cpu in prestat.keys():
+ total = poststat[cpu]['total'] - prestat[cpu]['total']
+ results[cpu] = {}
+ for k in prestat[cpu].keys():
+ if k == 'total': continue
+ # Calculate the percentage
+ results[cpu][k] = float(poststat[cpu][k] - prestat[cpu][k]) / float(total) * 100
+ return results
+
+ def register_method_args(self):
+ """
+ Register the CPU method arguments
+ """
+ return{
+ 'usage':{
+ 'args':{
+ 'sampletime':{
+ 'type': 'int',
+ 'default': SAMPLE_TIMER,
+ 'optional':True,
+ 'description':'How long to sample CPU usage',
+ },
+ },
+ 'description':'Gather CPU data over period and return percent averages',
+ },
+ 'jiffies':{
+ 'args':{},
+ 'description':'Fetch the CPU jiffies from /proc/stat',
+ },
+ }

Loading...