-
Notifications
You must be signed in to change notification settings - Fork 1
/
qstat_
139 lines (122 loc) · 3.07 KB
/
qstat_
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/php
<?php
/*
#################################################################
# Title : Qstat ping plugin for Munin v1.1
# Author : Håvard Pedersen
# Email : fuzzy76 @ fuzzy76 net
# Loosely based on the Qstat bash script plugin by Benjamin DUPUIS - Poil
#---------------------------------------------------------------#
# CHANGELOG
#
# v1.11 (3. july 2015)
# - Fixed PHP 5.4 compatibility (thanks to hannes)
#
# v1.1 (23. april 2010)
# - Added highest ping, lowest ping and query response time.
# - Now supports being run for testing with ./ in front
#
# v1.0 (initial release 22. april 2010)
# - Basic thing with average ping only
#
#%# family=manual
*/
$qstatloc = "/usr/bin/quakestat";
function doHelp() {
global $qstatloc;
echo "To test the script, just run qstatping_ GAMETYPE ADDRESS PORT
Run qstat to see the available gametypes (q3s, q4s, cods, etc)
To install for Munin you must ln -s /usr/share/munin/plugins/qstat_ /etc/munin/plugins/qstatping_GAMETYPE_ADDRESS_PORT
You might also have to set the path to qstat in $qstatloc
Have fun!
";
}
function doConfig() {
global $argv;
if ($argv[0] != "qstatping_") {
$parts = explode("_",$argv[0]);
$gametype = $parts[1];
$ip = $parts[2];
$port = $parts[3];
} else {
$gametype = $argv[1];
$ip = $argv[2];
$port = $argv[3];
}
echo "graph_title Ping on $gametype at $ip:$port
graph_vlabel Ping
graph_category games
minping.label Minimum ping
maxping.label Maximum ping
avgping.label Average ping
querytime.label Query time
";
}
function doFetch() {
global $argv, $qstatloc;
if (substr($argv[0],-(strlen("qstatping_"))) != "qstatping_") {
$parts = explode("_",$argv[0]);
$gametype = $parts[1];
$ip = $parts[2];
$port = $parts[3];
} else {
$gametype = $argv[1];
$ip = $argv[2];
$port = $argv[3];
}
$querytime = "U";
$output = array();
if ($gametype && $ip && $port) {
$time_start = microtime(true);
exec("$qstatloc -raw \";\" -nh -P -$gametype $ip:$port", $output);
$time_end = microtime(true);
$querytime = ($time_end - $time_start) * 1000;
array_shift($output);
$pingavg = 0;
$pingmax = -1;
$pingmin = 9999;
$playerscount = 0;
foreach ($output as $line) {
$player = explode(";", $line);
if (count($player) >= 3 && $player[2] != 999) {
if ($player[2] > $pingmax)
$pingmax = $player[2];
if ($player[2] < $pingmin)
$pingmin = $player[2];
$pingavg += $player[2];
$playerscount++;
}
}
if ($playerscount != 0)
$pingavg = $pingavg / $playerscount;
if ($playerscount != 0) {
echo "minping.value $pingmin
maxping.value $pingmax
avgping.value $pingavg
querytime.value $querytime
";
return;
}
}
echo "minping.value U
maxping.value U
avgping.value U
querytime.value $querytime
";
}
switch ($argv[1]) {
case "config":
doConfig();
break;
case "help":
case "?":
doHelp();
break;
case "autoconf":
echo "no (edit the script to set path to qstat)";
break;
case "fetch":
default:
doFetch();
}
?>