Checkmk RAW - "Integrate Nagios Plugins" does not execute script in WEB-GUI

Hello to the CheckMK Community.
I am creating this topic (I am using CheckMK Raw with OMD Version 2.3.0p21-cre) to resolve an issue related to the creation (or more precisely misbehaviour/misconfiguration) of the function “Integrate Nagios Plugins”.

More specifically, I have created a Custom Nagios-compatible script which:

  • Execute a web-api query (as follow) to retrieve the status of a specific service assigned to an host monitored by CheckMK: "
wget --quiet \
    --header="accept: application/json" \
    --header="Authorization: Bearer $AUTH_TOKEN" \
    --output-document=- \
    "$API_URL/$HOST_NAME/actions/show_service/invoke?service_description=$(echo $SERVICE_DESCRIPTION | sed 's/ /+/g')""
  • If the service is in state CRITICAL, then execute a custom bash script (contained in the same directory /opt/omd/sites/<site>/local/lib/nagios/plugins):
# Convert state code to Nagios-compatible status
case $state_code in
  0) state="OK" ; exit_code=0 ;;
  1) state="WARNING" ; exit_code=1 ;;
  2)
    state="CRITICAL"
    exit_code=2
    # Executing custom script if state is CRITICAL
    echo "Executing custom script for critical state..."
    ./custom_script.sh
    ;;
  3) state="UNKNOWN" ; exit_code=3 ;;
  *) state="UNKNOWN STATE" ; exit_code=3 ;;
esac

# Convert UNIX timestamp to human-readable date
last_check_date=$(date -d @"$last_check" '+%Y-%m-%d %H:%M:%S')

# Output Nagios-compatible result
echo "$state: $SERVICE_DESCRIPTION on $HOST_NAME | last_check=$last_check_date"
exit $exit_code

The problem that I am facing is that, if I execute the Nagios Plugin manually (so, first sudo omd su <sitename>, then ./custom_script) it works correctly (both the recognition of the service state, and the execution of the custom bash script).
However, once I create and assign a rule associated to this Nagios Plugin, then from the Web-UI it seems that it is able to recognize the change in the state of the monitored service, BUT the custom script custom_script.sh is not executed.

Am I missing something?
I have assigned full execution permission to the script (ugo+x).
The script custom_script.sh tries to connect via sshpass to 2 different machines and run an iperf3 -s and iperf3 -c sessions (so when I execute the Nagios Plugin manually I can see the traffic stream, while this does not happen with the automatic execution via custom rule).

I don’t know if can be helpfull, here the Nagios Script:

#!/bin/bash
#
# Nagios-compatible script to check the status of a service on CheckMK via Web-API
#
# Usage: ./nagios_script.sh -H <host> -S <service_description>
#
# Variables
API_URL="http://localhost/<sitename>/check_mk/api/1.0/objects/host"
AUTH_TOKEN="xxxxxxxxxxxxxxxxxx"

# Parse input arguments
while getopts "H:S:" opt; do
  case $opt in
    H) HOST_NAME="$OPTARG" ;;
    S) SERVICE_DESCRIPTION="$OPTARG" ;;
    *)
      echo "Usage: $0 -H <host> -S <service_description>"
      exit 3
      ;;
  esac
done

# Validate required arguments
if [ -z "$HOST_NAME" ] || [ -z "$SERVICE_DESCRIPTION" ]; then
  echo "Usage: $0 -H <host> -S <service_description>"
  exit 3
fi

# Execute API query using wget
response=$(wget --quiet \
    --header="accept: application/json" \
    --header="Authorization: Bearer $AUTH_TOKEN" \
    --output-document=- \
    "$API_URL/$HOST_NAME/actions/show_service/invoke?service_description=$(echo $SERVICE_DESCRIPTION | sed 's/ /+/g')")

# Check if API call was successful
if [ $? -ne 0 ]; then
  echo "CRITICAL: Unable to query API for $SERVICE_DESCRIPTION on $HOST_NAME"
  exit 2
fi

# Extract fields
description=$(echo "$response" | jq -r '.extensions.description')
host_name=$(echo "$response" | jq -r '.extensions.host_name')
state_code=$(echo "$response" | jq -r '.extensions.state')
last_check=$(echo "$response" | jq -r '.extensions.last_check')

# Convert state code to Nagios-compatible status
case $state_code in
  0) state="OK" ; exit_code=0 ;;
  1) state="WARNING" ; exit_code=1 ;;
  2)
    state="CRITICAL"
    exit_code=2
    # Perform custom script if state is CRITICAL
    echo "Executing custom script for critical state..."
    ./custom_script.sh
    ;;
  3) state="UNKNOWN" ; exit_code=3 ;;
  *) state="UNKNOWN STATE" ; exit_code=3 ;;
esac

# Convert UNIX timestamp to human-readable date
last_check_date=$(date -d @"$last_check" '+%Y-%m-%d %H:%M:%S')

# Output Nagios-compatible result
echo "$state: $SERVICE_DESCRIPTION on $HOST_NAME | last_check=$last_check_date"
exit $exit_code

Thank you for your assistance.

Just to integrate, the echo message is displayed, but the the bash script is completely ignored (see picture below).
Instead if run via CLI the script the custom_scrpt.sh is executed correctly

Nagios-script-triggered

I have also tried just with echo "Executing custom script for critical state..." > file.txt but from the Web-GUI the file is not generated (but Executing custom script for critical state… is not displayed)

Thank you for your assistance

This will not work. You have to include your script inside one single script.
The Nagios environment is not a “normal” environment as your test inside a bash shell was.

Thank you Andreas for the clarification, I will modify the script accordingly.
It is possible to close the Topic.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed. Contact an admin if you think this should be re-opened.