dc7d050ff862f0331e9c2a7ece7fe2185e60f6c6
[linksys-wrt3g-tools.git] / scripts / munin.php
1 #!/usr/bin/env php
2 <?php
3 /**
4  * Munin node plugin to monitor the router status.
5  * Part of Linksys WRT3G tools
6  *
7  * PHP version 5
8  *
9  * @category Tools
10  * @package  linksys-wrt3g-tools
11  * @author   Christian Weiske <cweiske@cweiske.de>
12  * @license  AGPL v3
13  * @link     http://cweiske.de/linksys-wrt3g-tools.htm
14  * @link     http://munin-monitoring.org/wiki/ConcisePlugins
15  */
16 require_once 'Wrt3g.php';
17
18 $options = array();
19 foreach (array('host', 'user', 'password') as $variable) {
20     if (isset($_SERVER[$variable])) {
21         $options[$variable] = $_SERVER[$variable];
22     }
23 }
24
25 if (isset($argv[1]) && $argv[1] == 'autoconf') {
26     $router = new Wrt3g();
27     $router->loadConfig($options);
28     if ($router->config->host === null) {
29         echo "no (no host configured)\n";
30     } else {
31         echo "yes\n";
32     }
33     exit();
34
35 } else if (isset($argv[1]) && $argv[1] == 'config') {
36     echo <<<TXT
37 graph_title WRT3G router status
38 graph_args --base 1000 -l 0 --upper-limit 7
39 graph_vlabel Status values
40 graph_category network
41 graph_info This graph shows the status of a WRT3G router
42 conn_connected.label Connected
43 conn_connected.info Router is connected
44 conn_connected.draw LINE2
45 conn_connecting.label Connecting
46 conn_connecting.info Router is connecting
47 conn_connecting.draw LINE2
48 conn_disconnected.label Disconnected
49 conn_disconnected.info Router is disconnected
50 conn_disconnected.draw LINE2
51 notavailable.label Not available
52 notavailable.info Router cannot be reached
53 notavailable.draw LINE2
54 type_gprs.label GPRS
55 type_gprs.info Connection via GPRS
56 type_gprs.draw LINE2
57 type_umts.label UMTS
58 type_umts.info Connection via UMTS
59 type_umts.draw LINE2
60
61 TXT;
62     exit();
63
64 } else if (isset($argv[1])
65     && ($argv[1] == 'help' || $argv[1] == '--help' || $argv[1] == '-h')
66 ) {
67     echo <<<TXT
68 munin-node plugin to monitor a linksys wrt3g router.
69
70 Supports environment variables: host, user, password
71
72 Commands:
73   autoconf  - Check if the plugin would work
74   config    - Show munin config values
75   <nothing> - Print status for munin-node consumption
76
77 TXT;
78     exit();
79 }
80
81
82 try {
83     $router = new Wrt3g();
84     $router->loadConfig($options);
85
86     $arStatus = $router->getConnectionStatus();
87
88     $conn = $arStatus['connection'];
89     if ($conn == 'disconnected') {
90         echo "conn_disconnected.value 1\n";
91     } else if ($conn == 'connecting') {
92         echo "conn_connecting.value 2\n";
93     } else if ($conn == 'connected') {
94         echo "conn_connected.value 3\n";
95     }
96
97     $type = strtolower($arStatus['type']);
98     if ($type == 'gprs') {
99         echo "type_gprs.value 4.5\n";
100     } else if ($type == 'umts') {
101         echo "type_umts.value 5\n";
102     }
103 } catch (Exception $e) {
104     echo "notavailable 0.5\n";
105 }
106 ?>