Skip to content
Snippets Groups Projects
test_monitoring.py 2.16 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Criticality: Low
Check that the monitoring graphs work.
'''
import os
from datetime import datetime
import sys
import imp

YELLOW = '\033[93m'
GREEN = '\033[92m'
RED = '\033[91m'
DEF = '\033[0m'

os.chdir(os.path.dirname(__file__))
if not os.path.isfile('../utils.py'):
    print('The envsetup configuration was not found.')
    sys.exit(1)
else:
    es_utils = imp.load_source('es_utils', '../utils.py')
    conf = es_utils.load_conf()


def print_color(txt, col):
    print('%s%s%s' % (col, txt, DEF))


def print_yellow(txt):
    print_color(txt, YELLOW)


def print_red(txt):
    print_color(txt, RED)


def print_green(txt):
    print_color(txt, GREEN)

MUNIN_WWW_PATH = '/var/cache/munin/www/'
Stéphane Diemer's avatar
Stéphane Diemer committed

def check_munin():
    print('Checking if monitoring works...')
    if not os.path.exists(MUNIN_WWW_PATH):
        print_red('Munin directory "%s" not found.' % MUNIN_WWW_PATH)
        return 1
    path = os.path.join(MUNIN_WWW_PATH, 'localdomain', 'localhost.localdomain', 'cpu-day.png')
    if not os.path.exists(path):
        path = None
        for name in os.listdir(MUNIN_WWW_PATH):
            if not name.endswith('.html') and name != 'static' and os.path.isfile(os.path.join(MUNIN_WWW_PATH, name, 'index.html')):
                for sub_name in os.listdir(os.path.join(MUNIN_WWW_PATH, name)):
                    p = os.path.join(MUNIN_WWW_PATH, name, sub_name, 'cpu-day.png')
                    if sub_name != 'index.html' and os.path.exists(p):
                        path = p
                        break
                if path:
                    break
    if not path:
        print_red('Munin host directory not found in "%s".' % MUNIN_WWW_PATH)
        return 1
    print('Found host directory: "%s".' % path)
    mtime = os.path.getmtime(path)
    d = datetime.fromtimestamp(mtime)
    now = datetime.now()
Florent Thiery's avatar
Florent Thiery committed
    diff_seconds = (now - d).total_seconds()
    if diff_seconds > 3600:
        print_red('Monitoring data is older than 1 hour and is probably not working.')
        return 1
    else:
        print_green('Monitoring data is not older than 1 hour, everything seems fine.')
        return 0


Stéphane Diemer's avatar
Stéphane Diemer committed
rc = check_munin()