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

# example agent output sections...
#
# ...for openSUSE 12:
# <<<zypper:sep(124)>>>
# 5 patches needed (2 security patches)
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-326 | 1       | recommended | needed | Softwarestack update
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-316 | 1       | security    | needed | bind: Fixed a remote denial of service
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-318 | 1       | recommended | needed | mdadm: fixed some race conditions during startup
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-320 | 1       | security    | needed | update for libxml2
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-321 | 1       | recommended | needed | sudo: fixed pam session leak and tls option handling
# Updates for openSUSE 12.1 12.1-1.4 | openSUSE-2012-324 | 1       | recommended | needed | util-linux: make mount honor 'noexec' and 'user' option
# 1 | apache | package | (any)
# 2 | mysql  | package | (any)

# ...for SLES11:
# <<<zypper:sep(124)>>>
# 4 patches needed (2 security patches)
# SLE11-SDK-SP4-Updates | sdksp4-apache2-mod_fcgid-12653 | 1       | security    | needed
# SLES11-SP4-Updates    | slessp4-mysql-12847            | 1       | security    | needed
# SLES11-SP4-Updates    | slessp4-timezone-12844         | 1       | recommended | needed
# SLES11-SP4-Updates    | slessp4-wget-12826             | 1       | recommended | needed

# ...new since SLES12:
# <<<zypper:sep(124)>>>
# 4 patches needed (1 security patches)
# SLES12-SP1-Updates | SUSE-SLE-SERVER-12-SP1-2016-1141 | security    | moderate  | ---         | needed | Security update for sqlite3
# SLES12-SP1-Updates | SUSE-SLE-SERVER-12-SP1-2016-1147 | recommended | moderate  | ---         | needed | Recommended update for dracut
# SLES12-SP1-Updates | SUSE-SLE-SERVER-12-SP1-2016-1149 | recommended | low       | ---         | needed | Recommended update for gcc48
# SLES12-SP1-Updates | SUSE-SLE-SERVER-12-SP1-2016-1150 | recommended | low       | ---         | needed | Recommended update for release-notes-sles

# <<<zypper:sep(124)>>>
# 4 patches needed (1 security patches)
# SLES12-SP0-Updates | SUSE-SLE-SERVER-12-2016-967  | recommended | low        | needed | Recommended update for timezone
# SLES12-SP0-Updates | SUSE-SLE-SERVER-12-2016-981  | recommended | moderate   | needed | Recommended update for mdadm
# SLES12-SP0-Updates | SUSE-SLE-SERVER-12-2016-987  | recommended | low        | needed | Recommended update for procps
# SLES12-SP0-Updates | SUSE-SLE-SERVER-12-2016-997  | recommended | moderate   | needed | Recommended update for bind


def inventory_zypper(info):
    # the agent section is only present when the agent has
    # detected that zypper is installed, therefore the check
    # can always register
    return [(None, {})]


def check_zypper(_no_item, _no_params, info):
    patch_types = {}
    updates = 0
    locks = []
    firstline = " ".join(info[0]) if info else ""
    if re.match("ERROR:", firstline):
        return 3, firstline
    for line in info:
        # 5 patches needed (2 security patches)
        if len(line) >= 5:
            patch_type = None
            if len(line) >= 7 and line[5].lower().strip() == 'needed':  # since SLES12
                patch_type = line[2].strip()
            elif line[4].lower().strip() == 'needed':
                patch_type = line[3].strip()
            if patch_type:
                patch_types.setdefault(patch_type, 0)
                patch_types[patch_type] += 1
                updates += 1
        elif len(line) == 4:
            locks.append(line[1])

    state = 0

    infotext = "%d updates" % updates
    if updates:
        patch_items = sorted(patch_types.items())
        infos = []
        for t, c in patch_items:
            if t == "security":
                marker = '(!!)'
                state = 2
            elif t == "recommended":
                marker = '(!)'
                state = max(state, 1)
            else:
                marker = ''
            infos.append("%s: %d%s" % (t, c, marker))
        infotext += " (" + ", ".join(infos) + ")"

    if locks:
        state = max(1, state)
        infotext += ", %d locks(!)" % len(locks)

    return state, infotext


check_info['zypper'] = {
    "check_function": check_zypper,
    "inventory_function": inventory_zypper,
    "service_description": "Zypper Updates",
    "group": "zypper",
}
