This snippet is used to create a script for monitoring sphinx status with Nagios via django-sphinx.
It returns 0 (OK) or 2 (CRITICAL).
Remember to change this strings ModelToMonitor
and app_name
.
Usage :
./manage your-controls-command --log
> /your/script/name.py
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 | import os
import sys
from optparse import make_option
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--log', '-l', dest='log', action="store_true", default=False,
help=u"returns nagios alert's script."),
)
help = u"Sphinx's administration tools."
output_transaction = True
def handle(self, **options):
if options.get('log', False):
return self.log_script()
def log_script(self):
"""docstring for log_script"""
path = __file__
for i in range(6):
path = os.path.dirname(path)
path2 = os.path.dirname(path)
params = {
'settings':os.environ['DJANGO_SETTINGS_MODULE'],
'pythonpath':path,
'pythonpath2':path2,
}
template = r"""#!/usr/bin/python
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'wanajob_pro.%(settings)s'
sys.path.append("%(pythonpath)s")
sys.path.append("%(pythonpath2)s")
from django.conf import settings
from django.db import models
from wanajob_pro.job_board.models import JobPost
# JobPost = models.get_model('job_board','JobPost')
def log_exit(lvl=2):
if lvl == 0:
print "OK: sphinx is up"
else:
print "CRITICAL: sphinx is down"
sys.exit(lvl)
if __name__ == '__main__':
try:
job = JobPost.search.query("").set_options(maxmatches=1, mode=6, limit=1)[0]
except:
log_exit(2)
log_exit(0)
""" % params
print template
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 8 months, 4 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 3 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Please login first before commenting.