#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

import os

# <<<inotify:sep(9)>>>
# monitored folder    /tmp/noti
# monitored file  /tmp/noti/test
# monitored file  /tmp/noti/other
# 1465470055  modify  /tmp/noti/test  5   1465470055
# 1465470055  open    /tmp/noti/test  5   1465470055
# 1465470055  modify  /tmp/noti/test  5   1465470055
# 1465470056  modify  /tmp/noti/test  5   1465470056
# 1465470056  open    /tmp/noti/test  5   1465470056


def parse_inotify(info):
    parsed = {
        "meta": {
            "warnings": {}
        },
        "configured": {
            "file": set([]),
            "folder": set([])
        },
        "stats": {}
    }
    for line in info:
        if line[0].startswith("warning"):
            parsed["meta"]["warnings"].setdefault(line[1], 0)
            parsed["meta"]["warnings"][line[1]] += 1
            continue
        elif line[0].startswith("configured"):
            parsed["configured"][line[1]].add(line[2])
            continue

        timestamp, mode, filepath = line[:3]
        parsed["stats"].setdefault(filepath, [])
        folderpath = os.path.dirname(filepath)
        parsed["stats"].setdefault(folderpath, [])

        file_dict = {}
        file_dict["timestamp"] = int(timestamp)
        file_dict["mode"] = mode
        # Unused
        if len(line) > 3:
            file_dict["size"] = line[3]
            file_dict["mtime"] = line[4]
        parsed["stats"][filepath].append(file_dict)
        parsed["stats"][folderpath].append(file_dict)

    return parsed


def inventory_inotify(parsed):
    for what in ["file", "folder"]:
        for path in parsed["configured"][what]:
            yield "%s %s" % (what.title(), path), {}


def check_inotify(item, params, parsed):
    what, path = item.split(" ", 1)
    if path not in parsed["configured"][what.lower()]:
        return

    data = parsed["stats"].get(path, {})

    last_status = get_item_state(None, default={})
    for file_dict in data:
        mode = file_dict["mode"]
        for key, value in file_dict.items():
            last_status.setdefault(mode, {})
            last_status[mode][key] = value

    set_item_state(None, last_status)
    now = time.time()

    def _check_levels(what):
        for mode, warn, crit in params.get("age_last_operation", []):
            if mode != what:
                continue
            timestamp = last_status[what]["timestamp"]
            if now - timestamp >= crit:
                return (2, "> %s" % get_age_human_readable(crit))
            elif now - timestamp >= warn:
                return (1, "> %s" % get_age_human_readable(warn))
        return

    for mode in sorted(last_status):
        attr = last_status[mode]
        levels_info = _check_levels(mode)
        if levels_info:
            state = levels_info[0]
            yield state, "Last %s: %s ago (%s)" % (
                mode.title(), get_age_human_readable(now - attr["timestamp"]), levels_info[1])
        else:
            yield 0, "Last %s: %s ago" % (mode.title(),
                                          get_age_human_readable(now - attr["timestamp"]))

    for mode, _warn, _crit in params.get("age_last_operation", []):
        if mode not in last_status:
            yield 3, "Last %s unknown" % mode.title()

    warnings = parsed.get("meta", {}).get("warnings")
    if warnings:
        yield 1, "Incomplete data!"
        for warning, count in warnings.items():
            yield 1, "%d Warnings: %s" % (count, warning)

    if not last_status:
        yield 0, "No data available yet"


check_info["inotify"] = {
    "parse_function": parse_inotify,
    "inventory_function": inventory_inotify,
    "check_function": check_inotify,
    "service_description": "INotify %s",
    "group": "inotify"
}
