root / plugins / sourceds / srcds_uptime @ 8589c6df
Historique | Voir | Annoter | Télécharger (2,65 ko)
| 1 | 113ddc97 | Ghost | #!/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 | # |
||
| 16 | # Magic markers - optional - used by installation scripts and |
||
| 17 | # munin-config: |
||
| 18 | # |
||
| 19 | #%# family=contrib |
||
| 20 | #%# capabilities=autoconf |
||
| 21 | |||
| 22 | use strict; |
||
| 23 | |||
| 24 | # Set library path correctly |
||
| 25 | use File::Basename; |
||
| 26 | if (-l $0) {
|
||
| 27 | push(@INC, dirname(readlink($0))); |
||
| 28 | } |
||
| 29 | push(@INC, dirname($0)); |
||
| 30 | |||
| 31 | 8589c6df | klemens | # Load Rcon module or exit with failure message |
| 32 | 113ddc97 | Ghost | if (!eval "require Rcon") {
|
| 33 | print "Failed to load Rcon module. "; |
||
| 34 | print "Make sure Rcon.pm is copied to Munin plugins directory.\n"; |
||
| 35 | exit 1; |
||
| 36 | } |
||
| 37 | |||
| 38 | # Parse hostname and port from the plugin filename |
||
| 39 | my ($HOST, $PORT) = $0 =~ m/.*_([^:]+)_(\d+)$/; |
||
| 40 | if (!defined($HOST) || !defined($PORT)) {
|
||
| 41 | print "Could not parse server address from filename.\n"; |
||
| 42 | exit 1; |
||
| 43 | } |
||
| 44 | |||
| 45 | # Load config variables or use default values |
||
| 46 | my $PASS = $ENV{srcdspass} || "";
|
||
| 47 | |||
| 48 | # Print config or do plugin test if asked |
||
| 49 | my $arg = shift(); |
||
| 50 | if ($arg eq 'config') {
|
||
| 51 | print_config(); |
||
| 52 | } elsif ($arg eq 'autoconf') {
|
||
| 53 | test_service(); |
||
| 54 | } |
||
| 55 | |||
| 56 | |||
| 57 | # |
||
| 58 | # Main program starts here |
||
| 59 | # |
||
| 60 | |||
| 61 | my $sock = Rcon::sock_connect($HOST, $PORT); |
||
| 62 | if (!$sock) {
|
||
| 63 | print "Could not open socket to $HOST:$PORT.\n"; |
||
| 64 | exit 1; |
||
| 65 | } |
||
| 66 | if (!Rcon::rcon_auth($sock, $PASS)) {
|
||
| 67 | print "Could not authenticate.\n"; |
||
| 68 | exit 1; |
||
| 69 | } |
||
| 70 | |||
| 71 | my $reply = Rcon::rcon_command($sock, "stats"); |
||
| 72 | if (!defined($reply)) {
|
||
| 73 | print "Did not receive reply from server.\n"; |
||
| 74 | exit 1; |
||
| 75 | } |
||
| 76 | my @reply = split(/\n/, $reply); |
||
| 77 | |||
| 78 | foreach my $statline (@reply) {
|
||
| 79 | next if ($statline !~ m/\s*[\w+\.]+\s+[\w+\.]+\s+[\w+\.]+\s+\d+\s+\d+\s+[\w+\.]+\s+\d+/); |
||
| 80 | my ($cpu, $in, $out, $uptime, $users, $fps, $players) = ($statline =~ m/^\s*([\w+\.]+)\s+([\w+\.]+)\s+([\w+\.]+)\s+(\d+)\s+(\d+)\s+([\w+\.]+)\s+(\d+)/); |
||
| 81 | |||
| 82 | $uptime /= 60; # Change to hours |
||
| 83 | |||
| 84 | if (defined($uptime)) {
|
||
| 85 | print "uptime.value $uptime\n"; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | |||
| 90 | sub print_config {
|
||
| 91 | print("graph_title Game server uptime at $HOST:$PORT\n",
|
||
| 92 | "graph_args --base 1000\n", |
||
| 93 | "graph_vlabel Uptime (hours)\n", |
||
| 94 | eb100e33 | dipohl | "graph_category games\n", |
| 95 | 113ddc97 | Ghost | "graph_info The uptime for Source game server, such as HL2, CS:S and DoD:S.\n"); |
| 96 | |||
| 97 | print ("uptime.label Uptime\n",
|
||
| 98 | "uptime.min 0\n", |
||
| 99 | "uptime.type GAUGE\n"); |
||
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | sub test_service {
|
||
| 104 | my $sock = Rcon::sock_connect($HOST, $PORT); |
||
| 105 | if (!$sock) {
|
||
| 106 | print "no (could not open socket to $HOST:$PORT)\n"; |
||
| 107 | exit 1; |
||
| 108 | } |
||
| 109 | if (!Rcon::rcon_auth($sock, $PASS)) {
|
||
| 110 | print "no (could not authenticate)\n"; |
||
| 111 | exit 1; |
||
| 112 | } |
||
| 113 | if (!defined(Rcon::rcon_command($sock, "stats"))) {
|
||
| 114 | print "no (did not receive reply from server)\n"; |
||
| 115 | exit 1; |
||
| 116 | } |
||
| 117 | |||
| 118 | print "yes\n"; |
||
| 119 | exit 0; |
||
| 120 | } |
