root / plugins / sourceds / srcds_inout @ eb100e33
Historique | Voir | Annoter | Télécharger (3,55 ko)
| 1 |
#!/usr/bin/perl |
|---|---|
| 2 |
# |
| 3 |
# 2007-06-26 |
| 4 |
# Written by Ghost |
| 5 |
# |
| 6 |
# 2008-04-16 |
| 7 |
# Update: Wildcard version |
| 8 |
# |
| 9 |
# 2008-11-12 |
| 10 |
# Update: Perl RCON system |
| 11 |
# |
| 12 |
# Configuration variables |
| 13 |
# |
| 14 |
# srcdspass - RCON password |
| 15 |
# srcdssamples - Number of samples to take (optional, default: 5) |
| 16 |
# |
| 17 |
# Magic markers - optional - used by installation scripts and |
| 18 |
# munin-config: |
| 19 |
# |
| 20 |
#%# family=contrib |
| 21 |
#%# capabilities=autoconf |
| 22 |
|
| 23 |
use strict; |
| 24 |
|
| 25 |
# Set library path correctly |
| 26 |
use File::Basename; |
| 27 |
if (-l $0) {
|
| 28 |
push(@INC, dirname(readlink($0))); |
| 29 |
} |
| 30 |
push(@INC, dirname($0)); |
| 31 |
|
| 32 |
# Load Rcon module or exit with failuer message |
| 33 |
if (!eval "require Rcon") {
|
| 34 |
print "Failed to load Rcon module. "; |
| 35 |
print "Make sure Rcon.pm is copied to Munin plugins directory.\n"; |
| 36 |
exit 1; |
| 37 |
} |
| 38 |
|
| 39 |
# Parse hostname and port from the plugin filename |
| 40 |
my ($HOST, $PORT) = $0 =~ m/.*_([^:]+)_(\d+)$/; |
| 41 |
if (!defined($HOST) || !defined($PORT)) {
|
| 42 |
print "Could not parse server address from filename.\n"; |
| 43 |
exit 1; |
| 44 |
} |
| 45 |
|
| 46 |
# Load config variables or use default values |
| 47 |
my $PASS = $ENV{srcdspass} || "";
|
| 48 |
my $SAMPLES = $ENV{srcdssamples} || 5;
|
| 49 |
|
| 50 |
# Print config or do plugin test if asked |
| 51 |
my $arg = shift(); |
| 52 |
if ($arg eq 'config') {
|
| 53 |
print_config(); |
| 54 |
} elsif ($arg eq 'autoconf') {
|
| 55 |
test_service(); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
# |
| 60 |
# Main program starts here |
| 61 |
# |
| 62 |
|
| 63 |
my $sock = Rcon::sock_connect($HOST, $PORT); |
| 64 |
if (!$sock) {
|
| 65 |
print "Could not open socket to $HOST:$PORT.\n"; |
| 66 |
exit 1; |
| 67 |
} |
| 68 |
if (!Rcon::rcon_auth($sock, $PASS)) {
|
| 69 |
print "Could not authenticate.\n"; |
| 70 |
exit 1; |
| 71 |
} |
| 72 |
|
| 73 |
my $in_avg = 0; |
| 74 |
my @in_samples; |
| 75 |
my $out_avg = 0; |
| 76 |
my @out_samples; |
| 77 |
|
| 78 |
for (my $i = 0; $i < $SAMPLES; $i++) {
|
| 79 |
my $reply = Rcon::rcon_command($sock, "stats"); |
| 80 |
if (!defined($reply)) {
|
| 81 |
print "Did not receive reply from server (sample $i).\n"; |
| 82 |
next; |
| 83 |
} |
| 84 |
my @reply = split(/\n/, $reply); |
| 85 |
|
| 86 |
foreach my $statline (@reply) {
|
| 87 |
next if ($statline !~ m/\s*[\w+\.]+\s+[\w+\.]+\s+[\w+\.]+\s+\d+\s+\d+\s+[\w+\.]+\s+\d+/); |
| 88 |
my ($cpu, $in, $out, $uptime, $users, $fps, $players) = ($statline =~ m/^\s*([\w+\.]+)\s+([\w+\.]+)\s+([\w+\.]+)\s+(\d+)\s+(\d+)\s+([\w+\.]+)\s+(\d+)/); |
| 89 |
|
| 90 |
if (defined($in) && defined($out)) {
|
| 91 |
$in = int($in); |
| 92 |
$out = int($out); |
| 93 |
push(@in_samples, $in); |
| 94 |
push(@out_samples, $out); |
| 95 |
} |
| 96 |
} |
| 97 |
select(undef, undef, undef, 0.2); # Wait moment before next sample |
| 98 |
} |
| 99 |
|
| 100 |
# MEAN |
| 101 |
if (@in_samples && @out_samples) {
|
| 102 |
foreach (@in_samples) {
|
| 103 |
$in_avg += int($_); |
| 104 |
} |
| 105 |
$in_avg /= ($#in_samples+1); |
| 106 |
$in_avg = int($in_avg); |
| 107 |
|
| 108 |
foreach (@out_samples) {
|
| 109 |
$out_avg += int($_); |
| 110 |
} |
| 111 |
$out_avg /= ($#out_samples+1); |
| 112 |
$out_avg = int($out_avg); |
| 113 |
|
| 114 |
print "in.value $in_avg\n"; |
| 115 |
print "out.value $out_avg\n"; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
sub print_config {
|
| 121 |
print("graph_title Server traffic at $HOST:$PORT\n",
|
| 122 |
"graph_args --base 1000\n", |
| 123 |
"graph_vlabel bits in (-) / out (+) per second\n", |
| 124 |
"graph_category games\n", |
| 125 |
"graph_info The traffic of incoming and outgoing data from Source game server, such as HL2, CS:S and DoD:S.\n"); |
| 126 |
|
| 127 |
print ( |
| 128 |
"in.type GAUGE\n", |
| 129 |
"out.type GAUGE\n", |
| 130 |
"in.min 0\n", |
| 131 |
"out.min 0\n", |
| 132 |
"in.label bps\n", |
| 133 |
"out.label bps\n", |
| 134 |
"in.graph no\n", |
| 135 |
"in.cdef in,8,*\n", |
| 136 |
"out.cdef out,8,*\n", |
| 137 |
"out.negative in\n" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
sub test_service {
|
| 143 |
my $sock = Rcon::sock_connect($HOST, $PORT); |
| 144 |
if (!$sock) {
|
| 145 |
print "no (could not open socket to $HOST:$PORT)\n"; |
| 146 |
exit 1; |
| 147 |
} |
| 148 |
if (!Rcon::rcon_auth($sock, $PASS)) {
|
| 149 |
print "no (could not authenticate)\n"; |
| 150 |
exit 1; |
| 151 |
} |
| 152 |
if (!defined(Rcon::rcon_command($sock, "stats"))) {
|
| 153 |
print "no (did not receive reply from server)\n"; |
| 154 |
exit 1; |
| 155 |
} |
| 156 |
|
| 157 |
print "yes\n"; |
| 158 |
exit 0; |
| 159 |
} |
