b25a2e453ec4376e2a05afa94bec50bb9eb350ba
[nivanova/samba-autobuild/.git] / source4 / scripting / python / samba / netcmd / processes.py
1 #   Unix SMB/CIFS implementation.
2 #   List processes (to aid debugging on systems without setproctitle)
3 #   Copyright (C) 2010-2011 Jelmer Vernooij <jelmer@samba.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Testbed for loadparm.c/params.c
19 #
20 # This module simply loads a specified configuration file and
21 # if successful, dumps it's contents to stdout. Note that the
22 # operation is performed with DEBUGLEVEL at 3.
23 #
24 # Useful for a quick 'syntax check' of a configuration file.
25 #
26
27 import os
28 import sys
29
30 import samba
31 import samba.getopt as options
32 from samba.netcmd import Command, CommandError, Option
33 from samba.messaging import Messaging
34
35 class cmd_processes(Command):
36     """List processes (to aid debugging on systems without setproctitle)."""
37
38     synopsis = "%prog [options]"
39
40     takes_optiongroups = {
41         "sambaopts": options.SambaOptions,
42         "versionopts": options.VersionOptions
43     }
44
45     takes_options = [
46         Option("--name", type=str,
47                help="Return only processes associated with one particular name"),
48         Option("--pid", type=int,
49                help="Return only names assoicated with one particular PID"),
50         ]
51
52     takes_args = []
53
54     def run(self, sambaopts, versionopts, section_name=None,
55             name=None, pid=None):
56
57         lp = sambaopts.get_loadparm()
58         logger = self.get_logger("processes")
59
60         msg_ctx = Messaging()
61
62         if name is not None:
63             ids = msg_ctx.irpc_servers_byname(name)
64             for server_id in ids:
65                 self.outf.write("%d\n" % server_id.pid)
66         elif pid is not None:
67             names = msg_ctx.irpc_all_servers()
68             for name in names:
69                 for server_id in name.ids:
70                     if server_id.pid == int(pid):
71                         self.outf.write("%s\n" % name.name)
72         else:
73             names = msg_ctx.irpc_all_servers()
74             self.outf.write(" Service:                PID \n")
75             self.outf.write("-----------------------------\n")
76             for name in names:
77                 for server_id in name.ids:
78                     self.outf.write("%-16s      %6d\n" % (name.name, server_id.pid))