/mandos/trunk

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

« back to all changes in this revision

Viewing changes to mandos-monitor

  • Committer: Teddy Hogeborn
  • Date: 2009-11-15 10:12:09 UTC
  • Revision ID: teddy@fukt.bsnet.se-20091115101209-d0zrfwkp6u0l7v7b
* mandos (MandosServer.handle_ipc): Better log message.
  (main/MandosDBusService.ClientNotFound): Add "address" argument.
                                           All callers changed.
* mandos-monitor (MandosClientWidget.__init__): Add "logger" argument.
  (MandosClientWidget.checker_completed,
  MandosClientWidget.checker_started, MandosClientWidget.got_secret,
  MandosClientWidget.rejected): New methods, connected to signals.
  (MandosClientWidget.update): Improve display.
  (UserInterface.client_not_found): New method, conneced to signal.
  (UserInterface.log_message): New; log with timestamp.
  (UserInterface.log_message_raw): Same as old "log_message".  Bug
                                  fix; always do "refresh()".

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
from __future__ import division, absolute_import, with_statement
5
5
 
6
6
import sys
 
7
import os
7
8
import signal
8
9
 
 
10
import datetime
 
11
 
9
12
import urwid.curses_display
10
13
import urwid
11
14
 
43
46
            self.properties = dict()
44
47
        else:
45
48
            self.properties = properties
46
 
        self.proxy.connect_to_signal("PropertyChanged",
 
49
        self.proxy.connect_to_signal(u"PropertyChanged",
47
50
                                     self.property_changed,
48
51
                                     client_interface,
49
52
                                     byte_arrays=True)
69
72
    """
70
73
    
71
74
    def __init__(self, server_proxy_object=None, update_hook=None,
72
 
                 delete_hook=None, *args, **kwargs):
 
75
                 delete_hook=None, logger=None, *args, **kwargs):
73
76
        # Called on update
74
77
        self.update_hook = update_hook
75
78
        # Called on delete
76
79
        self.delete_hook = delete_hook
77
80
        # Mandos Server proxy object
78
81
        self.server_proxy_object = server_proxy_object
 
82
        # Logger
 
83
        self.logger = logger
79
84
        
80
85
        # The widget shown normally
81
 
        self._text_widget = urwid.Text("")
 
86
        self._text_widget = urwid.Text(u"")
82
87
        # The widget shown when we have focus
83
 
        self._focus_text_widget = urwid.Text("")
 
88
        self._focus_text_widget = urwid.Text(u"")
84
89
        super(MandosClientWidget, self).__init__(
85
90
            update_hook=update_hook, delete_hook=delete_hook,
86
91
            *args, **kwargs)
87
92
        self.update()
88
93
        self.opened = False
 
94
        self.proxy.connect_to_signal(u"CheckerCompleted",
 
95
                                     self.checker_completed,
 
96
                                     client_interface,
 
97
                                     byte_arrays=True)
 
98
        self.proxy.connect_to_signal(u"CheckerStarted",
 
99
                                     self.checker_started,
 
100
                                     client_interface,
 
101
                                     byte_arrays=True)
 
102
        self.proxy.connect_to_signal(u"GotSecret",
 
103
                                     self.got_secret,
 
104
                                     client_interface,
 
105
                                     byte_arrays=True)
 
106
        self.proxy.connect_to_signal(u"Rejected",
 
107
                                     self.rejected,
 
108
                                     client_interface,
 
109
                                     byte_arrays=True)
 
110
    
 
111
    def checker_completed(self, exitstatus, condition, command):
 
112
        if exitstatus == 0:
 
113
            self.logger(u'Checker for client %s (command "%s")'
 
114
                        u' was successful'
 
115
                        % (self.properties[u"name"], command))
 
116
            return
 
117
        if os.WIFEXITED(condition):
 
118
            self.logger(u'Checker for client %s (command "%s")'
 
119
                        u' failed with exit code %s'
 
120
                        % (self.properties[u"name"], command,
 
121
                           os.WEXITSTATUS(condition)))
 
122
            return
 
123
        if os.WIFSIGNALED(condition):
 
124
            self.logger(u'Checker for client %s (command "%s")'
 
125
                        u' was killed by signal %s'
 
126
                        % (self.properties[u"name"], command,
 
127
                           os.WTERMSIG(condition)))
 
128
            return
 
129
        if os.WCOREDUMP(condition):
 
130
            self.logger(u'Checker for client %s (command "%s")'
 
131
                        u' dumped core'
 
132
                        % (self.properties[u"name"], command))
 
133
        self.logger(u'Checker for client %s completed mysteriously')
 
134
    
 
135
    def checker_started(self, command):
 
136
        self.logger(u'Client %s started checker "%s"'
 
137
                    % (self.properties[u"name"], unicode(command)))
 
138
    
 
139
    def got_secret(self):
 
140
        self.logger(u'Client %s received its secret'
 
141
                    % self.properties[u"name"])
 
142
    
 
143
    def rejected(self):
 
144
        self.logger(u'Client %s was rejected'
 
145
                    % self.properties[u"name"])
89
146
    
90
147
    def selectable(self):
91
148
        """Make this a "selectable" widget.
115
172
                          }
116
173
        
117
174
        # Rebuild focus and non-focus widgets using current properties
118
 
        self._text = (u'name="%(name)s", enabled=%(enabled)s'
119
 
                      % self.properties)
 
175
        self._text = (u'%(name)s: %(enabled)s'
 
176
                      % { u"name": self.properties[u"name"],
 
177
                          u"enabled":
 
178
                              (u"enabled"
 
179
                               if self.properties[u"enabled"]
 
180
                               else u"DISABLED")})
120
181
        if not urwid.supports_unicode():
121
182
            self._text = self._text.encode("ascii", "replace")
122
 
        textlist = [(u"normal", u"BLARGH: "), (u"bold", self._text)]
 
183
        textlist = [(u"normal", self._text)]
123
184
        self._text_widget.set_text(textlist)
124
185
        self._focus_text_widget.set_text([(with_standout[text[0]],
125
186
                                           text[1])
250
311
        self.log_wrap = u"any"
251
312
        
252
313
        self.rebuild()
253
 
        self.log_message((u"bold",
254
 
                          u"Mandos Monitor version " + version))
255
 
        self.log_message((u"bold",
256
 
                          u"q: Quit  ?: Help"))
 
314
        self.log_message_raw((u"bold",
 
315
                              u"Mandos Monitor version " + version))
 
316
        self.log_message_raw((u"bold",
 
317
                              u"q: Quit  ?: Help"))
257
318
        
258
319
        self.busname = domain + '.Mandos'
259
320
        self.main_loop = gobject.MainLoop()
270
331
            mandos_clients = dbus.Dictionary()
271
332
        
272
333
        (self.mandos_serv
273
 
         .connect_to_signal("ClientRemoved",
 
334
         .connect_to_signal(u"ClientRemoved",
274
335
                            self.find_and_remove_client,
275
336
                            dbus_interface=server_interface,
276
337
                            byte_arrays=True))
277
338
        (self.mandos_serv
278
 
         .connect_to_signal("ClientAdded",
 
339
         .connect_to_signal(u"ClientAdded",
279
340
                            self.add_new_client,
280
341
                            dbus_interface=server_interface,
281
342
                            byte_arrays=True))
 
343
        (self.mandos_serv
 
344
         .connect_to_signal(u"ClientNotFound",
 
345
                            self.client_not_found,
 
346
                            dbus_interface=server_interface,
 
347
                            byte_arrays=True))
282
348
        for path, client in mandos_clients.iteritems():
283
349
            client_proxy_object = self.bus.get_object(self.busname,
284
350
                                                      path)
290
356
                                               update_hook
291
357
                                               =self.refresh,
292
358
                                               delete_hook
293
 
                                               =self.remove_client),
 
359
                                               =self.remove_client,
 
360
                                               logger
 
361
                                               =self.log_message),
294
362
                            path=path)
295
363
    
 
364
    def client_not_found(self, fingerprint, address):
 
365
        self.log_message((u"Client with address %s and fingerprint %s"
 
366
                          u" could not be found" % (address,
 
367
                                                    fingerprint)))
 
368
    
296
369
    def rebuild(self):
297
370
        """This rebuilds the User Interface.
298
371
        Call this when the widget layout needs to change"""
307
380
            pass
308
381
        self.topwidget = urwid.Pile(self.uilist)
309
382
    
310
 
    def log_message(self, markup):
 
383
    def log_message(self, message):
 
384
        timestamp = datetime.datetime.now().isoformat()
 
385
        self.log_message_raw(timestamp + u": " + message)
 
386
    
 
387
    def log_message_raw(self, markup):
311
388
        """Add a log message to the log buffer."""
312
389
        self.log.append(urwid.Text(markup, wrap=self.log_wrap))
313
390
        if (self.max_log_length
315
392
            del self.log[0:len(self.log)-self.max_log_length-1]
316
393
        self.logbox.set_focus(len(self.logbox.body.contents),
317
394
                              coming_from=u"above")
 
395
        self.refresh()
318
396
    
319
397
    def toggle_log_display(self):
320
398
        """Toggle visibility of the log buffer."""
435
513
                if not self.log_visible:
436
514
                    self.log_visible = True
437
515
                    self.rebuild()
438
 
                self.log_message((u"bold",
439
 
                                  u"  ".join((u"q: Quit",
440
 
                                              u"?: Help",
441
 
                                              u"l: Log window toggle",
442
 
                                              u"TAB: Switch window",
443
 
                                              u"w: Wrap (log)"))))
444
 
                self.log_message((u"bold",
445
 
                                  u"  ".join((u"Clients:",
446
 
                                              u"e: Enable",
447
 
                                              u"d: Disable",
448
 
                                              u"r: Remove",
449
 
                                              u"s: Start new checker",
450
 
                                              u"S: Stop checker",
451
 
                                              u"C: Checker OK"))))
 
516
                self.log_message_raw((u"bold",
 
517
                                      u"  ".
 
518
                                      join((u"q: Quit",
 
519
                                            u"?: Help",
 
520
                                            u"l: Log window toggle",
 
521
                                            u"TAB: Switch window",
 
522
                                            u"w: Wrap (log)"))))
 
523
                self.log_message_raw((u"bold",
 
524
                                      u"  "
 
525
                                      .join((u"Clients:",
 
526
                                             u"e: Enable",
 
527
                                             u"d: Disable",
 
528
                                             u"r: Remove",
 
529
                                             u"s: Start new checker",
 
530
                                             u"S: Stop checker",
 
531
                                             u"C: Checker OK"))))
452
532
                self.refresh()
453
533
            elif key == u"tab":
454
534
                if self.topwidget.get_focus() is self.logbox: