#!/usr/bin/python3

import os
import sys
import re
import glob
import time

def timesuffix2sec(timesuffix):
    suf = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400}

    r = re.match('(\d+)(d|h|m|s|day|days|hour|hours|hr|min|minute|minutes|sec|seconds)?$', timesuffix)
    if r is None:
        raise ValueError('Cannot parse \'{}\'. Valid examples are: 10s, 5m, 2h'.format(timesuffix))

    r = re.match('(\d+)([dhmsDHMS])?', timesuffix)
    if r is None:
        raise ValueError('Bad time/suffix value {}'.format(timesuffix))

    n = int(r.group(1))
    try:
        suffix = r.group(2).lower()
    except:
        suffix = 's'
    return n * suf[suffix]


if len(sys.argv) == 1:
    method = 'info'
else:
    method = sys.argv[1]

if method == 'info':
    info = {
        'Protocol': '0.1',
        'Description': 'Report files which must be not empty (log files)',
        'Version': '0.1',
        'Methods': 'check makeconfig info'
    }
    for k, v in info.items():
        print("{}: {}".format(k, v))

elif method == 'check':
    # read parameters
    prefix = os.getenv('PREFIX')
    basename = os.getenv('BASENAME')
    pathlist = os.getenv('PATHLIST','/var/log/*')
    skip = os.getenv('SKIP','')
    maxage = timesuffix2sec(os.getenv('MAXAGE', '10m'))
    recursive = bool(int(os.getenv('RECURSIVE','1')))

    nfiles = 0
    filename = None

    n_ok = 0

    status = 'OK'
    details = ''

    for path in pathlist.split(' '):
        for curpath in glob.glob(path, recursive=recursive):
            if not os.path.isfile(curpath):
                continue
            if skip and re.search(skip, curpath):
                continue
            stat = os.stat(curpath)
            sz = stat.st_size
            age = time.time() - stat.st_mtime

            if age<maxage:
                continue
            if sz:
                n_ok += 1
                continue

            nfiles += 1
            filename = curpath
            status = 'ERR'

    if filename:
        # error
        if nfiles>1:
            details = "{} and {} more files".format(filename, nfiles-1)
        else:
            details = filename
    else:
        details = '{} files checked'.format(n_ok)

    print("NAME: {}{}".format(prefix, basename))
    print("TAGS: {}".format(basename))
    print("METHOD: heartbeat")
    print("DETAILS: {}".format(details))
    print("STATUS: {}".format(status))

elif method=='makeconfig':
    data="""
#
# Env configurational file for 'notempty' check
#

# Space-separated directories to check (glob)
PATHLIST=/var/log/mail.log

# recursive? if 1 and path ends with '**', it will match any file 
RECURSIVE=1

# Check only files with age older then MAXAGE 
# To avoid alerts for recently rotated files
MAXAGE=10m

# regex to skip these files (empty: do not skip anything)
SKIP=\.\d$|\.gz$
""".strip()

    print(data)