HiMemoryMonitor

From campisano.org
Jump to navigation Jump to search
#!/bin/sh
#
# hiMemoryMonitor



# Var

EMAIL="email@server.whr";
SUBJECT="domain.whr hiMemoryMonitor";
MAXLOAD=80;



# Test

if test ! `which mail`
then
   echo "Command mail not found, exit.";
   exit 1;
fi;

if test ! `which bc`
then
   echo "Command bc not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test ! `which sed`
then
   echo "Command sed not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;

if test ! `which top`
then
   echo "Command top not found, exit." | mail -s "${SUBJECT} error." "${EMAIL}";
   exit 1;
fi;



# Monitor

MEMTOTAL=$(sed -n "s/^MemTotal:\s\+\([0-9]\+\) kB/\1/p" /proc/meminfo);
MEMFREE=$(sed -n "s/^MemFree:\s\+\([0-9]\+\) kB/\1/p" /proc/meminfo);
BUFFERS=$(sed -n "s/^Buffers:\s\+\([0-9]\+\) kB/\1/p" /proc/meminfo);
CACHED=$(sed -n "s/^Cached:\s\+\([0-9]\+\) kB/\1/p" /proc/meminfo);

LOAD=$(((MEMTOTAL-(MEMFREE + BUFFERS + CACHED))*100 / MEMTOTAL));

if test $(echo "${LOAD} > ${MAXLOAD}" | bc) -eq 1;
then
    top -b -n 1 | head -25 | mail -s "${SUBJECT} Alert! MEMORY load high: ${LOAD}" "${EMAIL}";
fi;



# End