Passed
Push — develop ( 843492...60d531 )
by Nikolay
06:55 queued 12s
created

SysinfoManagementProcessor::getInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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