openwrt-munin-node/plugins.d/memory

129 lines
6.1 KiB
Bash
Executable File

#!/bin/sh
# this is just a complete fucking mess i tried my best to fix it
MINFO=`/bin/cat /proc/meminfo|/bin/sed 's/ \{1,\}/ /g;'`
MEMFREE=`echo "$MINFO"|/bin/grep "^MemFree:" |/usr/bin/awk '{print $2}'`
BUFFERS=`echo "$MINFO"|/bin/grep "^Buffers:" |/usr/bin/awk '{print $2}'`
CACHED=`echo "$MINFO"|/bin/grep "^Cached:" |/usr/bin/awk '{print $2}'`
SWAP_TOTAL=`echo "$MINFO"|/bin/grep "^SwapTotal:" |/usr/bin/awk '{print $2}'`
SWAP_FREE=`echo "$MINFO"|/bin/grep "^SwapFree:" |/usr/bin/awk '{print $2}'`
MEMTOTAL=`echo "$MINFO"|/bin/grep "^MemTotal:" |/usr/bin/awk '{print $2}'`
PAGETABLES=`echo "$MINFO"|/bin/grep "^PageTables:" |/usr/bin/awk '{print $2}'`
SWAPCACHED=`echo "$MINFO"|/bin/grep "^SwapCached:" |/usr/bin/awk '{print $2}'`
SWAPTOTAL=`echo "$MINFO"|/bin/grep "^SwapTotal:" |/usr/bin/awk '{print $2}'`
VMALLOCUSED=`echo "$MINFO"|/bin/grep "^VmallocUsed:" |/usr/bin/awk '{print $2}'`
SLAB=`echo "$MINFO"|/bin/grep "^Slab:" |/usr/bin/awk '{print $2}'`
MAPPED=`echo "$MINFO"|/bin/grep "^Mapped:" |/usr/bin/awk '{print $2}'`
COMMITTEDAS=`echo "$MINFO"|/bin/grep "^Committed_AS:" |/usr/bin/awk '{print $2}'`
ACTIVE=`echo "$MINFO"|/bin/grep "^Active:" |/usr/bin/awk '{print $2}'`
INACTIVE=`echo "$MINFO"|/bin/grep "^Inactive:" |/usr/bin/awk '{print $2}'`
ACTIVEANON=`echo "$MINFO"|/bin/grep "^ActiveAnon:" |/usr/bin/awk '{print $2}'`
ACTIVECACHE=`echo "$MINFO"|/bin/grep "^ActiveCache:" |/usr/bin/awk '{print $2}'`
INACTIVE=`echo "$MINFO"|/bin/grep "^Inactive:" |/usr/bin/awk '{print $2}'`
INACTDIRTY=`echo "$MINFO"|/bin/grep "^Inact_dirty:" |/usr/bin/awk '{print $2}'`
INACTLAUNDY=`echo "$MINFO"|/bin/grep "^Inact_laundry:"|/usr/bin/awk '{print $2}'`
INACTCLEAN=`echo "$MINFO"|/bin/grep "^Inact_clean:" |/usr/bin/awk '{print $2}'`
APPS=$(($MEMTOTAL - $MEMFREE - $BUFFERS - $CACHED))
SWAP=$(($SWAP_TOTAL - $SWAP_FREE))
if [ "$1" == "config" ];then
GRAPH_ORDER="apps";
test "$PAGETABLES" != "" && GRAPH_ORDER="$GRAPH_ORDER page_tables"
test "$SWAPCACHED" != "" && GRAPH_ORDER="$GRAPH_ORDER swap_cache"
test "$VMALLOCUSED" != "" && GRAPH_ORDER="$GRAPH_ORDER vmalloc_used"
test "$SLAB" != "" && GRAPH_ORDER="$GRAPH_ORDER slab"
GRAPH_ORDER="$GRAPH_ORDER cached buffers free swap"
echo "graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit $MEMTOTAL
graph_title Memory usage
graph_category system
graph_info This graph shows what the machine uses its memory for.
graph_order $GRAPH_ORDER
apps.label apps
apps.draw AREA
apps.info Memory used by user-space applications.
buffers.label buffers
buffers.draw STACK
buffers.info Block device (e.g. harddisk) cache. Also where 'dirty' blocks are stored until written.
swap.label swap
swap.draw STACK
swap.info Swap space used.
cached.label cache
cached.draw STACK
cached.info Parked file data (file content) cache.
free.label unused
free.draw STACK
free.info Wasted memory. Memory that is not used for anything at all."
[ ! -z "$SLAB" ] && echo "slab.label slab_cache
slab.draw STACK
slab.info Memory used by the kernel (major users are caches like inode, dentry, etc)."
[ ! -z "$SWAPCACHED" ] && echo "swap_cache.label swap_cache
swap_cache.draw STACK
swap_cache.info A piece of memory that keeps track of pages that have been fetched from swap but not yet been modified."
[ ! -z "$PAGETABLES" ] && echo "page_tables.label page_tables
page_tables.draw STACK
page_tables.info Memory used to map between virtual and physical memory addresses."
[ ! -z "$VMALLOCUSED" ] && echo "vmalloc_used.label vmalloc_used
vmalloc_used.draw STACK
vmalloc_used.info Virtual memory used by the kernel (used when the memory does not have to be physically contigious)."
[ ! -z "$COMMITTEDAS" ] && echo "committed.label committed
committed.draw LINE2
committed.warn $(($SWAPTOTAL + $MEMTOTAL))
committed.info The amount of memory that would be used if all the memory that's been allocated were to be used."
[ ! -z "$MAPPED" ] && echo "mapped.label mapped
mapped.draw LINE2
mapped.info All mmap()ed pages."
[ ! -z "$ACTIVE" ] && echo "active.label active
active.draw LINE2
active.info Memory recently used. Not reclaimed unless absolutely necessary."
[ ! -z "$ACTIVEANON" ] && echo "active_anon.label active_anon
active_anon.draw LINE1"
[ ! -z "$ACTIVECACHE" ] && echo "active_cache.label active_cache
active_cache.draw LINE1"
[ ! -z "$INACTIVE" ] && echo "inactive.label inactive
inactive.draw LINE2
inactive.info Memory not currently used."
[ ! -z "$INACTDIRTY" ] && echo "inact_dirty.label inactive_dirty
inact_dirty.draw LINE1
inact_dirty.info Memory not currently used, but in need of being written to disk."
[ ! -z "$INACTLAUNDRY" ] && echo "inact_laundry.label inactive_laundry
inact_laundry.draw LINE1"
[ ! -z "$INACTCLEAN" ] && echo "inact_clean.label inactive_clean
inact_clean.draw LINE1
inact_clean.info Memory not currently used."
exit 0
fi
echo "buffers.value $(($BUFFERS * 1024))
swap.value $(($SWAP * 1024))
cached.value $(($CACHED * 1024))
free.value $(($MEMFREE * 1024))"
[ ! -z "$SLAB" ] && echo "slab.value $(($SLAB * 1024))" && APPS=$(($APPS - $SLAB))
[ ! -z "$SWAPCACHED" ] && echo "swap_cache.value $(($SWAPCACHED * 1024))" && APPS=$(($APPS - $SWAPCACHED))
[ ! -z "$PAGETABLES" ] && echo "page_tables.value $(($PAGETABLES * 1024))" && APPS=$(($APPS - $PAGETABLES))
[ ! -z "$VMALLOCUSED" ] && echo "vmalloc_used.value $(($VMALLOCUSED * 1024))" && APPS=$(($APPS - $VMALLOCUSED))
[ ! -z "$COMMITTEDAS" ] && echo "committed.value $(($COMMITTEDAS * 1024))"
[ ! -z "$MAPPED" ] && echo "mapped.value $(($MAPPED * 1024))"
[ ! -z "$ACTIVE" ] && echo "active.value $(($ACTIVE * 1024))"
[ ! -z "$ACTIVEANON" ] && echo "active_anon.value $(($ACTIVEANON * 1024))"
[ ! -z "$ACTIVECACHE" ] && echo "active_cache.value $(($ACTIVECACHE * 1024))"
[ ! -z "$INACTIVE" ] && echo "inactive.value $(($INACTIVE * 1024))"
[ ! -z "$INACTDIRTY" ] && echo "inact_dirty.value $(($INACTDIRTY * 1024))"
[ ! -z "$INACTLAUNDRY" ] && echo "inact_laundry.value $(($INACTLAUNDRY * 1024))"
[ ! -z "$INACTCLEAN" ] && echo "inact_clean.value $(($INACTCLEAN * 1024))"
echo "apps.value $(($APPS * 1024))"