From 503c40cad3f1d9ea0a05a20325fb0f17083e5301 Mon Sep 17 00:00:00 2001 From: Cyberes Date: Sat, 30 Mar 2024 20:16:18 -0600 Subject: [PATCH] fix a couple checks --- check_curl.sh | 15 +++++++++++---- check_systemd_service.sh | 17 +++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/check_curl.sh b/check_curl.sh index fa586ed..591134d 100755 --- a/check_curl.sh +++ b/check_curl.sh @@ -15,7 +15,8 @@ usage() { -M Set the HTTP mode (default: GET) -t Timeout in seconds -s Ignore the response status code. - -e Expected status code." + -e Expected status code. + -S Sanitize HTML when printing to console. Good for Icinga2 Web." exit 3 } @@ -31,9 +32,10 @@ IGNORE_STATUS="" TIMEOUT="30" HTTP_METHOD="-X GET" EXPECTED_STATUS_CODE="" +SANITIZE_HTML=false # Parse arguments -while getopts "u:w:c:C:LH:M:IpR:st:e:h" opt; do +while getopts "u:w:c:C:LH:M:IpR:st:e:hS" opt; do case $opt in u) URL="$OPTARG" @@ -77,6 +79,9 @@ while getopts "u:w:c:C:LH:M:IpR:st:e:h" opt; do h) usage ;; + S) + SANITIZE_HTML=true + ;; *) echo "Invalid option: -$OPTARG" >&2 usage @@ -411,10 +416,12 @@ fi BODY_CONTAINS=" " if [ -n "$CRIT_STRING" ]; then BODY=$(curl -s $FOLLOW_REDIRECTS $INSECURE $HEADERS $RESOLVE $TIMEOUT $URL) + if $SANITIZE_HTML; then + BODY=$(echo "$BODY" | sed 's//\>/g') + fi # shellcheck disable=SC2076 if ! [[ $BODY =~ "$CRIT_STRING" ]]; then - # if ! echo "$BODY" | grep -q "$CRIT_STRING"; then - echo "CRITICAL - response body does not contain '$CRIT_STRING': $BODY" + echo "CRITICAL - response body does not contain the required string: $BODY" exit 2 else BODY_CONTAINS=" and contained substring " diff --git a/check_systemd_service.sh b/check_systemd_service.sh index dfe21d1..8147906 100755 --- a/check_systemd_service.sh +++ b/check_systemd_service.sh @@ -68,12 +68,21 @@ fi start_time=$(systemctl show "$UNIT_NAME" --property=ExecMainStartTimestamp --value) end_time=$(systemctl show "$UNIT_NAME" --property=ExecMainExitTimestamp --value) current_time=$(date +%s) -start_time_diff=$((current_time - $(date -d "$start_time" +%s))) -end_time_diff=$((current_time - $(date -d "$end_time" +%s))) +start_time_diff="undefined" +end_time_diff="undefined" + +if [ -n "$start_time" ]; then + start_time_diff=$((current_time - $(date -d "$start_time" +%s))) + start_time_diff_perf="| uptime=${start_time_diff}s" +fi +if [ -n "$end_time" ] && [ "$end_time" != "n/a" ]; then + end_time_diff=$((current_time - $(date -d "$end_time" +%s))) + end_time_diff_perf="| downtime=${end_time_diff}s" +fi if [ "$IS_TIMER" = true ] && { [ "$substate" = "exited" ] || [ "$active" = "inactive" ]; }; then - echo "OK - $UNIT_NAME is active and exited (returned $exit_code) and enabled. Exited at $end_time. | downtime=${end_time_diff}s" + echo "OK - $UNIT_NAME is active and exited (returned $exit_code) and enabled. Exited at $end_time. $end_time_diff_perf" else - echo "OK - $UNIT_NAME is active and enabled. Started at $start_time. | uptime=${start_time_diff}s" + echo "OK - $UNIT_NAME is active and enabled. Started at $start_time. $start_time_diff_perf" fi exit 0