linux-rt-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Raphaël Beamonte" <raphael.beamonte@gmail.com>
To: williams@redhat.com
Cc: linux-rt-users@vger.kernel.org,
	"Raphaël Beamonte" <raphael.beamonte@gmail.com>
Subject: [PATCH 3/3] Rewrite of the get_services method to make it cross-distribution
Date: Tue, 23 Oct 2012 11:45:05 -0400	[thread overview]
Message-ID: <1351007105-7852-4-git-send-email-raphael.beamonte@gmail.com> (raw)
In-Reply-To: <1351007105-7852-1-git-send-email-raphael.beamonte@gmail.com>

Changes the get_services method to call the new created
__get_services_sysvinit and __get_services_systemd methods
to be able to manage the two different init daemons.

Signed-off-by: Raphaël Beamonte <raphael.beamonte@gmail.com>
---
 rteval/rteval.py |   73 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 58 insertions(+), 15 deletions(-)

diff --git a/rteval/rteval.py b/rteval/rteval.py
index f54c1f4..829a49b 100644
--- a/rteval/rteval.py
+++ b/rteval/rteval.py
@@ -48,6 +48,9 @@ import signal
 import rtevalclient
 import ethtool
 import xmlrpclib
+import platform
+import fnmatch
+import glob
 from datetime import datetime
 from distutils import sysconfig
 
@@ -92,6 +95,7 @@ class RtEval(object):
         self.inifile = None
         self.cmd_options = {}
         self.start = datetime.now()
+        self.init = 'unknown'
 
         default_config = {
             'rteval': {
@@ -239,22 +243,61 @@ class RtEval(object):
                     topology.getCPUsockets()))
         return topology.getXMLdata()
 
-
-
-    def get_services(self):
-        rejects = ('capi', 'firstboot', 'functions', 'halt', 'iptables', 'ip6tables', 
-                   'killall', 'lm_sensors', 'microcode_ctl', 'network', 'ntpdate', 
-                   'rtctl', 'udev-post')
-        service_list = filter(lambda x: x not in rejects, os.listdir('/etc/rc.d/init.d'))
+    def __get_services_sysvinit(self):
+        reject = ('functions', 'halt', 'killall', 'single', 'linuxconf', 'kudzu',
+                  'skeleton', 'README', '*.dpkg-dist', '*.dpkg-old', 'rc', 'rcS',
+                  'single', 'reboot', 'bootclean.sh')
+        for sdir in ('/etc/init.d', '/etc/rc.d/init.d'):
+            if os.path.isdir(sdir):
+                servicesdir = sdir
+                break
+        if not servicesdir:
+            raise RuntimeError, "No services dir (init.d) found on your system"
+        self.debug("Services located in %s, going through each service file to check status" % servicesdir)
         ret_services = {}
-        self.debug("getting services status")
-        for s in service_list:
-            cmd = ['/sbin/service', s, 'status']
-            c = subprocess.Popen(cmd, stdout=subprocess.PIPE)
-            status = c.stdout.read().strip().translate(self.transtable, self.junk)
-            ret_services[s] = status
+        for service in glob.glob(os.path.join(servicesdir, '*')):
+            servicename = os.path.basename(service)
+            if not [1 for p in reject if fnmatch.fnmatch(servicename, p)] and os.access(service, os.X_OK):
+                cmd = '%s -qs "\(^\|\W\)status)" %s' % (getcmdpath('grep'), service)
+                c = subprocess.Popen(cmd, shell=True)
+                c.wait()
+                if c.returncode == 0:
+                    cmd = ['env', '-i', 'LANG="%s"' % os.environ['LANG'], 'PATH="%s"' % os.environ['PATH'], 'TERM="%s"' % os.environ['TERM'], service, 'status']
+                    c = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    c.wait()
+                    if c.returncode == 0 and (c.stdout.read() or c.stderr.read()):
+                        ret_services[servicename] = 'running'
+                    else:
+                        ret_services[servicename] = 'not running'
+                else:
+                    ret_services[servicename] = 'unknown'
         return ret_services
-            
+        
+    def __get_services_systemd(self):
+        ret_services = {}
+        cmd = '%s list-unit-files -t service --no-legend' % getcmdpath('systemctl')
+        self.debug("cmd: %s" % cmd)
+        c = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        for p in c.stdout:
+            # p are lines like "servicename.service status"
+            v = p.strip().split()
+            ret_services[v[0].split('.')[0]] = v[1]
+        return ret_services
+
+    def get_services(self):
+        cmd = [getcmdpath('ps'), '-ocomm=',  '1']
+        c = subprocess.Popen(cmd, stdout=subprocess.PIPE)
+        self.init = c.stdout.read().strip()
+        if self.init == 'systemd':
+            self.debug("Using systemd to get services status")
+            return self.__get_services_systemd()
+        elif self.init == 'init':
+            self.init = 'sysvinit'
+            self.debug("Using sysvinit to get services status")
+            return self.__get_services_sysvinit()
+        else:
+            raise RuntimeError, "Unknown init system (%s)" % self.init
+        return {}
 
     def get_kthreads(self):
         policies = {'FF':'fifo', 'RR':'rrobin', 'TS':'other', '?':'unknown' }
@@ -434,7 +477,7 @@ class RtEval(object):
         self.xmlreport.taggedvalue('memory_size', "%.3f" % self.memsize[0], {"unit": self.memsize[1]})
         self.xmlreport.closeblock()
 
-        self.xmlreport.openblock('services')
+        self.xmlreport.openblock('services', {'init': self.init})
         for s in self.services:
             self.xmlreport.taggedvalue("service", self.services[s], {"name": s})
         self.xmlreport.closeblock()
-- 
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2012-10-23 15:45 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-23 15:45 [PATCH 0/3] rteval cross-distribution patch Raphaël Beamonte
2012-10-23 15:45 ` [PATCH 1/3] Rewrite of the get_kthreads method to make it cross-distribution Raphaël Beamonte
2012-10-23 15:45 ` [PATCH 2/3] Adds getcmdpath method to use which to locate the used commands Raphaël Beamonte
2012-10-23 15:45 ` Raphaël Beamonte [this message]
2012-10-23 17:26 ` [PATCH 0/3] rteval cross-distribution patch Clark Williams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1351007105-7852-4-git-send-email-raphael.beamonte@gmail.com \
    --to=raphael.beamonte@gmail.com \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=williams@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).