#!/bin/sh

# ########################################################################
# This program is part of $PROJECT_NAME$
# License: GPL License (see COPYING)
# Authors:
#  Baron Schwartz, Roman Vynar
# ########################################################################

# ########################################################################
# Redirect STDERR to STDOUT; Nagios doesn't handle STDERR.
# ########################################################################
exec 2>&1

# ########################################################################
# Set up constants, etc.
# ########################################################################
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_DEPENDENT=4

# ########################################################################
# Run the program.
# ########################################################################
main() {

   # Get options
   for o; do
      case "${o}" in
         -w)        shift; OPT_WARN="${1}"; shift; ;;
         -d)        shift; ;; # left for backward-compatibility
         -c)        shift; OPT_CRIT="${1}"; shift; ;;
         --version) grep -A2 '^=head1 VERSION' "$0" | tail -n1; exit 0 ;;
         --help)    perl -00 -ne 'm/^  Usage:/ && print' "$0"; exit 0 ;;
         -*)        echo "Unknown option ${o}.  Try --help."; exit 1; ;;
      esac
   done
   OPT_WARN=${OPT_WARN:-90}
   OPT_CRIT=${OPT_CRIT:-95}
   if is_not_sourced; then
      if [ -n "$1" ]; then
         echo "WARN spurious command-line options: $@"
         exit 1
      fi
   fi

   NOTE="UNK cannot find memory statistics"
   TEMP=$(mktemp -t "${0##*/}.XXXXXX") || exit $?
   trap "rm -f '${TEMP}' >/dev/null 2>&1" EXIT
   if [ -e /proc/meminfo ] && cat /proc/meminfo > "${TEMP}" ; then
      USD_PCT=$(get_used_memory_linux "${TEMP}")
   elif which sysctl > /dev/null && sysctl -a > "${TEMP}" ; then
      USD_PCT=$(get_used_memory_bsd "${TEMP}")
   else
      echo $NOTE
      exit
   fi

   NOTE="Memory ${USD_PCT}% used"
   if [ "${USD_PCT:-0}" -ge "${OPT_CRIT}" ]; then
      NOTE="CRIT $NOTE. $(get_largest_process)"
   elif [ "${USD_PCT:-0}" -ge "${OPT_WARN}" ]; then
      NOTE="WARN $NOTE. $(get_largest_process)"
   else
      NOTE="OK $NOTE"
   fi

   # Build the common perf data output for graph trending
   PERFDATA="memory_used=${USD_PCT:-0};${OPT_WARN};${OPT_CRIT};0;100"
   echo "$NOTE | $PERFDATA"
}

# ########################################################################
# Find the largest process
# ########################################################################
get_largest_process () {
   ALL_PROCS=$(ps axo pid,%mem,ucomm | sort -k2 -nr)
   BIGGEST=$(echo $ALL_PROCS | head -n 1)
   BIG_PID=$(echo $BIGGEST | cut -d' ' -f1)
   BIG_MEM=$(echo $BIGGEST | cut -d' ' -f2)
   BIG_COMMAND=$(echo $BIGGEST | cut -d' ' -f3)
   echo "Largest process: ${BIG_COMMAND} (${BIG_PID}) = ${BIG_MEM}%"
}

# ########################################################################
# Get the used memory from /proc/meminfo
# Consider MemAvailable if available in the file (starting procps-ng 3.3.10).
# See https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
# ########################################################################
get_used_memory_linux() {
awk '/^MemTotal:/ {total = $2;}
     /^MemFree:/ {free = $2;}
     /^MemAvailable:/ {avail = $2;}
     /^Buffers:/ {buffers = $2;}
     /^Cached:/ {cached = $2;
        if (avail == "") avail = free + buffers + cached;
        printf "%d\n", (total - avail) / total * 100;}' "$1"
}
# ########################################################################
# Get the used memory from sysctl for BSD systems
# ########################################################################
get_used_memory_bsd() {
   awk '/vm.stats.vm.v_cache_count:/ {cache = $2}
        /vm.stats.vm.v_inactive_count:/ {inactive = $2}
        /vm.stats.vm.v_free_count:/ {free = $2}
        /vm.stats.vm.v_page_count:/ {
           total = $2;
           used = total - inactive - cache - free;
           printf "%d\n", used / total * 100}' "$1"
}

# ########################################################################
# Determine whether this program is being executed directly, or sourced/included
# from another file.
# ########################################################################
is_not_sourced() {
   [ "${0##*/}" = "pmp-check-unix-memory" ] || [ "${0##*/}" = "bash" -a "$_" = "$0" ]
}

# ########################################################################
# Execute the program if it was not included from another file.
# This makes it possible to include without executing, and thus test.
# ########################################################################
if is_not_sourced; then
   OUTPUT=$(main "$@")
   EXITSTATUS=$STATE_UNKNOWN
   case "${OUTPUT}" in
      UNK*)  EXITSTATUS=$STATE_UNKNOWN;  ;;
      OK*)   EXITSTATUS=$STATE_OK;       ;;
      WARN*) EXITSTATUS=$STATE_WARNING;  ;;
      CRIT*) EXITSTATUS=$STATE_CRITICAL; ;;
   esac
   echo "${OUTPUT}"
   exit $EXITSTATUS
fi

# ############################################################################
# Documentation
# ############################################################################
: <<'DOCUMENTATION'
=pod

=head1 NAME

pmp-check-unix-memory - Alert on low memory.

=head1 SYNOPSIS

  Usage: pmp-check-unix-memory [OPTIONS]
  Options:
    -c CRIT     Critical threshold; default 95%.
    -w WARN     Warning threshold; default 90%.
    --help      Print help and exit.
    --version   Print version and exit.
  Options must be given as --option value, not --option=value or -Ovalue.
  Use perldoc to read embedded documentation with more details.

=head1 DESCRIPTION

This Nagios plugin examines C</proc/meminfo> (Linux) or the output of C<sysctl> (BSD)
to check whether the system is running out of memory and finds the largest
process in memory from C<ps> output.

The plugin is tested on GNU/Linux and FreeBSD.

=head1 PRIVILEGES

This plugin does not access MySQL.

This plugin executes the following UNIX commands that may need special privileges:

=over

=item *

cat /proc/meminfo (Linux), sysctl (BSD)

=item *

ps

=back

=head1 COPYRIGHT, LICENSE, AND WARRANTY

This program is copyright 2012-$CURRENT_YEAR$ Baron Schwartz, 2012-$CURRENT_YEAR$ Percona Inc.
Feedback and improvements are welcome.

THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, version 2.  You should have received a copy of the GNU General
Public License along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.

=head1 VERSION

$PROJECT_NAME$ pmp-check-unix-memory $VERSION$

=cut

DOCUMENTATION