root / plugins / other / srcds_cpu @ 328129c0
Historique | Voir | Annoter | Télécharger (3,25 ko)
| 1 | 328129c0 | 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 | # 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 $cpu_avg = 0; |
||
| 74 | my @cpu_samples; |
||
| 75 | |||
| 76 | for (my $i = 0; $i < $SAMPLES; $i++) {
|
||
| 77 | my $reply = Rcon::rcon_command($sock, "stats"); |
||
| 78 | if (!defined($reply)) {
|
||
| 79 | print "Did not receive reply from server (sample $i).\n"; |
||
| 80 | next; |
||
| 81 | } |
||
| 82 | my @reply = split(/\n/, $reply); |
||
| 83 | |||
| 84 | foreach my $statline (@reply) {
|
||
| 85 | next if ($statline !~ m/\s*[\w+\.]+\s+[\w+\.]+\s+[\w+\.]+\s+\d+\s+\d+\s+[\w+\.]+\s+\d+/); |
||
| 86 | my ($cpu, $in, $out, $uptime, $users, $fps, $players) = ($statline =~ m/^\s*([\w+\.]+)\s+([\w+\.]+)\s+([\w+\.]+)\s+(\d+)\s+(\d+)\s+([\w+\.]+)\s+(\d+)/); |
||
| 87 | |||
| 88 | if (defined($cpu)) {
|
||
| 89 | push(@cpu_samples, $cpu); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | select(undef, undef, undef, 0.2); # Wait moment before next sample |
||
| 93 | } |
||
| 94 | |||
| 95 | # MEAN |
||
| 96 | if (@cpu_samples) {
|
||
| 97 | foreach (@cpu_samples) {
|
||
| 98 | $cpu_avg += int($_); |
||
| 99 | } |
||
| 100 | $cpu_avg /= ($#cpu_samples+1); |
||
| 101 | $cpu_avg = int($cpu_avg); |
||
| 102 | print "cpu.value $cpu_avg\n"; |
||
| 103 | } |
||
| 104 | |||
| 105 | # MEDIAN |
||
| 106 | #if (@cpu_samples) {
|
||
| 107 | # @cpu_samples = sort {$a <=> $b} @cpu_samples;
|
||
| 108 | # my $median = int($#cpu_samples / 2 + 0.5); |
||
| 109 | # $cpu_avg = $cpu_samples[$median]; |
||
| 110 | #} |
||
| 111 | |||
| 112 | |||
| 113 | sub print_config {
|
||
| 114 | print("graph_title Server CPU usage at $HOST:$PORT\n",
|
||
| 115 | "graph_args --base 1000\n", |
||
| 116 | "graph_vlabel CPU\n", |
||
| 117 | "graph_category SourceDS\n", |
||
| 118 | "graph_info The CPU usage of Source game server, such as HL2, CS:S and DoD:S.\n"); |
||
| 119 | |||
| 120 | print ("cpu.label CPU\n",
|
||
| 121 | "cpu.min 0\n", |
||
| 122 | "cpu.max 100\n", |
||
| 123 | "cpu.type GAUGE\n"); |
||
| 124 | |||
| 125 | exit 0; |
||
| 126 | } |
||
| 127 | |||
| 128 | |||
| 129 | sub test_service {
|
||
| 130 | my $sock = Rcon::sock_connect($HOST, $PORT); |
||
| 131 | if (!$sock) {
|
||
| 132 | print "no (could not open socket to $HOST:$PORT)\n"; |
||
| 133 | exit 1; |
||
| 134 | } |
||
| 135 | if (!Rcon::rcon_auth($sock, $PASS)) {
|
||
| 136 | print "no (could not authenticate)\n"; |
||
| 137 | exit 1; |
||
| 138 | } |
||
| 139 | if (!defined(Rcon::rcon_command($sock, "stats"))) {
|
||
| 140 | print "no (did not receive reply from server)\n"; |
||
| 141 | exit 1; |
||
| 142 | } |
||
| 143 | |||
| 144 | print "yes\n"; |
||
| 145 | exit 0; |
||
| 146 | } |
