#!/usr/bin/perl # # 2007-06-26 # Written by Ghost # # 2008-04-16 # Update: Wildcard version # # Requirements: "rcon" command written by [ASY]Zyrain # [ http://trog.qgl.org/browse/docs/code/hl2rcon.c ] # # Configuration variables # # srcdspass - RCON password # srcdsrcon - Path to rcon command (optional) # # Magic markers - optional - used by installation scripts and # munin-config: # #%# family=contrib #%# capabilities=autoconf use strict; my ($HOST, $PORT) = $0 =~ m/.*_([^:]+)_(\d+)$/; exit 1 if (!defined($HOST) || !defined($PORT)); my $PASS = $ENV{srcdspass} || ""; my $RCON = $ENV{srcdsrcon} || "/usr/local/bin/rcon"; my $SAMPLES = $ENV{srcdssamples} || 1; my $COMMAND = "$RCON -a$HOST -p$PORT -P$PASS stats"; my $arg = shift(); if ($arg eq 'config') { print_config(); exit(); } elsif ($arg eq 'autoconf') { unless (test_service() ) { print "yes\n"; } else { print "no\n"; } exit; } my $in_avg = 0; my @in_samples; my $out_avg = 0; my @out_samples; for (my $i = 0; $i < $SAMPLES; $i++) { my $statline; open (SERVICE, "$COMMAND |") or die ("Could not execute '$COMMAND': $!"); while() { $statline = $_; next if ($statline !~ m/\s*[\w+\.]+\s+[\w+\.]+\s+[\w+\.]+\s+\d+\s+\d+\s+[\w+\.]+\s+\d+/); my ($cpu, $in, $out, $uptime, $users, $fps, $players) = ($statline =~ m/^\s*([\w+\.]+)\s+([\w+\.]+)\s+([\w+\.]+)\s+(\d+)\s+(\d+)\s+([\w+\.]+)\s+(\d+)/); if (defined($in) && defined($out)) { $in = int($in); $out = int($out); push(@in_samples, $in); push(@out_samples, $out); } } select(undef, undef, undef, 0.2); # Wait moment before next sample } # MEAN if (@in_samples && @out_samples) { foreach (@in_samples) { $in_avg += int($_); } $in_avg /= ($#in_samples+1); $in_avg = int($in_avg); foreach (@out_samples) { $out_avg += int($_); } $out_avg /= ($#out_samples+1); $out_avg = int($out_avg); print "in.value $in_avg\n"; print "out.value $out_avg\n"; } sub print_config { my $num = 0; print("graph_title Server traffic at $HOST:$PORT\n", "graph_args --base 1000\n", "graph_vlabel bits in (-) / out (+) per second\n", "graph_category SourceDS\n", "graph_info The traffic of incoming and outgoing data from Source game server, such as HL2, CS:S and DoD:S.\n"); print ( "in.type GAUGE\n", "out.type GAUGE\n", "in.min 0\n", "out.min 0\n", "in.label bps\n", "out.label bps\n", "in.graph no\n", "in.cdef in,8,*\n", "out.cdef out,8,*\n", "out.negative in\n" ); } sub test_service { my $return = 1; system ("$RCON >/dev/null 2>/dev/null"); if ($? == 0) { system ("$COMMAND >/dev/null 2>/dev/null"); if ($? == 0) { print "yes\n"; $return = 0; } else { print "no (could not connect to server $HOST:$PORT, verify address and srcdspass)\n"; } } else { print "no (rcon command not found, see install instructions)\n"; } exit $return; }