/mandos/release

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/release

« back to all changes in this revision

Viewing changes to mandos-monitor

Merge in branch to interpret an empty device name to mean
"autodetect".

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
import UserList
21
21
 
 
22
import locale
 
23
 
 
24
locale.setlocale(locale.LC_ALL, u'')
 
25
 
22
26
# Some useful constants
23
27
domain = 'se.bsnet.fukt'
24
28
server_interface = domain + '.Mandos'
33
37
urwid.curses_display.curses.A_UNDERLINE |= (
34
38
    urwid.curses_display.curses.A_BLINK)
35
39
 
 
40
def isoformat_to_datetime(iso):
 
41
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
42
    if not iso:
 
43
        return None
 
44
    d, t = iso.split(u"T", 1)
 
45
    year, month, day = d.split(u"-", 2)
 
46
    hour, minute, second = t.split(u":", 2)
 
47
    second, fraction = divmod(float(second), 1)
 
48
    return datetime.datetime(int(year),
 
49
                             int(month),
 
50
                             int(day),
 
51
                             int(hour),
 
52
                             int(minute),
 
53
                             int(second),           # Whole seconds
 
54
                             int(fraction*1000000)) # Microseconds
 
55
 
36
56
class MandosClientPropertyCache(object):
37
57
    """This wraps a Mandos Client D-Bus proxy object, caches the
38
58
    properties and calls a hook function when any of them are
39
59
    changed.
40
60
    """
41
 
    def __init__(self, proxy_object=None, properties=None, *args,
42
 
                 **kwargs):
 
61
    def __init__(self, proxy_object=None, *args, **kwargs):
43
62
        self.proxy = proxy_object # Mandos Client proxy object
44
63
        
45
 
        if properties is None:
46
 
            self.properties = dict()
47
 
        else:
48
 
            self.properties = properties
 
64
        self.properties = dict()
49
65
        self.proxy.connect_to_signal(u"PropertyChanged",
50
66
                                     self.property_changed,
51
67
                                     client_interface,
52
68
                                     byte_arrays=True)
53
69
        
54
 
        if properties is None:
55
 
            self.properties.update(self.proxy.GetAll(client_interface,
56
 
                                                     dbus_interface =
57
 
                                                     dbus.PROPERTIES_IFACE))
 
70
        self.properties.update(
 
71
            self.proxy.GetAll(client_interface,
 
72
                              dbus_interface = dbus.PROPERTIES_IFACE))
58
73
        super(MandosClientPropertyCache, self).__init__(
59
 
            proxy_object=proxy_object,
60
 
            properties=properties, *args, **kwargs)
 
74
            proxy_object=proxy_object, *args, **kwargs)
61
75
    
62
76
    def property_changed(self, property=None, value=None):
63
77
        """This is called whenever we get a PropertyChanged signal
82
96
        # Logger
83
97
        self.logger = logger
84
98
        
 
99
        self._update_timer_callback_tag = None
 
100
        self.last_checker_failed = False
 
101
        
85
102
        # The widget shown normally
86
103
        self._text_widget = urwid.Text(u"")
87
104
        # The widget shown when we have focus
107
124
                                     self.rejected,
108
125
                                     client_interface,
109
126
                                     byte_arrays=True)
 
127
        last_checked_ok = isoformat_to_datetime(self.properties
 
128
                                                ["last_checked_ok"])
 
129
        if last_checked_ok is None:
 
130
            self.last_checker_failed = True
 
131
        else:
 
132
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
133
                                         - last_checked_ok)
 
134
                                        > datetime.timedelta
 
135
                                        (milliseconds=
 
136
                                         self.properties["interval"]))
 
137
        if self.last_checker_failed:
 
138
            self._update_timer_callback_tag = (gobject.timeout_add
 
139
                                               (1000,
 
140
                                                self.update_timer))
110
141
    
111
142
    def checker_completed(self, exitstatus, condition, command):
112
143
        if exitstatus == 0:
 
144
            if self.last_checker_failed:
 
145
                self.last_checker_failed = False
 
146
                gobject.source_remove(self._update_timer_callback_tag)
 
147
                self._update_timer_callback_tag = None
113
148
            self.logger(u'Checker for client %s (command "%s")'
114
149
                        u' was successful'
115
150
                        % (self.properties[u"name"], command))
 
151
            self.update()
116
152
            return
 
153
        # Checker failed
 
154
        if not self.last_checker_failed:
 
155
            self.last_checker_failed = True
 
156
            self._update_timer_callback_tag = (gobject.timeout_add
 
157
                                               (1000,
 
158
                                                self.update_timer))
117
159
        if os.WIFEXITED(condition):
118
160
            self.logger(u'Checker for client %s (command "%s")'
119
161
                        u' failed with exit code %s'
120
162
                        % (self.properties[u"name"], command,
121
163
                           os.WEXITSTATUS(condition)))
122
 
            return
123
 
        if os.WIFSIGNALED(condition):
 
164
        elif os.WIFSIGNALED(condition):
124
165
            self.logger(u'Checker for client %s (command "%s")'
125
166
                        u' was killed by signal %s'
126
167
                        % (self.properties[u"name"], command,
127
168
                           os.WTERMSIG(condition)))
128
 
            return
129
 
        if os.WCOREDUMP(condition):
 
169
        elif os.WCOREDUMP(condition):
130
170
            self.logger(u'Checker for client %s (command "%s")'
131
171
                        u' dumped core'
132
172
                        % (self.properties[u"name"], command))
133
 
        self.logger(u'Checker for client %s completed mysteriously')
 
173
        else:
 
174
            self.logger(u'Checker for client %s completed mysteriously')
 
175
        self.update()
134
176
    
135
177
    def checker_started(self, command):
136
178
        self.logger(u'Client %s started checker "%s"'
172
214
                          }
173
215
        
174
216
        # Rebuild focus and non-focus widgets using current properties
175
 
        self._text = (u'%(name)s: %(enabled)s'
 
217
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
176
218
                      % { u"name": self.properties[u"name"],
177
219
                          u"enabled":
178
220
                              (u"enabled"
179
221
                               if self.properties[u"enabled"]
180
 
                               else u"DISABLED")})
 
222
                               else u"DISABLED"),
 
223
                          u"timer": (unicode(datetime.timedelta
 
224
                                             (milliseconds =
 
225
                                              self.properties
 
226
                                              [u"timeout"])
 
227
                                             - (datetime.datetime
 
228
                                                .utcnow()
 
229
                                                - isoformat_to_datetime
 
230
                                                (max((self.properties
 
231
                                                 ["last_checked_ok"]
 
232
                                                 or
 
233
                                                 self.properties
 
234
                                                 ["created"]),
 
235
                                                    self.properties[u"last_enabled"]))))
 
236
                                     if (self.last_checker_failed
 
237
                                         and self.properties
 
238
                                         [u"enabled"])
 
239
                                     else u"")})
181
240
        if not urwid.supports_unicode():
182
241
            self._text = self._text.encode("ascii", "replace")
183
242
        textlist = [(u"normal", self._text)]
194
253
        if self.update_hook is not None:
195
254
            self.update_hook()
196
255
    
 
256
    def update_timer(self):
 
257
        "called by gobject"
 
258
        self.update()
 
259
        return True             # Keep calling this
 
260
    
197
261
    def delete(self):
 
262
        if self._update_timer_callback_tag is not None:
 
263
            gobject.source_remove(self._update_timer_callback_tag)
 
264
            self._update_timer_callback_tag = None
198
265
        if self.delete_hook is not None:
199
266
            self.delete_hook(self)
200
267
    
424
491
            return
425
492
        self.remove_client(client, path)
426
493
    
427
 
    def add_new_client(self, path, properties):
 
494
    def add_new_client(self, path):
428
495
        client_proxy_object = self.bus.get_object(self.busname, path)
429
496
        self.add_client(MandosClientWidget(server_proxy_object
430
497
                                           =self.mandos_serv,
431
498
                                           proxy_object
432
499
                                           =client_proxy_object,
433
 
                                           properties=properties,
434
500
                                           update_hook
435
501
                                           =self.refresh,
436
502
                                           delete_hook
437
 
                                           =self.remove_client),
 
503
                                           =self.remove_client,
 
504
                                           logger
 
505
                                           =self.log_message),
438
506
                        path=path)
439
507
    
440
508
    def add_client(self, client, path=None):
562
630
ui = UserInterface()
563
631
try:
564
632
    ui.run()
565
 
except:
 
633
except Exception, e:
 
634
    ui.log_message(unicode(e))
566
635
    ui.screen.stop()
567
636
    raise