/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: 2012-05-03 19:06:02 UTC
  • Revision ID: teddy@recompile.se-20120503190602-uqghef5rbpqdvybx
* mandos-monitor: Use new string format method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
52
52
domain = 'se.recompile'
53
53
server_interface = domain + '.Mandos'
54
54
client_interface = domain + '.Mandos.Client'
55
 
version = "1.4.1"
 
55
version = "1.5.3"
56
56
 
57
57
# Always run in monochrome mode
58
58
urwid.curses_display.curses.has_colors = lambda : False
131
131
        
132
132
        self._update_timer_callback_tag = None
133
133
        self._update_timer_callback_lock = 0
134
 
        self.last_checker_failed = False
135
134
        
136
135
        # The widget shown normally
137
136
        self._text_widget = urwid.Text("")
145
144
        
146
145
        last_checked_ok = isoformat_to_datetime(self.properties
147
146
                                                ["LastCheckedOK"])
148
 
        if last_checked_ok is None:
149
 
            self.last_checker_failed = True
150
 
        else:
151
 
            self.last_checker_failed = ((datetime.datetime.utcnow()
152
 
                                         - last_checked_ok)
153
 
                                        > datetime.timedelta
154
 
                                        (milliseconds=
155
 
                                         self.properties
156
 
                                         ["Interval"]))
157
147
        
158
 
        if self.last_checker_failed:
 
148
        if self.properties ["LastCheckerStatus"] != 0:
159
149
            self.using_timer(True)
160
150
        
161
151
        if self.need_approval:
182
172
                                         self.rejected,
183
173
                                         client_interface,
184
174
                                         byte_arrays=True))
185
 
        #self.logger('Created client %s' % (self.properties["Name"]))
 
175
        #self.logger('Created client {0}'
 
176
        #            .format(self.properties["Name"]))
186
177
    
187
178
    def property_changed(self, property=None, value=None):
188
179
        super(self, MandosClientWidget).property_changed(property,
189
180
                                                         value)
190
181
        if property == "ApprovalPending":
191
182
            using_timer(bool(value))
192
 
        
 
183
        if property == "LastCheckerStatus":
 
184
            using_timer(value != 0)
 
185
            #self.logger('Checker for client {0} (command "{1}") was '
 
186
            #            ' successful'.format(self.properties["Name"],
 
187
            #                                 command))
 
188
    
193
189
    def using_timer(self, flag):
194
190
        """Call this method with True or False when timer should be
195
191
        activated or deactivated.
210
206
    
211
207
    def checker_completed(self, exitstatus, condition, command):
212
208
        if exitstatus == 0:
213
 
            if self.last_checker_failed:
214
 
                self.last_checker_failed = False
215
 
                self.using_timer(False)
216
 
            #self.logger('Checker for client %s (command "%s")'
217
 
            #            ' was successful'
218
 
            #            % (self.properties["Name"], command))
219
209
            self.update()
220
210
            return
221
211
        # Checker failed
222
 
        if not self.last_checker_failed:
223
 
            self.last_checker_failed = True
224
 
            self.using_timer(True)
225
212
        if os.WIFEXITED(condition):
226
 
            self.logger('Checker for client %s (command "%s")'
227
 
                        ' failed with exit code %s'
228
 
                        % (self.properties["Name"], command,
229
 
                           os.WEXITSTATUS(condition)))
 
213
            self.logger('Checker for client {0} (command "{1}")'
 
214
                        ' failed with exit code {2}'
 
215
                        .format(self.properties["Name"], command,
 
216
                                os.WEXITSTATUS(condition)))
230
217
        elif os.WIFSIGNALED(condition):
231
 
            self.logger('Checker for client %s (command "%s")'
232
 
                        ' was killed by signal %s'
233
 
                        % (self.properties["Name"], command,
234
 
                           os.WTERMSIG(condition)))
 
218
            self.logger('Checker for client {0} (command "{1}") was'
 
219
                        ' killed by signal {2}'
 
220
                        .format(self.properties["Name"], command,
 
221
                                os.WTERMSIG(condition)))
235
222
        elif os.WCOREDUMP(condition):
236
 
            self.logger('Checker for client %s (command "%s")'
 
223
            self.logger('Checker for client {0} (command "{1}")'
237
224
                        ' dumped core'
238
 
                        % (self.properties["Name"], command))
 
225
                        .format(self.properties["Name"], command))
239
226
        else:
240
 
            self.logger('Checker for client %s completed'
241
 
                        ' mysteriously')
 
227
            self.logger('Checker for client {0} completed'
 
228
                        ' mysteriously'
 
229
                        .format(self.properties["Name"]))
242
230
        self.update()
243
231
    
244
232
    def checker_started(self, command):
245
233
        """Server signals that a checker started. This could be useful
246
234
           to log in the future. """
247
 
        #self.logger('Client %s started checker "%s"'
248
 
        #            % (self.properties["Name"], unicode(command)))
 
235
        #self.logger('Client {0} started checker "{1}"'
 
236
        #            .format(self.properties["Name"],
 
237
        #                    unicode(command)))
249
238
        pass
250
239
    
251
240
    def got_secret(self):
252
 
        self.last_checker_failed = False
253
 
        self.logger('Client %s received its secret'
254
 
                    % self.properties["Name"])
 
241
        self.logger('Client {0} received its secret'
 
242
                    .format(self.properties["Name"]))
255
243
    
256
244
    def need_approval(self, timeout, default):
257
245
        if not default:
258
 
            message = 'Client %s needs approval within %s seconds'
 
246
            message = 'Client {0} needs approval within {1} seconds'
259
247
        else:
260
 
            message = 'Client %s will get its secret in %s seconds'
261
 
        self.logger(message
262
 
                    % (self.properties["Name"], timeout/1000))
 
248
            message = 'Client {0} will get its secret in {1} seconds'
 
249
        self.logger(message.format(self.properties["Name"],
 
250
                                   timeout/1000))
263
251
        self.using_timer(True)
264
252
    
265
253
    def rejected(self, reason):
266
 
        self.logger('Client %s was rejected; reason: %s'
267
 
                    % (self.properties["Name"], reason))
 
254
        self.logger('Client {0} was rejected; reason: {1}'
 
255
                    .format(self.properties["Name"], reason))
268
256
    
269
257
    def selectable(self):
270
258
        """Make this a "selectable" widget.
296
284
        # Rebuild focus and non-focus widgets using current properties
297
285
 
298
286
        # Base part of a client. Name!
299
 
        base = ('%(name)s: '
300
 
                      % {"name": self.properties["Name"]})
 
287
        base = '{name}: '.format(name=self.properties["Name"])
301
288
        if not self.properties["Enabled"]:
302
289
            message = "DISABLED"
303
290
        elif self.properties["ApprovalPending"]:
312
299
            else:
313
300
                timer = datetime.timedelta()
314
301
            if self.properties["ApprovedByDefault"]:
315
 
                message = "Approval in %s. (d)eny?"
 
302
                message = "Approval in {0}. (d)eny?"
316
303
            else:
317
 
                message = "Denial in %s. (a)pprove?"
318
 
            message = message % unicode(timer).rsplit(".", 1)[0]
319
 
        elif self.last_checker_failed:
 
304
                message = "Denial in {0}. (a)pprove?"
 
305
            message = message.format(unicode(timer).rsplit(".", 1)[0])
 
306
        elif self.properties["LastCheckerStatus"] != 0:
320
307
            # When checker has failed, print a timer until client expires
321
308
            expires = self.properties["Expires"]
322
309
            if expires == "":
326
313
                                                     '%Y-%m-%dT%H:%M:%S.%f')
327
314
                timer = expires - datetime.datetime.utcnow()
328
315
            message = ('A checker has failed! Time until client'
329
 
                       ' gets disabled: %s'
330
 
                           % unicode(timer).rsplit(".", 1)[0])
 
316
                       ' gets disabled: {0}'
 
317
                       .format(unicode(timer).rsplit(".", 1)[0]))
331
318
        else:
332
319
            message = "enabled"
333
 
        self._text = "%s%s" % (base, message)
 
320
        self._text = "{0}{1}".format(base, message)
334
321
            
335
322
        if not urwid.supports_unicode():
336
323
            self._text = self._text.encode("ascii", "replace")
501
488
        self.main_loop = gobject.MainLoop()
502
489
    
503
490
    def client_not_found(self, fingerprint, address):
504
 
        self.log_message(("Client with address %s and fingerprint %s"
505
 
                          " could not be found" % (address,
506
 
                                                    fingerprint)))
 
491
        self.log_message("Client with address {0} and fingerprint"
 
492
                         " {1} could not be found"
 
493
                         .format(address, fingerprint))
507
494
    
508
495
    def rebuild(self):
509
496
        """This rebuilds the User Interface.
562
549
            client = self.clients_dict[path]
563
550
        except KeyError:
564
551
            # not found?
565
 
            self.log_message("Unknown client %r (%r) removed", name,
566
 
                             path)
 
552
            self.log_message("Unknown client {0!r} ({1!r}) removed"
 
553
                             .format(name, path))
567
554
            return
568
555
        client.delete()
569
556