Projet

Général

Profil

Révision c87e34be

IDc87e34beea0a49bfcba7cd25940c364e897b4736

Ajouté par Azelphur il y a plus de 13 ans

Added game module

Voir les différences:

plugins/game/game
1
#!/usr/bin/php
2
<?php
3

  
4
/*
5
 * README
6
 * This plugin makes use of the GameQ library to be able to query many different
7
 * types of game servers and display the results in munin. You can see a list of
8
 * supported games at http://gameq.sourceforge.net/#games
9
 *
10
 * This plugin has 2 environment variables:
11
 * game_config_file - Defaults to /etc/munin/munin-game.ini. The location of the config file
12
 * game_state_file - Defaults to /var/lib/munin/plugin-state/game. The location of the munin state directory
13
 *
14
 * (Probably) FAQ
15
 * Q: Why PHP?
16
 * A: The GameQ library is written in PHP, so it was either that or rewrite the entire library.
17
 *
18
 * Q: Why have you used a config file instead of environment variables?
19
 * A: Way too much to fit data inside an environment variable unfortunately. Imagine
20
 *    trying to fit information about 50 servers all inside one variable.
21
 *
22
 * Q: I want to ask you a question!
23
 * A: I'm on most IRC networks under the nick Azelphur, or email me. support@azelphur.com
24
 *
25
 * Q: It's not working!
26
 * A: Try running ./game autoconf, it'll probably tell you what's up.
27
 */
28

  
29
// Check environment variables
30
$var = getenv('game_config_file');
31
$config = ($var) ? $var : "/etc/munin/munin-game.ini";
32
$var = getenv('game_state_file');
33
$state = ($var) ? $var : "/var/lib/munin/plugin-state/game";
34

  
35
function p($str) {
36
    // Quick function to print a string with an EOL on the end
37
    echo $str . PHP_EOL;
38
}
39

  
40
function printMultigraph($ini_array, $machine_name, $title, $info, $max) {
41
    // Print out a standard graph config.
42
    p("multigraph $machine_name");
43
    p("graph_title $title");
44
    p("graph_vlabel players");
45
    p("graph_category gameserver");
46
    p("graph_info $info");
47
    p("graph_printf %6.0lf");
48
    
49
    if (isset($ini_array['settings'][$machine_name . '_colour']))
50
        p("players.colour " . $ini_array['settings'][$machine_name . '_colour']);
51

  
52
    if (isset($ini_array['settings'][$machine_name . '_draw']))
53
        p("players.draw " . $ini_array['settings'][$machine_name . '_draw']);
54

  
55
    p("players.label players");
56
    p("players.info Number of players");
57
    p("players.min 0");
58
    p("players.max $max");
59
}
60

  
61
function printValue($machine_name, $value) {
62
    // Print a value
63
    p("multigraph $machine_name");
64
    p("players.value " . $value);
65
}
66

  
67
function queryServers($ini_array) {
68
    // Query all game servers and return the results
69

  
70
    require "gameq/GameQ.php";
71

  
72
    // Populate the $servers array from the ini file, ready to pass to GameQ
73
    $servers = array();
74
    foreach ($ini_array as $section => $value) {
75
        if ($section != 'settings')
76
            $servers[$section] = array($value['game'], $value['address'], $value['port']);
77
    }
78

  
79
    // Create a new GameQ object and pass it a list of servers
80
    $gq = new GameQ();
81
    $gq->addServers($servers);
82

  
83
    // Set timeout from the config file
84
    $gq->setOption('timeout', $ini_array['settings']['timeout']);
85
    $gq->setOption('sock_count', $ini_array['settings']['sock_count']);
86
    $gq->setOption('sock_start', $ini_array['settings']['sock_start']);
87

  
88
    // This filter makes sure a subset of data is always available, next to the normal data
89
    $gq->setFilter('normalise');
90

  
91
    // Send request(s)
92
    $results = $gq->requestData();
93

  
94
    return $results;
95
}
96

  
97
// Parse command line arguments if required.
98
if (isset($_SERVER['argv'][1])) {
99
    if ($_SERVER['argv'][1] == 'config') {
100
        // Load our config.ini
101
        $ini_array = parse_ini_file($config, true);
102

  
103
        // Load games.ini so we can show pretty game names
104
        $games = parse_ini_file('gameq/GameQ/games.ini', true);
105

  
106
        // Query the game servers
107
        $results = queryServers($ini_array);
108
        // Cache the query in the state file
109
        $fp = fopen($state, 'w+') or die("I could not open state file."); 
110
        fwrite($fp, serialize($results)); 
111
        fclose($fp);
112

  
113

  
114
        // Loop through each server, printing graphs.
115
        foreach ($results as $name => $server) {
116
            // If sub graphs and the game total graphs are enabled, make this be a sub-graph.
117
            if ($ini_array['settings']['show_game_total'] && $ini_array['settings']['enable_sub_graphs'])
118
                $machine_name = "gameserver_" . $ini_array[$name]['game'] . ".$name";
119
            else
120
                $machine_name = "gameserver_" . $ini_array[$name]['game'] . "_$name";
121
            $title = $ini_array[$name]['name'] . " players";
122
            $info = "The number of players connected to the " . $games[$ini_array[$name]['game']]['name'] . " server";
123
            printMultigraph($ini_array, $machine_name, $title, $info, $server['gq_maxplayers']);
124
        }
125

  
126
        // Game total graphs.
127
        if ($ini_array['settings']['show_game_total']) {
128
            $game_total_max = array();
129

  
130
            // Count players connected to each game
131
            foreach ($results as $name => $server) {
132
                if (!isset($game_total_max[$ini_array[$name]['game']]))
133
                    $game_total_max[$ini_array[$name]['game']] = $server['gq_maxplayers'];
134
                else
135
                    $game_total_max[$ini_array[$name]['game']] += $server['gq_maxplayers'];
136
            }
137

  
138
            // Print all the game total graphs.
139
            foreach ($game_total_max as $game => $total)
140
            {
141
                $machine_name = "gameserver_" . $game;
142
                $title = "Total " . $games[$game]['name'] . " players";
143
                $info = "Total players connected to all " . $games[$game]['name'] . " servers";
144
                printMultigraph($ini_array, $machine_name, $title, $info, $total);
145
            }
146
        }
147

  
148
        // Global total graph
149
        if ($ini_array['settings']['show_global_total']) {
150
            $total_max = 0;
151
            // Add up all the players connected to all the servers
152
            foreach ($results as $name => $server) {
153
                $total_max += $server['gq_maxplayers'];
154
            }
155

  
156
            printMultigraph($ini_array, "gameserver", "Total Players", "Total players connected to all servers", $total_max);
157
        }
158
    }
159
    if ($_SERVER['argv'][1] == 'autoconf') {
160
        if (!@include("gameq/GameQ.php"))
161
            p("no (GameQ Library not found)");
162
        elseif (!file_exists($config))
163
            p("no ($config not found)");
164
        else
165
            p("yes");
166
    }
167
    die();
168
}
169

  
170
// No command line arguments, print values.
171

  
172
// Load our config.ini
173
$ini_array = parse_ini_file($config, true);
174

  
175
// Load games.ini so we can show pretty game names
176
$games = parse_ini_file('gameq/GameQ/games.ini', true);
177

  
178
$results = unserialize(file_get_contents($state)); 
179

  
180
// Print individual game values
181
foreach ($results as $name => $server){
182
    if ($ini_array['settings']['show_game_total'] && $ini_array['settings']['enable_sub_graphs'])
183
        $machine_name = "gameserver_" . $ini_array[$name]['game'] . ".$name";
184
    else
185
        $machine_name = "gameserver_" . $ini_array[$name]['game'] . "_$name";
186
    printValue($machine_name, $server['gq_numplayers']);
187
}
188

  
189
// Print game total values
190
if ($ini_array['settings']['show_game_total']) {
191
    $game_total = array();
192
    foreach ($results as $name => $server) {
193
        // Add up counts for the total graph
194
        if (!isset($game_total_max[$ini_array[$name]['game']]))
195
            $game_total[$ini_array[$name]['game']] = $server['gq_numplayers'];
196
        else
197
            $game_total[$ini_array[$name]['game']] += $server['gq_numplayers'];
198
    }
199
    foreach ($game_total as $game => $total)
200
    {
201
        $machine_name = "gameserver_" . $game;
202
        printValue($machine_name, $total);
203
    }
204
}
205

  
206
// Are global totals enabled?
207
if ($ini_array['settings']['show_global_total']) {
208
    $total = 0;
209
    foreach ($results as $name => $server) {
210
        $total += $server['gq_numplayers'];
211
    }
212
    printValue("gameserver", $total);
213
}
214

  
215

  
216
?>
plugins/game/munin-game.ini
1
[settings]
2
timeout = 1000                    ; Timeout in ms when attempting to connect to servers
3
sock_count = 64                   ; Maximum number of sockets that are used simultaneously
4
sock_start = 0                    ; Start of port range to use
5

  
6
; Note that changing any of the below options may change your graph names, which may reset your statistics.
7
show_global_total = True          ; Show a graph that shows the total players connected to all servers?
8
show_game_total = True            ; Show a graph that shows the total players connected to all servers of the same type?
9
enable_sub_graphs = True          ; Make each game be a sub-graph of it's game total graph.
10

  
11
; The following variables allow you to override the style of graphs.
12
; The server graphs are named gameserver_gamename.machinename, for example gameserver_tf2.myamazingserver
13
; The game total graphs are named gameserver_gamename, for example gameserver_tf2
14
; The global total graph is named gameserver.
15
;graph_name_colour = 000000        ; Override the color of graph_name, for example gameserver_et.myetserver_color
16
;graph_name_draw = LINE1          ; Override the draw method of graph_name, for example gameserver_
17

  
18
[mytf2server]                     ; Machine name of the server
19
name = "My TF2 Server"            ; Display name of the server
20
game = tf2                        ; Game type, see GameQ/games.ini inside the GameQ library for a list of valid options.
21
address = 1.2.3.4                 ; Server IP or hostname.
22
port = 27015                      ; Server port
23

  
24
[myetserver]
25
name = "My Enemy Territory Server"
26
game = et
27
address = 1.2.3.4
28
port = 27960

Formats disponibles : Unified diff