Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / network / upnp_router_ @ 1bed50bb

Historique | Voir | Annoter | Télécharger (4,51 ko)

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Plugin: UPNP router
5
 * Author: Sebastian S. (scitor@@el337.de)
6
 * Version: 0.1 (TESTING)
7
 *
8
 * Munin (http://munin.projects.linpro.no/) plugin for monitoring traffic on UPNP enabled routers.
9
 *
10
 * Requirements:
11
 ***************
12
 * - PHP (translate it for performance gain)
13
 * - upnpc (MiniUPnP client: http://miniupnp.tuxfamily.org/)
14
 *
15
 * Usage:
16
 ********
17
 * Copy to upnp_router_ your munin plugins directory (/usr/share/munin/plugins) and execute:
18
 *
19
 * $ munin-node-configure --shell
20
 *
21
 * Don't forget to enable UPNP on your router, the script should find any connections automatically.
22
 * Change the path of your upnpc binary if you didn't make install it.
23
 *
24
 ********************
25
 * TESTING VERSION! *
26
 ********************
27
 * Please Note: This version was only tested on one single router so far.
28
 * I tried to make the router detection pretty general, but you never know.
29
 * If you have a router with enabled upnp not being detected by this script
30
 * feel free to send me an email with your output of:
31
 *
32
 * $ upnpc -s
33
 *
34
 * command.
35
 *
36
 * Changelog:
37
 ************
38
 * version 0.1 - 26 Aug, 2009
39
 * Sebastian S. <scitor@@el337.de>
40
 * - initial author
41
 *
42
 **/
43
#%# family=auto
44
#%# capabilities=autoconf suggest
45

    
46
        $result = explode("\n",shell_exec('/usr/bin/upnpc -s'));
47

    
48
        foreach($result as $line)
49
        {
50
                switch(true)
51
                {
52
                        case preg_match('%Found .* IGD : http://([^/]+):\d+(?:/[^/]+)*/([^/]+)\b%', $line,$regs):
53
                                $IGD = $regs[1].'_'.$regs[2];
54
                                $routers[$IGD]['ip'] = $regs[1];
55
                                $routers[$IGD]['name'] = $regs[2];
56
                         break;
57

    
58
                        case preg_match('/ExternalIPAddress = \b((?:[0-9]{1,3}\.){3}[0-9]{1,3})\b/', $line, $regs):
59
                                $routers[$IGD]['ext_ip'] = $regs[1];
60
                         break;
61

    
62
                        case preg_match('/Status : ([^,]+),/', $line, $regs):
63
                                $routers[$IGD]['status'] = $regs[1];
64
                         break;
65

    
66
                        case preg_match('/Bytes:\s*Sent:\s*(\d+)\s*Recv:\s*(\d+)\s*/', $line, $regs):
67
                                $routers[$IGD]['bytes_in']  = $regs[2];
68
                                $routers[$IGD]['bytes_out'] = $regs[1];
69
                         break;
70

    
71
                        case preg_match('/Packets:\s*Sent:\s*(\d+)\s*Recv:\s*(\d+)\s*/', $line, $regs):
72
                                $routers[$IGD]['pkts_in']  = $regs[2];
73
                                $routers[$IGD]['pkts_out'] = $regs[1];
74
                         break;
75
                }
76
        }
77

    
78
        $param_IGD = pathinfo($argv[0],PATHINFO_BASENAME);
79
        if (preg_match('/upnp_router_(bytes|pkts)_((?:[0-9]{1,3}\.){3}[0-9]{1,3})_(.+)/i', $param_IGD, $regs))
80
        {
81
                $param_IGD = $regs[2].'_'.$regs[3];
82
                $param_type = $regs[1];
83
        }
84

    
85
        switch($argv[1])
86
        {
87
                case 'suggest':
88
                        echo 'bytes_'.implode("\nbytes_",array_keys($routers))."\n";
89
                        echo 'pkts_'.implode("\npkts_",array_keys($routers))."\n";
90
                 break;
91

    
92
                case 'autoconf':
93
                        echo (count($routers)>0?'yes':'no (no routers found)')."\n";
94
                 break;
95

    
96
                case 'config':
97
        echo "graph_order down up\n";
98
        echo "graph_title UPNP $param_IGD $param_type traffic\n";
99
        echo "graph_args --base 1000\n";
100
        echo "graph_vlabel $param_type in (-) / out (+) per \${graph_period}\n";
101
        echo "graph_category network\n";
102
        echo "graph_info This graph shows the incoming and outgoing $param_type of the UPNP router found at $param_IGD.\n";
103
        echo "down.label received\n";
104
        echo "down.type COUNTER\n";
105
        echo "down.graph no\n";
106
        //echo "down.cdef down,8,*\n";
107
        echo "up.label bps\n";
108
        echo "up.type COUNTER\n";
109
        echo "up.negative down\n";
110
        //echo "up.cdef up,8,*\n";
111
                 break;
112

    
113
                default:
114
                        if (isset($routers[$param_IGD]))
115
                        {
116
                                echo 'up.value '.$routers[$param_IGD][$param_type.'_out']."\n";
117
                                echo 'down.value '.$routers[$param_IGD][$param_type.'_in']."\n";
118
                        }
119
                 break;
120
        }