def setup_reactor(self): """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 self.options.max_timeout is not None: def terminator(): # Hasta la vista, twisted reactor.stop() print('Maximum timeout reached: {}s'.format( self.options.max_timeout)) reactor.callLater(self.options.max_timeout, terminator)
def main(*args): """Parse arguments, then build and run checks in a reactor.""" # We do this first 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 0 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", help="Maximum execution time.") parser.add_argument("--connect-timeout", dest="connect_timeout", action="store", default=10, type=float, help="Network connection timeout.") parser.add_argument("-U", "--unbuffered-output", dest="buffer_output", action="store_false", default=True, help="Don't buffer output, write to STDOUT right " "away.") 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.") options = parser.parse_args(list(args)) load_tls_certs(options.cacerts_path) if options.patterns: pattern = SumPattern(map(SimplePattern, options.patterns)) else: pattern = SimplePattern("*") 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) output = sys.stdout if options.show_duration: output = TimestampOutput(output) if options.buffer_output: # We buffer output so we can order it for human readable output output = OrderedOutput(output) include = options.include_tags.split(',') if options.include_tags else [] exclude = options.exclude_tags.split(',') if options.exclude_tags else [] results = ConsoleOutput(output=output, show_tracebacks=options.show_tracebacks, show_duration=options.show_duration, verbose=options.verbose) results = FailureCountingResultWrapper(results) with open(options.config_file) as f: descriptions = yaml.load(f) checks = build_checks(descriptions, options.connect_timeout, include, exclude) if options.max_timeout is not None: def terminator(): # Hasta la vista, twisted reactor.stop() print('Maximum timeout reached: {}s'.format(options.max_timeout)) reactor.callLater(options.max_timeout, terminator) if not options.validate: reactor.callWhenRunning(run_checks, checks, pattern, results) reactor.run() # Flush output, this really only has an effect when running buffered # output output.flush() if results.any_failed(): return 2 else: return 0