delete check_service_bsd.sh

add check_process.sh
This commit is contained in:
Cyberes 2023-06-08 12:16:02 -06:00
parent 342d66071a
commit ea1b2d7c1b
Signed by: cyberes
GPG Key ID: 6B4A33836A9500FE
2 changed files with 53 additions and 38 deletions

53
check_process.sh Executable file
View File

@ -0,0 +1,53 @@
#!/bin/bash
# Check if a process is running using Icinga2 and pgrep
# Usage: ./check_process.sh -p <process_name> -s <pretty_service_name>
# 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

View File

@ -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