-
Notifications
You must be signed in to change notification settings - Fork 517
/
Copy pathadmin_model.php
447 lines (416 loc) · 20.5 KB
/
admin_model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class Admin {
/**
* get running status of service
*
* @param string $name
* @return mixed true == running | false == stopped | null == not installed
*/
public static function getServiceStatus($name) {
@exec('systemctl show '.$name.' | grep State', $exec);
$status = array();
foreach ($exec as $line) {
$parts = explode('=',$line);
$status[$parts[0]] = $parts[1];
}
if (isset($status['LoadState']) && $status['LoadState'] === 'not-found') {
$return = null;
} else if (isset($status["ActiveState"]) && isset($status["SubState"])) {
return array(
'ActiveState' => $status["ActiveState"],
'SubState' => $status["SubState"]
);
} else {
$return = null;
}
return $return;
}
// Retrieve server information
public static function system_information() {
global $settings, $mysqli;
$result = $mysqli->query("select now() as datetime, time_format(timediff(now(),convert_tz(now(),@@session.time_zone,'+00:00')),'%H:%i') AS timezone");
$db = $result->fetch_array();
@list($system, $host, $kernel) = preg_split('/[\s,]+/', php_uname('a'), 5);
$services = array();
$services['emonhub'] = Admin::getServiceStatus('emonhub.service');
$services['mqtt_input'] = Admin::getServiceStatus('mqtt_input.service'); // depreciated, replaced with emoncms_mqtt
$services['emoncms_mqtt'] = Admin::getServiceStatus('emoncms_mqtt.service');
$services['feedwriter'] = Admin::getServiceStatus('feedwriter.service');
$services['service-runner'] = Admin::getServiceStatus('service-runner.service');
$services['emonPiLCD'] = Admin::getServiceStatus('emonPiLCD.service');
$services['redis-server'] = Admin::getServiceStatus('redis-server.service');
$services['mosquitto'] = Admin::getServiceStatus('mosquitto.service');
$services['demandshaper'] = Admin::getServiceStatus('demandshaper.service');
//@exec("hostname -I", $ip); $ip = $ip[0];
$meminfo = false;
if (@is_readable('/proc/meminfo')) {
$data = explode("\n", file_get_contents("/proc/meminfo"));
$meminfo = array();
foreach ($data as $line) {
if (strpos($line, ':') !== false) {
list($key, $val) = explode(":", $line);
$meminfo[$key] = 1024 * floatval( trim( str_replace( ' kB', '', $val ) ) );
}
}
}
$emoncms_modules = "";
$emoncmsModulesPath = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/')).'/Modules'; // Set the Modules path
$emoncmsModuleFolders = glob("$emoncmsModulesPath/*", GLOB_ONLYDIR); // Use glob to get all the folder names only
foreach($emoncmsModuleFolders as $emoncmsModuleFolder) { // loop through the folders
if ($emoncms_modules != "") $emoncms_modules .= " | ";
if (file_exists($emoncmsModuleFolder."/module.json")) { // JSON Version informatmion exists
$json = json_decode(file_get_contents($emoncmsModuleFolder."/module.json")); // Get JSON version information
$jsonAppName = $json->{'name'};
$jsonVersion = $json->{'version'};
if ($jsonAppName) {
$emoncmsModuleFolder = $jsonAppName;
}
if ($jsonVersion) {
$emoncmsModuleFolder = $emoncmsModuleFolder." v".$jsonVersion;
}
}
$emoncms_modules .= str_replace($emoncmsModulesPath."/", '', $emoncmsModuleFolder);
}
return array('date' => date('Y-m-d H:i:s T'),
'system' => $system,
'kernel' => $kernel,
'host' => $host,
'ip' => server('SERVER_ADDR'),
'uptime' => @exec('uptime'),
'http_server' => $_SERVER['SERVER_SOFTWARE'],
'php' => PHP_VERSION,
'zend' => (function_exists('zend_version') ? zend_version() : 'n/a'),
'db_server' => $settings['sql']['server'],
'db_ip' => gethostbyname($settings['sql']['server']),
'db_version' => $mysqli->server_info,
'db_stat' => $mysqli->stat(),
'db_date' => $db['datetime'] . " (UTC " . $db['timezone'] . ")",
'redis_server' => $settings['redis']['host'].":".$settings['redis']['port'],
'redis_ip' => gethostbyname($settings['redis']['host']),
'services' => $services,
'mqtt_server' => $settings['mqtt']['host'],
'mqtt_ip' => gethostbyname($settings['mqtt']['host']),
'mqtt_port' => $settings['mqtt']['port'],
'hostbyaddress' => @gethostbyaddr(gethostbyname($host)),
'http_proto' => $_SERVER['SERVER_PROTOCOL'],
'http_mode' => $_SERVER['GATEWAY_INTERFACE'],
'http_port' => $_SERVER['SERVER_PORT'],
'php_modules' => get_loaded_extensions(),
'mem_info' => $meminfo,
'partitions' => Admin::disk_list(),
'emoncms_modules' => $emoncms_modules,
'git_branch' => @exec("git -C " . substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/')) . " branch --contains HEAD"),
'git_URL' => @exec("git -C " . substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/')) . " ls-remote --get-url origin"),
'git_describe' => @exec("git -C " . substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/')) . " describe")
);
}
/**
* return array of mounted partitions
*
* @return array
*/
public static function disk_list()
{
$partitions = array();
// Fetch partition information from df command
// I would have used disk_free_space() and disk_total_space() here but
// there appears to be no way to get a list of partitions in PHP?
$output = array();
@exec('df --block-size=1 -x squashfs', $output);
foreach($output as $line)
{
$columns = array();
foreach(explode(' ', $line) as $column)
{
$column = trim($column);
if($column != '') $columns[] = $column;
}
// Only process 6 column rows
// (This has the bonus of ignoring the first row which is 7)
if(count($columns) == 6)
{
$filesystem = $columns[0];
$partition = $columns[5];
$partitions[$partition]['Temporary']['bool'] = in_array($columns[0], array('tmpfs', 'devtmpfs'));
$partitions[$partition]['Partition']['text'] = $partition;
$partitions[$partition]['FileSystem']['text'] = $filesystem;
if(is_numeric($columns[1]) && is_numeric($columns[2]) && is_numeric($columns[3]))
{
$partitions[$partition]['Size']['value'] = $columns[1];
$partitions[$partition]['Free']['value'] = $columns[3];
$partitions[$partition]['Used']['value'] = $columns[2];
}
else
{
// Fallback if we don't get numerical values
$partitions[$partition]['Size']['text'] = $columns[1];
$partitions[$partition]['Used']['text'] = $columns[2];
$partitions[$partition]['Free']['text'] = $columns[3];
}
$writeload = 0;
$writeloadtime = "";
global $redis;
if ($redis) {
// translate partition mount point to mmcblk0pX based name
$partition_name = false;
if ($partition=="/boot") $partition_name = "mmcblk0p1";
else if ($partition=="/") $partition_name = "mmcblk0p2";
else if ($partition=="/var/opt/emoncms") $partition_name = "mmcblk0p3";
else if ($partition=="/home/pi/data") $partition_name = "mmcblk0p3";
if ($partition_name) {
if ($sectors_written = @exec("awk '/$partition_name/ {print $10}' /proc/diskstats")) {
$last_sectors_written = 0;
if ($redis->exists("diskstats:$partition_name")) {
$last_sectors_written = $redis->get("diskstats:$partition_name");
$last_time = $redis->get("diskstats:time");
$elapsed = time() - $last_time;
$writeload = ($sectors_written-$last_sectors_written)*512/$elapsed;
$writeloadtime = $elapsed;
} else {
$redis->set("diskstats:$partition_name",$sectors_written);
$redis->set("diskstats:time",time());
$writeload = 0;
}
}
}
}
$partitions[$partition]['WriteLoad']['value'] = $writeload;
$partitions[$partition]['WriteLoadTime']['value'] = $writeloadtime;
}
}
return $partitions;
}
/**
* return an array of all installed php modules
*
* @param [type] $_modules
* @return void
*/
public static function php_modules($_modules) {
natcasesort($_modules);// sort case insensitive
$modules = [];// empty list
foreach($_modules as $ver=>$extension){
$module_version = phpversion($extension);// returns false if no version information
$modules[] = $module_version ? "$extension v$module_version" : $extension; // show version if available
}
return $modules;
}
/**
* return true if php is running on raspberry pi
*
* @return boolean
*/
public static function is_Pi() {
return @exec('ifconfig | grep b8:27:eb:');
}
/**
* get an array of raspberry pi properites
*
* @see: SoC 'hw' now not required - https://github.com/emoncms/emoncms/issues/1364
* @return array has keys [hw,]rev,sn,model * @return array has keys hw,rev,sn,model
*/
public static function get_rpi_info() {
// create empty array with all the required keys
$rpi_info = array_map(function($n) {return '';},array_flip(explode(',','rev,sn,model,emonpiRelease,cputemp,gputemp,currentfs')));
// exit with empty array if not a raspberry pi
if ( !Admin::is_Pi()) return $rpi_info;
// add the rpi details
$rpi_info['model'] = "Unknown";
if (@is_readable('/proc/cpuinfo') || true) {
//load model information
$rpi_revision = array();
if (@is_readable(__DIR__."/pi-model.json")) {
$rpi_revision = json_decode(file_get_contents(__DIR__."/pi-model.json"), true);
foreach ($rpi_revision as $k => $rev) {
if(empty($rev['Code'])) continue;
$rpi_revision[$rev['Code']] = $rev;
unset($rpi_revision[$k]);
}
}
//get cpu info
preg_match_all('/^(revision|serial|hardware)\\s*: (.*)/mi', file_get_contents("/proc/cpuinfo"), $matches);
$matches = array_filter($matches);
if(!empty($matches)) {
// $rpi_info['hw'] = "Broadcom ".$matches[2][0];
$rpi_info['rev'] = $matches[2][1];
$rpi_info['sn'] = $matches[2][2];
$rpi_info['model'] = '';
}
//build model string
if(!empty($rpi_revision[$rpi_info['rev']]) || 1) {
$empty_model = array_map(function($n) {return '';},array_flip(explode(',','Model,Revision,RAM,Manufacturer,currentfs')));
$model_info = !empty($rpi_revision[$rpi_info['rev']]) ? $rpi_revision[$rpi_info['rev']] : $empty_model;
$rpi_info['model'] = "Raspberry Pi ";
$model = !empty($model_info['Model']) ? $model_info['Model'] : 'N/A';
if (ctype_digit($model[0])) { //Raspberry Pi >= 2
$ver = $model[0];
$model = substr($model, 1);
$rpi_info['model'] .= $ver." Model ".$model;
}
else if (substr($model, 0, 2) == 'CM') { // Raspberry Pi Compute Module
$rpi_info['model'] .= " Compute Module";
if (ctype_digit($model[2]) && $model[2]>1) $rpi_info['model'] .= " ".$model[2];
}
else { //Raspberry Pi
$rpi_info['model'] .= " Model ".$model;
}
$rpi_info['model'] .= " Rev ".$model_info['Revision']." - ".$model_info['RAM']." (".$model_info['Manufacturer'].")";
}
$rpi_info['cputemp'] = number_format((int)@exec('cat /sys/class/thermal/thermal_zone0/temp')/1000, '2', '.', '')."°C";
$rpi_info['gputemp'] = @exec('/opt/vc/bin/vcgencmd measure_temp');
if(strpos($rpi_info['gputemp'], 'temp=' ) !== false ){
$rpi_info['gputemp'] = str_replace("temp=","", $rpi_info['gputemp']);
$rpi_info['gputemp'] = str_replace("'C","°C", $rpi_info['gputemp']);
}else{
$rpi_info['gputemp'] = "N/A";
$rpi_info['gputemp'] .= _(" (to show GPU temp execute this command from the console \"sudo usermod -G video www-data\" )");
}
// release
if (glob('/boot/emonSD-*')) {
foreach (glob("/boot/emonSD-*") as $emonpiRelease) {
$rpi_info['emonpiRelease'] = str_replace("/boot/", '', $emonpiRelease);
}
}
}
$rpi_info['currentfs'] = Admin::get_fs_state();
return $rpi_info;
}
/**
* return the current mosquitto server version
*
* @return string
*/
public static function mqtt_version() {
$v = '?';
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$v = "n/a";
} else {
if (file_exists('/usr/sbin/mosquitto')) {
$v = exec('/usr/sbin/mosquitto -h | grep -oP \'(?<=mosquitto\sversion\s)[0-9.]+(?=\s*)\'');
}
}
return $v;
}
public static function get_ram($mem_info){
// Ram information
$sysRam = array_map(function($n) {return '';},array_flip(explode(',','used,raw,percent,table,swap')));
if ($mem_info) {
$sysTotal = $mem_info['MemTotal'];
$sysRamUsed = $mem_info['MemTotal'] - $mem_info['MemFree'] - $mem_info['Buffers'] - $mem_info['Cached'];
$sysFree = $mem_info['MemTotal']-$sysRamUsed;
$sysRamPercentRaw = ($sysRamUsed / $mem_info['MemTotal']) * 100;
$sysRamPercent = sprintf('%.2f',$sysRamPercentRaw);
$sysRamPercentTable = number_format(round($sysRamPercentRaw, 2), 2, '.', '');
$sysSwap = array();
if ($mem_info['SwapTotal'] > 0) {
$sysSwap['total'] = Admin::formatSize($mem_info['SwapTotal']);
$sysSwap['used'] = Admin::formatSize($mem_info['SwapTotal'] - $mem_info['SwapFree']);
$sysSwap['free'] = Admin::formatSize($mem_info['SwapFree']);
$sysSwap['raw'] = (($mem_info['SwapTotal'] - $mem_info['SwapFree']) / $mem_info['SwapTotal']) * 100;
$sysSwap['percent'] = sprintf('%.2f',$sysSwap['raw']);
$sysSwap['table'] = number_format(round($sysSwap['raw'], 2), 2, '.', '');
}
$sysRam = array(
'total'=>Admin::formatSize($sysTotal),
'used'=>Admin::formatSize($sysRamUsed),
'free'=>Admin::formatSize($sysFree),
'raw'=>$sysRamPercentRaw,
'percent'=>$sysRamPercent,
'table'=>$sysRamPercentTable,
'swap'=>$sysSwap
);
}
return $sysRam;
}
/**
* return array of disk mounts with properties separated from original strings
*
* @param array $partitions
* @return void
*/
public static function get_mountpoints($partitions) {
// Filesystem Information
$mounts = array();
if (count($partitions) > 0) {
// echo "<tr><td><b>Disk</b></td><td><b>Mount</b></td><td><b>Stats</b></td></tr>\n";
foreach($partitions as $fs) {
if (!$fs['Temporary']['bool'] && $fs['FileSystem']['text']!= "none" && $fs['FileSystem']['text']!= "udev") {
$diskFree = $fs['Free']['value'];
$diskTotal = $fs['Size']['value'];
$diskUsed = $fs['Used']['value'];
$writeLoad = $fs['WriteLoad']['value'];
$writeLoadTime = $fs['WriteLoadTime']['value'];
$diskPercentRaw = ($diskUsed / $diskTotal) * 100;
$diskPercent = sprintf('%.2f',$diskPercentRaw);
$diskPercentTable = number_format(round($diskPercentRaw, 2), 2, '.', '');
if (strlen($fs['Partition']['text'])>30) {
$mountpoint = substr($fs['Partition']['text'],0,30)."...";
} else {
$mountpoint = $fs['Partition']['text'];
}
$writeloadstr = "n/a";
if ($writeLoadTime) {
$days = floor($writeLoadTime / 86400);
$hours = floor(($writeLoadTime - ($days*86400))/3600);
$mins = floor(($writeLoadTime - ($days*86400) - ($hours*3600))/60);
$writeloadstr = Admin::formatSize($writeLoad)."/s (";
if ($days) $writeloadstr .= $days." days ";
if ($hours) $writeloadstr .= $hours." hours ";
$writeloadstr .= $mins." mins)";
}
$mounts[] = array(
'free'=>Admin::formatSize($diskFree),
'total'=>Admin::formatSize($diskTotal),
'used'=>Admin::formatSize($diskUsed),
'writeload'=>$writeloadstr,
'raw'=>$diskPercentRaw,
'percent'=>$diskPercent,
'table'=>$diskPercentTable,
'mountpoint'=>$mountpoint
);
// echo "<tr><td class='subinfo'></td><td>".$mountpoint."</td><td><div class='progress progress-info' style='margin-bottom: 0;'><div class='bar' style='width: ".$diskPercentTable."%;'>Used: ".$diskPercent."% </div></div>";
// echo "<b>Total:</b> ".formatSize($diskTotal)."<b> Used:</b> ".formatSize($diskUsed)."<b> Free:</b> ".formatSize($diskFree)."</td></tr>\n";
}
}
}
return $mounts;
}
/**
* return read only state of the file system
*
* @return void
*/
public static function get_fs_state(){
$currentfs = "<b>read-only</b>";
exec('mount', $resexec);
$matches = null;
preg_match('/^\/dev\/mmcblk0p2 on \/ .*(\(rw).*/mi', implode("\n",$resexec), $matches);
if (!empty($matches)) {
$currentfs = "<b>read-write</b>";
}
if (!Admin::is_Pi()) $currentfs = '?';
return $currentfs;
}
/**
* return bytes as suitable unit
*
* @param number $bytes
* @return string
*/
private static function formatSize( $bytes ){
$types = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' );
for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
return( round( $bytes, 2 ) . " " . $types[$i] );
}
}
?>