/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
# -*- mode: python; coding: utf-8 -*-

import sys
import dbus
from optparse import OptionParser
import locale

locale.setlocale(locale.LC_ALL, u'')

tablewords = {
    'name': u'Name',
    'enabled': u'Enabled',
    'timeout': u'Timeout',
    'last_checked_ok': u'Last Successful Check',
    'created': u'Created',
    'interval': u'Interval',
    'host': u'Host',
    'fingerprint': u'Fingerprint',
    'checker_running': u'Check Is Running',
    'last_enabled': u'Last Enabled',
    'checker': u'Checker',
    }
defaultkeywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
                   'checker')
busname = 'org.mandos-system.Mandos'
server_path = '/Mandos'
server_interface = 'org.mandos_system.Mandos'
client_interface = 'org.mandos_system.Mandos.Client'
version = "1.0.2"

bus = dbus.SystemBus()
mandos_dbus_objc = bus.get_object(busname, server_path)
mandos_serv = dbus.Interface(mandos_dbus_objc,
                             dbus_interface = server_interface)
mandos_clients = mandos_serv.GetAllClientsWithProperties()

def print_clients():
    def valuetostring(x):
        if type(x) is dbus.Boolean:
            return u"Yes" if x else u"No"
        else:
            return unicode(x)
    format_string = u' '.join(u'%%-%ds' %
                              max(len(tablewords[key]),
                                  max(len(valuetostring(client[key]))
                                      for client in
                                      mandos_clients.itervalues()))
                              for key in keywords)
    print format_string % tuple(tablewords[key] for key in keywords) 
    for client in mandos_clients.itervalues():
        print format_string % tuple(valuetostring(client[key])
                                    for key in keywords)

parser = OptionParser(version = "%%prog %s" % version)
parser.add_option("-a", "--all", action="store_true",
                  help="Print all fields")
parser.add_option("-e", "--enable", action="store_true",
                  help="Enable specified client")
parser.add_option("-d", "--disable", action="store_true",
                  help="disable specified client")
parser.add_option("-b", "--bumptimeout", action="store_true",
                  help="Bump timeout of specified client")
parser.add_option("-s", "--startchecker", action="store_true",
                  help="Start checker for specified client")
parser.add_option("-S", "--stopchecker", action="store_true",
                  help="Stop checker for specified client")
options, client_names = parser.parse_args()

clients=[]
for name in client_names:
    for path, client in mandos_clients.iteritems():
        if client['name'] == name:
            client_objc = bus.get_object(busname, path)
            clients.append(dbus.Interface(client_objc,
                                          dbus_interface
                                          = client_interface))
            break
    else:
        print >> sys.stderr, "Client not found on server: %r" % name
        sys.exit(1)

if not clients:
    keywords = defaultkeywords
    if options.all:
        keywords = ('name', 'enabled', 'timeout', 'last_checked_ok',
                    'created', 'interval', 'host', 'fingerprint',
                    'checker_running', 'last_enabled', 'checker')
    print_clients()

for client in clients:
    if options.enable:
        client.Enable()
    if options.disable:
        client.Disable()
    if options.bumptimeout:
        client.BumpTimeout()
    if options.startchecker:
        client.StartChecker()
    if options.stopchecker:
        client.StopChecker()