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.