Skip to content

Instantly share code, notes, and snippets.

@johnmlang
Last active July 24, 2018 16:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnmlang/029478f1427b27395e42 to your computer and use it in GitHub Desktop.
Save johnmlang/029478f1427b27395e42 to your computer and use it in GitHub Desktop.
A collection of Munin plugins to monitor the Minecraft server and related services.
#!/bin/sh
## Munin plugin: CPU usage of Minecraft and Mark2 Processes
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Minecraft CPU Utilization
graph_category minecraft
graph_vlabel CPU Usage (%)
minecraft.label Minecraft
minecraft.info CPU utilization for the Minecraft server process.
minecraft.type GAUGE
mark2.label Mark2
mark2.info CPU utilization for the mark2 process.
mark2.type GAUGE
EOM
exit 0;;
esac
## Process IDs
MINECRAFT_PID=$( pgrep -f minecraft_server )
MARK2_PID=$( pgrep -f "mark2 start" )
## Plugin Output
echo -n "minecraft.value "
top -p ${MINECRAFT_PID} -b -n 1 | tail -n1 | awk '{ print $9 }'
echo -n "mark2.value "
top -p ${MARK2_PID} -b -n 1 | tail -n1 | awk '{ print $9 }'
#!/bin/sh
## Munin plugin: Overviewer map size
## URL to progress.json on map website
SIZE=http://map.survivalinc.info/size.txt
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Overviewer Map Size
graph_category minecraft
graph_info Disk space used by the Overviewer map.
graph_vlabel Size (bytes)
minecraft.label Survival Inc
minecraft.type GAUGE
EOM
exit 0;;
esac
## Plugin Output
echo -n "minecraft.value "
wget -qO - ${SIZE}
#!/bin/sh
## Munin plugin: Overviewer map render time
## URL to progress.json on map website
PROGRESS=http://map.survivalinc.info/progress.json
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Overviewer Rendering Time
graph_category minecraft
graph_info Time taken, in minutes, to render the Overviewer map.
graph_vlabel Time (minutes)
minecraft.label Survival Inc
minecraft.type GAUGE
EOM
exit 0;;
esac
## Plugin Output
echo -n "minecraft.value "
wget -qO - ${PROGRESS} | cut -d " " -f8 | cut -d \" -f1 | grep : | \
awk -F: '{ print ($1 * 60) + ($2) + ($3 / 60) }'
#!/bin/sh
## Munin plugin: Number of players connected to the Minecraft server
## Requires minecraft_query-cli.py from https://github.com/Dinnerbone/mcstatus
SERVER_IP=127.0.0.1
SERVER_PORT=25565
QUERY=/opt/mcstatus/minecraft_query-cli.py
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Minecraft Players Connected
graph_category minecraft
graph_vlabel Number of Players
graph_info Number of players connected to the Minecraft server.
minecraft.label Survival Inc
minecraft.type GAUGE
EOM
exit 0;;
esac
## Plugin Output
echo -n "minecraft.value "
python ${QUERY} -p ${SERVER_PORT} ${SERVER_IP} | grep numplayers | cut -c16- | \
cut -f 1 -d ,
#!/bin/sh
## Munin plugin: RAM usage of Minecraft and Mark2 Processes
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Minecraft Memory Utilization
graph_category minecraft
graph_vlabel RSS Usage (bytes)
minecraft.label Minecraft
minecraft.info RSS memory utilization for the Minecraft server process.
minecraft.type GAUGE
mark2.label Mark2
mark2.info RSS memory utilization for the mark2 process.
mark2.type GAUGE
EOM
exit 0;;
esac
## Process IDs
MINECRAFT_PID=$( pgrep -f minecraft_server )
MARK2_PID=$( pgrep -f "mark2 start" )
## Plugin Output
echo -n "minecraft.value "
ps -p ${MINECRAFT_PID} -o rss h | awk '{ print $1 * 1024 }'
echo -n "mark2.value "
ps -p ${MARK2_PID} -o rss h | awk '{ print $1 * 1024 }'
#!/bin/sh
## Munin plugin: Minecraft TPS (ticks per second)
## Run as the user that owns the minecraft server.
## Add the following to /etc/munin/plugin-conf.d/munin-node:
## [minecraft-tps]
## user minecraft
## Generate a debug report every 5 minutes, 1 minute before munin-update.
## Add the following to ${MINECRAFT}/scripts.txt
## 4-59/5 * * * * $mark2 send debug start; sleep 10; mark2 send debug stop
## Create folder to store auto-generated debug reports
## mkdir ${MINECRAFT}/tps
MINECRAFT=/home/minecraft/Survival-Inc
case $1 in
config)
cat <<'EOM'
graph_title Minecraft TPS
graph_category Minecraft
graph_vlabel ticks/second
tps.label TPS
EOM
exit 0;;
esac
## Plugin Output
## Print output in the format "${FIELDNAME}.value ${VALUE}"
#/usr/local/bin/mark2 send debug start
#sleep 10
#/usr/local/bin/mark2 send debug stop
#sleep 10
DEBUG=$( ls -t1 ${MINECRAFT}/debug | head -n 1 )
mv ${MINECRAFT}/debug/${DEBUG} ${MINECRAFT}/tps/
echo -n "tps.value "
grep "ticks per second" ${MINECRAFT}/tps/${DEBUG} | cut -d' ' -f5
#!/bin/sh
## Munin plugin: Minecraft user in-game time
## Path to usage script
#USAGEPATH=/opt/munin-minecraft-plugins/usage.sh
## Path to output of usage script
OUTPUT=/tmp/usage.log
## Run the script, this may be expensive...
#${USAGEPATH}
## An alternative would be to run the script on a longer interval with mark2 scripts.txt:
### 2-57/5 * * * * $/opt/munin-minecraft-plugins/usage.sh
case $1 in
config)
echo "graph_category minecraft"
echo "graph_info Time spent playing on the minecraft server during the last month"
echo "graph_title In-game time"
echo "graph_vlabel Time (min)"
for USER in $(awk -F" " '{ print $2 }' < ${OUTPUT}); do
echo "${USER}.label ${USER}"
echo "${USER}.type GAUGE"
done
exit 0
;;
esac
## Plugin Output
## Print output in the format "${FIELDNAME}.value ${VALUE}"
while IFS=" " read TIME USER; do
echo "${USER}.value ${TIME}"
done < ${OUTPUT}
#!/bin/sh
## Munin plugin: Size of Minecraft World
## Path to Minecraft world
WORLD=/home/minecraft/Survival-Inc/Skyline
## config: Tells Munin how to build the graph
case $1 in
config)
cat <<'EOM'
graph_title Minecraft World Size
graph_category minecraft
graph_info Disk space used by the Minecraft world.
graph_vlabel Size (bytes)
minecraft.label Survival Inc
minecraft.type GAUGE
EOM
exit 0;;
esac
## Plugin Output
echo -n "minecraft.value "
du ${WORLD} -sb | awk '{ print $1 }'
#!/bin/sh
## Munin plugin: speedtest
## http://digitalpardoe.co.uk/2013/04/29/monitoring-internet-connection-speed-with-munin
## https://github.com/sivel/speedtest-cli
OUTPUT=/tmp/speedtest.out
case $1 in
config)
cat <<'EOM'
graph_category network
graph_title Speedtest
graph_args --base 1000 -l 0
graph_vlabel Speed in Mbits/s / Time in ms
graph_scale no
down.label Download Speed
down.type GAUGE
down.draw LINE1
up.label Upload Speed
up.type GAUGE
up.draw LINE1
ping.label Ping Time
ping.type GAUGE
ping.draw LINE1
graph_info Graph of Internet Connection Speed and Ping Time
EOM
exit 0;;
esac
## Plugin Output
echo -n "down.value "
grep Download ${OUTPUT} | sed 's/[a-zA-Z:]* \([0-9]*\.[0-9]*\) [a-zA-Z/]*/\1/'
echo -n "up.value "
grep Upload ${OUTPUT} | sed 's/[a-zA-Z:]* \([0-9]*\.[0-9]*\) [a-zA-Z/]*/\1/'
echo -n "ping.value "
grep Ping ${OUTPUT} | sed 's/[a-zA-Z:]* \([0-9]*\.[0-9]*\) [a-zA-Z/]*/\1/'
#/bin/bash
## Script to calculate the how many minutes a user has been connected to the
## server in the past month.
## Base path to minecraft server
SERVERPATH="/path/to/server/"
## Paths to whitelist and server log
WHITELIST=${SERVERPATH}/white-list.txt
LOGDIR=${SERVERPATH}/logs
## Temporary file, will be deleted after running script
TEMPFILE=/tmp/usage-temp.log
## Path to save the list of users and connection times
OUTPUTLOG=/tmp/usage.log
## Remove the old connection log
rm ${OUTPUTLOG} 2> /dev/null
cd ${LOGDIR}
rm ${TEMPFILE} 2> /dev/null
for LOGFILE in $(find . -mtime -30 -type f | grep -v server | grep -v latest | sort); do
DATE=$(echo ${LOGFILE} | cut -c 3-12)
zcat ${LOGFILE} | awk -v DATE=${DATE} '$0=DATE $0' | grep 'joined the game\|left the game' | grep -v '<' | grep -v '*' >> ${TEMPFILE}
done
cat latest.log | awk -v DATE=${DATE} '$0=DATE $0' | grep 'joined the game\|left the game' | grep -v '<' | grep -v '*' >> ${TEMPFILE}
for USER in $(cat ${WHITELIST} ); do
grep -i ${USER} ${TEMPFILE} | awk -F" " '$5 ~ "joined" {s=$0;f=1;next} f && $5 ~ "left" {print s RS $0;f=0}' | sed ':a;N;s/joined the game\n/ /g' | awk -F" " '{ print $1 "+" $5 }' | tr [ T | tr -d ] >> ${TEMPFILE}-${USER}
## Begin counting at 0 seconds
TOTAL=0
## Iterate through the list of timestamp pair
for TIMESTAMPS in $(cat ${TEMPFILE}-${USER}); do
JOIN=$(echo "${TIMESTAMPS}" | awk -F"+" '{ print $1 }')
LEAVE=$(echo "${TIMESTAMPS}" | awk -F"+" '{ print $2 }')
## If both timestamps exist (always true?)...
if [ ! -z ${LEAVE} ]; then
## ... calculate the difference in seconds and add it to the total
SESSION=$(ddiff -f %S ${JOIN} ${LEAVE})
TOTAL=$((${SESSION}+${TOTAL}))
fi
done
## Convert total to minutes and round to the nearest minute
TOTAL=$( echo ${TOTAL} | awk '{ print ($1 / 60) }' | awk '{printf("%d\n",$1 + 0.5)}' )
## Append the total and username to the usage log
echo "${TOTAL} ${USER}" >> ${OUTPUTLOG}
rm ${TEMPFILE}-${USER} 2> /dev/null
done
## Sort the usage log by minutes in ascending order
sort -g -o ${OUTPUTLOG} ${OUTPUTLOG}
## Remove the temporary file
rm ${TEMPFILE} 2> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment