From 42c379f0dfdeb36598bb2636aa2b6e3ca4410930 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 29 Oct 2012 15:36:36 +1100 Subject: [PATCH] samba-tool: Add samba-tool processes subcommand 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 | 2 + source4/scripting/python/samba/netcmd/processes.py | 78 ++++++++++++++++++++++ .../python/samba/tests/samba_tool/processes.py | 35 ++++++++++ source4/selftest/tests.py | 1 + 4 files changed, 116 insertions(+) create mode 100644 source4/scripting/python/samba/netcmd/processes.py create mode 100644 source4/scripting/python/samba/tests/samba_tool/processes.py diff --git a/source4/scripting/python/samba/netcmd/main.py b/source4/scripting/python/samba/netcmd/main.py index af3abfd0d39..5f788235880 100644 --- a/source4/scripting/python/samba/netcmd/main.py +++ b/source4/scripting/python/samba/netcmd/main.py @@ -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 index 00000000000..751ab94c21f --- /dev/null +++ b/source4/scripting/python/samba/netcmd/processes.py @@ -0,0 +1,78 @@ +# Unix SMB/CIFS implementation. +# List processes (to aid debugging on systems without setproctitle) +# Copyright (C) 2010-2011 Jelmer Vernooij +# +# 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 . +# +# 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 index 00000000000..91a5266b54a --- /dev/null +++ b/source4/scripting/python/samba/tests/samba_tool/processes.py @@ -0,0 +1,35 @@ +# Unix SMB/CIFS implementation. +# Copyright (C) Andrew Bartlett 2012 +# +# based on time.py: +# Copyright (C) Sean Dague 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 . +# + +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") diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index ea0907139a9..ca5bdd39c1c 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -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") -- 2.11.4.GIT