Passed
Push — develop ( 368e3d...115c7b )
by Nikolay
12:16
created

GetInfoAction::getIfconfigInfo()   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 © 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::getDate();
72
        $content .= self::getUpTime();
73
        $content .= self::getCpu();
74
        $content .= self::getMemInfo();
75
        $content .= self::getStorageInfo();
76
        $content .= self::getIfconfigInfo();
77
        $content .= self::getUNameInfo();
78
        $content .= self::getArpInfo();
79
        $content .= self::getRouteInfo();
80
        $content .= self::getIptablesInfo();
81
        $content .= self::getPingInfo();
82
        $content .= self::getOpenSSLInfo();
83
        $content .= self::getAsteriskInfo();
84
        $content .= self::getChangedConfigFiles();
85
        $content .= self::getCorruptedFiles();
86
        $content .= PHP_EOL . PHP_EOL;
87
88
        return $content;
89
    }
90
91
    /**
92
     * Gets date time information
93
     *
94
     * @return string
95
     */
96
    public static function getDate(): string
97
    {
98
        $content    = '───────────────────────────────────────── Date ─────────────────────────────────────────';
99
        $content    .= PHP_EOL . PHP_EOL;
100
        $datePath = Util::which('date');
101
        $ut         = [];
102
        Processes::mwExec($datePath, $ut);
103
        $content .= implode(PHP_EOL, $ut). PHP_EOL;
104
        $content .= PHP_EOL . PHP_EOL;
105
        return $content;
106
    }
107
108
    /**
109
     * Returns PBX version
110
     *
111
     * @return string
112
     */
113
    private static function getPBXVersion(): string
114
    {
115
        $version = PbxSettings::getValueByKey(PbxSettingsConstants::PBX_VERSION);
116
        $installationType = PbxSettings::getValueByKey(PbxSettingsConstants::VIRTUAL_HARDWARE_TYPE);
117
        $content = '─────────────────────────────────────── PBXVersion ───────────────────────────────────────';
118
        $content .= PHP_EOL . PHP_EOL;
119
        $content .= $version . PHP_EOL;
120
        if ($installationType){
121
            $content .= 'Machine environment: '.$installationType . PHP_EOL;
122
        }
123
        $content .= PHP_EOL . PHP_EOL;
124
        return $content;
125
    }
126
127
    /**
128
     * Gets uptime information
129
     *
130
     * @return string
131
     */
132
    public static function getUpTime(): string
133
    {
134
        $content    = '───────────────────────────────────────── Uptime ─────────────────────────────────────────';
135
        $content    .= PHP_EOL . PHP_EOL;
136
        $uptimePath = Util::which('uptime');
137
        $ut         = [];
138
        Processes::mwExec($uptimePath, $ut);
139
        $uptime  = implode(PHP_EOL, $ut);
140
        $content .= $uptime . PHP_EOL;
141
        $content .= PHP_EOL . PHP_EOL;
142
        return $content;
143
    }
144
145
    /**
146
     * Gets CPU lod information
147
     *
148
     * @return string
149
     */
150
    public static function getCpu(): string
151
    {
152
        $content    = '───────────────────────────────────────── CPU load ───────────────────────────────────────';
153
        $content    .= PHP_EOL . PHP_EOL;
154
        $ut         = [];
155
        $grepPath   = Util::which('grep');
156
        $mpstatPath = Util::which('mpstat');
157
        Processes::mwExec("{$mpstatPath} | {$grepPath} all", $ut);
158
        preg_match("/^.*\s+all\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+.*\s+(.*)\s*.*/i", $ut[0], $matches);
159
        $rv = 100 - $matches[1];
160
161
        if (100 < $rv) {
162
            $rv = 100;
163
        }
164
165
        $content .= round($rv, 2) . PHP_EOL;
166
        $content .= PHP_EOL . PHP_EOL;
167
        return $content;
168
    }
169
170
    /**
171
     * Gets system memory information
172
     *
173
     * @return string
174
     */
175
    public static function getMemInfo(): string
176
    {
177
        $content  = '───────────────────────────────────────── MemInfo ────────────────────────────────────────';
178
        $content  .= PHP_EOL . PHP_EOL;
179
        $out      = [];
180
        $catPath  = Util::which('cat');
181
        $grepPath = Util::which('grep');
182
        $awkPath  = Util::which('awk');
183
        $freePath = Util::which('free');
184
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'Inactive:' | {$awkPath} '{print $2}'", $out);
185
        $inactive = round((1 * implode($out)) / 1024, 2);
186
        $content  .= "inactive = {$inactive}" . PHP_EOL;
187
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemFree:' | {$awkPath} '{print $2}'", $out);
188
        $free    = round((1 * implode($out)) / 1024, 2);
189
        $content .= "free = {$free}" . PHP_EOL;
190
        Processes::mwExec("{$catPath} /proc/meminfo | {$grepPath} -C 0 'MemTotal:' | {$awkPath} '{print $2}'", $out);
191
        $total   = round((1 * implode($out)) / 1024, 2);
192
        $content .= "total = {$total}" . PHP_EOL . PHP_EOL;
193
194
        $content .= '────────────────────────────────────────── Free ─────────────────────────────────────────';
195
        $content .= PHP_EOL . PHP_EOL;
196
        Processes::mwExec($freePath, $out);
197
        $content .= implode(PHP_EOL, $out) . PHP_EOL;
198
        $content .= PHP_EOL . PHP_EOL;
199
        return $content;
200
    }
201
202
    /**
203
     * Returns df -h information
204
     *
205
     * @return string
206
     */
207
    private static function getStorageInfo(): string
208
    {
209
        $content = '─────────────────────────────────────────── df ───────────────────────────────────────────';
210
        $content .= PHP_EOL . PHP_EOL;
211
        $dfPath  = Util::which('df');
212
        $out     = [];
213
        Processes::mwExec("{$dfPath} -h", $out);
214
        $dfOut   = implode(PHP_EOL, $out);
215
        $content .= $dfOut . PHP_EOL;
216
        $content .= PHP_EOL . PHP_EOL;
217
        return $content;
218
    }
219
220
    /**
221
     * Returns ifconfig information
222
     *
223
     * @return string
224
     */
225
    private static function getIfconfigInfo(): string
226
    {
227
        $content      = '─────────────────────────────────────── ifconfig ──────────────────────────────────────';
228
        $content     .= PHP_EOL . PHP_EOL;
229
        $busyboxPath  = Util::which('busybox');
230
        $out          = [];
231
        Processes::mwExec("$busyboxPath ifconfig", $out);
232
        $ifconfigOut  = implode(PHP_EOL, $out);
233
        $content     .= $ifconfigOut . PHP_EOL;
234
        $content .= PHP_EOL . PHP_EOL;
235
        return $content;
236
    }
237
238
    /**
239
     * Returns arp information
240
     *
241
     * @return string
242
     */
243
    private static function getArpInfo(): string
244
    {
245
        $content = '─────────────────────────────────────────── arp ──────────────────────────────────────────';
246
        $content .= PHP_EOL . PHP_EOL;
247
        $busyboxPath = Util::which('busybox');
248
        $out      = [];
249
        Processes::mwExec("$busyboxPath arp", $out);
250
        $arpOut   = implode(PHP_EOL, $out);
251
        $content .= $arpOut . PHP_EOL;
252
        $content .= PHP_EOL . PHP_EOL;
253
        return $content;
254
    }
255
256
    /**
257
     * Returns uname information
258
     *
259
     * @return string
260
     */
261
    private static function getUNameInfo(): string
262
    {
263
        $content = '─────────────────────────────────────────── OS Name ──────────────────────────────────────────';
264
        $content .= PHP_EOL . PHP_EOL;
265
        $busyboxPath = Util::which('busybox');
266
        $out      = [];
267
        Processes::mwExec("$busyboxPath uname -a", $out);
268
        $arpOut   = implode(PHP_EOL, $out);
269
        $content .= $arpOut . PHP_EOL;
270
        $content .= PHP_EOL . PHP_EOL;
271
        return $content;
272
    }
273
274
    /**
275
     * Returns route information
276
     *
277
     * @return string
278
     */
279
    private static function getRouteInfo(): string
280
    {
281
        $content   = '────────────────────────────────────────── route ─────────────────────────────────────────';
282
        $content   .= PHP_EOL . PHP_EOL;
283
        $busyboxPath = Util::which('busybox');
284
        $out       = [];
285
        Processes::mwExec("$busyboxPath route", $out);
286
        $routeOut = implode(PHP_EOL, $out);
287
        $content  .= $routeOut . PHP_EOL;
288
        $content .= PHP_EOL . PHP_EOL;
289
        return $content;
290
    }
291
292
    /**
293
     * Returns iptables information
294
     *
295
     * @return string
296
     */
297
    private static function getIptablesInfo(): string
298
    {
299
        $content      = '────────────────────────────────────────── iptables ──────────────────────────────────────';
300
        $content      .= PHP_EOL . PHP_EOL;
301
        $iptablesPath = Util::which('iptables');
302
        $out          = [];
303
        Processes::mwExec("{$iptablesPath} -S", $out);
304
        $iptablesOut = implode(PHP_EOL, $out);
305
        $content     .= $iptablesOut . PHP_EOL;
306
        $content .= PHP_EOL . PHP_EOL;
307
        return $content;
308
    }
309
310
    /**
311
     * Returns ping result to 8.8.8.8 and ya.ru
312
     *
313
     * @return string
314
     */
315
    private static function getPingInfo(): string
316
    {
317
        $content  = '──────────────────────────────────────────── ping ────────────────────────────────────────';
318
        $content  .= PHP_EOL . PHP_EOL;
319
        $pingPath = Util::which('ping');
320
        $out      = [];
321
        Processes::mwExec("{$pingPath} 8.8.8.8 -w 1", $out);
322
        $pingOut = implode(PHP_EOL, $out);
323
        Processes::mwExec("{$pingPath} ya.ru -w 1", $out);
324
        $ping2Out = implode(PHP_EOL, $out);
325
        $content  .= $pingOut . PHP_EOL;
326
        $content  .= PHP_EOL . PHP_EOL;
327
        $content  .= $ping2Out . PHP_EOL;
328
        $content .= PHP_EOL . PHP_EOL;
329
        return $content;
330
    }
331
332
    /**
333
     * Returns openssl check license information
334
     *
335
     * @return string
336
     */
337
    private static function getOpenSSLInfo(): string
338
    {
339
        $opensslPath = Util::which('openssl');
340
        $timeoutPath = Util::which('timeout');
341
342
        $content = '─────────────────────────────────────── openssl ─────────────────────────────────────────';
343
        $content .= PHP_EOL . PHP_EOL;
344
        $out     = [];
345
        Processes::mwExec("{$timeoutPath} 1 {$opensslPath} s_client -connect lic.miko.ru:443", $out);
346
        $opensslOut = implode(PHP_EOL, $out);
347
        $content    .= $opensslOut . PHP_EOL;
348
        $content .= PHP_EOL . PHP_EOL;
349
        return $content;
350
    }
351
352
    /**
353
     * Returns asterisk information
354
     *
355
     * @return string
356
     */
357
    private static function getAsteriskInfo(): string
358
    {
359
        $asteriskPath = Util::which('asterisk');
360
361
        $content = '────────────────────────────────── asterisk registrations ────────────────────────────────';
362
        $content .= PHP_EOL . PHP_EOL;
363
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show registrations' ", $out);
364
        $asteriskOut = implode(PHP_EOL, $out);
365
        $content     .= $asteriskOut . PHP_EOL;
366
367
        $content .= '────────────────────────────────── asterisk endpoints ───────────────────────────────────';
368
        $content .= PHP_EOL . PHP_EOL;
369
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show endpoints' ", $out);
370
        $asteriskOut = implode(PHP_EOL, $out);
371
        $content     .= $asteriskOut . PHP_EOL;
372
373
        $content .= '─────────────────────────────────── asterisk contacts ───────────────────────────────────';
374
        $content .= PHP_EOL . PHP_EOL;
375
        Processes::mwExec("{$asteriskPath} -rx 'pjsip show contacts' ", $out);
376
        $asteriskOut = implode(PHP_EOL, $out);
377
        $content     .= $asteriskOut . PHP_EOL;
378
        $content .= PHP_EOL . PHP_EOL;
379
        return $content;
380
    }
381
382
    /**
383
     * Returns changed config files
384
     *
385
     * @return string
386
     */
387
    private static function getChangedConfigFiles(): string
388
    {
389
        $content = '────────────────────────────────── Changed config files ─────────────────────────────────';
390
        $content .= PHP_EOL . PHP_EOL;
391
        $modeNone = CustomFiles::MODE_NONE;
392
        $files   = CustomFiles::find("mode!='$modeNone'");
393
        foreach ($files as $file) {
394
            $content .= "({$file->mode}){$file->filepath}" . PHP_EOL;
395
        }
396
        $content .= PHP_EOL . PHP_EOL;
397
        return $content;
398
    }
399
400
    /**
401
     * Returns changed core files
402
     *
403
     * @return string
404
     */
405
    private static function getCorruptedFiles(): string
406
    {
407
        $content = '──────────────────────────────────── Corrupted files ────────────────────────────────────';
408
        $content .= PHP_EOL . PHP_EOL;
409
        $files   = Main::checkForCorruptedFiles();
410
        foreach ($files as $file) {
411
            $content .= $file . PHP_EOL;
412
        }
413
        $content .= PHP_EOL . PHP_EOL;
414
        return $content;
415
    }
416
417
}