~ubuntuone-hackers/conn-check/trunk

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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
from __future__ import print_function
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
from builtins import map
from builtins import object
from argparse import ArgumentParser
from collections import defaultdict
import sys
from threading import Thread
import time
import traceback
import yaml

from twisted.internet import reactor
from twisted.internet.defer import (
    inlineCallbacks,
    )
from twisted.python.threadpool import ThreadPool

from . import get_version_string
from .check_impl import (
    FailureCountingResultWrapper,
    parallel_check,
    skipping_check,
    ResultTracker,
    )
from .checks import CHECK_ALIASES, CHECKS, load_tls_certs
from .patterns import (
    SimplePattern,
    SumPattern,
    )


def setup_reactor(max_timeout):
    """Setup the Twisted reactor with required customisations."""

    def make_daemon_thread(*args, **kw):
        """Create a daemon thread."""
        thread = Thread(*args, **kw)
        thread.daemon = True
        return thread

    threadpool = ThreadPool(minthreads=1)
    threadpool.threadFactory = make_daemon_thread
    reactor.threadpool = threadpool
    reactor.callWhenRunning(threadpool.start)

    if max_timeout is not None:
        def terminator():
            # Hasta la vista, twisted
            reactor.stop()
            print('Maximum timeout reached: {}s'.format(max_timeout))

        reactor.callLater(max_timeout, terminator)
    return reactor


def check_from_description(check_description):
    _type = check_description['type']

    if _type in CHECK_ALIASES:
        _type = CHECK_ALIASES[_type]

    check = CHECKS.get(_type, None)
    if check is None:
        raise AssertionError("Unknown check type: {}, available checks: {}"
                             .format(_type, list(CHECKS.keys())))
    for arg in check['args']:
        if arg not in check_description:
            raise AssertionError('{} missing from check: {}'.format(arg,
                                 check_description))

    if 'port' in check_description:
        port = check_description.get('port')
        try:
            check_description['port'] = int(port)
        except TypeError:
            # swap the TypeError for the more generic AssertionError
            # that we are using for configuration faults
            # and is caught further up
            raise AssertionError('Port is invalid for check: {}'.format(
                                 check_description))

    res = check['fn'](**check_description)
    return res


def filter_tags(check, include, exclude):
    if not include and not exclude:
        return True

    check_tags = set(check.get('tags', []))

    if include:
        result = bool(check_tags.intersection(include))
    else:
        result = not bool(check_tags.intersection(exclude))

    return result


def build_checks(check_descriptions, connect_timeout, include_tags,
                 exclude_tags, skip_checks=False):
    def set_timeout(desc):
        new_desc = dict(timeout=connect_timeout)
        new_desc.update(desc)
        return new_desc
    check_descriptions = [c for c in check_descriptions if filter_tags(c, include_tags, exclude_tags)]

    subchecks = [check_from_description(c) for c in list(map(set_timeout, check_descriptions))]

    if skip_checks:
        strategy_wrapper = skipping_check
    else:
        strategy_wrapper = parallel_check
    return strategy_wrapper(subchecks)


@inlineCallbacks
def run_checks(checks, pattern, results):
    """Make and run all the pertinent checks."""
    try:
        yield checks.check(pattern, results)
    finally:
        reactor.stop()


class NagiosCompatibleArgsParser(ArgumentParser):

    def error(self, message):
        """A patched version of ArgumentParser.error.

        Does the same thing as ArgumentParser.error, e.g. prints an error
        message and exits, but does so with an exit code of 3 rather than 2,
        to maintain compatibility with Nagios checks.
        """
        self.print_usage(sys.stderr)
        self.exit(3, '{}: error: {}\n'.format(self.prog, message))


class TimestampOutput(object):

    def __init__(self, output):
        self.start = time.time()
        self.output = output

    def write(self, data):
        self.output.write("{:.3f}: {}".format(time.time() - self.start, data))


class OrderedOutput(object):
    """Outputs check results ordered by FAILED, SUCCESSFUL, SKIPPED checks."""

    def __init__(self, output):
        self.output = output

        self.failed = defaultdict(list)
        self.messages = defaultdict(list)
        self.skipped = []

    def write(self, data):
        if data[:7] == 'SKIPPED':
            self.skipped.append(data)
            return

        name, message = data.split(' ', 1)

        # Standard check name format is {type}:{host}:{port}
        name_parts = name.split(':', 2)
        try:
            name_parts[2] = ''
        except IndexError:
            pass
        name = ':'.join(name_parts)

        if message[0:6] == 'FAILED':
            self.failed[name].append(data)
        else:
            self.messages[name].append(data)

    def flush(self):
        for _type in ('failed', 'messages'):
            for name, messages in sorted(getattr(self, _type).items()):
                messages.sort()
                list(map(self.output.write, messages))

        self.skipped.sort()
        list(map(self.output.write, self.skipped))


class ConsoleOutput(ResultTracker):
    """Outputs check results to STDOUT."""

    def __init__(self, output, verbose, show_tracebacks, show_duration):
        """Initialize an instance."""
        super(ConsoleOutput, self).__init__()
        self.output = output
        self.verbose = verbose
        self.show_tracebacks = show_tracebacks
        self.show_duration = show_duration

    def format_duration(self, duration):
        if not self.show_duration:
            return ""
        return ": ({:.3f} ms)".format(duration)

    def notify_start(self, name, info):
        """Register the start of a check."""
        if self.verbose:
            if info:
                info = " ({})".format(info)
            else:
                info = ''
            self.output.write("Starting {}{}...\n".format(name, info))

    def notify_skip(self, name):
        """Register a check being skipped."""
        self.output.write("SKIPPED: {}\n".format(name))

    def notify_success(self, name, duration):
        """Register a success."""
        self.output.write("{} OK{}\n".format(
            name, self.format_duration(duration)))

    def notify_failure(self, name, info, exc_info, duration):
        """Register a failure."""
        message = str(exc_info[1]).split("\n")[0]
        if info:
            message = "({}) {}".format(info, message)
        self.output.write("{} FAILED{} - {}\n".format(
            name, self.format_duration(duration), message))

        if self.show_tracebacks:
            formatted = traceback.format_exception(exc_info[0],
                                                   exc_info[1],
                                                   exc_info[2],
                                                   None)
            lines = "".join(formatted).split("\n")
            if len(lines) > 0 and len(lines[-1]) == 0:
                lines.pop()
            indented = "\n".join(["  {}".format(line) for line in lines])
            self.output.write("{}\n".format(indented))


class Command(object):
    """CLI command runner for the main conn-check endpoint."""

    def __init__(self, args):
        self.make_arg_parser()
        self.parse_options(args)
        self.wrap_output(sys.stdout)
        self.load_descriptions()

    def make_arg_parser(self):
        """Set up an arg parser with our options."""

        parser = NagiosCompatibleArgsParser()
        parser.add_argument("config_file",
                            help="Config file specifying the checks to run.")
        parser.add_argument("patterns", nargs='*',
                            help="Patterns to filter the checks.")
        parser.add_argument("-v", "--verbose", dest="verbose",
                            action="store_true", default=False,
                            help="Show additional status")
        parser.add_argument("-d", "--duration", dest="show_duration",
                            action="store_true", default=False,
                            help="Show duration")
        parser.add_argument("-t", "--tracebacks", dest="show_tracebacks",
                            action="store_true", default=False,
                            help="Show tracebacks on failure")
        parser.add_argument("--validate", dest="validate",
                            action="store_true", default=False,
                            help="Only validate the config file,"
                            " don't run checks.")
        parser.add_argument("--version", dest="print_version",
                            action="store_true", default=False,
                            help="Print the currently installed version.")
        parser.add_argument("--tls-certs-path", dest="cacerts_path",
                            action="store", default="/etc/ssl/certs/",
                            help="Path to TLS CA certificates.")
        parser.add_argument("--max-timeout", dest="max_timeout", type=float,
                            action="store", default=9,
                            help="Maximum execution time, in seconds.")
        parser.add_argument("--connect-timeout", dest="connect_timeout",
                            action="store", default=5, type=float,
                            help="Network connection timeout, in seconds.")
        parser.add_argument("-u", "--unbuffered-output", dest="buffer_output",
                            action="store_false", default=True,
                            help="Don't buffer output, write to STDOUT right "
                            "away.")
        parser.add_argument("--dry-run",
                            dest="dry_run", action="store_true",
                            default=False,
                            help="Skip all checks, just print out"
                            " what would be run.")
        group = parser.add_mutually_exclusive_group()
        group.add_argument("--include-tags", dest="include_tags",
                           action="store", default="",
                           help="Comma separated list of tags to include.")
        group.add_argument("--exclude-tags", dest="exclude_tags",
                           action="store", default="",
                           help="Comma separated list of tags to exclude.")
        self.parser = parser

    def parse_options(self, args):
        """Parse args (e.g. sys.argv) into options and set some config."""

        options = self.parser.parse_args(list(args))

        include_tags = []
        if options.include_tags:
            include_tags = options.include_tags.split(',')
            include_tags = [tag.strip() for tag in include_tags]
        options.include_tags = include_tags

        exclude_tags = []
        if options.exclude_tags:
            exclude_tags = options.exclude_tags.split(',')
            exclude_tags = [tag.strip() for tag in exclude_tags]
        options.exclude_tags = exclude_tags

        if options.patterns:
            self.patterns = SumPattern(list(map(SimplePattern, options.patterns)))
        else:
            self.patterns = SimplePattern("*")
        self.options = options

    def wrap_output(self, output):
        """Wraps an output stream (e.g. sys.stdout) from options."""

        if self.options.show_duration:
            output = TimestampOutput(output)
        if self.options.buffer_output:
            # We buffer output so we can order it for human readable output
            output = OrderedOutput(output)

        results = ConsoleOutput(output=output,
                                show_tracebacks=self.options.show_tracebacks,
                                show_duration=self.options.show_duration,
                                verbose=self.options.verbose)
        if not self.options.dry_run:
            results = FailureCountingResultWrapper(results)

        self.output = output
        self.results = results

    def load_descriptions(self):
        """Pre-load YAML checks file into a descriptions property."""

        with open(self.options.config_file) as f:
            self.descriptions = yaml.safe_load(f)

    def run(self):
        """Run/validate/dry-run the given command with options."""

        try:
            checks = build_checks(self.descriptions,
                                self.options.connect_timeout,
                                self.options.include_tags,
                                self.options.exclude_tags,
                                self.options.dry_run)
        except AssertionError as e:
            # Configuration fault, suppress traceback
            # but display message
            self.output.write(str(e))
            self.output.flush()
            return 4

        if not self.options.validate:
            if not self.options.dry_run:
                load_tls_certs(self.options.cacerts_path)

            setup_reactor(self.options.max_timeout)
            reactor.callWhenRunning(run_checks, checks, self.patterns,
                                    self.results)
            reactor.run()

            # Flush output, this really only has an effect when running
            # buffered output
            self.output.flush()

            if not self.options.dry_run and self.results.any_failed():
                return 2

        return 0


def parse_version_arg():
    """Manually check for --version in args and output version info.

    We need to do this early because ArgumentParser won't let us mix
    and match non-default positional argument with a flag argument.
    """
    if '--version' in sys.argv:
        sys.stdout.write('conn-check {}\n'.format(get_version_string()))
        return True


def run(*args):
    if parse_version_arg():
        return 0

    cmd = Command(args)
    return cmd.run()


def main():
    sys.exit(run(*sys.argv[1:]))


if __name__ == '__main__':
    main()