#!/bin/bash

: <<=cut

=head1 NAME

minecraft-users-ram_ - monitor ressource usage of a local minecraft server

=head1 APPLICABLE SYSTEMS

Every system with a running minecraft server.


=head1 USAGE

Symlink this plugin to /etc/munin/plugins/ by adding the TCP port number used
by the local mincecraft server. Afterwards restart the munin-node.

Some more instructions: http://wiki.natenom.name/minecraft/munin-plugin


=head1 AUTHOR

  2011 Natenom
  2017 Leandro Späth


=head1 MAGIC MARKERS

  #%# family=manual
  #%# capabilities=

=cut


MC_PORT=${0##*_}

if [ "$1" = "config" ]
then
    echo "graph_title Minecraft-Server (Port $MC_PORT)"
    echo 'graph_category games'
    echo 'graph_scale no'
    echo 'graph_vlabel Players / RAM / CPU'
    echo 'users.label Players'
    echo 'ramusage.label RAM usage in GiB'
    echo 'cpuusage.label CPU usage'
    exit 0
fi

PLAYERCOUNT=$(LC_LANG=C netstat -tn | grep ":$MC_PORT " | grep ESTABLISHED | wc -l)
MC_PID=$(netstat -tlpn | grep ":$MC_PORT " | sed -n -e '1p' | awk '{ print $7 }' | cut -d '/' -f1)
if [ ! -z "${MC_PID}" ]
then #is running
    MEMGiB=$(ps -p "$MC_PID" -o rss | sed -n '2p' | awk '{ print $1 / 1024 / 1024 }')
    CPU=$(top -bp "$MC_PID" -n 1 | sed -n '$p' | awk '{ print $10 / 100; }')
else
    MEMGiB="U"
    CPU="U"
fi

echo "users.value $PLAYERCOUNT"
echo "ramusage.value $MEMGiB"
echo "cpuusage.value $CPU"
