import sys from optparse import make_option from django.core.management.base import BaseCommand class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('--log', '-l', dest='log', action="store_true", default=False, help=u"prints nagios alert level."), ) help = u"Sphinx's administration tools." output_transaction = True def handle(self, **options): if options.get('log', False): return self.log() def log(self): """ Prints the sphinx state for Nagios """ from django.db import models ModelToMonitor = models.get_model('app_name','ModelName') try: # doit retourner la 1re entree de l'index sphinx job = ModelToMonitor.search.query("").set_options(maxmatches=1, mode=6, limit=1)[0] except: return self.log_exit(2) return self.log_exit(0) def log_exit(self, lvl=2): if lvl == 0: print "OK: Sphinx is up" else: print "CRITICAL: Sphinx is down" sys.exit(lvl)