HEX
Server: Apache/2.4.57 (Unix) OpenSSL/1.0.2k-fips
System: Linux sv2.tribox.net.br 3.10.0-1160.102.1.el7.x86_64 #1 SMP Tue Oct 17 15:42:21 UTC 2023 x86_64
User: bucalon (1051)
PHP: 8.2.13
Disabled: system,exec,shell_exec,popen,passthru,proc_open,posix_getpwnam,posix_getpwuid,posix_kill,pcntl_fork,escapeshellarg,escapeshellcmd,proc_close,dl,show_source,posix_mkfifo,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname,pcntl_exec,expect_popen,pcntl_signal_get_handler,pcntl_async_signals,link,symlink,syslog,ld,mb_send_mail,imap_open,imap_mail,libvirt_connect,gnupg_init,stream_socket_sendto,stream_socket_client,parse_ini_file,curl_multi_exec
Upload Files
File: //bin/check_mk_caching_agent
#!/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.

# ------------------------------------------------------------------ #
#   Checkmk Caching Agent                                           #
#                                                                    #
#   This is a wrapper for the normal check_mk_agent. It optimizes    #
#   situations where - due to fully redundant monitoring - the       #
#   target host is queried by two or even more monitoring servers    #
#   in parallel. It will cache with output of the agent and send     #
#   the contents of the cache file to servers that have not seen     #
#   that output yet. No time criteria is needed, so this is working  #
#   without configuration, regardless of your check interval. It     #
#   is not even neccessary that all servers are polling with the     #
#   same interval.                                                   #
#                                                                    #
#   We apoplogize for the fact that this wrapper is running only     #
#   on Linux, currently and uses features of the BASH (for sake      #
#   of execution speed). An adaption for other Unices should not     #
#   be very difficult.                                               #
#                                                                    #
#   In order to use this wrapper, simply call it instead of          #
#   check_mk_agent. This works via SSH and via xinetd. I'm not       #
#   sure if the normal inetd gives you access to the REMOTE_HOST     #
#   ip address, which is absolutely needed here.                     #
# ------------------------------------------------------------------ #

export MK_CACHEDIR="/var/cache/check_mk"

# Determine the IP address of the remote Nagios server.
# xinetd sends us the IP address of the remote host via
# the environment variable REMOTE_HOST. SSH sets a variable
# SSH_CONNECTION where the remote IP address is the first
# part of a space-separated list. Of both fails, we set
# the address to 'unknown'

if [ -z "$REMOTE_HOST" ]; then
    REMOTE_HOST=${SSH_CONNECTION%% *}
    if [ -z "$REMOTE_HOST" ]; then
        REMOTE_HOST=unknown
    fi
fi

# We keep the cache and state files in the configuration directory.
# This is not fully FHS-compliant - I guess - but (1) consistent
# with the logwatch extension and (2) configurable for the admin
# during setup.

mkdir -p $MK_CACHEDIR
CACHEFILE=$MK_CACHEDIR/agentcache
LOCKFILE=$CACHEFILE.lock
SEENFILE=$CACHEFILE.seenby.$REMOTE_HOST

# Check if we need to refresh the cache file. This needs
# to be done in mutual exclusion in order to avoid a clash
# with other monitoring servers calling us in parallel. This
# is not very unlikely since the agent might run a couple
# of seconds
(
    flock --timeout 180 --exclusive 200 || exit 1

    if [ ! -e "$CACHEFILE" ] || [ -e "$SEENFILE" ]; then
        check_mk_agent >$CACHEFILE
        rm -f $CACHEFILE.seenby.*
    fi
    # Send cache file and mark it as seen by our remote host
    cat $CACHEFILE
    touch $SEENFILE
) 200>$LOCKFILE