samba-tool: Add samba-tool processes subcommand
authorAndrew Bartlett <abartlet@samba.org>
Mon, 29 Oct 2012 04:36:36 +0000 (15:36 +1100)
committerAndrew Bartlett <abartlet@samba.org>
Tue, 30 Oct 2012 21:13:56 +0000 (08:13 +1100)
This will allow administrators to inspect the process list in a
similar way to what running on a platform with setproctitle might
permit.

--pid= returns the registered server names for a PID (eg kdc, cldap_server)
--name= returns the pids registered with a particular name.

Andrew Bartlett

source4/scripting/python/samba/netcmd/main.py
source4/scripting/python/samba/netcmd/processes.py [new file with mode: 0644]
source4/scripting/python/samba/tests/samba_tool/processes.py [new file with mode: 0644]
source4/selftest/tests.py

index af3abfd0d390906a8a86d8df6176fa79282c623e..5f78823588084bf5a331f268add36b4c9f400a69 100644 (file)
@@ -38,6 +38,7 @@ from samba.netcmd.testparm import cmd_testparm
 from samba.netcmd.time import cmd_time
 from samba.netcmd.user import cmd_user
 from samba.netcmd.vampire import cmd_vampire
+from samba.netcmd.processes import cmd_processes
 
 
 class cmd_sambatool(SuperCommand):
@@ -66,3 +67,4 @@ class cmd_sambatool(SuperCommand):
     subcommands["time"] = cmd_time()
     subcommands["user"] = cmd_user()
     subcommands["vampire"] = cmd_vampire()
+    subcommands["processes"] = cmd_processes()
diff --git a/source4/scripting/python/samba/netcmd/processes.py b/source4/scripting/python/samba/netcmd/processes.py
new file mode 100644 (file)
index 0000000..751ab94
--- /dev/null
@@ -0,0 +1,78 @@
+#   Unix SMB/CIFS implementation.
+#   List processes (to aid debugging on systems without setproctitle)
+#   Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
+#
+# Testbed for loadparm.c/params.c
+#
+# This module simply loads a specified configuration file and
+# if successful, dumps it's contents to stdout. Note that the
+# operation is performed with DEBUGLEVEL at 3.
+#
+# Useful for a quick 'syntax check' of a configuration file.
+#
+
+import os
+import sys
+
+import samba
+import samba.getopt as options
+from samba.netcmd import Command, CommandError, Option
+from samba.messaging import Messaging
+
+class cmd_processes(Command):
+    """List processes (to aid debugging on systems without setproctitle)."""
+
+    synopsis = "%prog [options]"
+
+    takes_optiongroups = {
+        "sambaopts": options.SambaOptions,
+        "versionopts": options.VersionOptions
+    }
+
+    takes_options = [
+        Option("--name", type=str,
+               help="Return only processes associated with one particular name"),
+        Option("--pid", type=int,
+               help="Return only names assoicated with one particular PID"),
+        ]
+
+    takes_args = []
+
+    def run(self, sambaopts, versionopts, section_name=None,
+            name=None, pid=None):
+
+        lp = sambaopts.get_loadparm()
+        logger = self.get_logger("processes")
+
+        msg_ctx = Messaging()
+
+        if name is not None:
+            ids = msg_ctx.irpc_servers_byname(name)
+            for server_id in ids:
+                print "%d\n" % server_id.pid
+        elif pid is not None:
+            names = msg_ctx.irpc_all_servers()
+            for name in names:
+                for server_id in name.ids:
+                    if server_id.pid == int(pid):
+                        print "%s\n" % name.name
+        else:
+            names = msg_ctx.irpc_all_servers()
+            for name in names:
+                print "%s: " % name.name
+                for server_id in name.ids:
+                    print "%d " % server_id.pid
+                print "\n"
diff --git a/source4/scripting/python/samba/tests/samba_tool/processes.py b/source4/scripting/python/samba/tests/samba_tool/processes.py
new file mode 100644 (file)
index 0000000..91a5266
--- /dev/null
@@ -0,0 +1,35 @@
+# Unix SMB/CIFS implementation.
+# Copyright (C) Andrew Bartlett 2012
+#
+# based on time.py:
+# Copyright (C) Sean Dague <sdague@linux.vnet.ibm.com> 2011
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
+#
+
+import os
+from samba.tests.samba_tool.base import SambaToolCmdTest
+
+class ProcessCmdTestCase(SambaToolCmdTest):
+    """Tests for samba-tool process subcommands"""
+
+    def test_name(self):
+        """Run processes command"""
+        (result, out, err) = self.runcmd("processes", "--name", "samba")
+        self.assertCmdSuccess(result, "Ensuring processes ran successfully")
+
+    def test_all(self):
+        """Run processes command"""
+        (result, out, err) = self.runcmd("processes")
+        self.assertCmdSuccess(result, "Ensuring processes ran successfully")
index ea0907139a9b6706c10374039567493a7b154815..ca5bdd39c1c30ec984d1e0fa0311e9b0f9298f87 100755 (executable)
@@ -405,6 +405,7 @@ planpythontestsuite("dc:local", "samba.tests.dcerpc.bare")
 planpythontestsuite("dc:local", "samba.tests.dcerpc.unix")
 planpythontestsuite("dc:local", "samba.tests.dcerpc.srvsvc")
 planpythontestsuite("dc:local", "samba.tests.samba_tool.timecmd")
+planpythontestsuite("dc:local", "samba.tests.samba_tool.processes")
 planpythontestsuite("dc:local", "samba.tests.samba_tool.user")
 planpythontestsuite("dc:local", "samba.tests.samba_tool.group")
 planpythontestsuite("plugin_s4_dc:local", "samba.tests.samba_tool.ntacl")