|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) MIKO LLC - All Rights Reserved |
|
4
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited |
|
5
|
|
|
* Proprietary and confidential |
|
6
|
|
|
* Written by Nikolay Beketov, 7 2020 |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace MikoPBX\PBXCoreREST\Lib; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
use MikoPBX\Core\System\Storage; |
|
14
|
|
|
use MikoPBX\Core\System\Util; |
|
15
|
|
|
use Phalcon\Di\Injectable; |
|
16
|
|
|
|
|
17
|
|
|
class SystemManagementProcessor extends Injectable |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Получение сведений о системе. |
|
22
|
|
|
* |
|
23
|
|
|
* @return PBXApiResult |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function getInfo(): PBXApiResult |
|
26
|
|
|
{ |
|
27
|
|
|
$res = new PBXApiResult(); |
|
28
|
|
|
$res->processor = __METHOD__; |
|
29
|
|
|
$res->success = true; |
|
30
|
|
|
|
|
31
|
|
|
$storage = new Storage(); |
|
32
|
|
|
$res->data = [ |
|
33
|
|
|
'disks' => $storage->getAllHdd(), |
|
34
|
|
|
'cpu' => self::getCpu(), |
|
35
|
|
|
'uptime' => self::getUpTime(), |
|
36
|
|
|
'mem' => self::getMemInfo(), |
|
37
|
|
|
]; |
|
38
|
|
|
$res->processor = __METHOD__; |
|
39
|
|
|
return $res; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Возвращает информацию по загрузке CPU. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function getCpu() |
|
46
|
|
|
{ |
|
47
|
|
|
$ut = []; |
|
48
|
|
|
$grepPath = Util::which('grep'); |
|
49
|
|
|
$mpstatPath = Util::which('mpstat'); |
|
50
|
|
|
Util::mwExec("{$mpstatPath} | {$grepPath} all", $ut); |
|
51
|
|
|
preg_match("/^.*\s+all\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+(.*)\s*.*/i", $ut[0], $matches); |
|
52
|
|
|
$rv = 100 - $matches[1]; |
|
53
|
|
|
|
|
54
|
|
|
if (100 < $rv) { |
|
55
|
|
|
$rv = 100; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return round($rv, 2); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Получаем информацию по времени работы ПК. |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getUpTime(): string |
|
65
|
|
|
{ |
|
66
|
|
|
$ut = []; |
|
67
|
|
|
$uptimePath = Util::which('uptime'); |
|
68
|
|
|
$awkPath = Util::which('awk'); |
|
69
|
|
|
Util::mwExec("{$uptimePath} | {$awkPath} -F \" |,\" '{print $5}'", $ut); |
|
70
|
|
|
|
|
71
|
|
|
return implode('', $ut); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Получаем информацию по оперативной памяти. |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function getMemInfo(): array |
|
78
|
|
|
{ |
|
79
|
|
|
$result = []; |
|
80
|
|
|
$out = []; |
|
81
|
|
|
$catPath = Util::which('cat'); |
|
82
|
|
|
$grepPath = Util::which('grep'); |
|
83
|
|
|
$awkPath = Util::which('awk'); |
|
84
|
|
|
Util::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'Inactive:' | {$awkPath} '{print $2}'", $out); |
|
85
|
|
|
$result['inactive'] = round((1 * implode($out)) / 1024, 2); |
|
86
|
|
|
Util::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemFree:' | {$awkPath} '{print $2}'", $out); |
|
87
|
|
|
$result['free'] = round((1 * implode($out)) / 1024, 2); |
|
88
|
|
|
Util::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemTotal:' | {$awkPath} '{print $2}'", $out); |
|
89
|
|
|
$result['total'] = round((1 * implode($out)) / 1024, 2); |
|
90
|
|
|
|
|
91
|
|
|
return $result; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Upgrade MikoPBX from uploaded IMG file |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $tempFilename path to uploaded image |
|
98
|
|
|
* |
|
99
|
|
|
* @return PBXApiResult |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function upgradeFromImg(string $tempFilename): PBXApiResult |
|
102
|
|
|
{ |
|
103
|
|
|
$res = new PBXApiResult(); |
|
104
|
|
|
$res->processor = __METHOD__; |
|
105
|
|
|
$res->success = true; |
|
106
|
|
|
$res->data['message']='In progress...'; |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
if (!file_exists($tempFilename)){ |
|
110
|
|
|
$res->success = false; |
|
111
|
|
|
$res->messages[]="Update file '{$tempFilename}' not found."; |
|
112
|
|
|
return $res; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if ( ! file_exists('/var/etc/cfdevice')) { |
|
116
|
|
|
$res->success = false; |
|
117
|
|
|
$res->messages[]="The system is not installed"; |
|
118
|
|
|
return $res; |
|
119
|
|
|
} |
|
120
|
|
|
$dev = trim(file_get_contents('/var/etc/cfdevice')); |
|
121
|
|
|
|
|
122
|
|
|
$link = '/tmp/firmware_update.img'; |
|
123
|
|
|
Util::createUpdateSymlink($tempFilename, $link); |
|
124
|
|
|
$mikopbx_firmwarePath = Util::which('mikopbx_firmware'); |
|
125
|
|
|
Util::mwExecBg("{$mikopbx_firmwarePath} recover_upgrade {$link} /dev/{$dev}"); |
|
126
|
|
|
return $res; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |