Projet

Général

Profil

Télécharger (9,04 ko) Statistiques
| Branche: | Tag: | Révision:

univnautes / usr / local / www / includes / functions.inc.php @ 0d4b6b89

1
<?
2
/*
3
	pfSense_MODULE:	ajax
4
*/
5

    
6
if(Connection_Aborted()) {
7
	exit;
8
}
9

    
10
require_once("config.inc");
11
require_once("pfsense-utils.inc");
12

    
13
function get_stats() {
14
	$stats['cpu'] = cpu_usage();
15
	$stats['mem'] = mem_usage();
16
	$stats['uptime'] = get_uptime();
17
	$stats['states'] = get_pfstate();
18
	$stats['temp'] = get_temp();
19
	$stats['datetime'] = update_date_time();
20
	$stats['interfacestatistics'] = get_interfacestats();
21
	$stats['interfacestatus'] = get_interfacestatus();
22
	$stats['gateways'] = get_gatewaystats();
23
	$stats['cpufreq'] = get_cpufreq();
24
	$stats['load_average'] = get_load_average();
25
	$stats['mbuf'] = get_mbuf();
26
	$stats['mbufpercent'] = get_mbuf(true);
27
	$stats['statepercent'] = get_pfstate(true);
28
	$stats = join("|", $stats);
29
	return $stats;
30
}
31

    
32
function get_gatewaystats() {
33
	$a_gateways = return_gateways_array();
34
	$gateways_status = array();
35
	$gateways_status = return_gateways_status(true);
36
	$data = "";
37
	$isfirst = true;
38
	foreach($a_gateways as $gname => $gw) {
39
		if(!$isfirst)
40
			$data .= ",";
41
		$isfirst = false;
42
		$data .= $gw['name'] . ",";
43
		if ($gateways_status[$gname]) {
44
			$data .= lookup_gateway_ip_by_name($gname) . ",";
45
			$gws = $gateways_status[$gname];
46
			switch(strtolower($gws['status'])) {
47
			case "none":
48
				$online = "Online";
49
				$bgcolor = "#90EE90";  // lightgreen
50
				break;
51
			case "down":
52
				$online = "Offline";
53
				$bgcolor = "#F08080";  // lightcoral
54
				break;
55
			case "delay":
56
				$online = "Latency";
57
				$bgcolor = "#F0E68C";  // khaki
58
				break;
59
			case "loss":
60
				$online = "Packetloss";
61
				$bgcolor = "#F0E68C";  // khaki
62
				break;
63
			default:
64
				$online = "Pending";
65
				break;
66
			}
67
		} else {
68
			$data .= "~,";
69
			$gws['delay'] = "~";
70
			$gws['loss'] = "~";
71
			$online = "Unknown";
72
			$bgcolor = "#ADD8E6";  // lightblue
73
		}
74
		$data .= ($online == "Pending") ? "{$online},{$online}," : "{$gws['delay']},{$gws['loss']},";
75
		$data .= "<table><tr><td bgcolor=\"$bgcolor\">&nbsp;$online&nbsp;</td></td></tr></table>";
76
	}
77
	return $data;
78
}
79

    
80
function get_uptime() {
81
	$uptime = get_uptime_sec();
82

    
83
	if(intval($uptime) == 0)
84
		return;
85

    
86
	$updays = (int)($uptime / 86400);
87
	$uptime %= 86400;
88
	$uphours = (int)($uptime / 3600);
89
	$uptime %= 3600;
90
	$upmins = (int)($uptime / 60);
91
	$uptime %= 60;
92
	$upsecs = (int)($uptime);
93

    
94
	$uptimestr = "";
95
	if ($updays > 1)
96
		$uptimestr .= "$updays Days ";
97
	else if ($updays > 0)
98
		$uptimestr .= "1 Day ";
99

    
100
	if ($uphours > 1)
101
		$hours = "s";
102

    
103
	if ($upmins > 1)
104
		$minutes = "s";
105

    
106
	if ($upmins > 1)
107
		$seconds = "s";
108

    
109
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
110
	return $uptimestr;
111
}
112

    
113
/* Calculates non-idle CPU time and returns as a percentage */
114
function cpu_usage() {
115
	$duration = 1;
116
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
117
	$cpuTicks = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
118
	sleep($duration);
119
	$cpuTicks2 = array_combine($diff, explode(" ", `/sbin/sysctl -n kern.cp_time`));
120

    
121
	$totalStart = array_sum($cpuTicks);
122
	$totalEnd = array_sum($cpuTicks2);
123

    
124
	// Something wrapped ?!?!
125
	if ($totalEnd <= $totalStart)
126
		return 0;
127

    
128
	// Calculate total cycles used
129
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
130

    
131
	// Calculate the percentage used
132
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
133

    
134
	return $cpuUsage;
135
}
136

    
137
function get_pfstate($percent=false) {
138
	global $config;
139
	$matches = "";
140
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0)
141
		$maxstates="{$config['system']['maximumstates']}";
142
	else
143
		$maxstates=pfsense_default_state_size();
144
	$curentries = `/sbin/pfctl -si |grep current`;
145
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
146
		$curentries = $matches[1];
147
	}
148
	if (!is_numeric($curentries))
149
		$curentries = 0;
150
	if ($percent)
151
		if (intval($maxstates) > 0)
152
			return round(($curentries / $maxstates) * 100, 0);
153
		else
154
			return "NA";
155
	else
156
		return $curentries . "/" . $maxstates;
157
}
158

    
159
function has_temp() {
160
	/* no known temp monitors available at present */
161

    
162
	/* should only reach here if there is no hardware monitor */
163
	return false;
164
}
165

    
166
function get_hwtype() {
167
	return;
168
}
169

    
170
function get_mbuf($percent=false) {
171
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
172
	list( $mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max ) = explode( "/", $mbufs_output);
173
	if ($percent)
174
		if ($mbufs_max > 0)
175
			return round(($mbufs_total / $mbufs_max) * 100, 0);
176
		else
177
			return "NA";
178
	else
179
		return "{$mbufs_total}/{$mbufs_max}";
180
}
181

    
182
function get_temp() {
183
	$temp_out = "";
184
	exec("/sbin/sysctl dev.cpu.0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
185
	$temp_out = trim($dfout[0]);
186
	if ($temp_out == "") {
187
		exec("/sbin/sysctl hw.acpi.thermal.tz0.temperature | /usr/bin/awk '{ print $2 }' | /usr/bin/cut -d 'C' -f 1", $dfout);
188
		$temp_out = trim($dfout[0]);
189
	}
190

    
191
	return $temp_out;
192
}
193

    
194
/* Get mounted filesystems and usage. Do not display entries for virtual filesystems (e.g. devfs, nullfs, unionfs) */
195
function get_mounted_filesystems() {
196
	$mout = "";
197
	$filesystems = array();
198
	exec("/bin/df -Tht ufs,zfs,cd9660 | /usr/bin/awk '{print $1, $2, $3, $6, $7;}'", $mout);
199

    
200
	/* Get rid of the header */
201
	array_shift($mout);
202
	foreach ($mout as $fs) {
203
		$f = array();
204
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
205

    
206
		/* We dont' want the trailing % sign. */
207
		$f['percent_used'] = trim($f['percent_used'], '%');
208

    
209
		$filesystems[] = $f;
210
	}
211
	return $filesystems;
212
}
213

    
214
function disk_usage($slice = '/') {
215
	$dfout = "";
216
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
217
	$diskusage = trim($dfout[0]);
218

    
219
	return $diskusage;
220
}
221

    
222
function swap_usage() {
223
	exec("/usr/sbin/swapinfo", $swap_info);
224
	$swap_used = "";
225
	foreach ($swap_info as $line)
226
		if (preg_match('/(\d+)%$/', $line, $matches)) {
227
			$swap_used = $matches[1];
228
			break;
229
		}
230

    
231
	return $swap_used;
232
}
233

    
234
function mem_usage() {
235
	$memory = "";
236
	exec("/sbin/sysctl -n vm.stats.vm.v_page_count vm.stats.vm.v_inactive_count " .
237
		"vm.stats.vm.v_cache_count vm.stats.vm.v_free_count", $memory);
238

    
239
	$totalMem = $memory[0];
240
	$availMem = $memory[1] + $memory[2] + $memory[3];
241
	$usedMem = $totalMem - $availMem;
242
	if ($totalMem > 0)
243
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
244
	else
245
		$memUsage = "NA";
246

    
247
	return $memUsage;
248
}
249

    
250
function update_date_time() {
251
	$datetime = date("D M j G:i:s T Y");
252
	return $datetime;
253
}
254

    
255
function get_cpufreq() {
256
	$cpufreqs = "";
257
	$out = "";
258
	exec("/sbin/sysctl -n dev.cpu.0.freq_levels", $cpufreqs);
259
	$cpufreqs = explode(" ", trim($cpufreqs[0]));
260
	$maxfreq = explode("/", $cpufreqs[0]);
261
	$maxfreq = $maxfreq[0];
262
	$curfreq = "";
263
	exec("/sbin/sysctl -n dev.cpu.0.freq", $curfreq);
264
	$curfreq = trim($curfreq[0]);
265
	if (($curfreq > 0) && ($curfreq != $maxfreq))
266
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
267
	return $out;
268
}
269

    
270
function get_cpu_count($show_detail = false) {
271
	$cpucount = "";
272
	exec("/sbin/sysctl -n kern.smp.cpus", $cpucount);
273
	$cpucount = $cpucount[0];
274

    
275
	if ($show_detail) {
276
		$cpudetail = "";
277
		exec("/usr/bin/grep 'SMP.*package.*core' /var/log/dmesg.boot | /usr/bin/cut -f2- -d' '", $cpudetail);
278
		$cpucount = $cpudetail[0];
279
	}
280
	return $cpucount;
281
}
282

    
283
function get_load_average() {
284
	$load_average = "";
285
	exec("/usr/bin/uptime | /usr/bin/sed 's/^.*: //'", $load_average);
286
	return $load_average[0];
287
}
288

    
289
function get_interfacestats() {
290
	global $config;
291
	//build interface list for widget use
292
	$ifdescrs = get_configured_interface_list();
293

    
294
	$array_in_packets = array();
295
	$array_out_packets = array();
296
	$array_in_bytes = array();
297
	$array_out_bytes = array();
298
	$array_in_errors = array();
299
	$array_out_errors = array();
300
	$array_collisions = array();
301
	$array_interrupt = array();
302
	$new_data = "";
303

    
304
	//build data arrays
305
	foreach ($ifdescrs as $ifdescr => $ifname){
306
		$ifinfo = get_interface_info($ifdescr);
307
			$new_data .= "{$ifinfo['inpkts']},";
308
			$new_data .= "{$ifinfo['outpkts']},";
309
			$new_data .= format_bytes($ifinfo['inbytes']) . ",";
310
			$new_data .= format_bytes($ifinfo['outbytes']) . ",";
311
			if (isset($ifinfo['inerrs'])){
312
				$new_data .= "{$ifinfo['inerrs']},";
313
				$new_data .= "{$ifinfo['outerrs']},";
314
			}
315
			else{
316
				$new_data .= "0,";
317
				$new_data .= "0,";
318
			}
319
			if (isset($ifinfo['collisions']))
320
				$new_data .= htmlspecialchars($ifinfo['collisions']) . ",";
321
			else
322
				$new_data .= "0,";
323
	}//end for
324

    
325
	return $new_data;
326
}
327

    
328
function get_interfacestatus() {
329
	$data = "";
330
	global $config;
331

    
332
	//build interface list for widget use
333
	$ifdescrs = get_configured_interface_with_descr();
334

    
335
	foreach ($ifdescrs as $ifdescr => $ifname){
336
		$ifinfo = get_interface_info($ifdescr);
337
		$data .= $ifname . ",";
338
		if($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
339
			$data .= "up";
340
		}else if ($ifinfo['status'] == "no carrier") {
341
			$data .= "down";
342
		}else if ($ifinfo['status'] == "down") {
343
			$data .= "block";
344
		}
345
		$data .= ",";
346
		if ($ifinfo['ipaddr'])
347
			$data .= htmlspecialchars($ifinfo['ipaddr']);
348
		$data .= ",";
349
		if ($ifinfo['status'] != "down")
350
			$data .= htmlspecialchars($ifinfo['media']);
351

    
352
		$data .= "~";
353

    
354
	}
355
	return $data;
356
}
357

    
358
?>
    (1-1/1)