Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
1533 richard 1
<?php
2
    //
3
    // vnStat PHP frontend (c)2006-2010 Bjorge Dijkstra (bjd@jooz.net)
4
    //
5
    // This program is free software; you can redistribute it and/or modify
6
    // it under the terms of the GNU General Public License as published by
7
    // the Free Software Foundation; either version 2 of the License, or
8
    // (at your option) any later version.
9
    //
10
    // This program is distributed in the hope that it will be useful,
11
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    // GNU General Public License for more details.
14
    //
15
    // You should have received a copy of the GNU General Public License
16
    // along with this program; if not, write to the Free Software
17
    // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
    //
19
    //
1675 richard 20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html
1533 richard 21
    // for more information.
22
    //
1675 richard 23
 
1533 richard 24
    //
25
    // Valid values for other parameters you can pass to the script.
26
    // Input parameters will always be limited to one of the values listed here.
27
    // If a parameter is not provided or invalid it will revert to the default,
28
    // the first parameter in the list.
29
    //
30
    if (isset($_SERVER['PHP_SELF']))
31
    {
32
	$script = $_SERVER['PHP_SELF'];
33
    }
34
    elseif (isset($_SERVER['SCRIPT_NAME']))
35
    {
36
	$script = $_SERVER['SCRIPT_NAME'];
37
    }
38
    else
39
    {
40
	die('can\'t determine script name!');
41
    }
1675 richard 42
 
1533 richard 43
    $page_list  = array('s','h','d','m');
1675 richard 44
 
1533 richard 45
    $graph_list = array('large','small','none');
1675 richard 46
 
1533 richard 47
    $page_title['s'] = T('summary');
48
    $page_title['h'] = T('hours');
49
    $page_title['d'] = T('days');
50
    $page_title['m'] = T('months');
51
 
1675 richard 52
 
1533 richard 53
    //
54
    // functions
55
    //
56
    function validate_input()
57
    {
58
        global $page,  $page_list;
59
        global $iface, $iface_list;
60
        global $graph, $graph_list;
61
	global $colorscheme, $style;
62
        //
63
        // get interface data
64
        //
65
        $page = isset($_GET['page']) ? $_GET['page'] : '';
66
        $iface = isset($_GET['if']) ? $_GET['if'] : '';
67
        $graph = isset($_GET['graph']) ? $_GET['graph'] : '';
68
        $style = isset($_GET['style']) ? $_GET['style'] : '';
69
 
70
        if (!in_array($page, $page_list))
71
        {
72
            $page = $page_list[0];
73
        }
74
 
75
        if (!in_array($iface, $iface_list))
76
        {
77
            $iface = $iface_list[0];
78
        }
79
 
80
        if (!in_array($graph, $graph_list))
81
        {
82
            $graph = $graph_list[0];
83
        }
84
 
85
	$tp = "./themes/$style";
1675 richard 86
        if (!is_dir($tp) || !file_exists("$tp/theme.php") || !preg_match('/^[a-z0-9-_]+$/i', $style))
1533 richard 87
        {
88
	    $style = DEFAULT_COLORSCHEME;
89
        }
90
    }
91
 
1675 richard 92
 
93
    function get_vnstat_data($use_label=true)
1533 richard 94
    {
95
        global $iface, $vnstat_bin, $data_dir;
96
        global $hour,$day,$month,$top,$summary;
97
 
2589 rexy 98
	$vnstat_data = array();
1533 richard 99
        if (!isset($vnstat_bin) || $vnstat_bin == '')
100
        {
101
	    if (file_exists("$data_dir/vnstat_dump_$iface"))
102
	    {
103
        	$vnstat_data = file("$data_dir/vnstat_dump_$iface");
1675 richard 104
	    }
1533 richard 105
        }
106
        else
107
        {
108
            $fd = popen("$vnstat_bin --dumpdb -i $iface", "r");
2589 rexy 109
            if (is_resource($fd))
110
            {
111
            	$buffer = '';
112
            	while (!feof($fd)) {
113
                	$buffer .= fgets($fd);
114
            	}
115
            	$vnstat_data = explode("\n", $buffer);
116
            	pclose($fd);
1533 richard 117
            }
118
        }
119
 
120
 
121
        $day = array();
122
        $hour = array();
123
        $month = array();
124
        $top = array();
125
 
2589 rexy 126
        if (isset($vnstat_data[0]) && strpos($vnstat_data[0], 'Error') !== false) {
1675 richard 127
          return;
128
        }
129
 
1533 richard 130
        //
131
        // extract data
132
        //
1675 richard 133
        foreach($vnstat_data as $line)
1533 richard 134
        {
135
            $d = explode(';', trim($line));
136
            if ($d[0] == 'd')
137
            {
138
                $day[$d[1]]['time']  = $d[2];
139
                $day[$d[1]]['rx']    = $d[3] * 1024 + $d[5];
140
                $day[$d[1]]['tx']    = $d[4] * 1024 + $d[6];
141
                $day[$d[1]]['act']   = $d[7];
1675 richard 142
                if ($d[2] != 0 && $use_label)
1533 richard 143
                {
144
                    $day[$d[1]]['label'] = strftime(T('datefmt_days'),$d[2]);
145
                    $day[$d[1]]['img_label'] = strftime(T('datefmt_days_img'), $d[2]);
146
                }
1675 richard 147
                elseif($use_label)
1533 richard 148
                {
149
                    $day[$d[1]]['label'] = '';
1675 richard 150
                    $day[$d[1]]['img_label'] = '';
151
                }
1533 richard 152
            }
153
            else if ($d[0] == 'm')
154
            {
155
                $month[$d[1]]['time'] = $d[2];
156
                $month[$d[1]]['rx']   = $d[3] * 1024 + $d[5];
157
                $month[$d[1]]['tx']   = $d[4] * 1024 + $d[6];
158
                $month[$d[1]]['act']  = $d[7];
1675 richard 159
                if ($d[2] != 0 && $use_label)
1533 richard 160
                {
161
                    $month[$d[1]]['label'] = strftime(T('datefmt_months'), $d[2]);
162
                    $month[$d[1]]['img_label'] = strftime(T('datefmt_months_img'), $d[2]);
163
                }
1675 richard 164
                else if ($use_label)
1533 richard 165
                {
166
                    $month[$d[1]]['label'] = '';
1675 richard 167
                    $month[$d[1]]['img_label'] = '';
1533 richard 168
                }
169
            }
170
            else if ($d[0] == 'h')
171
            {
172
                $hour[$d[1]]['time'] = $d[2];
173
                $hour[$d[1]]['rx']   = $d[3];
174
                $hour[$d[1]]['tx']   = $d[4];
175
                $hour[$d[1]]['act']  = 1;
1675 richard 176
                if ($d[2] != 0 && $use_label)
1533 richard 177
                {
178
                    $st = $d[2] - ($d[2] % 3600);
179
                    $et = $st + 3600;
180
                    $hour[$d[1]]['label'] = strftime(T('datefmt_hours'), $st).' - '.strftime(T('datefmt_hours'), $et);
181
                    $hour[$d[1]]['img_label'] = strftime(T('datefmt_hours_img'), $d[2]);
182
                }
1675 richard 183
                else if ($use_label)
1533 richard 184
                {
185
                    $hour[$d[1]]['label'] = '';
186
                    $hour[$d[1]]['img_label'] = '';
187
                }
188
            }
189
            else if ($d[0] == 't')
1675 richard 190
            {
1533 richard 191
                $top[$d[1]]['time'] = $d[2];
192
                $top[$d[1]]['rx']   = $d[3] * 1024 + $d[5];
193
                $top[$d[1]]['tx']   = $d[4] * 1024 + $d[6];
194
                $top[$d[1]]['act']  = $d[7];
1675 richard 195
                if($use_label)
196
                {
197
                    $top[$d[1]]['label'] = strftime(T('datefmt_top'), $d[2]);
198
                    $top[$d[1]]['img_label'] = '';
199
                }
1533 richard 200
            }
201
            else
202
            {
203
                $summary[$d[0]] = isset($d[1]) ? $d[1] : '';
204
            }
205
        }
1675 richard 206
 
1533 richard 207
        rsort($day);
208
        rsort($month);
209
        rsort($hour);
210
    }
211
?>