#!/bin/bash

#
# $Id: installmemmon.sh 21 2010-11-18 09:37:16Z deploy $
#

SCRIPT=/usr/local/bin/memmon.sh
LOG=/var/log/memmon.txt
cat << EOF > $SCRIPT
#!/bin/bash
date; uptime; free -m;
vmstat 1 5
ps axfww -o ruser,pid,%cpu,%mem,stat,time,cmd
if which iptables 2>&1 > /dev/null; then
  if [ -e /tmp/iptables_default ]; then
    iptables -L -n | diff /tmp/iptables_default - | awk '{print "IPTABLES: " \$0}'
  fi
  iptables -L -n > /tmp/iptables_default
fi
if [ -e /tmp/dmesg_default ]; then
	dmesg | diff -u /tmp/dmesg_default - | grep '^+' | awk '{print "DMESG:" \$0}'
fi
dmesg > /tmp/dmesg_default
EOF

chmod +x $SCRIPT

# create a cronjob that runs every few minutes to log the memory usage
echo "0-59/10 * * * * root $SCRIPT >> $LOG" > /etc/cron.d/memmon
/etc/init.d/cron* restart

# create a logrotate entry so the log file does not get too large
echo "$LOG {}" > /etc/logrotate.d/memmon

