From 113ddc970a3b5e9c8abc8956b01c5c4878ce9be0 Mon Sep 17 00:00:00 2001 From: Ghost Date: Wed, 27 Jun 2007 17:25:07 +0200 Subject: [PATCH] Initial version --- plugins/other/srcds_uptime | 120 +++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100755 plugins/other/srcds_uptime diff --git a/plugins/other/srcds_uptime b/plugins/other/srcds_uptime new file mode 100755 index 00000000..cb7cdb35 --- /dev/null +++ b/plugins/other/srcds_uptime @@ -0,0 +1,120 @@ +#!/usr/bin/perl +# +# 2007-06-26 +# Written by Ghost +# +# 2008-04-16 +# Update: Wildcard version +# +# 2008-11-12 +# Update: Perl RCON system +# +# Configuration variables +# +# srcdspass - RCON password +# +# Magic markers - optional - used by installation scripts and +# munin-config: +# +#%# family=contrib +#%# capabilities=autoconf + +use strict; + +# Set library path correctly +use File::Basename; +if (-l $0) { + push(@INC, dirname(readlink($0))); +} +push(@INC, dirname($0)); + +# Load Rcon module or exit with failuer message +if (!eval "require Rcon") { + print "Failed to load Rcon module. "; + print "Make sure Rcon.pm is copied to Munin plugins directory.\n"; + exit 1; +} + +# Parse hostname and port from the plugin filename +my ($HOST, $PORT) = $0 =~ m/.*_([^:]+)_(\d+)$/; +if (!defined($HOST) || !defined($PORT)) { + print "Could not parse server address from filename.\n"; + exit 1; +} + +# Load config variables or use default values +my $PASS = $ENV{srcdspass} || ""; + +# Print config or do plugin test if asked +my $arg = shift(); +if ($arg eq 'config') { + print_config(); +} elsif ($arg eq 'autoconf') { + test_service(); +} + + +# +# Main program starts here +# + +my $sock = Rcon::sock_connect($HOST, $PORT); +if (!$sock) { + print "Could not open socket to $HOST:$PORT.\n"; + exit 1; +} +if (!Rcon::rcon_auth($sock, $PASS)) { + print "Could not authenticate.\n"; + exit 1; +} + +my $reply = Rcon::rcon_command($sock, "stats"); +if (!defined($reply)) { + print "Did not receive reply from server.\n"; + exit 1; +} +my @reply = split(/\n/, $reply); + +foreach my $statline (@reply) { + 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+)/); + + $uptime /= 60; # Change to hours + + if (defined($uptime)) { + print "uptime.value $uptime\n"; + } +} + + +sub print_config { + print("graph_title Game server uptime at $HOST:$PORT\n", + "graph_args --base 1000\n", + "graph_vlabel Uptime (hours)\n", + "graph_category SourceDS\n", + "graph_info The uptime for Source game server, such as HL2, CS:S and DoD:S.\n"); + + print ("uptime.label Uptime\n", + "uptime.min 0\n", + "uptime.type GAUGE\n"); +} + + +sub test_service { + my $sock = Rcon::sock_connect($HOST, $PORT); + if (!$sock) { + print "no (could not open socket to $HOST:$PORT)\n"; + exit 1; + } + if (!Rcon::rcon_auth($sock, $PASS)) { + print "no (could not authenticate)\n"; + exit 1; + } + if (!defined(Rcon::rcon_command($sock, "stats"))) { + print "no (did not receive reply from server)\n"; + exit 1; + } + + print "yes\n"; + exit 0; +}