/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
35
35
import os
36
36
import warnings
37
37
import datetime
38
 
import locale
39
 
import logging
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
53
 
53
54
log = logging.getLogger(os.path.basename(sys.argv[0]))
56
57
 
57
58
logging.captureWarnings(True)   # Show warnings via the logging system
58
59
 
59
 
locale.setlocale(locale.LC_ALL, "")
 
60
locale.setlocale(locale.LC_ALL, '')
60
61
 
61
 
logging.getLogger("dbus.proxies").setLevel(logging.CRITICAL)
 
62
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
62
63
 
63
64
# Some useful constants
64
 
domain = "se.recompile"
65
 
server_interface = domain + ".Mandos"
66
 
client_interface = domain + ".Mandos.Client"
 
65
domain = 'se.recompile'
 
66
server_interface = domain + '.Mandos'
 
67
client_interface = domain + '.Mandos.Client'
67
68
version = "1.8.9"
68
69
 
69
70
try:
89
90
                             int(fraction*1000000))  # Microseconds
90
91
 
91
92
 
92
 
class MandosClientPropertyCache:
 
93
class MandosClientPropertyCache(object):
93
94
    """This wraps a Mandos Client D-Bus proxy object, caches the
94
95
    properties and calls a hook function when any of them are
95
96
    changed.
166
167
                                         self.rejected,
167
168
                                         client_interface,
168
169
                                         byte_arrays=True))
169
 
        log.debug("Created client %s", self.properties["Name"])
 
170
        log.debug('Created client %s', self.properties["Name"])
170
171
 
171
172
    def using_timer(self, flag):
172
173
        """Call this method with True or False when timer should be
184
185
    def checker_completed(self, exitstatus, condition, command):
185
186
        if exitstatus == 0:
186
187
            log.debug('Checker for client %s (command "%s")'
187
 
                      " succeeded", self.properties["Name"], command)
 
188
                      ' succeeded', self.properties["Name"], command)
188
189
            self.update()
189
190
            return
190
191
        # Checker failed
191
192
        if os.WIFEXITED(condition):
192
193
            log.info('Checker for client %s (command "%s") failed'
193
 
                     " with exit code %d", self.properties["Name"],
 
194
                     ' with exit code %d', self.properties["Name"],
194
195
                     command, os.WEXITSTATUS(condition))
195
196
        elif os.WIFSIGNALED(condition):
196
197
            log.info('Checker for client %s (command "%s") was'
197
 
                     " killed by signal %d", self.properties["Name"],
 
198
                     ' killed by signal %d', self.properties["Name"],
198
199
                     command, os.WTERMSIG(condition))
199
200
        self.update()
200
201
 
248
249
        # Rebuild focus and non-focus widgets using current properties
249
250
 
250
251
        # Base part of a client. Name!
251
 
        base = "{name}: ".format(name=self.properties["Name"])
 
252
        base = '{name}: '.format(name=self.properties["Name"])
252
253
        if not self.properties["Enabled"]:
253
254
            message = "DISABLED"
254
255
            self.using_timer(False)
276
277
                timer = datetime.timedelta(0)
277
278
            else:
278
279
                expires = (datetime.datetime.strptime
279
 
                           (expires, "%Y-%m-%dT%H:%M:%S.%f"))
 
280
                           (expires, '%Y-%m-%dT%H:%M:%S.%f'))
280
281
                timer = max(expires - datetime.datetime.utcnow(),
281
282
                            datetime.timedelta())
282
 
            message = ("A checker has failed! Time until client"
283
 
                       " gets disabled: {}"
 
283
            message = ('A checker has failed! Time until client'
 
284
                       ' gets disabled: {}'
284
285
                       .format(str(timer).rsplit(".", 1)[0]))
285
286
            self.using_timer(True)
286
287
        else:
407
408
        return ret
408
409
 
409
410
 
410
 
class UserInterface:
 
411
class UserInterface(object):
411
412
    """This is the entire user interface - the whole screen
412
413
    with boxes, lists of client widgets, etc.
413
414
    """
470
471
                           "Mandos Monitor version " + version))
471
472
        self.add_log_line(("bold", "q: Quit  ?: Help"))
472
473
 
473
 
        self.busname = domain + ".Mandos"
 
474
        self.busname = domain + '.Mandos'
474
475
        self.main_loop = GLib.MainLoop()
475
476
 
476
477
    def client_not_found(self, key_id, address):