#!/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 output from agent:
# Number of tasks: 15
# Name: System:EventManager
#         Id: 1
#         Runtime ID: 1314160393
#         Class: EventManager
#         State: Started
# Name: System:AVS
#         Id: 2
#         Runtime ID: 1314160398
#         Class: AVS
#         State: Started
# Name: System:Quarantine
#         Id: 3
#         Runtime ID: 1314160399
#         Class: Quarantine
#         State: Started
# Name: System:Statistics
#         Id: 4
#         Runtime ID: 1314160396
#         Class: Statistics
#         State: Started
#


def inventory_kaspersky_av_tasks(info):
    inventory = []
    jobs = ['Real-time protection', 'System:EventManager']
    for line in [x for x in info if x[0].startswith("Name")]:
        job = " ".join(line[1:])
        if job in jobs:
            inventory.append((job, None))
    return inventory


def check_kaspersky_av_tasks(item, _no_params, info):
    found = False
    for line in info:
        if found:
            if line[0].startswith('State'):
                state = 0
                if line[1] != "Started":
                    state = 2
                return state, "Current state is " + line[1]
        if line[0].startswith('Name') and " ".join(line[1:]) == item:
            found = True
    return 3, "Task not found in agent output"


check_info["kaspersky_av_tasks"] = {
    "check_function": check_kaspersky_av_tasks,
    "inventory_function": inventory_kaspersky_av_tasks,
    "service_description": "AV Task %s",
}
