samba_dnsupdate: make rodc_dns_update() more robust against timing problems
[samba.git] / source4 / scripting / bin / smbstatus
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 #
4 #  provide information on connected users and open files
5 #  Copyright (c) Jelmer Vernooij 2008
6 #
7 #  Based on the original in EJS:
8 #  Copyright Andrew Tridgell 2005
9 #  Released under the GNU GPL version 3 or later
10 #
11
12 import os, sys
13
14 # make sure the script dies immediately when hitting control-C,
15 # rather than raising KeyboardInterrupt. As we do all database
16 # operations using transactions, this is safe.
17 import signal
18 signal.signal(signal.SIGINT, signal.SIG_DFL)
19
20 sys.path.insert(0, "bin/python")
21
22 import optparse
23 import samba.getopt as options
24 from samba import irpc, messaging
25
26 def show_sessions(conn):
27     """show open sessions"""
28
29     sessions = next(conn.smbsrv_information(irpc.SMBSRV_INFO_SESSIONS))
30     print("User                                  Client      Connected at")
31     print("-" * 79)
32     for session in sessions:
33         fulluser = "%s/%s" % (session.account_name, session.domain_name)
34         print("%-30s %16s   %s" % (fulluser,
35                                    session.client_ip,
36                                    sys.httptime(session.connect_time)))
37     print()
38
39 def show_tcons(open_connection):
40     """show open tree connects"""
41     conn = open_connection("smb_server")
42     tcons = next(conn.smbsrv_information(irpc.SMBSRV_INFO_TCONS))
43     print "Share                                 Client      Connected at"
44     print "-" * 79
45     for tcon in tcons:
46         print "%-30s %16s   %s" % (tcon.share_name, tcon.client_ip, sys.httptime(tcon.connect_time))
47
48
49 def show_nbt(open_connection):
50     """show nbtd information"""
51     conn = open_connection("nbt_server")
52     stats = next(conn.nbtd_information(irpc.NBTD_INFO_STATISTICS))
53     print "NBT server statistics:"
54     fields = [("total_received", "Total received"),
55               ("total_sent", "Total sent"),
56               ("query_count", "Query count"),
57               ("register_count", "Register count"),
58               ("release_count", "Release count")]
59     for (field, description) in fields:
60         print "\t%s:\t%s" % (description, getattr(stats, field))
61     print
62
63 parser = optparse.OptionParser("%s [options]" % sys.argv[0])
64 sambaopts = options.SambaOptions(parser)
65 parser.add_option_group(sambaopts)
66 parser.add_option("--messaging-path", type="string", metavar="PATH",
67                   help="messaging path")
68 parser.add_option("--nbt", help="show NetBIOS status", action="store_true")
69
70 opts, args = parser.parse_args()
71
72 lp = sambaopts.get_loadparm()
73
74 print "%s" % lp.get("server string")
75
76 messaging_path = (opts.messaging_path or os.path.join(lp.get("private dir"), "smbd.tmp", "messaging"))
77
78 def open_connection(name):
79     return messaging.ClientConnection(name, messaging_path=messaging_path)
80
81 if opts.nbt:
82     show_nbt(open_connection)
83 else:
84     try:
85         conn = open_connection("smb_server")
86     except RuntimeError, (num, msg):
87         if msg == 'NT_STATUS_OBJECT_NAME_NOT_FOUND':
88             print "No active connections"
89     else:
90         show_sessions(conn)
91         show_tcons(conn)