Passed
Push — develop ( 25b01c...9683b0 )
by Портнов
04:53
created

SysinfoManagementProcessor::getUNameInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 11
rs 9.9666
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * MikoPBX - free phone system for small business
4
 * Copyright (C) 2017-2020 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;
21
22
use Exception;
23
use MikoPBX\Common\Models\CustomFiles;
24
use MikoPBX\Common\Models\PbxSettings;
25
use MikoPBX\Core\System\Processes;
26
use MikoPBX\Core\System\Util;
27
use MikoPBX\Service\Main;
28
use Phalcon\Di;
29
use Phalcon\Di\Injectable;
30
31
class SysinfoManagementProcessor extends Injectable
32
{
33
    /**
34
     * Processes System requests
35
     *
36
     * @param array $request
37
     *
38
     * @return \MikoPBX\PBXCoreREST\Lib\PBXApiResult
39
     */
40
    public static function callBack(array $request): PBXApiResult
41
    {
42
        $action         = $request['action'];
43
        $res            = new PBXApiResult();
44
        $res->processor = __METHOD__;
45
        switch ($action) {
46
            case 'getInfo':
47
                $res = self::getInfo();
48
                break;
49
            case 'getExternalIpInfo':
50
                $res = self::getExternalIpInfo();
51
                break;
52
            default:
53
                $res             = new PBXApiResult();
54
                $res->processor  = __METHOD__;
55
                $res->messages[] = "Unknown action - {$action} in sysinfoCallBack";
56
        }
57
58
        $res->function = $action;
59
60
        return $res;
61
    }
62
63
    /**
64
     * Gets system information
65
     *
66
     * @return PBXApiResult
67
     */
68
    private static function getInfo(): PBXApiResult
69
    {
70
        $res            = new PBXApiResult();
71
        $res->processor = __METHOD__;
72
        $res->success   = true;
73
74
        $di           = Di::getDefault();
75
        $dirsConfig   = $di->getShared('config');
76
        $filenameTmp  = $dirsConfig->path('www.downloadCacheDir') . '/' . __FUNCTION__ . '_' . time() . '.txt';
77
78
        $content = self::prepareSysyinfoContent();
79
80
        file_put_contents($filenameTmp, $content);
81
        Util::addRegularWWWRights($filenameTmp);
82
        $res->data['filename'] = $filenameTmp;
83
        $res->processor        = __METHOD__;
84
85
        return $res;
86
    }
87
88
    /**
89
     * Prepares system information collection
90
     *
91
     * @return string
92
     */
93
    public static function prepareSysyinfoContent(): string
94
    {
95
        $content = self::getPBXVersion();
96
        $content .= self::getDate();
97
        $content .= self::getUpTime();
98
        $content .= self::getCpu();
99
        $content .= self::getMemInfo();
100
        $content .= self::getStorageInfo();
101
        $content .= self::getIfconfigInfo();
102
        $content .= self::getUNameInfo();
103
        $content .= self::getArpInfo();
104
        $content .= self::getRouteInfo();
105
        $content .= self::getIptablesInfo();
106
        $content .= self::getPingInfo();
107
        $content .= self::getOpenSSLInfo();
108
        $content .= self::getAsteriskInfo();
109
        $content .= self::getChangedConfigFiles();
110
        $content .= self::getCorruptedFiles();
111
        $content .= PHP_EOL . PHP_EOL;
112
113
        return $content;
114
    }
115
116
    /**
117
     * Gets date time information
118
     *
119
     * @return string
120
     */
121
    public static function getDate(): string
122
    {
123
        $content    = '───────────────────────────────────────── Date ─────────────────────────────────────────';
124
        $content    .= PHP_EOL . PHP_EOL;
125
        $datePath = Util::which('date');
126
        $ut         = [];
127
        Processes::mwExec($datePath, $ut);
128
        $content .= implode(PHP_EOL, $ut). PHP_EOL;
129
        $content .= PHP_EOL . PHP_EOL;
130
        return $content;
131
    }
132
133
    /**
134
     * Returns PBX version
135
     *
136
     * @return string
137
     */
138
    private static function getPBXVersion(): string
139
    {
140
        $version = PbxSettings::getValueByKey('PBXVersion');
141
        $content = '─────────────────────────────────────── PBXVersion ───────────────────────────────────────';
142
        $content .= PHP_EOL . PHP_EOL;
143
        $content .= $version . PHP_EOL;
144
        $content .= PHP_EOL . PHP_EOL;
145
        return $content;
146
    }
147
148
    /**
149
     * Gets uptime information
150
     *
151
     * @return string
152
     */
153
    public static function getUpTime(): string
154
    {
155
        $content    = '───────────────────────────────────────── Uptime ─────────────────────────────────────────';
156
        $content    .= PHP_EOL . PHP_EOL;
157
        $uptimePath = Util::which('uptime');
158
        $ut         = [];
159
        Processes::mwExec($uptimePath, $ut);
160
        $uptime  = implode(PHP_EOL, $ut);
161
        $content .= $uptime . PHP_EOL;
162
        $content .= PHP_EOL . PHP_EOL;
163
        return $content;
164
    }
165
166
    /**
167
     * Gets CPU lod information
168
     *
169
     * @return string
170
     */
171
    public static function getCpu(): string
172
    {
173
        $content    = '───────────────────────────────────────── CPU load ───────────────────────────────────────';
174
        $content    .= PHP_EOL . PHP_EOL;
175
        $ut         = [];
176
        $grepPath   = Util::which('grep');
177
        $mpstatPath = Util::which('mpstat');
178
        Processes::mwExec("{$mpstatPath} | {$grepPath} all", $ut);
179
        preg_match("/^.*\s+all\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+(.*)\s*.*/i", $ut[0], $matches);
180
        $rv = 100 - $matches[1];
181
182
        if (100 < $rv) {
183
            $rv = 100;
184
        }
185
186
        $content .= round($rv, 2) . PHP_EOL;
187
        $content .= PHP_EOL . PHP_EOL;
188
        return $content;
189
    }
190
191
    /**
192
     * Gets system memory information
193
     *
194
     * @return string
195
     */
196
    public static function getMemInfo(): string
197
    {
198
        $content  = '───────────────────────────────────────── MemInfo ────────────────────────────────────────';
199
        $content  .= PHP_EOL . PHP_EOL;
200
        $out      = [];
201
        $catPath  = Util::which('cat');
202
        $grepPath = Util::which('grep');
203
        $awkPath  = Util::which('awk');
204
        $freePath = Util::which('free');
205
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'Inactive:' | {$awkPath} '{print $2}'", $out);
206
        $inactive = round((1 * implode($out)) / 1024, 2);
207
        $content  .= "inactive = {$inactive}" . PHP_EOL;
208
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemFree:' | {$awkPath} '{print $2}'", $out);
209
        $free    = round((1 * implode($out)) / 1024, 2);
210
        $content .= "free = {$free}" . PHP_EOL;
211
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemTotal:' | {$awkPath} '{print $2}'", $out);
212
        $total   = round((1 * implode($out)) / 1024, 2);
213
        $content .= "total = {$total}" . PHP_EOL . PHP_EOL;
214
215
        $content .= '────────────────────────────────────────── Free ─────────────────────────────────────────';
216
        $content .= PHP_EOL . PHP_EOL;
217
        Processes::mwExec($freePath, $out);
218
        $content .= implode(PHP_EOL, $out) . PHP_EOL;
219
        $content .= PHP_EOL . PHP_EOL;
220
        return $content;
221
    }
222
223
    /**
224
     * Returns df -h information
225
     *
226
     * @return string
227
     */
228
    private static function getStorageInfo(): string
229
    {
230
        $content = '─────────────────────────────────────────── df ───────────────────────────────────────────';
231
        $content .= PHP_EOL . PHP_EOL;
232
        $dfPath  = Util::which('df');
233
        $out     = [];
234
        Processes::mwExec("{$dfPath} -h", $out);
235
        $dfOut   = implode(PHP_EOL, $out);
236
        $content .= $dfOut . PHP_EOL;
237
        $content .= PHP_EOL . PHP_EOL;
238
        return $content;
239
    }
240
241
    /**
242
     * Returns ifconfig information
243
     *
244
     * @return string
245
     */
246
    private static function getIfconfigInfo(): string
247
    {
248
        $content      = '─────────────────────────────────────── ifconfig ──────────────────────────────────────';
249
        $content     .= PHP_EOL . PHP_EOL;
250
        $busyboxPath  = Util::which('busybox');
251
        $out          = [];
252
        Processes::mwExec("$busyboxPath ifconfig", $out);
253
        $ifconfigOut  = implode(PHP_EOL, $out);
254
        $content     .= $ifconfigOut . PHP_EOL;
255
        $content .= PHP_EOL . PHP_EOL;
256
        return $content;
257
    }
258
259
    /**
260
     * Returns arp information
261
     *
262
     * @return string
263
     */
264
    private static function getArpInfo(): string
265
    {
266
        $content = '─────────────────────────────────────────── arp ──────────────────────────────────────────';
267
        $content .= PHP_EOL . PHP_EOL;
268
        $busyboxPath = Util::which('busybox');
269
        $out      = [];
270
        Processes::mwExec("$busyboxPath arp", $out);
271
        $arpOut   = implode(PHP_EOL, $out);
272
        $content .= $arpOut . PHP_EOL;
273
        $content .= PHP_EOL . PHP_EOL;
274
        return $content;
275
    }
276
277
    /**
278
     * Returns uname information
279
     *
280
     * @return string
281
     */
282
    private static function getUNameInfo(): string
283
    {
284
        $content = '─────────────────────────────────────────── OS Name ──────────────────────────────────────────';
285
        $content .= PHP_EOL . PHP_EOL;
286
        $busyboxPath = Util::which('busybox');
287
        $out      = [];
288
        Processes::mwExec("$busyboxPath uname -a", $out);
289
        $arpOut   = implode(PHP_EOL, $out);
290
        $content .= $arpOut . PHP_EOL;
291
        $content .= PHP_EOL . PHP_EOL;
292
        return $content;
293
    }
294
295
    /**
296
     * Returns route information
297
     *
298
     * @return string
299
     */
300
    private static function getRouteInfo(): string
301
    {
302
        $content   = '────────────────────────────────────────── route ─────────────────────────────────────────';
303
        $content   .= PHP_EOL . PHP_EOL;
304
        $busyboxPath = Util::which('busybox');
305
        $out       = [];
306
        Processes::mwExec("$busyboxPath route", $out);
307
        $routeOut = implode(PHP_EOL, $out);
308
        $content  .= $routeOut . PHP_EOL;
309
        $content .= PHP_EOL . PHP_EOL;
310
        return $content;
311
    }
312
313
    /**
314
     * Returns iptables information
315
     *
316
     * @return string
317
     */
318
    private static function getIptablesInfo(): string
319
    {
320
        $content      = '────────────────────────────────────────── iptables ──────────────────────────────────────';
321
        $content      .= PHP_EOL . PHP_EOL;
322
        $iptablesPath = Util::which('iptables');
323
        $out          = [];
324
        Processes::mwExec("{$iptablesPath} -S", $out);
325
        $iptablesOut = implode(PHP_EOL, $out);
326
        $content     .= $iptablesOut . PHP_EOL;
327
        $content .= PHP_EOL . PHP_EOL;
328
        return $content;
329
    }
330
331
    /**
332
     * Returns ping result to 8.8.8.8 and ya.ru
333
     *
334
     * @return string
335
     */
336
    private static function getPingInfo(): string
337
    {
338
        $content  = '──────────────────────────────────────────── ping ────────────────────────────────────────';
339
        $content  .= PHP_EOL . PHP_EOL;
340
        $pingPath = Util::which('ping');
341
        $out      = [];
342
        Processes::mwExec("{$pingPath} 8.8.8.8 -w 1", $out);
343
        $pingOut = implode(PHP_EOL, $out);
344
        Processes::mwExec("{$pingPath} ya.ru -w 1", $out);
345
        $ping2Out = implode(PHP_EOL, $out);
346
        $content  .= $pingOut . PHP_EOL;
347
        $content  .= PHP_EOL . PHP_EOL;
348
        $content  .= $ping2Out . PHP_EOL;
349
        $content .= PHP_EOL . PHP_EOL;
350
        return $content;
351
    }
352
353
    /**
354
     * Returns openssl check license information
355
     *
356
     * @return string
357
     */
358
    private static function getOpenSSLInfo(): string
359
    {
360
        $opensslPath = Util::which('openssl');
361
        $timeoutPath = Util::which('timeout');
362
363
        $content = '─────────────────────────────────────── openssl ─────────────────────────────────────────';
364
        $content .= PHP_EOL . PHP_EOL;
365
        $out     = [];
366
        Processes::mwExec("{$timeoutPath} 1 {$opensslPath} s_client -connect lic.miko.ru:443", $out);
367
        $opensslOut = implode(PHP_EOL, $out);
368
        $content    .= $opensslOut . PHP_EOL;
369
        $content .= PHP_EOL . PHP_EOL;
370
        return $content;
371
    }
372
373
    /**
374
     * Returns asterisk information
375
     *
376
     * @return string
377
     */
378
    private static function getAsteriskInfo(): string
379
    {
380
        $asteriskPath = Util::which('asterisk');
381
382
        $content = '────────────────────────────────── asterisk registrations ────────────────────────────────';
383
        $content .= PHP_EOL . PHP_EOL;
384
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show registrations' ", $out);
385
        $asteriskOut = implode(PHP_EOL, $out);
386
        $content     .= $asteriskOut . PHP_EOL;
387
388
        $content .= '────────────────────────────────── asterisk endpoints ───────────────────────────────────';
389
        $content .= PHP_EOL . PHP_EOL;
390
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show endpoints' ", $out);
391
        $asteriskOut = implode(PHP_EOL, $out);
392
        $content     .= $asteriskOut . PHP_EOL;
393
394
        $content .= '─────────────────────────────────── asterisk contacts ───────────────────────────────────';
395
        $content .= PHP_EOL . PHP_EOL;
396
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show contacts' ", $out);
397
        $asteriskOut = implode(PHP_EOL, $out);
398
        $content     .= $asteriskOut . PHP_EOL;
399
        $content .= PHP_EOL . PHP_EOL;
400
        return $content;
401
    }
402
403
    /**
404
     * Returns changed config files
405
     *
406
     * @return string
407
     */
408
    private static function getChangedConfigFiles(): string
409
    {
410
        $content = '────────────────────────────────── Changed config files ─────────────────────────────────';
411
        $content .= PHP_EOL . PHP_EOL;
412
        $files   = CustomFiles::find('mode!="none"');
413
        foreach ($files as $file) {
414
            $content .= "({$file->mode}){$file->filepath}" . PHP_EOL;
415
        }
416
        $content .= PHP_EOL . PHP_EOL;
417
        return $content;
418
    }
419
420
    /**
421
     * Returns changed core files
422
     *
423
     * @return string
424
     */
425
    private static function getCorruptedFiles(): string
426
    {
427
        $content = '──────────────────────────────────── Corrupted files ────────────────────────────────────';
428
        $content .= PHP_EOL . PHP_EOL;
429
        $files   = Main::checkForCorruptedFiles();
430
        foreach ($files as $file) {
431
            $content .= $file . PHP_EOL;
432
        }
433
        $content .= PHP_EOL . PHP_EOL;
434
        return $content;
435
    }
436
437
    /**
438
     * Returns public IP address of this system
439
     *
440
     * @return PBXApiResult
441
     */
442
    public static function getExternalIpInfo(): PBXApiResult
443
    {
444
        $res            = new PBXApiResult();
445
        $res->processor = __METHOD__;
446
447
        $curl = curl_init();
448
        if ($curl === false) {
449
            $res->messages[] = 'CURL initialization error';
450
451
            return $res;
452
        }
453
        $url = 'https://ipinfo.io/json';
454
        curl_setopt($curl, CURLOPT_URL, $url);
455
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
456
        curl_setopt($curl, CURLOPT_TIMEOUT, 2);
457
458
        try {
459
            $resultRequest = curl_exec($curl);
460
        } catch (Exception $e) {
461
            $res->messages[] = $e->getMessage();
462
463
            return $res;
464
        }
465
        curl_close($curl);
466
        if (is_string($resultRequest)
467
            && Util::isJson($resultRequest)) {
468
            $res->success    = true;
469
            $response        = json_decode($resultRequest, true);
470
            $res->data['ip'] = $response['ip'];
471
        } else {
472
            $res->messages[] = 'Error format data ';
473
        }
474
475
        return $res;
476
    }
477
478
}