Subversion Repositories ALCASAR

Rev

Rev 2191 | Rev 2266 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
324 richard 1
<?php
2191 tom.houday 2
# $Id: stats.php 2229 2017-05-15 11:46:48Z richard $
3
 
324 richard 4
require('/etc/freeradius-web/config.php');
5
require('../lib/sql/nas_list.php');
6
require_once('../lib/xlat.php');
7
?>
8
<html>
9
<head>
10
<title>Analyse des comptes</title>
2191 tom.houday 11
<meta http-equiv="Content-Type" content="text/html; charset=<?= $config['general_charset'] ?>">
324 richard 12
<link rel="stylesheet" href="style.css">
13
</head>
14
<body>
15
<center>
16
 
17
<?php
18
require_once('../lib/functions.php');
19
 
20
if (is_file("../lib/sql/drivers/$config[sql_type]/functions.php"))
21
	include_once("../lib/sql/drivers/$config[sql_type]/functions.php");
22
else{
23
	echo <<<EOM
24
<b>Could not include SQL library functions. Aborting</b>
25
</body>
26
</html>
27
EOM;
28
	exit();
29
}
30
 
31
$stats_num = array();
32
 
33
$date = strftime('%A, %e %B %Y, %T %Z');
34
$now = time();
35
 
1831 raphael.pi 36
if (!isset($before))
37
{
38
	$before = date($config['sql_date_format'], $now + 86400);
39
}
40
if (!isset($after))
41
{
42
	$after = date($config['sql_date_format'], $now - 604800 );
43
}
44
$after_time = strtotime($after);
45
$before_time = strtotime($before);
324 richard 46
$days[0] = $after;
47
$counter = $after_time + 86400;
48
$i = 1;
49
while($counter < $before_time){
1831 raphael.pi 50
	$days[$i++] = date($config['sql_date_format'],$counter);	
324 richard 51
	$counter += 86400;
52
}
53
$days[$i] = $before;
54
$num_days = $i;
55
 
1831 raphael.pi 56
$column1 = (isset($column1)) ? "$column1" : 'sessions';
324 richard 57
$column[1] = "$column1";
1831 raphael.pi 58
$selected1["$column1"] = 'selected';
59
 
60
$column2 = (isset($column2)) ? "$column2" : 'usage';
324 richard 61
$column[2] = "$column2";
1831 raphael.pi 62
$selected2["$column2"] = 'selected';
63
 
64
$column3 = (isset($column3)) ? "$column3" : 'download';
324 richard 65
$column[3] = "$column3";
66
$selected3["$column3"] = 'selected';
67
 
68
$message['sessions'] = 'sessions';
69
$message['usage'] = 'total usage time';
70
$message['usage'] = 'temps d\'utilisation total ';
71
$message['upload'] = 'uploads';
72
$message['download'] = 'downloads';
1831 raphael.pi 73
if ($config['general_stats_use_totacct'] == 'yes'){
324 richard 74
	$sql_val['sessions'] = 'connnum';
75
	$sql_val['usage'] = 'conntotduration';
2229 richard 76
	$sql_val['upload'] = 'outputoctets'; # invert with 'inputoctets' in order to display it more logically
77
	$sql_val['download'] = 'intputoctets';
324 richard 78
}
79
else{
80
	$sql_val['usage'] = 'acctsessiontime';
81
	$sql_val['upload'] = 'acctinputoctets';
82
	$sql_val['download'] = 'acctoutputoctets';
83
}
1831 raphael.pi 84
$fun['sessions'] = 'nothing';
85
$fun['usage'] = 'time2strclock';
86
$fun['upload'] = 'bytes2str';
87
$fun['download'] = 'bytes2str';
88
$sql_val['user'] = (!isset($login) || $login == '' ) ? "WHERE username LIKE '%'" : "WHERE username = '$login'";
324 richard 89
for ($j = 1; $j <= 3; $j++){
90
	$tmp = "{$sql_val[$column[$j]]}";
1831 raphael.pi 91
	$res[$j] = (!isset($tmp)) ? "COUNT(radacctid) AS res_$j" : "sum($tmp) AS res_$j";
324 richard 92
}
93
$i = 1;
1831 raphael.pi 94
$servers['all'] = 'all';
324 richard 95
foreach ($nas_list as $nas){
1831 raphael.pi 96
	$name = $nas['name'];
97
	if ($nas['ip'] == '')
324 richard 98
		continue;
1831 raphael.pi 99
	$servers[$name] = $nas['ip'];
324 richard 100
	$i++;
101
}
102
ksort($servers);
1831 raphael.pi 103
if(isset($server))
104
{
105
	if ($server != 'all' && $server != ''){
106
		$server = da_sql_escape_string($link,$server);
107
		$s = "AND nasipaddress = '$server'";
108
	}
324 richard 109
}
110
$sql_extra_query = '';
1831 raphael.pi 111
if (isset($config['sql_accounting_extra_query']))
112
{
113
	$sql_extra_query = xlat($config['sql_accounting_extra_query'],$login,$config);
114
}
115
else
116
{
117
	$sql_extra_query = "";
118
}
324 richard 119
 
120
$link = @da_sql_pconnect($config);
1831 raphael.pi 121
if ($link)
122
{
123
	$data['sum'] = array();
124
	$data['sum'][1] = "";
125
	$data['sum'][2] = "";
126
	$data['sum'][3] = "";
127
 
128
	$stats_num[1] = "";
129
	$stats_num[2] = "";
130
	$stats_num[3] = "";
131
 
324 richard 132
	for ($i = $num_days;$i > -1; $i--){
133
		$day = "$days[$i]";
1831 raphael.pi 134
		if ($config['general_stats_use_totacct'] == 'yes')
324 richard 135
			$search = @da_sql_query($link,$config,
136
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_total_accounting_table]
137
			$sql_val[user] AND acctdate = '$day' $s $sql_extra_query;");
138
		else
139
			$search = @da_sql_query($link,$config,
140
			"SELECT $res[1],$res[2],$res[3] FROM $config[sql_accounting_table]
141
			$sql_val[user] AND acctstoptime >= '$day 00:00:00' 
142
			AND acctstoptime <= '$day 23:59:59' $s $sql_extra_query;");
143
		if ($search){
144
			$row = @da_sql_fetch_array($search,$config);
1831 raphael.pi 145
			$data[$day][1] = $row['res_1'];
146
			$data['sum'][1] += $row['res_1'];
324 richard 147
			$stats_num[1] = ($data[$day][1]) ? $stats_num[1] + 1 : $stats_num[1];
1831 raphael.pi 148
			$data[$day][2] = $row['res_2'];
149
			$data['sum'][2] += $row['res_2'];
324 richard 150
			$stats_num[2] = ($data[$day][2]) ? $stats_num[2] + 1 : $stats_num[2];
1831 raphael.pi 151
			$data[$day][3] = $row['res_3'];
152
			$data['sum'][3] += $row['res_3'];
324 richard 153
			$stats_num[3] = ($data[$day][3]) ? $stats_num[3] + 1 : $stats_num[3];
154
		}
155
		else
156
			echo "<b>Database query failed: " . da_sql_error($link,$config) . "</b><br>\n";
157
	}
158
}
159
else
160
	echo "<b>Could not connect to SQL database</b><br>\n";
161
 
162
$stats_num[1] = ($stats_num[1]) ? $stats_num[1] : 1;
163
$stats_num[2] = ($stats_num[2]) ? $stats_num[2] : 1;
164
$stats_num[3] = ($stats_num[3]) ? $stats_num[3] : 1;
165
 
166
$data['avg'][1] = ceil($data['sum'][1] / $stats_num[1]);
167
$data['avg'][2] = ceil($data['sum'][2] / $stats_num[2]);
168
$data['avg'][3] = ceil($data['sum'][3] / $stats_num[3]);
169
 
170
$data['avg'][1] = $fun[$column[1]]($data['avg'][1]);
171
$data['avg'][2] = $fun[$column[2]]($data['avg'][2]);
172
$data['avg'][3] = $fun[$column[3]]($data['avg'][3]);
173
 
174
$data['sum'][1] = $fun[$column[1]]($data['sum'][1]);
175
$data['sum'][2] = $fun[$column[2]]($data['sum'][2]);
176
$data['sum'][3] = $fun[$column[3]]($data['sum'][3]);
177
 
1831 raphael.pi 178
$max[1] = 0;
179
$max[2] = 0;
180
$max[3] = 0;
181
 
324 richard 182
for ($i = 0; $i <= $num_days; $i++){
183
	$day = "$days[$i]";
184
	$max[1] = ($max[1] > $data[$day][1] ) ? $max[1] : $data[$day][1];
185
	$max[2] = ($max[2] > $data[$day][2] ) ? $max[2] : $data[$day][2];
186
	$max[3] = ($max[3] > $data[$day][3] ) ? $max[3] : $data[$day][3];
187
 
188
}
189
for ($i = 0; $i <= $num_days; $i++){
190
	$day = "$days[$i]";
191
	for ($j = 1; $j <= 3; $j++){
192
		$tmp = $data[$day][$j];
193
		if (!$max[$j])
194
			$p = $w = $c = 0;
195
		else{
196
			$p = floor(100 * ($tmp / $max[$j]));
197
			$w = floor(70 * ($tmp / $max[$j]));
198
			$c = hexdec('f0e9e2') - (258 * $p);
199
			$c = dechex($c);
200
		}
201
		if (!$w)
202
			$w++;
203
		$perc[$day][$j] = $p . "%";
204
		$width[$day][$j] = $w;
205
		$color[$day][$j] = $c;
206
	}
207
 
208
	$data[$day][1] = $fun[$column[1]]($data[$day][1]);
209
	$data[$day][2] = $fun[$column[2]]($data[$day][2]);
210
	$data[$day][3] = $fun[$column[3]]($data[$day][3]);
211
}
212
 
1831 raphael.pi 213
$data['max'][1] = $fun[$column[1]]($max[1]);
214
$data['max'][2] = $fun[$column[2]]($max[2]);
215
$data['max'][3] = $fun[$column[3]]($max[3]);
324 richard 216
 
217
require('../html/stats.html.php');
218
?>