nagios: add a check_user_zephyr_mirror_liveness plugin.

It will alert when our users' mirrors don't appear to be running, as
assessed by having recently made a get_message API request.

(imported from commit 4b8c5f51b007568a90a92f7b095c51f3566d5117)
This commit is contained in:
Jessica McKellar 2012-11-15 16:25:50 -05:00
parent fcf5eb8f1f
commit b86ddf4ddc
1 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,58 @@
#!/usr/bin/env python
"""
Nagios plugin to check that our MIT users' Zephyr mirrors are running.
It must be run on a machine that is using the live database for the
Django ORM.
"""
import datetime
import os
import sys
import time
os.environ['DJANGO_SETTINGS_MODULE'] = "humbug.settings"
sys.path.append('/home/humbug/humbug')
sys.path.append('/home/humbug/humbug/zephyr')
from django.contrib.auth.models import User
from zephyr.models import UserActivity
USERS_TO_IGNORE = (
'rhkeeler@mit.edu',
)
states = {
"OK": 0,
"WARNING": 1,
"CRITICAL": 2,
"UNKNOWN": 3
}
def report(state, short_msg, too_old=None):
too_old_data = ""
if too_old:
too_old_data = "\nLast call to get_message for out of date mirrors:\n" + "\n".join(
["%16s: %s" % (user.user_profile.user.email,
user.last_visit.strftime("%Y-%m-%d %H:%M %Z")
) for user in too_old]
)
print "%s: %s%s" % (state, short_msg, too_old_data)
exit(states[state])
now = datetime.datetime.utcnow()
too_old = []
for user in UserActivity.objects.filter(query="/api/v1/get_messages",
client__name="zephyr_mirror"):
if user.user_profile.user.email in USERS_TO_IGNORE:
continue
if user.last_visit.replace(tzinfo=None) < now - datetime.timedelta(minutes=5):
too_old.append(user)
if not too_old:
report("OK", "All mirrors active")
else:
report("CRITICAL", "Some mirrors inactive", too_old)