Skip to content

Instantly share code, notes, and snippets.

@mikej312
Created July 7, 2020 20:43
Show Gist options
  • Save mikej312/79bcceebeb67567e08e036b28b42bb1d to your computer and use it in GitHub Desktop.
Save mikej312/79bcceebeb67567e08e036b28b42bb1d to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# OpenBSD Memory Plugin for Nagios
#
# The original verion of ths cscript was found here:
# https://exchange.nagios.org/directory/Plugins/Operating-Systems/BSD/OpenBSD/OpenBSD-Memory/details
#
# Updated the way the memory values are collected since top now outputs values in gigabytes
#
# Copyright 2008 - [email protected]
# Version: 0.2
# Date: 2008-12-31
#
# Description:
# This plugin checks the used/free/total
# memory of OpenBSD. You can set warning
# and critical integer/precentage(%) values
# with -w and -c. With -p you can output
# performance data.
#
# Install:
# Install the nagios-plugins package, copy
# the script to /usr/local/libexec/nagios/
# and set the executable bit.
#
# Example usage:
# /usr/local/libexec/nagios/check_openbsd_mem.sh -w 15% -c 10% -p
# Will produce following output:
# OK - Free: 856M Used: 145M Total: 1001M | 856;145;1001;150;100
#
PROGNAME=$(echo $0 | awk -F\/ '{ print $NF }')
LIBEXEC="/usr/local/libexec/nagios"
. $LIBEXEC/utils.sh
print_usage()
{
echo "Usage: $0 [-w <integer value>] [-c <integer value>] [-p]"
echo "Usage: $0 -h"
echo ""
echo "Arguments:"
echo " -w <integer value>"
echo " Generate warning state if free memory under integer/percentage(%) value"
echo " -c <integer value>"
echo " Generate critical state if free memory under integer/percentage(%) value"
echo " -p"
echo " Output performance data"
echo ""
}
print_help()
{
echo "OpenBSD memory status plugin for Nagios"
echo ""
print_usage
}
while test -n "$1"; do
case "$1" in
-h)
print_help
exit $STATE_OK
;;
-w)
if [ $(echo $2 | grep "%$") ]; then
percent_warn=$2
else
int_warn=$2
fi
shift
;;
-c)
if [ $(echo $2 | grep "%$") ]; then
percent_crit=$2
else
int_crit=$2
fi
shift
;;
-p)
perf=1
;;
*)
echo "Unknown argument: $1"
print_usage
exit $STATE_UNKNOWN
;;
esac
shift
done
# get total memory
TOTAL_MEM=$(( $(sysctl -n hw.physmem) / 1024 / 1024 ))
# get free memory
FREE_MEM=$(vmstat | tail -1 | awk '{ print $4 }' | sed 's/M//g')
# calculate used memory
USED_MEM=$(($TOTAL_MEM - $FREE_MEM))
# checks -w and -c parameter and transform input
# percentage warning
if [ -n "$percent_warn" ]; then
percent_warn=$(echo $percent_warn | sed 's/%//g')
if [ $percent_warn -gt 100 ]; then
echo "Error: Percentage of warning (-w) over 100%."
exit $STATE_UNKNOWN
else
WARN=$(($TOTAL_MEM*$percent_warn/100))
fi
fi
# percentage critical
if [ -n "$percent_crit" ]; then
percent_crit=$(echo $percent_crit | sed 's/%//g')
if [ $percent_crit -gt 100 ]; then
echo "Error: Percentage of critical (-c) over 100%."
exit $STATE_UNKNOWN
else
CRIT=$(($TOTAL_MEM*$percent_crit/100))
fi
fi
# integer warning
if [ -n "$int_warn" ]; then
WARN="$int_warn"
fi
# integer critical
if [ -n "$int_crit" ]; then
CRIT="$int_crit"
fi
# output with or without performance data
if [ "$perf" = 1 ]; then
OUTPUT="Free: "$FREE_MEM"M Used: "$USED_MEM"M Total: "$TOTAL_MEM"M | $FREE_MEM;$USED_MEM;$TOTAL_MEM;$WARN;$CRIT"
else
OUTPUT="Free: "$FREE_MEM"M Used: "$USED_MEM"M Total: "$TOTAL_MEM"M"
fi
# checks critical parameter if any specified
if [ -n "$CRIT" ]; then
if [ $FREE_MEM -lt $CRIT ]; then
echo -n "CIRTICAL - $OUTPUT"
exit $STATE_CRITICAL
fi
fi
# checks warning parameter if any specified
if [ -n "$WARN" ];then
if [ $FREE_MEM -lt $WARN ]; then
echo -n "WARNING - $OUTPUT"
exit $STATE_WARNING
fi
fi
# output for STATE_OK
echo -n "OK - $OUTPUT"
exit $STATE_OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment