Project

General

Profile

Download (9.39 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * functions.inc.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2013-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 * http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21

    
22
if (Connection_Aborted()) {
23
	exit;
24
}
25

    
26
require_once("config.inc");
27
require_once("pfsense-utils.inc");
28

    
29
function get_stats() {
30
	$stats['cpu'] = cpu_usage();
31
	$stats['mem'] = mem_usage();
32
	$stats['uptime'] = get_uptime();
33
	$stats['states'] = get_pfstate();
34
	$stats['temp'] = get_temp();
35
	$stats['datetime'] = update_date_time();
36
	$stats['cpufreq'] = get_cpufreq();
37
	$stats['load_average'] = get_load_average();
38
	get_mbuf($stats['mbuf'], $stats['mbufpercent']);
39
	$stats['statepercent'] = get_pfstate(true);
40
	$stats = join("|", $stats);
41
	return $stats;
42
}
43

    
44
function get_uptime() {
45
	$uptime = get_uptime_sec();
46

    
47
	if (intval($uptime) == 0) {
48
		return;
49
	}
50

    
51
	$updays = (int)($uptime / 86400);
52
	$uptime %= 86400;
53
	$uphours = (int)($uptime / 3600);
54
	$uptime %= 3600;
55
	$upmins = (int)($uptime / 60);
56
	$uptime %= 60;
57
	$upsecs = (int)($uptime);
58

    
59
	$uptimestr = "";
60
	if ($updays > 1) {
61
		$uptimestr .= "$updays Days ";
62
	} else if ($updays > 0) {
63
		$uptimestr .= "1 Day ";
64
	}
65

    
66
	if ($uphours > 1) {
67
		$hours = "s";
68
	}
69

    
70
	if ($upmins > 1) {
71
		$minutes = "s";
72
	}
73

    
74
	if ($upmins > 1) {
75
		$seconds = "s";
76
	}
77

    
78
	$uptimestr .= sprintf("%02d Hour$hours %02d Minute$minutes %02d Second$seconds", $uphours, $upmins, $upsecs);
79
	return $uptimestr;
80
}
81

    
82
/* Calculates non-idle CPU time and returns as a percentage */
83
function cpu_usage() {
84
	$duration = 1;
85
	$diff = array('user', 'nice', 'sys', 'intr', 'idle');
86
	$cpuTicks = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
87
	sleep($duration);
88
	$cpuTicks2 = array_combine($diff, explode(" ", get_single_sysctl('kern.cp_time')));
89

    
90
	$totalStart = array_sum($cpuTicks);
91
	$totalEnd = array_sum($cpuTicks2);
92

    
93
	// Something wrapped ?!?!
94
	if ($totalEnd <= $totalStart) {
95
		return 0;
96
	}
97

    
98
	// Calculate total cycles used
99
	$totalUsed = ($totalEnd - $totalStart) - ($cpuTicks2['idle'] - $cpuTicks['idle']);
100

    
101
	// Calculate the percentage used
102
	$cpuUsage = floor(100 * ($totalUsed / ($totalEnd - $totalStart)));
103

    
104
	return $cpuUsage;
105
}
106

    
107
function get_pfstate($percent=false) {
108
	global $config;
109
	$matches = "";
110
	if (isset($config['system']['maximumstates']) and $config['system']['maximumstates'] > 0) {
111
		$maxstates="{$config['system']['maximumstates']}";
112
	} else {
113
		$maxstates=pfsense_default_state_size();
114
	}
115
	$curentries = `/sbin/pfctl -si |grep current`;
116
	if (preg_match("/([0-9]+)/", $curentries, $matches)) {
117
		$curentries = $matches[1];
118
	}
119
	if (!is_numeric($curentries)) {
120
		$curentries = 0;
121
	}
122
	if ($percent) {
123
		if (intval($maxstates) > 0) {
124
			return round(($curentries / $maxstates) * 100, 0);
125
		} else {
126
			return "NA";
127
		}
128
	} else {
129
		return $curentries . "/" . $maxstates;
130
	}
131
}
132

    
133
function has_temp() {
134
	/* no known temp monitors available at present */
135

    
136
	/* should only reach here if there is no hardware monitor */
137
	return false;
138
}
139

    
140
function get_hwtype() {
141
	return;
142
}
143

    
144
function get_mbuf(&$mbuf, &$mbufpercent) {
145
	$mbufs_output=trim(`/usr/bin/netstat -mb | /usr/bin/grep "mbuf clusters in use" | /usr/bin/awk '{ print $1 }'`);
146
	list($mbufs_current, $mbufs_cache, $mbufs_total, $mbufs_max) = explode("/", $mbufs_output);
147
	$mbuf = "{$mbufs_total}/{$mbufs_max}";
148
	$mbufpercent = ($mbufs_max > 0) ? round(($mbufs_total / $mbufs_max) * 100, 0) : "NA";
149
}
150

    
151
function get_temp() {
152
	$temp_out = get_single_sysctl("dev.cpu.0.temperature");
153
	if ($temp_out == "") {
154
		$temp_out = get_single_sysctl("hw.acpi.thermal.tz0.temperature");
155
	}
156

    
157
	// Remove 'C' from the end and spaces
158
	$temp_out = trim(rtrim($temp_out, 'C'));
159

    
160
	if ($temp_out[0] == '-') {
161
		return '';
162
	}
163

    
164
	return $temp_out;
165
}
166

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

    
173
	/* Get rid of the header */
174
	array_shift($mout);
175
	foreach ($mout as $fs) {
176
		$f = array();
177
		list($f['device'], $f['type'], $f['total_size'], $f['percent_used'], $f['mountpoint']) = explode(' ', $fs);
178

    
179
		/* We dont' want the trailing % sign. */
180
		$f['percent_used'] = trim($f['percent_used'], '%');
181

    
182
		$filesystems[] = $f;
183
	}
184
	return $filesystems;
185
}
186

    
187
function disk_usage($slice = '/') {
188
	$dfout = "";
189
	exec("/bin/df -h {$slice} | /usr/bin/tail -n 1 | /usr/bin/awk '{ print $5 }' | /usr/bin/cut -d '%' -f 1", $dfout);
190
	$diskusage = trim($dfout[0]);
191

    
192
	return $diskusage;
193
}
194

    
195
function swap_usage() {
196
	exec("/usr/sbin/swapinfo", $swap_info);
197
	$swap_used = "";
198
	foreach ($swap_info as $line) {
199
		if (preg_match('/(\d+)%$/', $line, $matches)) {
200
			$swap_used = $matches[1];
201
			break;
202
		}
203
	}
204

    
205
	return $swap_used;
206
}
207

    
208
function mem_usage() {
209
	$totalMem = get_single_sysctl("vm.stats.vm.v_page_count");
210
	if ($totalMem > 0) {
211
		$inactiveMem = get_single_sysctl("vm.stats.vm.v_inactive_count");
212
		$cachedMem = get_single_sysctl("vm.stats.vm.v_cache_count");
213
		$freeMem = get_single_sysctl("vm.stats.vm.v_free_count");
214
		$usedMem = $totalMem - ($inactiveMem + $cachedMem + $freeMem);
215
		$memUsage = round(($usedMem * 100) / $totalMem, 0);
216
	} else {
217
		$memUsage = "NA";
218
	}
219

    
220
	return $memUsage;
221
}
222

    
223
function update_date_time() {
224
	$datetime = date("D M j G:i:s T Y");
225
	return $datetime;
226
}
227

    
228
function get_cpufreq() {
229
	$cpufreqs = "";
230
	$out = "";
231
	$cpufreqs = explode(" ", get_single_sysctl('dev.cpu.0.freq_levels'));
232
	$maxfreq = explode("/", $cpufreqs[0]);
233
	$maxfreq = $maxfreq[0];
234
	$curfreq = "";
235
	$curfreq = get_single_sysctl('dev.cpu.0.freq');
236
	if (($curfreq > 0) && ($curfreq != $maxfreq)) {
237
		$out = "Current: {$curfreq} MHz, Max: {$maxfreq} MHz";
238
	}
239
	return $out;
240
}
241

    
242
function get_cpu_crypto_support() {
243
	$machine = get_single_sysctl('hw.machine');
244
	$accelerated_arm_platforms = array('am335x');
245
	$cpucrypto_type = "";
246

    
247
	switch ($machine) {
248
		case 'amd64':
249
			$cpucrypto_type = "AES-NI CPU Crypto: ";
250
			exec("/usr/bin/grep -c '  Features.*AESNI' /var/log/dmesg.boot", $cpucrypto_present);
251
			if ($cpucrypto_present[0] > 0) {
252
				$cpucrypto_type .= "Yes ";
253
				$cpucrypto_type .= ($cpucrypto_active) ? "(active)" : "(inactive)";
254
			} else {
255
				$cpucrypto_type .= "No";
256
			}
257
			$cpucrypto_active = is_module_loaded('aesni');
258
		case 'arm':
259
			$armplatform = get_single_sysctl('hw.platform');
260
			if (in_array($armplatform, $accelerated_arm_platforms)) {
261
				/* No drivers yet, so mark inactive! */
262
				$cpucrypto_type = "{$armplatform} built-in CPU Crypto (inactive)";
263
			}
264
		default:
265
			/* Unknown/unidentified platform */
266
	}
267

    
268
	if (!empty($cpucrypto_type)) {
269
		return $cpucrypto_type;
270
	} else {
271
		return "CPU Crypto: None/Unknown Platform";
272
	}
273
}
274

    
275
function get_cpu_count($show_detail = false) {
276
	$cpucount = get_single_sysctl('kern.smp.cpus');
277

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

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

    
292
function get_interfacestats() {
293
	global $config;
294
	//build interface list for widget use
295
	$ifdescrs = get_configured_interface_list();
296

    
297
	$array_in_packets = array();
298
	$array_out_packets = array();
299
	$array_in_bytes = array();
300
	$array_out_bytes = array();
301
	$array_in_errors = array();
302
	$array_out_errors = array();
303
	$array_collisions = array();
304
	$array_interrupt = array();
305
	$new_data = "";
306

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

    
328
	return $new_data;
329
}
330

    
331
function get_interfacestatus() {
332
	$data = "";
333
	global $config;
334

    
335
	//build interface list for widget use
336
	$ifdescrs = get_configured_interface_with_descr();
337

    
338
	foreach ($ifdescrs as $ifdescr => $ifname) {
339
		$ifinfo = get_interface_info($ifdescr);
340
		$data .= $ifname . "^";
341
		if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
342
			$data .= "up";
343
		} else if ($ifinfo['status'] == "no carrier") {
344
			$data .= "down";
345
		} else if ($ifinfo['status'] == "down") {
346
			$data .= "block";
347
		}
348
		$data .= "^";
349
		if ($ifinfo['ipaddr']) {
350
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddr']) . "</strong>";
351
		}
352
		$data .= "^";
353
		if ($ifinfo['ipaddrv6']) {
354
			$data .= "<strong>" . htmlspecialchars($ifinfo['ipaddrv6']) . "</strong>";
355
		}
356
		$data .= "^";
357
		if ($ifinfo['status'] != "down") {
358
			$data .= htmlspecialchars($ifinfo['media']);
359
		}
360

    
361
		$data .= "~";
362

    
363
	}
364
	return $data;
365
}
366

    
367
?>
    (1-1/1)