munin-contrib/plugins/user/membyuser

63 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
#
# Plugin to monitor Memory usage inspired by cpubyuser
#
# Usage: Place in /etc/munin/plugins/ (or link it there using ln -s)
# Add this to your /etc/munin/plugin-conf.d/munin-node:
# [membyuser]
# user root # required if /proc can't be read from by any user!
## 2012-05-23 Sebastien Campion
# changed on 2019-08-30 by pcy <pcy@ulyssis.org>:
# - change category from 'memory' to 'system' (so it appears next to cpubyuser)
# - use rss instead of vsz
# - more robust username enumeration
USERS="$(ps ax --format uname | tail +2 | sort -u | grep -v -e '^root$')"
if [ "$1" = "autoconf" ]; then
if [ -n "$USERS" ]; then
echo "yes"
else
echo "\$USERS not defined."
fi
exit
fi
if [ "$1" = "config" ]; then
echo "graph_args --base 1024"
echo "graph_title Memory usage, by user"
echo "graph_category memory"
echo "graph_info This graph shows memory usage, for monitored users."
echo "graph_vlabel Bytes"
echo "graph_period second"
_USERS=${USERS//[-.]/_}
echo "graph_order $_USERS others"
FIRSTUSER=1;
for USER in $USERS "others"; do
_USER=${USER//[-.]/_}
echo "${_USER}.label $USER"
echo "${_USER}.info Memory used by user $USER"
echo "${_USER}.type GAUGE"
echo "${_USER}.draw AREASTACK"
done
exit
fi
ps -e -o rss,user | \
awk -v USERS="$USERS" '
{ if ($2 != "USER") MEM_USER[$2]+=$1 }
END {
others_sum = 0
for (user in MEM_USER) {
m = match(USERS,user)
if (m != 0) {
_user=user
gsub(/[-.]/,"_", _user);
print _user".value", (MEM_USER[user] * 1024)
} else
others_sum += (MEM_USER[user] * 1024)
}
print "others.value", others_sum;
}'