/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: Björn Påhlsson
  • Date: 2010-09-07 18:41:35 UTC
  • mfrom: (417 mandos)
  • mto: (237.4.3 mandos-release)
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: belorn@fukt.bsnet.se-20100907184135-hcd2aou8r2trz8cy
merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
urwid.curses_display.curses.A_UNDERLINE |= (
41
41
    urwid.curses_display.curses.A_BLINK)
42
42
 
 
43
def isoformat_to_datetime(iso):
 
44
    "Parse an ISO 8601 date string to a datetime.datetime()"
 
45
    if not iso:
 
46
        return None
 
47
    d, t = iso.split(u"T", 1)
 
48
    year, month, day = d.split(u"-", 2)
 
49
    hour, minute, second = t.split(u":", 2)
 
50
    second, fraction = divmod(float(second), 1)
 
51
    return datetime.datetime(int(year),
 
52
                             int(month),
 
53
                             int(day),
 
54
                             int(hour),
 
55
                             int(minute),
 
56
                             int(second),           # Whole seconds
 
57
                             int(fraction*1000000)) # Microseconds
 
58
 
43
59
class MandosClientPropertyCache(object):
44
60
    """This wraps a Mandos Client D-Bus proxy object, caches the
45
61
    properties and calls a hook function when any of them are
85
101
        # Logger
86
102
        self.logger = logger
87
103
        
 
104
        self._update_timer_callback_tag = None
 
105
        self.last_checker_failed = False
 
106
        
88
107
        # The widget shown normally
89
108
        self._text_widget = urwid.Text(u"")
90
109
        # The widget shown when we have focus
114
133
                                     self.rejected,
115
134
                                     client_interface,
116
135
                                     byte_arrays=True)
 
136
        last_checked_ok = isoformat_to_datetime(self.properties
 
137
                                                ["last_checked_ok"])
 
138
        if last_checked_ok is None:
 
139
            self.last_checker_failed = True
 
140
        else:
 
141
            self.last_checker_failed = ((datetime.datetime.utcnow()
 
142
                                         - last_checked_ok)
 
143
                                        > datetime.timedelta
 
144
                                        (milliseconds=
 
145
                                         self.properties["interval"]))
 
146
        if self.last_checker_failed:
 
147
            self._update_timer_callback_tag = (gobject.timeout_add
 
148
                                               (1000,
 
149
                                                self.update_timer))
117
150
    
118
151
    def checker_completed(self, exitstatus, condition, command):
119
152
        if exitstatus == 0:
120
 
            #self.logger(u'Checker for client %s (command "%s")'
121
 
            #            u' was successful'
122
 
            #            % (self.properties[u"name"], command))
 
153
            if self.last_checker_failed:
 
154
                self.last_checker_failed = False
 
155
                gobject.source_remove(self._update_timer_callback_tag)
 
156
                self._update_timer_callback_tag = None
 
157
            self.logger(u'Checker for client %s (command "%s")'
 
158
                        u' was successful'
 
159
                        % (self.properties[u"name"], command))
 
160
            self.update()
123
161
            return
 
162
        # Checker failed
 
163
        if not self.last_checker_failed:
 
164
            self.last_checker_failed = True
 
165
            self._update_timer_callback_tag = (gobject.timeout_add
 
166
                                               (1000,
 
167
                                                self.update_timer))
124
168
        if os.WIFEXITED(condition):
125
169
            self.logger(u'Checker for client %s (command "%s")'
126
170
                        u' failed with exit code %s'
127
171
                        % (self.properties[u"name"], command,
128
172
                           os.WEXITSTATUS(condition)))
129
 
            return
130
 
        if os.WIFSIGNALED(condition):
 
173
        elif os.WIFSIGNALED(condition):
131
174
            self.logger(u'Checker for client %s (command "%s")'
132
175
                        u' was killed by signal %s'
133
176
                        % (self.properties[u"name"], command,
134
177
                           os.WTERMSIG(condition)))
135
 
            return
136
 
        if os.WCOREDUMP(condition):
 
178
        elif os.WCOREDUMP(condition):
137
179
            self.logger(u'Checker for client %s (command "%s")'
138
180
                        u' dumped core'
139
181
                        % (self.properties[u"name"], command))
140
 
        self.logger(u'Checker for client %s completed mysteriously')
 
182
        else:
 
183
            self.logger(u'Checker for client %s completed mysteriously')
 
184
        self.update()
141
185
    
142
186
    def checker_started(self, command):
143
187
        #self.logger(u'Client %s started checker "%s"'
190
234
        # Rebuild focus and non-focus widgets using current properties
191
235
 
192
236
        # Base part of a client. Name!
193
 
        self._text = (u'%(name)s: '
 
237
        base = (u'%(name)s: '
194
238
                      % {u"name": self.properties[u"name"]})
 
239
        if not self.properties[u"enabled"]:
 
240
            message = u"DISABLED"
 
241
        elif self.last_checker_failed:
 
242
            timeout = datetime.timedelta(milliseconds
 
243
                                         = self.properties[u"timeout"])
 
244
            last_ok = isoformat_to_datetime(
 
245
                max((self.properties["last_checked_ok"]
 
246
                     or self.properties["created"]),
 
247
                    self.properties[u"last_enabled"]))
 
248
            timer = timeout - (datetime.datetime.utcnow() - last_ok)
195
249
 
196
 
        if self.properties[u"approved_pending"]:
 
250
            message = (u'A checker has failed! Time until client gets diabled: %s'
 
251
                           % unicode(timer))
 
252
        elif self.properties[u"approved_pending"]:
197
253
            if self.properties[u"approved_by_default"]:
198
 
                self._text += u"Connection established to client. (d)eny?"
 
254
                message = u"Connection established to client. (d)eny?"
199
255
            else:
200
 
                self._text += u"Seeks approval to send secret. (a)pprove?"
 
256
                message = u"Seeks approval to send secret. (a)pprove?"
201
257
        else:
202
 
            self._text += (u'%(enabled)s'
203
 
                           % {u"enabled":
204
 
                               (u"enabled"
205
 
                                if self.properties[u"enabled"]
206
 
                                else u"DISABLED")})
 
258
            message = u"enabled"
 
259
        self._text = "%s%s" % (base, message)
 
260
            
207
261
        if not urwid.supports_unicode():
208
262
            self._text = self._text.encode("ascii", "replace")
209
263
        textlist = [(u"normal", self._text)]
220
274
        if self.update_hook is not None:
221
275
            self.update_hook()
222
276
    
 
277
    def update_timer(self):
 
278
        "called by gobject"
 
279
        self.update()
 
280
        return True             # Keep calling this
 
281
    
223
282
    def delete(self):
 
283
        if self._update_timer_callback_tag is not None:
 
284
            gobject.source_remove(self._update_timer_callback_tag)
 
285
            self._update_timer_callback_tag = None
224
286
        if self.delete_hook is not None:
225
287
            self.delete_hook(self)
226
288