From ea1b2d7c1be5f72c9bdc2435a2239c76f5654bb0 Mon Sep 17 00:00:00 2001 From: Drake Panzer Date: Thu, 8 Jun 2023 12:16:02 -0600 Subject: [PATCH] delete check_service_bsd.sh add check_process.sh --- check_process.sh | 53 ++++++++++++++++++++++++++++++++++++++++++++ check_service_bsd.sh | 38 ------------------------------- 2 files changed, 53 insertions(+), 38 deletions(-) create mode 100755 check_process.sh delete mode 100755 check_service_bsd.sh diff --git a/check_process.sh b/check_process.sh new file mode 100755 index 0000000..f5b1c5b --- /dev/null +++ b/check_process.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Check if a process is running using Icinga2 and pgrep +# Usage: ./check_process.sh -p -s + +# Exit codes +STATE_OK=0 +STATE_WARNING=1 +STATE_CRITICAL=2 +STATE_UNKNOWN=3 + +# Check if the process name and pretty service name are provided +while getopts ":p:s:" opt; do + case $opt in + p) + process_name="$OPTARG" + ;; + s) + pretty_service_name="$OPTARG" + ;; + \?) + echo "Invalid option: -$OPTARG" >&2 + exit $STATE_UNKNOWN + ;; + :) + echo "Option -$OPTARG requires an argument." >&2 + exit $STATE_UNKNOWN + ;; + esac +done + +if [ -z "$process_name" ]; then + echo "UNKNOWN - Process name not provided" + exit $STATE_UNKNOWN +fi + +if [ -z "$pretty_service_name" ]; then + pretty_service_name="$process_name" +fi + +# Check if the process is running +process_count=$(pgrep -f "$process_name" 2>/dev/null | wc -l) + +if [ $? -ne 0 ]; then + echo "UNKNOWN - Check failed" + exit $STATE_UNKNOWN +elif [ $process_count -gt 0 ]; then + echo "OK - $pretty_service_name is running" + exit $STATE_OK +else + echo "CRITICAL - $pretty_service_name is not running" + exit $STATE_CRITICAL +fi diff --git a/check_service_bsd.sh b/check_service_bsd.sh deleted file mode 100755 index a6a56cc..0000000 --- a/check_service_bsd.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/sh - -# Parse named arguments -while [ "$#" -gt 0 ]; do - case "$1" in - --service) - SERVICE="$2" - shift 2 - ;; - *) - echo "Unknown option: $1" - exit 3 - ;; - esac -done - -# Check if the service name is provided -if [ -z "$SERVICE" ]; then - echo "UNKNOWN - Service name not provided" - exit 3 -fi - -# Check if the service is running -service_status=$(service ${SERVICE} onestatus) - -# If the service is running, return OK -if echo "$service_status" | grep -iq "${SERVICE} is running \(as\|with\) pid [0-9]*."; then - echo "OK - ${SERVICE} is running" - exit 0 -# If the service is not running, return CRITICAL -elif echo "$service_status" | grep -iq "${SERVICE} is not running."; then - echo "CRITICAL - ${SERVICE} is not running" - exit 2 -# If the check failed, return UNKNOWN -else - echo "UNKNOWN - Check failed" - exit 3 -fi