#!/bin/bash
# Copyright (C) 2019 Checkmk 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.

# This script makes a statistics of the usage of the various check plugins.
# It makes a Livestatus-Connection and outputs a list of plugins and the
# information on how many different hosts this plugin is being used.

if [ "$OMD_ROOT" ]; then
    LIVE_SOCKET=$OMD_ROOT/tmp/run/live
elif [ -z "$LIVE_SOCKET" ]; then
    echo "Please do an 'export LIVE_SOCKET=/path/to/your/livestatus/socket'" >&2
    echo "and then call me again." >&2
    exit 1
fi

echo -e "GET services\nColumns: host_name check_command" |
    unixcat "$LIVE_SOCKET" |
    sort -u |
    cut -d\; -f2 |
    cut -d\! -f1 |
    sort |
    uniq -c |
    sort -rn
