#!/usr/bin/env python2.7
"""Celery worker status checker"""

import sys

class NagiosPlugin(object):
    """Nagios Plugin base class"""

    OK = 0
    WARNING = 1
    CRITICAL = 2
    UNKNOWN = 3

    def __init__(self, warning, critical, *args, **kwargs):
        self.warning = warning
        self.critical = critical

    def run_check(self):
        raise NotImplementedError

    def ok_state(self, msg):
        print "OK - {}".format(msg)
        sys.exit(self.OK)

    def warning_state(self, msg):
        print "WARNING - {}".format(msg)
        sys.exit(self.WARNING)

    def critical_state(self, msg):
        print "CRITICAL - {}".format(msg)
        sys.exit(self.CRITICAL)

    def unknown_state(self, msg):
        print "UNKNOWN - {}".format(msg)
        sys.exit(self.UNKNOWN)

@@ -53,16 +53,59 @@
 cker%22%22%22%0A
+import argparse%0Aimport re%0Aimport subprocess
 %0Aimport 
@@ -145,224 +145,47 @@
     
-%22%22%22Nagios Plugin base class%22%22%22%0A%0A    OK = 0%0A    WARNING = 1%0A    CRITICAL = 2%0A    UNKNOWN = 3%0A%0A    def __init__(self, warning, critical, *args, **kwargs):%0A        self.warning = warning%0A        self.critical = critical
+OK = 0%0A    CRITICAL = 2%0A    UNKNOWN = 3
 %0A%0A  
@@ -338,115 +338,8 @@
 K)%0A%0A
-    def warning_state(self, msg):%0A        print %22WARNING - %7B%7D%22.format(msg)%0A        sys.exit(self.WARNING)%0A%0A
     
@@ -550,8 +550,1643 @@
 NKNOWN)%0A
+%0A%0Aclass CeleryWorkerCheck(NagiosPlugin):%0A    def __init__(self, workers, service):%0A        self._workers = workers%0A        self._service = service%0A        self._check_cmd = %5B'service', service, 'status'%5D%0A        self._status_output = None%0A        self._critical_workers = %5B%5D%0A%0A    def run_check(self):%0A        try:%0A            self.status_output = subprocess.check_output(self._check_cmd)%0A        except subprocess.CalledProcessError:%0A            self.unknown_state('Unable to get worker status(es)')%0A        self._check_status_output()%0A        self._report_results()%0A%0A    def _check_status_output(self):%0A        for worker in self._workers:%0A            self._check_worker_status(worker)%0A%0A    def _check_worker_status(self, worker):%0A        regex_worker_pattern = 'celeryd %5C(node %7B%7D%5C) %5C(pid %5Cd+%5C) is running'.format(worker)%0A        worker_status_is_running = re.findall(regex_worker_pattern, self.status_output)%0A        if not worker_status_is_running:%0A            self._critical_workers.append(worker)%0A%0A    def _report_results(self):%0A        if self._critical_workers:%0A            self.critical_state(%22%7B%7D worker(s) down%22.format(', '.join(self._critical_workers)))%0A        self.ok_state(%22All workers running%22)%0A%0A%0Adef main():%0A    parser = argparse.ArgumentParser(description='Celery worker status checker')%0A    parser.add_argument('workers', nargs='+', help=%22Worker node names to check.%22)%0A    parser.add_argument('--service', default='celeryd', help=%22Service script used to manage celery.%22)%0A    args = parser.parse_args()%0A%0A    cwc = CeleryWorkerCheck(args.workers, args.service)%0A    cwc.run_check()%0A%0Aif __name__ == '__main__':%0A    main()%0A

Implement CeleryWorkerCheck class