Nagios не включает plugin для мониторинга памяти. Вероятнее всего потому что нет единой команды для подсчета свободной памяти. Так как вся свободная память идет в буферы и в кэш
Логика скрипта:
free=`free -m | grep "buffers/cache" | awk '{print $4}'`
used=` free -m | grep "buffers/cache" | awk '{print $3}'`
total=$(($free+$used))
result=$(echo "$used / $total * 100" |bc -l|cut -c -2)
Полный листинг скрипта:
#!/bin/bash
#
# Script to check memory usage on Linux. Ignores memory used by disk cache.
#
# Requires the bc command
#
print_help() {
echo "Usage:"
echo "[-w] Warning level as a percentage"
echo "[-c] Critical level as a percentage"
exit 0
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit 0
;;
-w)
warn_level=$2
shift
;;
-c)
critical_level=$2
shift
;;
*)
echo "Unknown Argument: $1"
print_help
exit 3
;;
esac
shift
done
if [ "$warn_level" == "" ]; then
echo "No Warning Level Specified"
print_help
exit 3;
fi
if [ "$critical_level" == "" ]; then
echo "No Critical Level Specified"
print_help
exit 3;
fi
free=`free -m | grep "buffers/cache" | awk '{print $4}'`
used=` free -m | grep "buffers/cache" | awk '{print $3}'`
total=$(($free+$used))
result=$(echo "$used / $total * 100" |bc -l|cut -c -2)
if [ "$result" -lt "$warn_level" ]; then
echo "Memory OK. $result% used."
exit 0;
elif [ "$result" -ge "$warn_level" ] && [ "$result" -le "$critical_level" ]; then
echo "Memory WARNING. $result% used."
exit 1;
elif [ "$result" -gt "$critical_level" ]; then
echo "Memory CRITICAL. $result% used."
exit 2;
fi
Использование скрипта
# ./check_memory -w 50 -c 55 Memory OK. 33% used. # # ./check_memory -w 25 -c 20 Memory CRITICAL. 33% used.
Добавляем мониторинг памяти в Nagios
На сервере в Nagios hosts добавляем:
# Service definition
define service{
# Name of service template to use
use generic-service
host_name TEST
service_description LA
is_volatile 0
check_period 24x7
max_check_attempts 3
normal_check_interval 1
retry_check_interval 1
#contact_groups admins
notification_interval 120
notification_period 24x7
notification_options c,r
check_command check_nrpe!check_memory
}
Перезагружаем Nagios
# service nagios restart
На Машине клиенте добавляем данный скрипт в папку с плагинами Nagios
# vi /usr/lib/nagios/plugins/check_memory # Копируем скрипт # chmod 755 check_memory
В файле /etc/nagios/nrpe.cfg задаем параметры проверки warning и critical
command[check_memory]=/usr/lib/nagios/plugins/check_memory -w 85 -c 90
Перезагружаем nrpe
# service nrpe restart
Результат:
