summaryrefslogtreecommitdiff
path: root/tools/check-flimsy.py
blob: 8d33d9ea54a6ae6e6b7069eeed0ef41c6e49c06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python

import urllib2
import json
import socket
import datetime
import sys
import time
import smtplib
import atexit
import socket
import signal
from email.mime.text import MIMEText


def dump(obj):
  for attr in dir(obj):
    print "obj.%s = %s" % (attr, getattr(obj, attr))


def testurl(url, timeout):
  """
  returns a list, which first item is status, and optional second item is extra information, like:
    ['ok']
  of
    ['other', 'overheating']
  status:
    'ok'  - check ok
    'head too old': arg = time diff in seconds
    'connection refused'
    'connection timeout'
    'response timeout'
    'other': arg = error string - other unknown errors
  
  """

  try:
    f = urllib2.urlopen(url, timeout=timeout)
    now = datetime.datetime.utcnow()
    obj = json.load(f)
    if 'timestamp' in obj:
      x = datetime.datetime.utcfromtimestamp(obj['timestamp'] / 1000) + datetime.timedelta(milliseconds = obj['timestamp'] % 1000)
      secs = (now - x).total_seconds()
      if secs > 7200:
        return ['head too old', secs]
      return ['ok']
    return ['other', 'no timestamp in reply']
    
  except urllib2.URLError as e:
    #print 'except urllib2.URLError'
    #print repr(e.reason)

    reason_type = type(e.reason).__name__

    if reason_type == 'error':
      err_str = e.args[0].strerror
      if err_str == 'Connection refused':
        return ['connection refused']
      else:
        return ['other', str(e)]

    elif reason_type == 'timeout':
      # timeout when connecting
      return ['connection timeout']

    else:
      return ['other', str(e)]

  except IOError as e:
    #print 'except IOError'
    
    if type(e).__name__ == 'timeout':
      # timeout when reading
      return ['response timeout']

    else:
      return ['other', str(e)]

  except:
    return ['other', str(sys.exc_info()[0])]
    

"""
    print repr(type(e.reason).__name__)
    print 'repr(e) ', repr(e)
    print 'e ', e
    print 'e.args ', e.args
    print 'e.errno ', e.errno
    print 'e.strerror ', e.strerror
    dump(e)
    print 'repr(e.args[0]) ', repr(e.args[0])
    dump (e.args[0])


"""

myhostname = None

def sendmsg(newstate, state, l, lastchange, adminonly=False):
  global myhostname
  if not myhostname:
    myhostname = socket.gethostname()

  txt = 'checkflimsy on %s\n' % (myhostname)
  txt += 'state: %s\n' % newstate
  if newstate == 'head too old':
    txt += 'head too old: %f seconds\n' % l[1]
  if newstate == 'other':
    txt += 'message: %s\n' % str(l[1])
  txt += 'last state change: %s\n' % lastchange.replace(microsecond=0).isoformat(' ')
  delta = datetime.datetime.now().replace(microsecond=0) - lastchange.replace(microsecond=0)
  txt += 'time since last state change: %s\n' % str(delta)
  txt += '(previous state: %s)\n' % state

  print txt # XXX

  msg = MIMEText(txt)

  fromaddr = 'foo-admin@example.net'
  toaddrs = ['foo@example.net']
  if adminonly:
    toaddrs = ['foo@example.net']
    

  subj = '%s flimsy status: %s' % (myhostname, newstate)
  if newstate == 'other':
    subj += ' - %s' % str(l[1])
  if newstate not in ['ok', 'starting']:
    subj += ' - WARNING'
  msg['Subject'] = subj
  msg['From'] = fromaddr
  msg['To'] = ', '.join(toaddrs)

  # Send the message via our own SMTP server, but don't include the
  # envelope header.
  s = smtplib.SMTP('localhost')
  s.sendmail(fromaddr, toaddrs, msg.as_string())
  s.quit()


lastchange = None
state = None

def runloop(url):
  timeout = 10
  sleeptime = 30
  repeattime = 7200
  lasttold = None
  global lastchange
  global state

  while True:
    #print '.' # XXX
    l = testurl(url, timeout)
    newstate = l[0]

    now = datetime.datetime.now()
    
    if state != newstate:
      lastchange = now
      lasttold = now
      if state == None:
        # starting
        print 'check-flimsy started at %s' % (str(now))
        sendmsg('starting', state, l, lastchange, adminonly=True)
      else:
        sendmsg(newstate, state, l, lastchange)
      state = newstate
    elif state != 'ok' and (now - lasttold) > datetime.timedelta(seconds=repeattime):
      lasttold = now
      sendmsg(newstate, state, l, lastchange)

    time.sleep(sleeptime)


"""    
    'ok'  - check ok
    'head too old': arg = time diff in seconds
    'connection refused'
    'connection timeout'
    'response timeout'
    'other': arg = error string - other unknown errors
"""


def exitproc():
  sendmsg('EXITING', state, ['-'], lastchange, adminonly=True)


def main():
  url = 'https://flimsy.ct.nordu.net:8080/ct/v1/get-sth'
  #url = 'http://127.0.0.1:8080'

  while True:
    try:
      atexit.register(exitproc)
      signal.signal(signal.SIGHUP, exitproc)

      runloop(url)
    except:
      print "Unexpected error from runloop():", sys.exc_info()[0]
      raise # XXX


main()