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

# Reason for this no-op: shellcheck disable=... before the first command disables the error for the
# entire script.
:

# Disable unused variable error (needed to keep track of version)
# shellcheck disable=SC2034
CMK_VERSION="2.0.0p21"

#   .--queries-------------------------------------------------------------.
#   |                                        _                             |
#   |                   __ _ _   _  ___ _ __(_) ___  ___                   |
#   |                  / _` | | | |/ _ \ '__| |/ _ \/ __|                  |
#   |                 | (_| | |_| |  __/ |  | |  __/\__ \                  |
#   |                  \__, |\__,_|\___|_|  |_|\___||___/                  |
#   |                     |_|                                              |
#   '----------------------------------------------------------------------'


function get_tag_stats () {
    local socket="$1"
    local site="$2"
    local header="$3"
    local tags=($(echo "$header" | tr ' ' '\n'))
    if [ "${#tags[@]}" -ne 0 ] && [ -S "$socket" ];
    then
        local tags_query="GET hosts\n"
        for tag in "${tags[@]}";
        do
            tags_query="${tags_query}Stats: custom_variables ~ TAGS ${tag}\n"
        done
        echo "Tags|$header|$(echo -e "${tags_query}" | waitmax 3 "/omd/sites/${site}/bin/unixcat" "${socket}")"
    fi
}


function get_check_command_stats () {
    local socket="$1"
    local site="$2"
    local header="$3"
    local service_check_commands=($(echo "$header" | tr ' ' '\n'))
    if [ "${#service_check_commands[@]}" -ne 0 ] && [ -S "$socket" ];
    then
        local service_check_commands_query="GET services\n"
        for service_check_command in "${service_check_commands[@]}";
        do
            service_check_commands_query="${service_check_commands_query}Stats: check_command ~~ ${service_check_command}$\n"
        done
        echo "Service check commands|$header|$(echo -e "${service_check_commands_query}" | waitmax 3 "/omd/sites/${site}/bin/unixcat" "${socket}")"
    fi
}


#.
#   .--main----------------------------------------------------------------.
#   |                                       _                              |
#   |                       _ __ ___   __ _(_)_ __                         |
#   |                      | '_ ` _ \ / _` | | '_ \                        |
#   |                      | | | | | | (_| | | | | |                       |
#   |                      |_| |_| |_|\__,_|_|_| |_|                       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

SITES=
TAGS=
SERVICE_CHECK_COMMANDS=


if [ -e "$MK_CONFDIR/site_object_counts.cfg" ];
then
    . "$MK_CONFDIR/site_object_counts.cfg"
fi


if type omd >/dev/null;
then
    echo "<<<site_object_counts:sep(124)>>>"
    if [ -n "$SITES" ];
    then
        sites=($(echo "$SITES" | tr ' ' '\n'))
    else
        sites=($(omd sites | cut -d' ' -f1))
    fi

    if [ -n "$TAGS" ];
    then
        tags="$TAGS"
    fi

    if [ -n "$SERVICE_CHECK_COMMANDS" ];
    then
        service_check_commands="$SERVICE_CHECK_COMMANDS"
    fi

    for site in "${sites[@]}";
    do
        site_tags="TAGS_$site"
        site_tags=${!site_tags}
        if [ -n "$tags" ] && [ -n "$site_tags" ];
        then
            site_tags="$tags $site_tags"
        elif [ -n "$tags" ];
        then
            site_tags="$tags"
        fi

        site_service_check_commands="SERVICE_CHECK_COMMANDS_$site"
        site_service_check_commands=${!site_service_check_commands}
        if [ -n "$service_check_commands" ] && [ -n "$site_service_check_commands" ];
        then
            site_service_check_commands="$service_check_commands $site_service_check_commands"
        elif [ -n "$service_check_commands" ];
        then
            site_service_check_commands="$service_check_commands"
        fi

        socket="/omd/sites/${site}/tmp/run/live"

        echo "[[[$site]]]"
        get_tag_stats "$socket" "$site" "$site_tags"
        get_check_command_stats "$socket" "$site" "$site_service_check_commands"
    done
fi
