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

os.chdir(os.path.dirname(__file__))
if not os.path.isfile('../utils.py'):
    print('The envsetup configuration was not found.')
    sys.exit(1)
u = imp.load_source('es_utils', '../utils.py')
conf = u.load_conf()
MUNIN_WWW_PATH = '/var/cache/munin/www/'
Stéphane Diemer's avatar
Stéphane Diemer committed

def check_munin():
    u.log('Checking if monitoring works...')
    if not os.path.exists(MUNIN_WWW_PATH):
        p = subprocess.run(['dpkg', '-s', 'munin'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
        if p.returncode == 0:
            u.error('Munin directory "%s" not found.' % MUNIN_WWW_PATH)
            return 1
        else:
            u.error('Munin is not installed.')
            return 2

    # get cpu day graph of each host
    paths = list()
    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)):
                path = os.path.join(MUNIN_WWW_PATH, name, sub_name, 'cpu-day.png')
                if sub_name != 'index.html' and os.path.exists(path):
                    paths.append(path)
    if not paths:
        u.error('No Munin host directory was found in "%s".' % MUNIN_WWW_PATH)

    # check graph mtime
    error = False
    for path in paths:
        u.log('Checking graph "%s" modification date...' % path)
        mtime = os.path.getmtime(path)
        d = datetime.fromtimestamp(mtime)
        now = datetime.now()
        diff_seconds = (now - d).total_seconds()
        if diff_seconds > 3600:
            u.error('The graph is older than 1 hour. The monitoring is probably not working.')
            u.success('The graph is not older than 1 hour.')
    return 1 if error else 0
if __name__ == '__main__':
    rc = check_munin()
    sys.exit(rc)