#!/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:
# <<<vms_users>>>
# AEP 2 - - 1
# SYSTEM 1
# TCPIP$FTP - - - 1


def inventory_vms_users(info):
    if len(info) > 0:
        return [(None, {})]


def check_vms_users(item, params, info):

    infos = []
    num_sessions = 0
    for line in info:
        # complete missing columns
        padding = [0] * (5 - len(line))
        interactive, _subproc, _batch, _network = list(map(saveint, line[1:])) + padding
        if interactive:
            num_sessions += interactive
            infos.append(("%s: %d" % (line[0], interactive)))

    perfdata = [("sessions", num_sessions)]

    if num_sessions:
        return (0, "Interactive users: " + ", ".join(infos), perfdata)
    return (0, "No interactive users", perfdata)


check_info["vms_users"] = {
    "check_function": check_vms_users,
    "inventory_function": inventory_vms_users,
    "service_description": "VMS Users",
    "has_perfdata": True,
}
