Subversion Repositories ALCASAR

Rev

Rev 1675 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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