Current File : //proc/3/root/lib/check_mk_agent/plugins/mk_redis
#!/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.0p23"

# sample output of pgrep command
# 1051 /usr/bin/redis-server 127.0.0.1:6380
# 1324 /usr/bin/redis-server 127.0.0.1:6379

# example cfg file /etc/check_mk/mk_redis.cfg
#
# REDIS_INSTANCES=(My_First_Redis My_Second_Redis My_socket_Redis)
#
# REDIS_HOST_My_First_Redis="127.0.0.1"
# REDIS_PORT_My_First_Redis="6380"
# REDIS_PASSWORD_My_First_Redis='MYPASSWORD'
#
# REDIS_HOST_My_Second_Redis="127.0.0.1"
# REDIS_PORT_My_Second_Redis="6379"

# REDIS_HOST_My_socket_Redis="/var/redis/redis.sock"
# REDIS_PORT_My_socket_Redis="unix-socket"

set -e -o pipefail

REDIS_INSTANCES=()
IS_DETECTED=false
REDIS_CLI_CMD=${REDIS_CLI_CMD:-redis-cli} # To be overloaded by tests
WAITMAX_CMD=${WAITMAX_CMD:-waitmax}       # To be overloaded by tests

load_config() {
    # source optional configuration file
    if [ -e "$MK_CONFDIR/mk_redis.cfg" ]; then
        # shellcheck disable=SC1090
        . "$MK_CONFDIR/mk_redis.cfg"
    fi
}

redis_args() {
    INSTANCE=$1

    HOST="REDIS_HOST_$INSTANCE"
    PORT="REDIS_PORT_$INSTANCE"
    PASSWORD="REDIS_PASSWORD_$INSTANCE"

    # if autodetection is used, rewrite instance name for section output
    if [[ "$IS_DETECTED" == true ]]; then
        INSTANCE="${!HOST};${!PORT}"
    fi

    if [[ "${!PORT}" == "unix-socket" ]]; then
        REDIS_ARGS=("-s" "${!HOST}")
    else
        REDIS_ARGS=("-h" "${!HOST}" "-p" "${!PORT}")
    fi

    if [[ "${!PASSWORD}" ]] && [[ "${!PASSWORD}" != "None" ]]; then
        REDIS_ARGS+=("-a" "${!PASSWORD}")
    fi

    REDIS_ARGS+=("info")
}

load_config

# if no servers in config file, try to detect
if [ ${#REDIS_INSTANCES[@]} -eq 0 ]; then
    IS_DETECTED=true
    # find instances and remove entries like "*:6879", possible with docker container
    DETECTED=$(pgrep -xa "redis-server" 2>/dev/null | awk '/:[0-9]+/ && !/\*/ { print $3 }')

    # add found redis instances
    for REDIS_INSTANCE in $DETECTED; do
        for inst in $REDIS_INSTANCE; do
            IFS=":" read -ra parts <<<"$inst"

            # dot of IP can not be used in variable names
            REDIS_NAME=$(echo "$inst" | tr :. _)

            # create dynamic variables
            declare "REDIS_HOST_$REDIS_NAME=${parts[0]}"
            declare "REDIS_PORT_$REDIS_NAME=${parts[1]}"

            # append instance to array
            REDIS_INSTANCES+=($REDIS_NAME)
        done
    done
fi

# print redis section, if servers are found
if [ "${REDIS_INSTANCES[*]}" ]; then
    echo -e "<<<redis_info:sep(58)>>>"
else
    exit 0
fi

for INSTANCE in ${REDIS_INSTANCES[*]}; do
    redis_args $INSTANCE
    # print server section
    echo "[[[$INSTANCE|${!HOST}|${!PORT}]]]"

    # execute command
    $WAITMAX_CMD 3 "${REDIS_CLI_CMD}" "${REDIS_ARGS[@]}" || true
    echo
done