Collection of CheckMK checks (see https://checkmk.com/). All checks and plugins are provided as is. Absolutely no warranty. Send any comments to thl-cmk[at]outlook[dot]com

Skip to content
Snippets Groups Projects
Commit 926fd45f authored by thl-cmk's avatar thl-cmk :flag_na:
Browse files

Delete curl.sh

parent 43f36067
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
#
# Author: thl-cmk[at]outlook[dot]com
# URL : https://thl-cmk.hopto.org
# Date : 2022-02-20
#
# Wrapper around: https://curl.se/
#
# curl plugin for the check_mk linux agent
#
# 2022-02-21: added curl_option to the output
# 2022-02-24: changed to use -w %{json} instead of -w @$curl.format
# extended search for curl executable to agent dir
# 2022-02-25: removed separate url and error_level from output
# 2022-02-27: added support to search for strings in cURL output
# 2022-03-03: fixed handling of spaces in options string
# 2022-03-05: added support for search in header strings
# 2022-03-06: added support for ssl/tls session info
# 2022-03-11: added --verbose --stderr $TEMP_DIR/curl_session to CURL_OPTIONS
# 2022-03-13: moved curl_item files to curl sub directory under $MK_CONFDIR
# 2022-03-15: moved curl options from curl.cfg to curl_item_#.options
# 2022-03-17: added regex pattern match
#
# ToDo: add plugin timeout enforcement
CONF_DIR="/etc/check_mk"
LIB_DIR="/usr/lib/check_mk_agent"
TEMP_DIR="/var/tmp"
CURL_OPTIONS="--disable --write-out %{json} --silent --verbose --include --stderr $TEMP_DIR/curl_session"
CURL_CONFIG=curl.cfg
CURL_RUN="FIRST"
CURL_OUTPUT="$TEMP_DIR/curl_output"
CURL_HEADER="$TEMP_DIR/curl_header"
CURL_SESSION_FILE="$TEMP_DIR/curl_session"
CURL_RESULT=""
function find_curl_strings {
# usage: search_strings file_to_search_in extension_for_file_with_search_strings json_key
# check for expected strings in response
if [ -f "$1" ]; then
if [ -f "$CONF_DIR/curl/$CURL_ITEM.$2" ]; then
local ROUND="FIRST"
CURL_RESULT=$CURL_RESULT$(printf '{"%s":{"%s":[' "$SERVICE_NAME" "$3")
while read -r LINE; do
if [ "$ROUND" = "SECOND" ]; then
CURL_RESULT=$CURL_RESULT$(printf ',')
else
ROUND="SECOND"
fi
# ToDo: remove grep, read $CURL_OUTPUT to $CURL_FILE and search in there
grep -q -s "$LINE" "$1"
CURL_RESULT=$CURL_RESULT$(printf '["%s", %s]' "$LINE" $?)
done <"$CONF_DIR/curl/$CURL_ITEM.$2"
CURL_RESULT=$CURL_RESULT$(printf ']}}')
fi
# rm "$1"
fi
}
function find_curl_session_info {
# usage: search_marker json_key replace_new_line
if [ -f "$CURL_SESSION_FILE" ]; then
CURL_RESULT="$CURL_RESULT"$(printf '{"%s":{"%s":[' "$SERVICE_NAME" "$2")
local ROUND="FIRST"
while read -r LINE; do
if [ "${LINE:0:1}" = "$1" ]; then
if [ "$ROUND" = "SECOND" ]; then
CURL_RESULT=$CURL_RESULT$(printf ',')
else
ROUND="SECOND"
fi
LINE=${LINE//"\""/"\\\""}
if [ "$3" = "YES" ]; then
local NEW_LINE
NEW_LINE=$(printf '"%s"' "${LINE:1:${#LINE}-2}")
CURL_RESULT="$CURL_RESULT$NEW_LINE"
else
CURL_RESULT=$CURL_RESULT$(printf '"%s"' "${LINE:1}")
fi
fi
done <"$CURL_SESSION_FILE"
CURL_RESULT="$CURL_RESULT]}}"
# rm "$CURL_SESSION_FILE"
fi
}
function find_curl_regex {
local GREP_OPTIONS="-q -s -o -z -P"
if [ -f "$CURL_OUTPUT" ]; then
if [ -f "$CONF_DIR/curl/$CURL_ITEM.regex" ]; then
if [ "$CURL_REG_MULTI" = "multiline" ]; then
CURL_REG_MULTI="(?sm)"
else
CURL_REG_MULTI=""
fi
while read -r LINE; do
if [ "$CURL_REG_CASE" = "case" ]; then
LINE="'$CURL_REG_MULTI$LINE'"
eval grep $GREP_OPTIONS $LINE "$CURL_OUTPUT"
CURL_REG_RESULT=$?
else
GREP_OPTIONS="$GREP_OPTIONS"" -i"
LINE="'$CURL_REG_MULTI$LINE'"
eval grep $GREP_OPTIONS $LINE $CURL_OUTPUT
CURL_REG_RESULT=$?
fi
CURL_RESULT=$CURL_RESULT$(printf '{"%s":{"regex":%s}}' "$SERVICE_NAME" "$CURL_REG_RESULT")
done < "$CONF_DIR/curl/$CURL_ITEM.regex"
fi
fi
}
function cleanup {
if [ -f "$CURL_OUTPUT" ]; then
rm "$CURL_OUTPUT"
fi
if [ -f "$CURL_HEADER" ]; then
rm "$CURL_HEADER"
fi
if [ -f "$CURL_SESSION_FILE" ]; then
rm "$CURL_SESSION_FILE"
fi
}
printf "<<<curl:sep(0)>>>\n"
if [ -f "$MK_LIBDIR/bin/curl" ]; then
CURL_EXECUTABLE=$MK_LIBDIR/bin/curl
elif [ -f "$LIB_DIR/bin/curl" ]; then
CURL_EXECUTABLE=$LIB_DIR/bin/curl
elif [ -f "$(which curl)" ]; then
CURL_EXECUTABLE=$(which curl)
else
printf '{"ERROR":"executable file curl not found"}\n'
exit
fi
if [ -f "$MK_CONFDIR/$CURL_CONFIG" ]; then
CURL_CONFIG=$MK_CONFDIR/$CURL_CONFIG
CONF_DIR="$MK_CONFDIR"
elif [ -f "$CONF_DIR/$CURL_CONFIG" ]; then
CURL_CONFIG=$CONF_DIR/$CURL_CONFIG
else
printf '{"ERROR":"config file %s not found"}\n' "$CURL_CONFIG"
exit
fi
# start JSON
printf "{"
while read -r LINE; do
SERVICE_NAME=$(echo "$LINE" | awk -F'|' '{print $1}')
CURL_ITEM=$(echo "$LINE" | awk -F'|' '{print $2}')
CURL_REGEX=$(echo "$LINE" | awk -F'|' '{print $3}')
CURL_REG_CASE=$(echo "$CURL_REGEX" | awk -F'_' '{print $1}')
CURL_REG_MULTI=$(echo "$CURL_REGEX" | awk -F'_' '{print $2}')
# FIELDS=($(awk -F"|" '{$1=$1} 1' <<<"${LINE}"))
if [ "$CURL_RUN" = "SECOND" ]; then
printf ','
else
CURL_RUN="SECOND"
fi
cleanup
printf '"%s":{"data":' "$SERVICE_NAME"
eval "$CURL_EXECUTABLE" "$CURL_OPTIONS" --config "$CONF_DIR/curl/$CURL_ITEM.options"
printf '}'
# check for expected strings in response
find_curl_strings "$CURL_OUTPUT" "search_response" "expected_response"
# check for expected strings in headers
find_curl_strings "$CURL_HEADER" "search_header" "expected_header"
# retrieve SSL/TLS session info
find_curl_session_info "*" "TLS_INFO" "NO"
# retrieve request header
find_curl_session_info ">" "REQUEST_HEADER" "YES"
# retrieve response header
find_curl_session_info "<" "RESPONSE_HEADER" "YES"
# check for regex match
find_curl_regex
done <"$CURL_CONFIG"
printf "}\n"
# end JSON
echo "$CURL_RESULT"
# clean up
cleanup
unset CONF_DIR
unset LIB_DIR
unset CURL_EXECUTABLE
unset CURL_FORMAT
unset CURL_CONFIG
unset CURL_OPTIONS
unset CURL_RUN
unset CURL_OUTPUT
unset CURL_HEADER
unset CURL_SESSION_FILE
unset SERVICE_NAME
unset URL
unset CURL_REG_CASE
unset CURL_REG_MULTI
exit 0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment