/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
52
    str = unicode
51
53
 
55
57
 
56
58
logging.captureWarnings(True)   # Show warnings via the logging system
57
59
 
58
 
locale.setlocale(locale.LC_ALL, "")
 
60
locale.setlocale(locale.LC_ALL, '')
59
61
 
60
 
logging.getLogger("dbus.proxies").setLevel(logging.CRITICAL)
 
62
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
61
63
 
62
64
# Some useful constants
63
 
domain = "se.recompile"
64
 
server_interface = domain + ".Mandos"
65
 
client_interface = domain + ".Mandos.Client"
 
65
domain = 'se.recompile'
 
66
server_interface = domain + '.Mandos'
 
67
client_interface = domain + '.Mandos.Client'
66
68
version = "1.8.9"
67
69
 
68
70
try:
165
167
                                         self.rejected,
166
168
                                         client_interface,
167
169
                                         byte_arrays=True))
168
 
        log.debug("Created client %s", self.properties["Name"])
 
170
        log.debug('Created client %s', self.properties["Name"])
169
171
 
170
172
    def using_timer(self, flag):
171
173
        """Call this method with True or False when timer should be
183
185
    def checker_completed(self, exitstatus, condition, command):
184
186
        if exitstatus == 0:
185
187
            log.debug('Checker for client %s (command "%s")'
186
 
                      " succeeded", self.properties["Name"], command)
 
188
                      ' succeeded', self.properties["Name"], command)
187
189
            self.update()
188
190
            return
189
191
        # Checker failed
190
192
        if os.WIFEXITED(condition):
191
193
            log.info('Checker for client %s (command "%s") failed'
192
 
                     " with exit code %d", self.properties["Name"],
 
194
                     ' with exit code %d', self.properties["Name"],
193
195
                     command, os.WEXITSTATUS(condition))
194
196
        elif os.WIFSIGNALED(condition):
195
197
            log.info('Checker for client %s (command "%s") was'
196
 
                     " killed by signal %d", self.properties["Name"],
 
198
                     ' killed by signal %d', self.properties["Name"],
197
199
                     command, os.WTERMSIG(condition))
198
200
        self.update()
199
201
 
247
249
        # Rebuild focus and non-focus widgets using current properties
248
250
 
249
251
        # Base part of a client. Name!
250
 
        base = "{name}: ".format(name=self.properties["Name"])
 
252
        base = '{name}: '.format(name=self.properties["Name"])
251
253
        if not self.properties["Enabled"]:
252
254
            message = "DISABLED"
253
255
            self.using_timer(False)
275
277
                timer = datetime.timedelta(0)
276
278
            else:
277
279
                expires = (datetime.datetime.strptime
278
 
                           (expires, "%Y-%m-%dT%H:%M:%S.%f"))
 
280
                           (expires, '%Y-%m-%dT%H:%M:%S.%f'))
279
281
                timer = max(expires - datetime.datetime.utcnow(),
280
282
                            datetime.timedelta())
281
 
            message = ("A checker has failed! Time until client"
282
 
                       " gets disabled: {}"
 
283
            message = ('A checker has failed! Time until client'
 
284
                       ' gets disabled: {}'
283
285
                       .format(str(timer).rsplit(".", 1)[0]))
284
286
            self.using_timer(True)
285
287
        else:
469
471
                           "Mandos Monitor version " + version))
470
472
        self.add_log_line(("bold", "q: Quit  ?: Help"))
471
473
 
472
 
        self.busname = domain + ".Mandos"
 
474
        self.busname = domain + '.Mandos'
473
475
        self.main_loop = GLib.MainLoop()
474
476
 
475
477
    def client_not_found(self, key_id, address):