summaryrefslogtreecommitdiff
path: root/trove-overseer
blob: 790fe5694cb8ef43ed71de11324aeafdeb19f4e1 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/python
#
# Copyright (C) 2014  Codethink Limited
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys, os, yaml
from optparse import OptionParser
from sys import stderr, stdout, exit, argv
from smtplib import SMTP
from email.utils import formatdate
from socket import gethostname
import tempfile
import subprocess

verbose = False

def utter(msg):
    if verbose:
        stdout.write(msg + "\n")
        stdout.flush()

class html_output:
    html_head = """<html>
        <head>
            <title>Trove Health Monitor status for %s</title>
            <style type="text/css">
                table.trove {
                    width: 100%%;
                    border-width: 1px;
                    border-spacing: 2px;
                    border-style: solid;
                    border-color: #000;
                    border-collapse: collapse;
                }
                table.trove th {
                    border-width: 1px;
                    padding: 5px;
                    border-style: solid;
                    border-color: #000;
                }
                table.trove td {
                    border-width: 1px;
                    padding: 5px;
                    border-style: solid;
                    border-color: #000;
                }
            </style>
        </head>
        <body>
            <h1>Trove Heath Monitor</h1>
            <p>Tests begin at %s</p>
            <table class="trove">
                <thead>
                    <th>Test name</th>
                    <th>Description</th>
                    <th>Threshold</th>
                    <th>Current value</th>
                    <th>Result</th>
                </thead>
    """ % (gethostname(), formatdate())

    def __init__(self, filename):
        self.filename = filename
        self.tempfile = tempfile.NamedTemporaryFile(prefix = filename, delete = False)
        self.fh = self.tempfile.file
        utter("HTML temporary filename is %s" % self.tempfile.name)
        fh = self.fh
        
        fh.write(self.html_head)
    
    def result(self, testname, testparam, result, success, msg):
        fh = self.fh
        fh.write("<tr><td>%s</td><td>%s</td>" % (testname, testparam['name']))
        
        if 'threshold' in testparam:
            fh.write("<td>%s " % str(testparam['threshold']))
            fh.write(testparam['type'] if 'test' in testparam else "high")
        else:
            fh.write("<td>Pass/Fail")
        
        fh.write("</td>")
        fh.write("<td>%s</td>" % str(result))
        fh.write("<td><font color='%s'>%s</font></td>" %
            (
                "green" if success else "red",
                "OK" if success else "<strong>FAILURE</strong><br>%s" % msg
            ))
        fh.write("</tr>")
    
    def finish(self):
        fh = self.fh
        fh.write("</table></body></html>")
        fh.close()
        os.rename(self.tempfile.name, self.filename)

class email_output:
    body = "Subject: Trove Overseer failure report from %s\n" % (gethostname())
    failures = False
    def __init__(self, config):
        self.config = config
        self.body += "From: %s\nTo: %s\n" % (config['mailfrom'], config['rcptto'])
        self.body += "Date: %s\n\n" % formatdate()
    
    def result(self, testname, testparam, result, success, msg):
        if success:
            return
        self.failures = True
        self.body += ("\nTest %s (%s) failed:" % (testparam['name'], testname))
        if 'threshold' in testparam:
            self.body += ("\n\tThreshold: %s ") % str(testparam['threshold'])
            self.body += testparam['type'] if 'type' in testparam else "high"
        self.body += ("\n\tResult:    %s" % str(result))
        if msg:
            self.body += ("\n\t%s") % msg
        
        self.body += "\n\n"
    
    def finish(self):
        if self.failures == False:
            return
        config = self.config
        utter("SMTP server:  %s" % config['server'])
        conn = SMTP(host = config['server'])
        if verbose:
            conn.set_debuglevel(256)
        conn.sendmail(config['mailfrom'], config['rcptto'], self.body)
        conn.quit()
        
def human_to_bytes(threshold):
    if type(threshold) == int or type(threshold) == float:
        return threshold
    
    suffix = threshold[-1:].upper()
    number = float(threshold[:-1])
    
    if suffix == "K":
        number *= 1024
    elif suffix == "M":
        number *= (1024 * 1024)
    elif suffix == "G":
        number *= (1024 * 1024 * 1024)
    elif suffix == "T":
        number *= (1024 * 1024 * 1024 * 1024)
    else:
        utter("Unknown power suffix %s!" % suffix)
        return float(threshold)
        
    return number
       
def bytes_to_human(num):
    for x in ['','K','M','G','T']:
        if num < 1024.0:
            return "%3.0f%s" % (num, x)
        num /= 1024.0
       
def report_failure(testname, testparam, msg, result = 0):
    utter("Test '%s' %s failed: %s" % (testname, testparam, msg))
    result = bytes_to_human(result)
    if html_report:
        html_report.result(testname, testparam, result, False, msg)
    if email_report:
        email_report.result(testname, testparam, result, False, msg)
    
def run_command(command):
    p = subprocess.Popen(command, shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.STDOUT)
    return p
    
def run_test(testname, testparam):
    utter("processing test '%s'" % testname)
    if 'path' in testparam:
        test_cmd = testparam["path"]
    else:
        test_cmd = "/usr/share/trove-overseer/plugins/" + testname
    
    if 'parameters' in testparam:
        test_cmd = test_cmd + (" %s" % testparam['parameters'])
    
    pipe = run_command(test_cmd)
    result = pipe.stdout.readline()
    exitcode = pipe.wait()
    
    if exitcode != 0:
        result += pipe.stdout.read()
        report_failure(testname, testparam,
                       "Test failed with non-zero result: %d, %s" %
                       (exitcode, result), result = exitcode)
        return
    
    if not 'threshold' in testparam:
        # this is a pass/fail test: assume success
        if html_report:
            html_report.result(testname, testparam, 1, True, "")
        return

    result = float(result)
    
    hightest = True
    if 'type' in testparam and testparam['type'] == "low":
        hightest = False
    
    threshold = human_to_bytes(testparam['threshold'])
    utter("test returned %d, threshold is %d\n" % (result, threshold))
    
    if hightest and result > threshold:
        report_failure(testname, testparam,
                       "Result above threshold of %s" % testparam['threshold'],
                       result = result)
        return
    if not hightest and result < threshold:
        report_failure(testname, testparam,
                       "Result below threshold of %s" % testparam['threshold'],
                       result = result)
        return
    
    if html_report:
        html_report.result(testname, testparam, result, True, "")
    
def main():
    if os.getenv("OVERSEER_DEBUG") != None:
        global verbose
        verbose = True
   
    if len(argv) > 1:
        webpage = argv[1]
    else:
        webpage = None
   
    if os.system("git pull -q") != 0:
        stderr.write("Unable to execute git pull\n")
        exit(1)
        
    try:    
        config_file = open("overseer.yaml", "r")
    except IOError as e:
        stderr.write("Unable to open overseer.yaml: %s\n" % e.strerror)
        exit(1)

    try:
        config_data = yaml.load(config_file)
    except yaml.scanner.ScannerError as e:
        stderr.write("YAML scanner error: %s\n" % e)
        exit(1)
    except yaml.parser.ParserError as e:
        stderr.write("YAML parser error: %s\n" % e)
        exit(1)
        
    if not 'overseer' in config_data:
        stderr.write("Configuration contains no overseer data.\n")
        exit(1)

    global html_report
    if webpage != None:
        html_report = html_output(webpage)
    else:
        html_report = False
    
    global email_report
    if 'email' in config_data['overseer']:
        utter("Creating email report")
        email_report = email_output(config_data['overseer']['email'])
    else:
        utter("Skipping email report")
        email_report = False
    
    for test in config_data['overseer']['tests']:
        (testname, testparam) = test.iteritems().next()
        run_test(testname, testparam)

    if html_report:
        html_report.finish()
    
    if email_report:
        email_report.finish()
        
if __name__ == '__main__':
    main()