/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: 2019-11-03 18:44:41 UTC
  • Revision ID: teddy@recompile.se-20191103184441-1vhjuf06hjqgfohh
mandos-monitor: Use Python's standard loggging module

* mandos-monitor: Use Python's standard loggging module, also for
                  warnings.  Suppress BytesWarning from urwid when
                  exiting.
  (log): New global logger object.  This replaces UserInterface
        log_message().
  (MandosClientWidget.__init__): Remove "logger" argument.
  (MandosClientWidget.using_timer): Wrap self.update_timer using new
                                    glib_safely() function.
  (glib_safely): New function to log any exceptions instead of letting
                 exceptions propagate up to GLib.
  (UserInterface.__init__): Remove "log_level" argument.  Set new
                            "loghandler" attribute, instance of new
                            "UILogHandler".
  (UserInterface.log_message): Removed.
  (UserInterface.log_message_raw): Renamed to "add_log_line"; all
                                   callers changed.  Also fix
                                   off-by-one error in max_log_length
                                   logic.
  (UserInterface.run): Add self.loghandler to logger "log". Wrap
                       self.process_input using new glib_safely()
                       function.
  (UserInterface.stop): Remove self.loghandler from logger "log".
  (UserInterface.process_input): Make verbosity toggle affect log
                                 level of logger "log".
  (UILogHandler): New.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python3 -bbI
 
1
#!/usr/bin/python3 -bb
2
2
# -*- mode: python; coding: utf-8 -*-
3
3
#
4
4
# Mandos Monitor - Control and monitor the Mandos server
23
23
#
24
24
# Contact the authors at <mandos@recompile.se>.
25
25
#
 
26
 
26
27
from __future__ import (division, absolute_import, print_function,
27
28
                        unicode_literals)
28
 
 
29
29
try:
30
30
    from future_builtins import *
31
31
except ImportError:
32
32
    pass
33
33
 
34
34
import sys
35
 
import logging
36
35
import os
37
36
import warnings
38
37
import datetime
39
 
import locale
40
38
 
41
39
import urwid.curses_display
42
40
import urwid
46
44
 
47
45
import dbus
48
46
 
 
47
import locale
 
48
 
 
49
import logging
 
50
 
49
51
if sys.version_info.major == 2:
50
 
    __metaclass__ = type
51
52
    str = unicode
52
 
    input = raw_input
53
 
 
54
 
# Show warnings by default
55
 
if not sys.warnoptions:
56
 
    warnings.simplefilter("default")
57
53
 
58
54
log = logging.getLogger(os.path.basename(sys.argv[0]))
59
55
logging.basicConfig(level="NOTSET", # Show all messages
61
57
 
62
58
logging.captureWarnings(True)   # Show warnings via the logging system
63
59
 
64
 
locale.setlocale(locale.LC_ALL, "")
 
60
locale.setlocale(locale.LC_ALL, '')
65
61
 
66
 
logging.getLogger("dbus.proxies").setLevel(logging.CRITICAL)
 
62
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
67
63
 
68
64
# Some useful constants
69
 
domain = "se.recompile"
70
 
server_interface = domain + ".Mandos"
71
 
client_interface = domain + ".Mandos.Client"
72
 
version = "1.8.14"
 
65
domain = 'se.recompile'
 
66
server_interface = domain + '.Mandos'
 
67
client_interface = domain + '.Mandos.Client'
 
68
version = "1.8.9"
73
69
 
74
70
try:
75
71
    dbus.OBJECT_MANAGER_IFACE
94
90
                             int(fraction*1000000))  # Microseconds
95
91
 
96
92
 
97
 
class MandosClientPropertyCache:
 
93
class MandosClientPropertyCache(object):
98
94
    """This wraps a Mandos Client D-Bus proxy object, caches the
99
95
    properties and calls a hook function when any of them are
100
96
    changed.
171
167
                                         self.rejected,
172
168
                                         client_interface,
173
169
                                         byte_arrays=True))
174
 
        log.debug("Created client %s", self.properties["Name"])
 
170
        log.debug('Created client %s', self.properties["Name"])
175
171
 
176
172
    def using_timer(self, flag):
177
173
        """Call this method with True or False when timer should be
189
185
    def checker_completed(self, exitstatus, condition, command):
190
186
        if exitstatus == 0:
191
187
            log.debug('Checker for client %s (command "%s")'
192
 
                      " succeeded", self.properties["Name"], command)
 
188
                      ' succeeded', self.properties["Name"], command)
193
189
            self.update()
194
190
            return
195
191
        # Checker failed
196
192
        if os.WIFEXITED(condition):
197
193
            log.info('Checker for client %s (command "%s") failed'
198
 
                     " with exit code %d", self.properties["Name"],
 
194
                     ' with exit code %d', self.properties["Name"],
199
195
                     command, os.WEXITSTATUS(condition))
200
196
        elif os.WIFSIGNALED(condition):
201
197
            log.info('Checker for client %s (command "%s") was'
202
 
                     " killed by signal %d", self.properties["Name"],
 
198
                     ' killed by signal %d', self.properties["Name"],
203
199
                     command, os.WTERMSIG(condition))
204
200
        self.update()
205
201
 
253
249
        # Rebuild focus and non-focus widgets using current properties
254
250
 
255
251
        # Base part of a client. Name!
256
 
        base = "{name}: ".format(name=self.properties["Name"])
 
252
        base = '{name}: '.format(name=self.properties["Name"])
257
253
        if not self.properties["Enabled"]:
258
254
            message = "DISABLED"
259
255
            self.using_timer(False)
281
277
                timer = datetime.timedelta(0)
282
278
            else:
283
279
                expires = (datetime.datetime.strptime
284
 
                           (expires, "%Y-%m-%dT%H:%M:%S.%f"))
 
280
                           (expires, '%Y-%m-%dT%H:%M:%S.%f'))
285
281
                timer = max(expires - datetime.datetime.utcnow(),
286
282
                            datetime.timedelta())
287
 
            message = ("A checker has failed! Time until client"
288
 
                       " gets disabled: {}"
 
283
            message = ('A checker has failed! Time until client'
 
284
                       ' gets disabled: {}'
289
285
                       .format(str(timer).rsplit(".", 1)[0]))
290
286
            self.using_timer(True)
291
287
        else:
412
408
        return ret
413
409
 
414
410
 
415
 
class UserInterface:
 
411
class UserInterface(object):
416
412
    """This is the entire user interface - the whole screen
417
413
    with boxes, lists of client widgets, etc.
418
414
    """
475
471
                           "Mandos Monitor version " + version))
476
472
        self.add_log_line(("bold", "q: Quit  ?: Help"))
477
473
 
478
 
        self.busname = domain + ".Mandos"
 
474
        self.busname = domain + '.Mandos'
479
475
        self.main_loop = GLib.MainLoop()
480
476
 
481
477
    def client_not_found(self, key_id, address):