Subversion Repositories ALCASAR

Rev

Go to most recent revision | 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
    //
20
    // see file COPYING or at http://www.gnu.org/licenses/gpl.html 
21
    // for more information.
22
    //
23
 
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
    }
42
 
43
    $page_list  = array('s','h','d','m');
44
 
45
    $graph_list = array('large','small','none');
46
 
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
 
52
 
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";
86
        if (!is_dir($tp) || !file_exists("$tp/theme.php"))
87
        {
88
	    $style = DEFAULT_COLORSCHEME;
89
        }
90
    }
91
 
92
 
93
    function get_vnstat_data()    
94
    {
95
        global $iface, $vnstat_bin, $data_dir;
96
        global $hour,$day,$month,$top,$summary;
97
 
98
        if (!isset($vnstat_bin) || $vnstat_bin == '')
99
        {
100
	    if (file_exists("$data_dir/vnstat_dump_$iface"))
101
	    {
102
        	$vnstat_data = file("$data_dir/vnstat_dump_$iface");
103
	    }	    
104
	    else
105
	    {
106
		$vnstat_data = array();
107
	    }
108
        }
109
        else
110
        {
111
            $fd = popen("$vnstat_bin --dumpdb -i $iface", "r");
112
            $buffer = '';
113
            while (!feof($fd)) {
114
                $buffer .= fgets($fd);
115
            }
116
            $vnstat_data = explode("\n", $buffer);
117
            pclose($fd);
118
        }
119
 
120
 
121
        $day = array();
122
        $hour = array();
123
        $month = array();
124
        $top = array();
125
 
126
        //
127
        // extract data
128
        //
129
        foreach($vnstat_data as $line) 
130
        {
131
            $d = explode(';', trim($line));
132
            if ($d[0] == 'd')
133
            {
134
                $day[$d[1]]['time']  = $d[2];
135
                $day[$d[1]]['rx']    = $d[3] * 1024 + $d[5];
136
                $day[$d[1]]['tx']    = $d[4] * 1024 + $d[6];
137
                $day[$d[1]]['act']   = $d[7];
138
                if ($d[2] != 0)
139
                {
140
                    $day[$d[1]]['label'] = strftime(T('datefmt_days'),$d[2]);
141
                    $day[$d[1]]['img_label'] = strftime(T('datefmt_days_img'), $d[2]);
142
                }
143
                else
144
                {
145
                    $day[$d[1]]['label'] = '';
146
                    $day[$d[1]]['img_label'] = '';          
147
                }           
148
            }
149
            else if ($d[0] == 'm')
150
            {
151
                $month[$d[1]]['time'] = $d[2];
152
                $month[$d[1]]['rx']   = $d[3] * 1024 + $d[5];
153
                $month[$d[1]]['tx']   = $d[4] * 1024 + $d[6];
154
                $month[$d[1]]['act']  = $d[7];
155
                if ($d[2] != 0)
156
                {
157
                    $month[$d[1]]['label'] = strftime(T('datefmt_months'), $d[2]);
158
                    $month[$d[1]]['img_label'] = strftime(T('datefmt_months_img'), $d[2]);
159
                }
160
                else
161
                {
162
                    $month[$d[1]]['label'] = '';
163
                    $month[$d[1]]['img_label'] = '';            
164
                }
165
            }
166
            else if ($d[0] == 'h')
167
            {
168
                $hour[$d[1]]['time'] = $d[2];
169
                $hour[$d[1]]['rx']   = $d[3];
170
                $hour[$d[1]]['tx']   = $d[4];
171
                $hour[$d[1]]['act']  = 1;
172
                if ($d[2] != 0)
173
                {
174
                    $st = $d[2] - ($d[2] % 3600);
175
                    $et = $st + 3600;
176
                    $hour[$d[1]]['label'] = strftime(T('datefmt_hours'), $st).' - '.strftime(T('datefmt_hours'), $et);
177
                    $hour[$d[1]]['img_label'] = strftime(T('datefmt_hours_img'), $d[2]);
178
                }
179
                else
180
                {
181
                    $hour[$d[1]]['label'] = '';
182
                    $hour[$d[1]]['img_label'] = '';
183
                }
184
            }
185
            else if ($d[0] == 't')
186
            {   
187
                $top[$d[1]]['time'] = $d[2];
188
                $top[$d[1]]['rx']   = $d[3] * 1024 + $d[5];
189
                $top[$d[1]]['tx']   = $d[4] * 1024 + $d[6];
190
                $top[$d[1]]['act']  = $d[7];
191
                $top[$d[1]]['label'] = strftime(T('datefmt_top'), $d[2]);
192
                $top[$d[1]]['img_label'] = '';
193
            }
194
            else
195
            {
196
                $summary[$d[0]] = isset($d[1]) ? $d[1] : '';
197
            }
198
        }
199
        if (count($day) == 0)
200
            $day[0] = 'nodata';
201
        rsort($day);
202
 
203
        if (count($month) == 0)
204
            $month[0] = 'nodata';
205
        rsort($month);
206
 
207
        if (count($hour) == 0)
208
            $hour[0] = 'nodata';
209
        rsort($hour);
210
    }
211
?>