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

# <<<apt:sep(0)>>>
# Inst dpkg [1.17.5ubuntu5.3] (1.17.5ubuntu5.4 Ubuntu:14.04/trusty-updates [amd64])
# Inst libtasn1-6-dev [3.4-3ubuntu0.1] (3.4-3ubuntu0.2 Ubuntu:14.04/trusty-updates [amd64]) []
# Inst libtasn1-6 [3.4-3ubuntu0.1] (3.4-3ubuntu0.2 Ubuntu:14.04/trusty-updates [amd64])
# Inst ntpdate [1:4.2.6.p5+dfsg-3ubuntu2.14.04.2] (1:4.2.6.p5+dfsg-3ubuntu2.14.04.3 Ubuntu:14.04/trusty-security [amd64])
# Inst udev [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
# Inst libudev1 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
# Inst libpam-systemd [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
# Inst systemd-services [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64]) []
# Inst libsystemd-daemon0 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
# Inst libsystemd-login0 [204-5ubuntu20.10] (204-5ubuntu20.11 Ubuntu:14.04/trusty-updates [amd64])
# Inst libpolkit-gobject-1-0 [0.105-4ubuntu2] (0.105-4ubuntu2.14.04.1 Ubuntu:14.04/trusty-updates [amd64])
# Inst libxext-dev [2:1.3.2-1] (2:1.3.2-1ubuntu0.0.14.04.1 Ubuntu:14.04/trusty-security [amd64]) []

# or

# <<<apt:sep(0)>>>
# Remv default-java-plugin [2:1.8-58]
# Remv icedtea-8-plugin [1.6.2-3.1]
# Inst default-jre [2:1.8-58] (2:1.8-58+deb9u1 Debian:9.11/oldstable [amd64]) []
# Inst default-jre-headless [2:1.8-58] (2:1.8-58+deb9u1 Debian:9.11/oldstable [amd64])

from cmk.base.plugins.agent_based.utils.apt import NOTHING_PENDING_FOR_INSTALLATION

factory_settings["apt_default_levels"] = {
    "normal": 1,
    "removals": 1,
    "security": 2,
}


def inventory_apt(section):
    yield None, {}


def _format(action, packages, verbose=False):
    message = "%d %s" % (len(packages), action)
    if verbose and packages:
        message += " (%s)" % (
            ", ".join(
                packages,
            )
        )

    return message


def check_apt(_no_item, params, section):
    if not any(section):
        yield 0, NOTHING_PENDING_FOR_INSTALLATION
        return

    if section.no_esm_support:
        yield 2, "System could receive security updates, but needs extended support license"
        return

    yield (
        section.updates and params["normal"] or 0,
        _format("normal updates", section.updates),
        [("normal_updates", len(section.updates))],
    )

    # Only show removals when necessary as they are very rare.
    if section.removals:
        yield (
            params["removals"],
            _format("auto removals", section.removals, verbose=True),
            [("removals", len(section.removals))],
        )

    yield (
        section.sec_updates and params["security"] or 0,
        _format("security updates", section.sec_updates, verbose=True),
        [("security_updates", len(section.sec_updates))],
    )


check_info["apt"] = {
    "inventory_function": inventory_apt,
    "check_function": check_apt,
    "service_description": "APT Updates",
    "group": "apt",
    "has_perfdata": True,
    "default_levels_variable": "apt_default_levels",
}
