#!/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.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.df import *  # pylint: disable=wildcard-import,unused-wildcard-import
# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.size_trend import *  # pylint: disable=wildcard-import,unused-wildcard-import
import json


def parse_cadvisor_df(info):
    diskstat_info = json.loads(info[0][0])
    parsed = {}
    for diskstat_name, diskstat_entries in diskstat_info.items():
        if len(diskstat_entries) != 1:
            continue
        try:
            parsed[diskstat_name] = float(diskstat_entries[0]["value"])
        except KeyError:
            continue
    return parsed


def check_cadvisor_df(item, params, parsed):
    size_mb = parsed["df_size"] / 1024**2
    avail_mb = size_mb - (parsed["df_used"] / 1024**2)
    reserved_mb = 0
    inodes_total = parsed["inodes_total"]
    inodes_free = parsed["inodes_free"]
    return df_check_filesystem_single(item, size_mb, avail_mb, reserved_mb, inodes_total,
                                      inodes_free, params)


check_info["cadvisor_df"] = {
    'parse_function': parse_cadvisor_df,
    'inventory_function': discover_single,
    'check_function': check_cadvisor_df,
    'service_description': 'Filesystem',
    'has_perfdata': True,
}
