Passed
Push — develop ( 9d9a8f...564466 )
by Nikolay
05:09
created

GetInfoAction::getEnvironment()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 18
rs 9.8333
cc 4
nc 8
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright © 2017-2024 Alexey Portnov and Nikolay Beketov
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with this program.
17
 * If not, see <https://www.gnu.org/licenses/>.
18
 */
19
20
namespace MikoPBX\PBXCoreREST\Lib\Sysinfo;
21
22
use MikoPBX\Common\Models\CustomFiles;
23
use MikoPBX\Common\Models\PbxSettings;
24
use MikoPBX\Common\Models\PbxSettingsConstants;
25
use MikoPBX\Core\System\Processes;
26
use MikoPBX\Core\System\Util;
27
use MikoPBX\PBXCoreREST\Lib\PBXApiResult;
28
use MikoPBX\Service\Main;
29
use Phalcon\Di;
30
31
/**
32
 * Gets a collection of the system information and put it into temp file.
33
 *
34
 * @package MikoPBX\PBXCoreREST\Lib\Sysinfo
35
 */
36
class GetInfoAction extends \Phalcon\Di\Injectable
37
{
38
    /**
39
     * Gets a collection of the system information and put it into temp file.
40
     *
41
     * @return PBXApiResult An object containing the result of the API call.
42
     */
43
    public static function main(): PBXApiResult
44
    {
45
        $res            = new PBXApiResult();
46
        $res->processor = __METHOD__;
47
        $res->success   = true;
48
49
        $di           = Di::getDefault();
50
        $dirsConfig   = $di->getShared('config');
51
        $filenameTmp  = $dirsConfig->path('www.downloadCacheDir') . '/' . __FUNCTION__ . '_' . time() . '.txt';
52
53
        $content = self::prepareSysyinfoContent();
54
55
        file_put_contents($filenameTmp, $content);
56
        Util::addRegularWWWRights($filenameTmp);
57
        $res->data['filename'] = $filenameTmp;
58
        $res->processor        = __METHOD__;
59
60
        return $res;
61
    }
62
63
    /**
64
     * Prepares system information collection
65
     *
66
     * @return string
67
     */
68
    public static function prepareSysyinfoContent(): string
69
    {
70
        $content = self::getPBXVersion();
71
        $content .= self::getEnvironment();
72
        $content .= self::getDate();
73
        $content .= self::getUpTime();
74
        $content .= self::getCpu();
75
        $content .= self::getMemInfo();
76
        $content .= self::getStorageInfo();
77
        $content .= self::getIfconfigInfo();
78
        $content .= self::getUNameInfo();
79
        $content .= self::getArpInfo();
80
        $content .= self::getRouteInfo();
81
        $content .= self::getIptablesInfo();
82
        $content .= self::getPingInfo();
83
        $content .= self::getOpenSSLInfo();
84
        $content .= self::getAsteriskInfo();
85
        $content .= self::getChangedConfigFiles();
86
        $content .= self::getCorruptedFiles();
87
        $content .= PHP_EOL . PHP_EOL;
88
89
        return $content;
90
    }
91
92
    /**
93
     * Gets date time information
94
     *
95
     * @return string
96
     */
97
    public static function getDate(): string
98
    {
99
        $content    = '───────────────────────────────────────── Date ─────────────────────────────────────────';
100
        $content    .= PHP_EOL . PHP_EOL;
101
        $datePath = Util::which('date');
102
        $ut         = [];
103
        Processes::mwExec($datePath, $ut);
104
        $content .= implode(PHP_EOL, $ut). PHP_EOL;
105
        $content .= PHP_EOL . PHP_EOL;
106
        return $content;
107
    }
108
109
    /**
110
     * Returns PBX version
111
     *
112
     * @return string
113
     */
114
    private static function getPBXVersion(): string
115
    {
116
        $version = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION);
117
        $content = '─────────────────────────────────────── PBXVersion ───────────────────────────────────────';
118
        $content .= PHP_EOL . PHP_EOL;
119
        $content .= $version . PHP_EOL;
120
        $content .= PHP_EOL . PHP_EOL;
121
        return $content;
122
    }
123
124
    /**
125
     * Gets uptime information
126
     *
127
     * @return string
128
     */
129
    public static function getUpTime(): string
130
    {
131
        $content    = '───────────────────────────────────────── Uptime ─────────────────────────────────────────';
132
        $content    .= PHP_EOL . PHP_EOL;
133
        $uptimePath = Util::which('uptime');
134
        $ut         = [];
135
        Processes::mwExec($uptimePath, $ut);
136
        $uptime  = implode(PHP_EOL, $ut);
137
        $content .= $uptime . PHP_EOL;
138
        $content .= PHP_EOL . PHP_EOL;
139
        return $content;
140
    }
141
142
    /**
143
     * Gets CPU lod information
144
     *
145
     * @return string
146
     */
147
    public static function getCpu(): string
148
    {
149
        $content    = '───────────────────────────────────────── CPU load ───────────────────────────────────────';
150
        $content    .= PHP_EOL . PHP_EOL;
151
        $ut         = [];
152
        $grepPath   = Util::which('grep');
153
        $mpstatPath = Util::which('mpstat');
154
        Processes::mwExec("{$mpstatPath} | {$grepPath} all", $ut);
155
        preg_match("/^.*\s+all\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+(.*)\s*.*/i", $ut[0], $matches);
156
        $rv = 100 - $matches[1];
157
158
        if (100 < $rv) {
159
            $rv = 100;
160
        }
161
162
        $content .= round($rv, 2) . PHP_EOL;
163
        $content .= PHP_EOL . PHP_EOL;
164
        return $content;
165
    }
166
167
    /**
168
     * Gets system memory information
169
     *
170
     * @return string
171
     */
172
    public static function getMemInfo(): string
173
    {
174
        $content  = '───────────────────────────────────────── MemInfo ────────────────────────────────────────';
175
        $content  .= PHP_EOL . PHP_EOL;
176
        $out      = [];
177
        $catPath  = Util::which('cat');
178
        $grepPath = Util::which('grep');
179
        $awkPath  = Util::which('awk');
180
        $freePath = Util::which('free');
181
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'Inactive:' | {$awkPath} '{print $2}'", $out);
182
        $inactive = round((1 * implode($out)) / 1024, 2);
183
        $content  .= "inactive = {$inactive}" . PHP_EOL;
184
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemFree:' | {$awkPath} '{print $2}'", $out);
185
        $free    = round((1 * implode($out)) / 1024, 2);
186
        $content .= "free = {$free}" . PHP_EOL;
187
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemTotal:' | {$awkPath} '{print $2}'", $out);
188
        $total   = round((1 * implode($out)) / 1024, 2);
189
        $content .= "total = {$total}" . PHP_EOL . PHP_EOL;
190
191
        $content .= '────────────────────────────────────────── Free ─────────────────────────────────────────';
192
        $content .= PHP_EOL . PHP_EOL;
193
        Processes::mwExec($freePath, $out);
194
        $content .= implode(PHP_EOL, $out) . PHP_EOL;
195
        $content .= PHP_EOL . PHP_EOL;
196
        return $content;
197
    }
198
199
    /**
200
     * Returns df -h information
201
     *
202
     * @return string
203
     */
204
    private static function getStorageInfo(): string
205
    {
206
        $content = '─────────────────────────────────────────── df ───────────────────────────────────────────';
207
        $content .= PHP_EOL . PHP_EOL;
208
        $dfPath  = Util::which('df');
209
        $out     = [];
210
        Processes::mwExec("{$dfPath} -h", $out);
211
        $dfOut   = implode(PHP_EOL, $out);
212
        $content .= $dfOut . PHP_EOL;
213
        $content .= PHP_EOL . PHP_EOL;
214
        return $content;
215
    }
216
217
    /**
218
     * Returns ifconfig information
219
     *
220
     * @return string
221
     */
222
    private static function getIfconfigInfo(): string
223
    {
224
        $content      = '─────────────────────────────────────── ifconfig ──────────────────────────────────────';
225
        $content     .= PHP_EOL . PHP_EOL;
226
        $ifconfig  = Util::which('ifconfig');
227
        $out          = [];
228
        Processes::mwExec("$ifconfig", $out);
229
        $ifconfigOut  = implode(PHP_EOL, $out);
230
        $content     .= $ifconfigOut . PHP_EOL;
231
        $content .= PHP_EOL . PHP_EOL;
232
        return $content;
233
    }
234
235
    /**
236
     * Returns arp information
237
     *
238
     * @return string
239
     */
240
    private static function getArpInfo(): string
241
    {
242
        $content = '─────────────────────────────────────────── arp ──────────────────────────────────────────';
243
        $content .= PHP_EOL . PHP_EOL;
244
        $arp = Util::which('arp');
245
        $out      = [];
246
        Processes::mwExec("$arp", $out);
247
        $arpOut   = implode(PHP_EOL, $out);
248
        $content .= $arpOut . PHP_EOL;
249
        $content .= PHP_EOL . PHP_EOL;
250
        return $content;
251
    }
252
253
    /**
254
     * Returns uname information
255
     *
256
     * @return string
257
     */
258
    private static function getUNameInfo(): string
259
    {
260
        $content = '─────────────────────────────────────────── OS Name ──────────────────────────────────────────';
261
        $content .= PHP_EOL . PHP_EOL;
262
        $uname = Util::which('uname');
263
        $out      = [];
264
        Processes::mwExec("$uname -a", $out);
265
        $arpOut   = implode(PHP_EOL, $out);
266
        $content .= $arpOut . PHP_EOL;
267
        $content .= PHP_EOL . PHP_EOL;
268
        return $content;
269
    }
270
271
    /**
272
     * Returns route information
273
     *
274
     * @return string
275
     */
276
    private static function getRouteInfo(): string
277
    {
278
        $content   = '────────────────────────────────────────── route ─────────────────────────────────────────';
279
        $content   .= PHP_EOL . PHP_EOL;
280
        $route = Util::which('route');
281
        $out       = [];
282
        Processes::mwExec($route, $out);
283
        $routeOut = implode(PHP_EOL, $out);
284
        $content  .= $routeOut . PHP_EOL;
285
        $content .= PHP_EOL . PHP_EOL;
286
        return $content;
287
    }
288
289
    /**
290
     * Returns iptables information
291
     *
292
     * @return string
293
     */
294
    private static function getIptablesInfo(): string
295
    {
296
        $content      = '────────────────────────────────────────── iptables ──────────────────────────────────────';
297
        $content      .= PHP_EOL . PHP_EOL;
298
        $iptablesPath = Util::which('iptables');
299
        $out          = [];
300
        Processes::mwExec("{$iptablesPath} -S", $out);
301
        $iptablesOut = implode(PHP_EOL, $out);
302
        $content     .= $iptablesOut . PHP_EOL;
303
        $content .= PHP_EOL . PHP_EOL;
304
        return $content;
305
    }
306
307
    /**
308
     * Returns ping result to 8.8.8.8 and ya.ru
309
     *
310
     * @return string
311
     */
312
    private static function getPingInfo(): string
313
    {
314
        $content  = '──────────────────────────────────────────── ping ────────────────────────────────────────';
315
        $content  .= PHP_EOL . PHP_EOL;
316
        $pingPath = Util::which('ping');
317
        $out      = [];
318
        Processes::mwExec("{$pingPath} 8.8.8.8 -w 1", $out);
319
        $pingOut = implode(PHP_EOL, $out);
320
        Processes::mwExec("{$pingPath} ya.ru -w 1", $out);
321
        $ping2Out = implode(PHP_EOL, $out);
322
        $content  .= $pingOut . PHP_EOL;
323
        $content  .= PHP_EOL . PHP_EOL;
324
        $content  .= $ping2Out . PHP_EOL;
325
        $content .= PHP_EOL . PHP_EOL;
326
        return $content;
327
    }
328
329
    /**
330
     * Returns openssl check license information
331
     *
332
     * @return string
333
     */
334
    private static function getOpenSSLInfo(): string
335
    {
336
        $opensslPath = Util::which('openssl');
337
        $timeoutPath = Util::which('timeout');
338
339
        $content = '─────────────────────────────────────── openssl ─────────────────────────────────────────';
340
        $content .= PHP_EOL . PHP_EOL;
341
        $out     = [];
342
        Processes::mwExec("{$timeoutPath} 1 {$opensslPath} s_client -connect lic.miko.ru:443", $out);
343
        $opensslOut = implode(PHP_EOL, $out);
344
        $content    .= $opensslOut . PHP_EOL;
345
        $content .= PHP_EOL . PHP_EOL;
346
        return $content;
347
    }
348
349
    /**
350
     * Returns asterisk information
351
     *
352
     * @return string
353
     */
354
    private static function getAsteriskInfo(): string
355
    {
356
        $asteriskPath = Util::which('asterisk');
357
358
        $content = '────────────────────────────────── asterisk registrations ────────────────────────────────';
359
        $content .= PHP_EOL . PHP_EOL;
360
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show registrations' ", $out);
361
        $asteriskOut = implode(PHP_EOL, $out);
362
        $content     .= $asteriskOut . PHP_EOL;
363
364
        $content .= '────────────────────────────────── asterisk endpoints ───────────────────────────────────';
365
        $content .= PHP_EOL . PHP_EOL;
366
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show endpoints' ", $out);
367
        $asteriskOut = implode(PHP_EOL, $out);
368
        $content     .= $asteriskOut . PHP_EOL;
369
370
        $content .= '─────────────────────────────────── asterisk contacts ───────────────────────────────────';
371
        $content .= PHP_EOL . PHP_EOL;
372
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show contacts' ", $out);
373
        $asteriskOut = implode(PHP_EOL, $out);
374
        $content     .= $asteriskOut . PHP_EOL;
375
        $content .= PHP_EOL . PHP_EOL;
376
        return $content;
377
    }
378
379
    /**
380
     * Returns changed config files
381
     *
382
     * @return string
383
     */
384
    private static function getChangedConfigFiles(): string
385
    {
386
        $content = '────────────────────────────────── Changed config files ─────────────────────────────────';
387
        $content .= PHP_EOL . PHP_EOL;
388
        $modeNone = CustomFiles::MODE_NONE;
389
        $files   = CustomFiles::find("mode!='$modeNone'");
390
        foreach ($files as $file) {
391
            $content .= "({$file->mode}){$file->filepath}" . PHP_EOL;
392
        }
393
        $content .= PHP_EOL . PHP_EOL;
394
        return $content;
395
    }
396
397
    /**
398
     * Returns changed core files
399
     *
400
     * @return string
401
     */
402
    private static function getCorruptedFiles(): string
403
    {
404
        $content = '──────────────────────────────────── Corrupted files ────────────────────────────────────';
405
        $content .= PHP_EOL . PHP_EOL;
406
        $files   = Main::checkForCorruptedFiles();
407
        foreach ($files as $file) {
408
            $content .= $file . PHP_EOL;
409
        }
410
        $content .= PHP_EOL . PHP_EOL;
411
        return $content;
412
    }
413
414
    /**
415
     * Prepare system environment information
416
     * @return string
417
     */
418
    private static function getEnvironment(): string
419
    {
420
        $content    = '───────────────────────────────────────── Environment ─────────────────────────────────────────';
421
        $content    .= PHP_EOL . PHP_EOL;
422
        $installationType = PbxSettings::getValueByKey(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE);
423
        if ($installationType){
424
            $content .= 'Machine hardware: '.$installationType . PHP_EOL;
425
        }
426
        $hyperVisor = GetHypervisorInfoAction::main();
427
        if ($hyperVisor->success) {
428
            $content .= "Hypervisor: {$hyperVisor->data['Hypervisor']}" . PHP_EOL;
429
        }
430
        $dmi = GetDMIInfoAction::main();
431
        if ($dmi->success) {
432
            $content .= "DMI: {$dmi->data['DMI']}" . PHP_EOL;
433
        }
434
        $content .= PHP_EOL . PHP_EOL;
435
        return $content;
436
    }
437
438
}