#!/usr/bin/env python

"""
Nagios plugin to check that Zephyr personals mirrors are forwarding.

This script works by just monitoring the files under
/home/zulip/mirror_status, which are updated by the Zephyr personals
mirrors when they receive the messages sent every minute by
/etc/cron.d/test_zephyr_personal_mirrors
"""
from __future__ import print_function

import os
import time

RESULTS_DIR = "/home/zulip/mirror_status"

states = {
    "OK": 0,
    "WARNING": 1,
    "CRITICAL": 2,
    "UNKNOWN": 3
    }

def report(state, output):
    print("%s\n%s" % (state, output))
    exit(states[state])

output = ""
down_count = 0
for results_file_name in os.listdir(RESULTS_DIR):
    this_state = "OK"
    results_file = os.path.join(RESULTS_DIR, results_file_name)
    data = open(results_file).read().strip()
    last_check = os.stat(results_file).st_mtime
    time_since_last_check = time.time() - last_check
    # time_since_last_check threshhold needs to be strictly greater
    # than 1 minute, since with cron we expect intervals of at least 1
    # minute without any update
    if data.split("\n")[-1].strip() != "0" or time_since_last_check >= 90:
        down_count += 1
        this_state = "DOWN"
    last_check_ts = time.strftime("%Y-%m-%d %H:%M %Z", time.gmtime(last_check))
    output += "%s: %s (%s)\n" % (results_file, this_state, last_check_ts)

if down_count == 0:
    state = "OK"
elif down_count < 5:
    state = "WARNING"
else:
    state = "CRITICAL"

report(state, output)
