/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 at bsnet
  • Date: 2010-09-10 17:06:26 UTC
  • mto: This revision was merged to the branch mainline in revision 427.
  • Revision ID: teddy@fukt.bsnet.se-20100910170626-exo8e7ptkb9ncg29
* Makefile (install-server): Install dbus-mandos.conf as
                             "/etc/dbus-1/system.d/mandos.conf".
  (purge-server): Remove "/etc/dbus-1/system.d/mandos.conf".

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
 
24
24
locale.setlocale(locale.LC_ALL, u'')
25
25
 
26
 
import logging
27
 
logging.getLogger('dbus.proxies').setLevel(logging.CRITICAL)
28
 
 
29
26
# Some useful constants
30
27
domain = 'se.bsnet.fukt'
31
28
server_interface = domain + '.Mandos'
32
29
client_interface = domain + '.Mandos.Client'
33
 
version = "1.0.15"
 
30
version = "1.0.14"
34
31
 
35
32
# Always run in monochrome mode
36
33
urwid.curses_display.curses.has_colors = lambda : False
40
37
urwid.curses_display.curses.A_UNDERLINE |= (
41
38
    urwid.curses_display.curses.A_BLINK)
42
39
 
 
40
def isoformat_to_datetime(iso):
 
41
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
42
    if not iso:
 
43
        return None
 
44
    d, t = iso.split(u"T", 1)
 
45
    year, month, day = d.split(u"-", 2)
 
46
    hour, minute, second = t.split(u":", 2)
 
47
    second, fraction = divmod(float(second), 1)
 
48
    return datetime.datetime(int(year),
 
49
                             int(month),
 
50
                             int(day),
 
51
                             int(hour),
 
52
                             int(minute),
 
53
                             int(second),           # Whole seconds
 
54
                             int(fraction*1000000)) # Microseconds
 
55
 
43
56
class MandosClientPropertyCache(object):
44
57
    """This wraps a Mandos Client D-Bus proxy object, caches the
45
58
    properties and calls a hook function when any of them are
58
71
            self.proxy.GetAll(client_interface,
59
72
                              dbus_interface = dbus.PROPERTIES_IFACE))
60
73
        super(MandosClientPropertyCache, self).__init__(
61
 
            *args, **kwargs)
 
74
            proxy_object=proxy_object, *args, **kwargs)
62
75
    
63
76
    def property_changed(self, property=None, value=None):
64
77
        """This is called whenever we get a PropertyChanged signal
83
96
        # Logger
84
97
        self.logger = logger
85
98
        
 
99
        self._update_timer_callback_tag = None
 
100
        self.last_checker_failed = False
 
101
        
86
102
        # The widget shown normally
87
103
        self._text_widget = urwid.Text(u"")
88
104
        # The widget shown when we have focus
104
120
                                     self.got_secret,
105
121
                                     client_interface,
106
122
                                     byte_arrays=True)
107
 
        self.proxy.connect_to_signal(u"NeedApproval",
108
 
                                     self.need_approval,
109
 
                                     client_interface,
110
 
                                     byte_arrays=True)
111
123
        self.proxy.connect_to_signal(u"Rejected",
112
124
                                     self.rejected,
113
125
                                     client_interface,
114
126
                                     byte_arrays=True)
 
127
        last_checked_ok = isoformat_to_datetime(self.properties
 
128
                                                ["last_checked_ok"])
 
129
        if last_checked_ok is None:
 
130
            self.last_checker_failed = True
 
131
        else:
 
132
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
133
                                         - last_checked_ok)
 
134
                                        > datetime.timedelta
 
135
                                        (milliseconds=
 
136
                                         self.properties["interval"]))
 
137
        if self.last_checker_failed:
 
138
            self._update_timer_callback_tag = (gobject.timeout_add
 
139
                                               (1000,
 
140
                                                self.update_timer))
115
141
    
116
142
    def checker_completed(self, exitstatus, condition, command):
117
143
        if exitstatus == 0:
118
 
            #self.logger(u'Checker for client %s (command "%s")'
119
 
            #            u' was successful'
120
 
            #            % (self.properties[u"name"], command))
 
144
            if self.last_checker_failed:
 
145
                self.last_checker_failed = False
 
146
                gobject.source_remove(self._update_timer_callback_tag)
 
147
                self._update_timer_callback_tag = None
 
148
            self.logger(u'Checker for client %s (command "%s")'
 
149
                        u' was successful'
 
150
                        % (self.properties[u"name"], command))
 
151
            self.update()
121
152
            return
 
153
        # Checker failed
 
154
        if not self.last_checker_failed:
 
155
            self.last_checker_failed = True
 
156
            self._update_timer_callback_tag = (gobject.timeout_add
 
157
                                               (1000,
 
158
                                                self.update_timer))
122
159
        if os.WIFEXITED(condition):
123
160
            self.logger(u'Checker for client %s (command "%s")'
124
161
                        u' failed with exit code %s'
125
162
                        % (self.properties[u"name"], command,
126
163
                           os.WEXITSTATUS(condition)))
127
 
            return
128
 
        if os.WIFSIGNALED(condition):
 
164
        elif os.WIFSIGNALED(condition):
129
165
            self.logger(u'Checker for client %s (command "%s")'
130
166
                        u' was killed by signal %s'
131
167
                        % (self.properties[u"name"], command,
132
168
                           os.WTERMSIG(condition)))
133
 
            return
134
 
        if os.WCOREDUMP(condition):
 
169
        elif os.WCOREDUMP(condition):
135
170
            self.logger(u'Checker for client %s (command "%s")'
136
171
                        u' dumped core'
137
172
                        % (self.properties[u"name"], command))
138
 
        self.logger(u'Checker for client %s completed mysteriously')
 
173
        else:
 
174
            self.logger(u'Checker for client %s completed mysteriously')
 
175
        self.update()
139
176
    
140
177
    def checker_started(self, command):
141
 
        #self.logger(u'Client %s started checker "%s"'
142
 
        #            % (self.properties[u"name"], unicode(command)))
143
 
        pass
 
178
        self.logger(u'Client %s started checker "%s"'
 
179
                    % (self.properties[u"name"], unicode(command)))
144
180
    
145
181
    def got_secret(self):
146
182
        self.logger(u'Client %s received its secret'
147
183
                    % self.properties[u"name"])
148
184
    
149
 
    def need_approval(self, timeout, default):
150
 
        if not default:
151
 
            message = u'Client %s needs approval within %s seconds'
152
 
        else:
153
 
            message = u'Client %s will get its secret in %s seconds'
154
 
        self.logger(message
155
 
                    % (self.properties[u"name"], timeout/1000))
156
 
    
157
 
    def rejected(self, reason):
158
 
        self.logger(u'Client %s was rejected; reason: %s'
159
 
                    % (self.properties[u"name"], reason))
 
185
    def rejected(self):
 
186
        self.logger(u'Client %s was rejected'
 
187
                    % self.properties[u"name"])
160
188
    
161
189
    def selectable(self):
162
190
        """Make this a "selectable" widget.
186
214
                          }
187
215
        
188
216
        # Rebuild focus and non-focus widgets using current properties
189
 
        self._text = (u'%(name)s: %(enabled)s'
 
217
        self._text = (u'%(name)s: %(enabled)s%(timer)s'
190
218
                      % { u"name": self.properties[u"name"],
191
219
                          u"enabled":
192
220
                              (u"enabled"
193
221
                               if self.properties[u"enabled"]
194
 
                               else u"DISABLED")})
 
222
                               else u"DISABLED"),
 
223
                          u"timer": (unicode(datetime.timedelta
 
224
                                             (milliseconds =
 
225
                                              self.properties
 
226
                                              [u"timeout"])
 
227
                                             - (datetime.datetime
 
228
                                                .utcnow()
 
229
                                                - isoformat_to_datetime
 
230
                                                (max((self.properties
 
231
                                                 ["last_checked_ok"]
 
232
                                                 or
 
233
                                                 self.properties
 
234
                                                 ["created"]),
 
235
                                                    self.properties[u"last_enabled"]))))
 
236
                                     if (self.last_checker_failed
 
237
                                         and self.properties
 
238
                                         [u"enabled"])
 
239
                                     else u"")})
195
240
        if not urwid.supports_unicode():
196
241
            self._text = self._text.encode("ascii", "replace")
197
242
        textlist = [(u"normal", self._text)]
208
253
        if self.update_hook is not None:
209
254
            self.update_hook()
210
255
    
 
256
    def update_timer(self):
 
257
        "called by gobject"
 
258
        self.update()
 
259
        return True             # Keep calling this
 
260
    
211
261
    def delete(self):
 
262
        if self._update_timer_callback_tag is not None:
 
263
            gobject.source_remove(self._update_timer_callback_tag)
 
264
            self._update_timer_callback_tag = None
212
265
        if self.delete_hook is not None:
213
266
            self.delete_hook(self)
214
267
    
241
294
#             self.proxy.unpause()
242
295
#         elif key == u"RET":
243
296
#             self.open()
244
 
        elif key == u"+":
245
 
            self.proxy.Approve(True)
246
 
        elif key == u"-":
247
 
            self.proxy.Approve(False)
248
297
        else:
249
298
            return key
250
299