#!/bin/bash
#munin plugin for ram usage monitoring of minetestserver for given port
#author: Ritchie
#thanks Pitriss for advices
#inspired by plugin minecraft-users-ram_
#https://github.com/munin-monitoring/contrib/blob/master/plugins/minecraft/minecraft-users-ram_
#Author & instructions: http://wiki.natenom.name/minecraft/munin-plugin
LC_LANG=C
MT_PORT=${0##*_}
if [ "$1" = "config" ]
then
printf 'graph_title Minetestserver memory usage (Port %s)\n' ${MT_PORT}
printf 'graph_vlabel MiB\n'
printf 'ramusage.label memory usage\n'
printf 'graph_category minetest'
# printf 'graph_info Shows RAM usage by minetestserver process'
exit 0
fi
MT_PID=$(netstat -ulpn 2>/dev/null | grep ${MT_PORT} | sed -n -e '1p' | awk '{ print $6 }' | cut -d'/' -f1)
if [ ! -z "${MT_PID}" ]
then #is running
# MEMORYRSS=$(ps v ${MT_PID} | awk '{print $8}' | tail -n1)
MEMORYRSS=$(cat /proc/${MT_PID}/status | grep RSS | awk '{print $2}')
MEMMiB=$(echo "scale=2;${MEMORYRSS}/1024" | bc -l)
else
MEMMiB=0
fi
printf "ramusage.value %3.2f\n" "${MEMMiB}"
#debugging spatnyho syntaxu pro ps - byla spatne promenna MT_PID, protoze tento skript nebyl spousten rootem a netstat neukazal PID
#printf "MT_PID" "${MT_PID}"
#printf "MT_PORT" "${MT_PORT}"
#echo "MT_PID ${MT_PID}"
#echo "MT_PORT ${MT_PORT}"
#TEST=$(netstat -ulpn 2>/dev/null | grep ${MT_PORT})
#echo "${TEST}"