Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
3100 rexy 1
<?php
2
/**
3
 * SSH Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI SSH class
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
10
 * @copyright 2012 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
12
 * @version   SVN: $Id: class.SSH.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * SSH sysinfo class
17
 * get all the required information from SSH
18
 *
19
 * @category  PHP
20
 * @package   PSI SSH class
21
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
22
 * @copyright 2022 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class SSH extends GNU
28
{
29
    /**
30
     * content of the system status
31
     *
32
     * @var string
33
     */
34
    private $_sysstatus = null;
35
 
36
    /**
37
     * content of the system performance status
38
     *
39
     * @var string
40
     */
41
    private $_sysperformance = null;
42
 
43
    /**
44
     * content of the sys ver systeminfo
45
     *
46
     * @var string
47
     */
48
    private $_sysversysteminfo = null;
49
 
50
    /**
51
     * content of the show status
52
     *
53
     * @var string
54
     */
55
    private $_showstatus = null;
56
 
57
    /**
58
     * OS type
59
     *
60
     * @var string
61
     */
62
    private $_ostype = null;
63
 
64
    /**
65
     * check system type
66
     */
67
    public function __construct($blockname = false)
68
    {
69
        parent::__construct($blockname);
70
        $this->_ostype = $this->sys->getOS();
71
        switch ($this->_ostype) {
72
        case '4.2BSD':
73
        case 'AIX':
74
        case 'Darwin':
75
        case 'DragonFly':
76
        case 'FreeBSD':
77
        case 'HI-UX/MPP':
78
        case 'Haiku':
79
        case 'Minix':
80
        case 'NetBSD':
81
        case 'OpenBSD':
82
        case 'QNX':
83
        case 'SunOS':
84
            $this->error->addError("__construct()", "OS ".$this->_ostype. " is not supported via SSH");
85
            break;
86
        case 'GNU':
87
        case 'Linux':
88
            break;
89
        default:
90
            if ($this->getSystemStatus() !== '') {
91
                $this->_ostype = 'FortiOS';
92
                $this->sys->setOS('Linux');
93
            } elseif ($this->getSysVerSysteminfo() !== '') {
94
                $this->_ostype = 'DrayOS';
95
                $this->sys->setOS('DrayOS');
96
            }
97
        }
98
    }
99
 
100
    /**
101
     * get os specific encoding
102
     *
103
     * @see PSI_Interface_OS::getEncoding()
104
     *
105
     * @return string
106
     */
107
    public function getEncoding()
108
    {
109
//        if (($this->_ostype === 'FortiOS') || ($this->_ostype === 'DrayOS') || ($this->_ostype === 'SSH')) {
110
//            return 'UTF-8';
111
//        }
112
        //return null;
113
    }
114
 
115
    /**
116
     * get os specific language
117
     *
118
     * @see PSI_Interface_OS::getLanguage()
119
     *
120
     * @return string
121
     */
122
    public function getLanguage()
123
    {
124
        //return null;
125
    }
126
 
127
    private function getSystemStatus()
128
    {
129
        if ($this->_sysstatus === null) {
130
            if (CommonFunctions::executeProgram('get', 'system status', $resulte, false) && ($resulte !== "")
131
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
132
                $this->_sysstatus = substr($resulte, strlen($resulto[1][0]));
133
            } else {
134
                $this->_sysstatus =  '';
135
            }
136
        }
137
 
138
        return $this->_sysstatus;
139
    }
140
 
141
    private function getSystemPerformance()
142
    {
143
        if ($this->_sysperformance === null) {
144
            if (CommonFunctions::executeProgram('get', 'system performance status', $resulte, false) && ($resulte !== "")
145
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
146
                $this->_sysperformance = substr($resulte, strlen($resulto[1][0]));
147
            } else {
148
                $this->_sysperformance =  '';
149
            }
150
        }
151
 
152
        return $this->_sysperformance;
153
    }
154
 
155
    private function getSysVerSysteminfo()
156
    {
157
        if ($this->_sysversysteminfo === null) {
158
            if (CommonFunctions::executeProgram('sys', 'ver systeminfo', $resulte, false, PSI_EXEC_TIMEOUT_INT, '>') && ($resulte !== "")
159
               && preg_match('/([\s\S]+> sys ver systeminfo)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
160
                $this->_sysversysteminfo = substr($resulte, strlen($resulto[1][0]));
161
            } else {
162
                $this->_sysversysteminfo =  '';
163
            }
164
        }
165
 
166
        return $this->_sysversysteminfo;
167
    }
168
 
169
    private function getShowStatus()
170
    {
171
        if ($this->_showstatus === null) {
172
            if (CommonFunctions::executeProgram('show', 'status', $resulte, false, PSI_EXEC_TIMEOUT_INT, '>') && ($resulte !== "")
173
               && preg_match('/([\s\S]+> show status)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
174
                $this->_showstatus = substr($resulte, strlen($resulto[1][0]));
175
            } else {
176
                $this->_showstatus =  '';
177
            }
178
        }
179
 
180
        return $this->_showstatus;
181
    }
182
    /**
183
     * Physical memory information and Swap Space information
184
     *
185
     * @return void
186
     */
187
    protected function _memory($mbuf = null, $sbuf = null)
188
    {
189
        switch ($this->_ostype) {
190
        case 'FortiOS':
191
            if (CommonFunctions::executeProgram('get', 'hardware memory', $resulte, false) && ($resulte !== "")
192
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
193
                parent::_memory(substr($resulte, strlen($resulto[1][0])));
194
            }
195
            break;
196
        case 'DrayOS':
197
            if (($sysstat = $this->getSysVerSysteminfo()) !== '') {
198
                $machine= '';
199
                if (preg_match("/ Total memory usage : \d+ % \((\d+)K\/(\d+)K\)/", $sysstat, $buf)) {
200
                    $this->sys->setMemTotal($buf[2]*1024);
201
                    $this->sys->setMemUsed($buf[1]*1024);
202
                    $this->sys->setMemFree(($buf[2]-$buf[1])*1024);
203
                }
204
            }
205
            break;
206
        case 'GNU':
207
        case 'Linux':
208
            if (!CommonFunctions::executeProgram('cat', '/proc/meminfo', $mbuf, false) || ($mbuf === "")) {
209
                $mbuf = null;
210
            }
211
            if (!CommonFunctions::executeProgram('cat', '/proc/swaps', $sbuf, false) || ($sbuf === "")) {
212
                $sbuf = null;
213
            }
214
            if (($mbuf !== null) || ($sbuf !== null)) {
215
                parent::_memory($mbuf, $sbuf);
216
            }
217
        }
218
 
219
    }
220
 
221
    /**
222
     * USB devices
223
     *
224
     * @return void
225
     */
226
    protected function _usb($bufu = null)
227
    {
228
        switch ($this->_ostype) {
229
        case 'FortiOS':
230
            $bufr = '';
231
            if (CommonFunctions::executeProgram('fnsysctl', 'cat /proc/bus/usb/devices', $resulte, false) && ($resulte !== "")
232
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
233
                $resulti = substr($resulte, strlen($resulto[1][0]));
234
                if (preg_match('/(\n.*[\$#])$/', $resulti, $resulto, PREG_OFFSET_CAPTURE)) {
235
                    $bufr = substr($resulti, 0, $resulto[1][1]);
236
                    if (count(preg_split('/\n/', $bufr, -1, PREG_SPLIT_NO_EMPTY)) >= 2) {
237
                        parent::_usb($bufr);
238
                    }
239
                }
240
            }
241
            break;
242
        case 'GNU':
243
        case 'Linux':
244
            parent::_usb();
245
        }
246
    }
247
 
248
    /**
249
     * Network devices
250
     * includes also rx/tx bytes
251
     *
252
     * @return void
253
     */
254
    protected function _network($bufr = null)
255
    {
256
        switch ($this->_ostype) {
257
        case 'FortiOS':
258
            $bufr = '';
259
            if (CommonFunctions::executeProgram('fnsysctl', 'ifconfig', $resulte, false) && ($resulte !== "")
260
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
261
                $resulti = substr($resulte, strlen($resulto[1][0]));
262
                if (preg_match('/(\n.*[\$#])$/', $resulti, $resulto, PREG_OFFSET_CAPTURE)) {
263
                    $bufr = substr($resulti, 0, $resulto[1][1]);
264
                    if (count(preg_split('/\n/', $bufr, -1, PREG_SPLIT_NO_EMPTY)) < 2) {
265
                        $bufr = '';
266
                    }
267
                }
268
            }
269
 
270
            if ($bufr !== '') {
271
                parent::_network($bufr);
272
            } else {
273
                $netdevs = array();
274
                if (CommonFunctions::executeProgram('diagnose', 'ip address list', $resulte, false) && ($resulte !== "")
275
                   && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
276
                    $strBuf = substr($resulte, strlen($resulto[1][0]));
277
                    $lines = preg_split('/\n/', $strBuf, -1, PREG_SPLIT_NO_EMPTY);
278
                    foreach ($lines as $line) if (preg_match('/^IP=(\S+)->.+ devname=(\S+)$/', $line, $buf)) {
279
                        if (!isset($netdevs[$buf[2]])) {
280
                            $netdevs[$buf[2]] = $buf[1];
281
                        } else {
282
                            $netdevs[$buf[2]] .= ';'.$buf[1];
283
                        }
284
                    }
285
                }
286
                if (CommonFunctions::executeProgram('diagnose', 'ipv6 address list', $resulte, false) && ($resulte !== "")
287
                   && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
288
                    $strBuf = substr($resulte, strlen($resulto[1][0]));
289
                    $lines = preg_split('/\n/', $strBuf, -1, PREG_SPLIT_NO_EMPTY);
290
                    foreach ($lines as $line) if (preg_match('/ devname=(\S+) .+ addr=(\S+)/', $line, $buf)) {
291
                        if (!preg_match('/^fe80::/i', $buf[2])) {
292
                            if (!isset($netdevs[$buf[1]])) {
293
                                $netdevs[$buf[1]] = $buf[2];
294
                            } else {
295
                                $netdevs[$buf[1]] .= ';'.$buf[2];
296
                            }
297
                        }
298
                    }
299
                }
300
 
301
                foreach ($netdevs as $netname=>$netinfo) {
302
                    if (!preg_match('/^vsys_/i', $netname)) {
303
                        $dev = new NetDevice();
304
//                        if ($netname === 'root') {
305
//                            $dev->setName('lo');
306
//                        } else {
307
                            $dev->setName($netname);
308
//                        }
309
                        $this->sys->setNetDevices($dev);
310
                        $dev->setInfo($netinfo);
311
                    }
312
                }
313
            }
314
            break;
315
        case 'DrayOS':
316
            $macarray = array();
317
            if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR)) {
318
                if (CommonFunctions::executeProgram('sys', 'iface', $resulte, false, PSI_EXEC_TIMEOUT_INT, '>') && ($resulte !== "")
319
                   && preg_match('/([\s\S]+> sys iface)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
320
                    $lines = preg_split("/\n/", substr($resulte, strlen($resulto[1][0])), -1, PREG_SPLIT_NO_EMPTY);
321
                    $ipaddr = 'LAN';
322
                    foreach ($lines as $line) {
323
                        if (preg_match("/^IP Address:\s+([\d\.]+)\s/", trim($line), $ar_buf)) {
324
                            if ($ipaddr === false) {
325
                                $ipaddr = $ar_buf[1];
326
                            }
327
                        } elseif (preg_match("/^MAC:\s+([\d\-A-F]+)/", trim($line), $ar_buf)) {
328
                            if ($ipaddr !== '0.0.0.0') {
329
                                $macarray[$ipaddr] = $ar_buf[1];
330
                            }
331
                            $ipaddr = false;
332
                        }
333
                    }
334
                }
335
            }
336
 
337
            $lantxrate = false;
338
            $lanrxrate = false;
339
            if (defined('PSI_SHOW_NETWORK_ACTIVE_SPEED') && PSI_SHOW_NETWORK_ACTIVE_SPEED) {
340
                if ((($bufr = $this->getShowStatus()) !== '') && preg_match('/IP Address:[\d\.]+[ ]+Tx Rate:(\d+)[ ]+Rx Rate:(\d+)/m', $bufr, $ar_buf)) {
341
                    $lantxrate = $ar_buf[1];
342
                    $lanrxrate = $ar_buf[2];
343
                }
344
            }
345
 
346
            $notwaslan = true;
347
            if (CommonFunctions::executeProgram('show', 'lan', $resulte, false, PSI_EXEC_TIMEOUT_INT, '>') && ($resulte !== "")
348
               && preg_match('/([\s\S]+> show lan)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
349
                $lines = preg_split("/\n/", substr($resulte, strlen($resulto[1][0])), -1, PREG_SPLIT_NO_EMPTY);
350
                foreach ($lines as $line) {
351
                    if (preg_match("/^\[V\](\S+)\s+([\d\.]+)\s/", trim($line), $ar_buf)) {
352
                        $dev = new NetDevice();
353
                        $dev->setName($ar_buf[1]);
354
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
355
                            $dev->setInfo($ar_buf[2]);
356
                            if (isset($macarray['LAN'])) {
357
                                $dev->setInfo($macarray['LAN'].';'.$ar_buf[2]);
358
                            } else {
359
                                $dev->setInfo($ar_buf[2]);
360
                            }
361
                        }
362
                        if ($lantxrate !== false) {
363
                            $dev->setTxRate($lantxrate);
364
                        }
365
                        if ($lanrxrate !== false) {
366
                            $dev->setRxRate($lanrxrate);
367
                        }
368
                        $this->sys->setNetDevices($dev);
369
                        if (preg_match('/^LAN/', $ar_buf[1])) {
370
                            $notwaslan = false;
371
                        }
372
                    }
373
                }
374
            }
375
            if (($bufr = $this->getShowStatus()) !== '') {
376
                $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
377
                $last = false;
378
                $dev = null;
379
                foreach ($lines as $line) {
380
                    if (preg_match("/^(.+) Status/", trim($line), $ar_buf)) {
381
                        if (($last !== false) && (($last !== 'LAN') || $notwaslan)) {
382
                            $this->sys->setNetDevices($dev);
383
                        }
384
                        $dev = new NetDevice();
385
                        $last = preg_replace('/\s+/', '', $ar_buf[1]);
386
                        $dev->setName($last);
387
                    } else {
388
                        if ($last !== false) {
389
                            if (preg_match('/ IP:([\d\.]+)[ ]+GW/', $line, $ar_buf) || preg_match('/IP Address:([\d\.]+)[ ]+Tx/', $line, $ar_buf)) {
390
                                if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
391
                                    if ($last === 'LAN') {
392
                                        if (isset($macarray['LAN'])) {
393
                                            $dev->setInfo($macarray['LAN'].';'.$ar_buf[1]);
394
                                        }
395
                                        if ($lantxrate !== false) {
396
                                            $dev->setTxRate($lantxrate);
397
                                        }
398
                                        if ($lanrxrate !== false) {
399
                                            $dev->setRxRate($lanrxrate);
400
                                        }
401
                                    } elseif (isset($macarray[$ar_buf[1]])) {
402
                                        $dev->setInfo($macarray[$ar_buf[1]].';'.$ar_buf[1]);
403
                                    } else {
404
                                       $dev->setInfo($ar_buf[1]);
405
                                    }
406
                                }
407
                            } elseif (preg_match('/TX Packets:\d+[ ]+TX Rate\(bps\):(\d+)[ ]+RX Packets:\d+[ ]+RX Rate\(bps\):(\d+)/', $line, $ar_buf)) {
408
                                if (defined('PSI_SHOW_NETWORK_ACTIVE_SPEED') && PSI_SHOW_NETWORK_ACTIVE_SPEED) {
409
                                    $dev->setTxRate($ar_buf[1]);
410
                                    $dev->setRxRate($ar_buf[2]);
411
                                }
412
                            }
413
                        }
414
                    }
415
                }
416
                if (($last !== false) && (($last !== 'LAN') || $notwaslan)) {
417
                    $this->sys->setNetDevices($dev);
418
                }
419
            }
420
            break;
421
        case 'GNU':
422
        case 'Linux':
423
            parent::_network();
424
        }
425
    }
426
 
427
    /**
428
     * CPU information
429
     * All of the tags here are highly architecture dependant.
430
     *
431
     * @return void
432
     */
433
    protected function _cpuinfo($bufr = null)
434
    {
435
        switch ($this->_ostype) {
436
        case 'FortiOS':
437
            if (CommonFunctions::executeProgram('get', 'hardware cpu', $resulte, false) && ($resulte !== "")
438
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
439
                parent::_cpuinfo(substr($resulte, strlen($resulto[1][0])));
440
            }
441
            break;
442
        case 'DrayOS':
443
            if (preg_match_all("/CPU(\d+) speed:[ ]*(\d+) MHz/m", $sysinfo = $this->getSysVerSysteminfo(), $bufarr)) {
444
                foreach ($bufarr[1] as $index=>$nr) {
445
                    $dev = new CpuDevice();
446
                    $dev->setModel('CPU'.$nr);
447
                    $dev->setCpuSpeed($bufarr[2][$index]);
448
                    if (PSI_LOAD_BAR) {
449
                        $dev->setLoad($this->_parseProcStat('cpu'.$nr));
450
                    }
451
                    $this->sys->setCpus($dev);
452
                }
453
//                $this->_cpu_loads['cpu'] = $buf[1];
454
//                if (preg_match("/CPU1 speed/", $sysinfo)) {
455
//                    $this->_cpu_loads['cpu0'] = $buf[1];
456
//                }
457
            }
458
            break;
459
        case 'GNU':
460
        case 'Linux':
461
            if (CommonFunctions::executeProgram('cat', '/proc/cpuinfo', $resulte, false) && ($resulte !== "")) {
462
                parent::_cpuinfo($resulte);
463
            }
464
        }
465
    }
466
 
467
    /**
468
     * Machine
469
     *
470
     * @return void
471
     */
472
    protected function _machine()
473
    {
474
        switch ($this->_ostype) {
475
        case 'FortiOS':
476
            if (($sysstat = $this->getSystemStatus()) !== '') {
477
                $machine= '';
478
                if (preg_match("/^Version: (\S+) v/", $sysstat, $buf)) {
479
                    $machine = $buf[1];
480
                }
481
                if (preg_match("/\nSystem Part-Number: (\S+)\n/", $sysstat, $buf)) {
482
                    $machine .= ' '.$buf[1];
483
                }
484
                if (preg_match("/\nBIOS version: (\S+)\n/", $sysstat, $buf)) {
485
                    if (trim($machine) !== '') {
486
                        $machine .= ', BIOS '.$buf[1];
487
                    } else {
488
                        $machine = 'BIOS '.$buf[1];
489
                    }
490
                }
491
                $machine = trim($machine);
492
 
493
                if ($machine !== '') {
494
                    $this->sys->setMachine($machine);
495
                }
496
            }
497
            break;
498
        case 'DrayOS':
499
            if (($sysstat = $this->getSysVerSysteminfo()) !== '') {
500
                $machine= '';
501
                if (preg_match("/[\r\n]Router Model: (\S+) /", $sysstat, $buf)) {
502
                    $machine = $buf[1];
503
                }
504
                if (preg_match("/[\r\n]Revision: (.+)[\r\n]/", $sysstat, $buf)) {
505
                    $machine .= ' '.$buf[1];
506
                }
507
                $machine = trim($machine);
508
 
509
                if ($machine !== '') {
510
                    $this->sys->setMachine($machine);
511
                }
512
            }
513
            break;
514
 
515
        case 'GNU':
516
        case 'Linux':
517
            parent::_machine();
518
        }
519
    }
520
 
521
    /**
522
     * Hostname
523
     *
524
     * @return void
525
     */
526
    protected function _hostname()
527
    {
528
        switch ($this->_ostype) {
529
        case 'FortiOS':
530
//            $hostname = PSI_EMU_HOSTNAME;
531
            if (preg_match("/\nHostname: ([^\n]+)\n/", $this->getSystemStatus(), $buf)) {
532
                $this->sys->setHostname(trim($buf[1]));
533
//                $hostname = trim($buf[1]);
534
            }
535
 
536
//            $ip = gethostbyname($hostname);
537
//            if ($ip != $hostname) {
538
//                $this->sys->setHostname(gethostbyaddr($ip));
539
//            } else {
540
//                $this->sys->setHostname($hostname);
541
//            }
542
            break;
543
        case 'DrayOS':
544
            if (preg_match("/[\r\n]Router Name: ([^\n\r]+)[\r\n]/", $this->getSysVerSysteminfo(), $buf)) {
545
                $this->sys->setHostname(trim($buf[1]));
546
            }
547
            break;
548
        case 'GNU':
549
        case 'Linux':
550
            parent::_hostname();
551
        }
552
 
553
    }
554
 
555
    /**
556
     * filesystem information
557
     *
558
     * @return void
559
     */
560
    protected function _filesystems()
561
    {
562
        switch ($this->_ostype) {
563
        case 'FortiOS':
564
            if (CommonFunctions::executeProgram('fnsysctl', 'df -k', $resulte, false) && ($resulte !== "")
565
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
566
                $resulti = substr($resulte, $resulto[1][1]);
567
                $df = preg_split("/\n/", $resulti, -1, PREG_SPLIT_NO_EMPTY);
568
                foreach ($df as $df_line) {
569
                    $df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
570
                    if (count($df_buf1) != 2) {
571
                        continue;
572
                    }
573
                    if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
574
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
575
                        if (count($df_buf) == 6) {
576
                            $df_buf[5] = trim($df_buf[5]);
577
                            $dev = new DiskDevice();
578
                            $dev->setName(trim($df_buf[0]));
579
                            if ($df_buf[2] < 0) {
580
                                $dev->setTotal($df_buf[3] * 1024);
581
                                $dev->setUsed($df_buf[3] * 1024);
582
                            } else {
583
                                $dev->setTotal($df_buf[1] * 1024);
584
                                $dev->setUsed($df_buf[2] * 1024);
585
                                if ($df_buf[3]>0) {
586
                                    $dev->setFree($df_buf[3] * 1024);
587
                                }
588
                            }
589
                            if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
590
                            $dev->setFsType('unknown');
591
                            $this->sys->setDiskDevices($dev);
592
                        }
593
                    }
594
                }
595
            }
596
            break;
597
        case 'DrayOS':
598
            if (CommonFunctions::executeProgram('nand', 'usage', $resulte, false, PSI_EXEC_TIMEOUT_INT, '>') && ($resulte !== "")
599
               && preg_match('/([\s\S]+> nand usage)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
600
                $df = substr($resulte, strlen($resulto[1][0]));
601
 
602
                if (preg_match('/Usecfg/', $df)) { // fix for Vigor2135ac v4.4.2
603
                    $df = preg_replace("/(cfg|bin)/", "\n$1", substr($resulte, strlen($resulto[1][0])));
604
                    $percent = '';
605
                } else {
606
                    $percent = '%';
607
                }
608
 
609
                $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
610
                foreach ($df as $df_line) {
611
                    if (preg_match("/^(\S+)[ ]+(\d+)[ ]+(\d+)[ ]+(\d+)[ ]+(\d+)".$percent."/", trim($df_line), $df_buf)) {
612
                        $dev = new DiskDevice();
613
                        $dev->setName($df_buf[1]);
614
                        $dev->setTotal($df_buf[2]);
615
                        $dev->setUsed($df_buf[3]);
616
                        $dev->setFree($df_buf[4]);
617
                        $dev->setFsType('NAND');
618
                        $this->sys->setDiskDevices($dev);
619
                    }
620
                }
621
            }
622
            break;
623
        case 'GNU':
624
        case 'Linux':
625
            parent::_filesystems();
626
        }
627
    }
628
 
629
    /**
630
     * Distribution
631
     *
632
     * @return void
633
     */
634
    protected function _distro()
635
    {
636
        switch ($this->_ostype) {
637
        case 'FortiOS':
638
            if (preg_match("/^Version: \S+ (v[^\n]+)\n/", $this->getSystemStatus(), $buf)) {
639
                $this->sys->setDistribution('FortiOS '.trim($buf[1]));
640
            }
641
            $this->sys->setDistributionIcon('FortiOS.png');
642
            break;
643
        case 'DrayOS':
644
            if (preg_match("/ Version: ([^\n]+)\n/", $this->getSysVerSysteminfo(), $buf)) {
645
                $this->sys->setDistribution('DrayOS '.trim($buf[1]));
646
            }
647
            $this->sys->setDistributionIcon('DrayOS.png');
648
            break;
649
        case 'GNU':
650
        case 'Linux':
651
            parent::_distro();
652
        }
653
//        if ($this->_ostype !== null) {
654
//            $this->sys->setDistributionIcon($this->_ostype);
655
//        }
656
    }
657
 
658
    /**
659
     * fill the load for a individual cpu, through parsing /proc/stat for the specified cpu
660
     *
661
     * @param String $cpuline cpu for which load should be meassured
662
     *
663
     * @return int
664
     */
665
    protected function _parseProcStat($cpuline)
666
    {
667
        if ($this->_cpu_loads === null) {
668
            $this->_cpu_loads = array();
669
            switch ($this->_ostype) {
670
            case 'FortiOS':
671
                if (($strBuf = $this->getSystemPerformance()) !== '') {
672
                    $lines = preg_split('/\n/', $strBuf, -1, PREG_SPLIT_NO_EMPTY);
673
                    foreach ($lines as $line) if (preg_match('/^CPU(\d*) states: \d+% user \d+% system \d+% nice (\d+)% idle /', $line, $buf)) {
674
                        $this->_cpu_loads['cpu'.$buf[1]] = 100-$buf[2];
675
                    }
676
                }
677
                break;
678
            case 'DrayOS':
679
                if (preg_match("/CPU usage :[ ]*(\d+) %/", $sysinfo = $this->getSysVerSysteminfo(), $buf)) {
680
                    $this->_cpu_loads['cpu'] = $buf[1];
681
                    if (preg_match("/CPU1 speed/", $sysinfo) && !preg_match("/CPU2 speed/", $sysinfo)) { //only one cpu
682
                        $this->_cpu_loads['cpu1'] = $buf[1];
683
                    }
684
                }
685
            }
686
        }
687
        if (isset($this->_cpu_loads[$cpuline])) {
688
            return $this->_cpu_loads[$cpuline];
689
        } else {
690
            return null;
691
        }
692
     }
693
 
694
    /**
695
     * Processor Load
696
     * optionally create a loadbar
697
     *
698
     * @return void
699
     */
700
    protected function _loadavg($bufr = null)
701
    {
702
        switch ($this->_ostype) {
703
        case 'FortiOS':
704
            if (CommonFunctions::executeProgram('fnsysctl', 'cat /proc/loadavg', $resulte, false) && ($resulte !== "")
705
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
706
                parent::_loadavg(substr($resulte, strlen($resulto[1][0])));
707
            }
708
            break;
709
        case 'DrayOS':
710
            if (PSI_LOAD_BAR) {
711
                $this->sys->setLoadPercent($this->_parseProcStat('cpu'));
712
            }
713
            break;
714
        case 'GNU':
715
        case 'Linux':
716
            parent::_loadavg();
717
        }
718
    }
719
 
720
    /**
721
     * UpTime
722
     * time the system is running
723
     *
724
     * @return void
725
     */
726
    protected function _uptime($bufu = null)
727
    {
728
        switch ($this->_ostype) {
729
        case 'FortiOS':
730
            if (preg_match("/\nUptime: ([^\n]+)\n/", $this->getSystemPerformance(), $buf)) {
731
                parent::_uptime('up '.trim($buf[1]));
732
            }
733
            break;
734
        case 'DrayOS':
735
            if (preg_match("/System Uptime:([\d:]+)/", $this->getShowStatus(), $buf)) {
736
                parent::_uptime('up '.trim($buf[1]));
737
            }
738
            break;
739
        case 'GNU':
740
        case 'Linux':
741
            if (CommonFunctions::executeProgram('cat', '/proc/uptime', $resulte, false) && ($resulte !== "")) {
742
                $ar_buf = preg_split('/ /', $resulte);
743
                $this->sys->setUptime(trim($ar_buf[0]));
744
            } else {
745
                parent::_uptime();
746
            }
747
        }
748
    }
749
 
750
    /**
751
     * Kernel Version
752
     *
753
     * @return void
754
     */
755
    protected function _kernel()
756
    {
757
        switch ($this->_ostype) {
758
        case 'FortiOS':
759
            if (CommonFunctions::executeProgram('fnsysctl', 'cat /proc/version', $resulte, false) && ($resulte !== "")
760
               && preg_match('/^(.*[\$#]\s*)/', $resulte, $resulto, PREG_OFFSET_CAPTURE)) {
761
                $strBuf = substr($resulte, $resulto[1][1]);
762
                if (preg_match('/version\s+(\S+)/', $strBuf, $ar_buf)) {
763
                    $verBuf = $ar_buf[1];
764
                    if (preg_match('/ SMP /', $strBuf)) {
765
                        $verBuf .= ' (SMP)';
766
                    }
767
                    $this->sys->setKernel($verBuf);
768
                }
769
            }
770
            break;
771
        case 'GNU':
772
        case 'Linux':
773
            parent::_kernel();
774
        }
775
    }
776
 
777
    /**
778
     * get the information
779
     *
780
     * @return void
781
     */
782
    public function build()
783
    {
784
        $this->error->addWarning("The SSH version of phpSysInfo is a work in progress, some things currently don't work");
785
        switch ($this->_ostype) {
786
        case 'FortiOS':
787
            if (!$this->blockname || $this->blockname==='vitals') {
788
                $this->_distro();
789
                $this->_hostname();
790
                $this->_kernel();
791
                $this->_uptime();
792
//                $this->_users();
793
                $this->_loadavg();
794
//                $this->_processes();
795
            }
796
            if (!$this->blockname || $this->blockname==='hardware') {
797
                $this->_machine();
798
                $this->_cpuinfo();
799
                //$this->_virtualizer();
800
//                $this->_pci();
801
                $this->_usb();
802
//                $this->_i2c();
803
            }
804
            if (!$this->blockname || $this->blockname==='memory') {
805
                $this->_memory();
806
            }
807
            if (!$this->blockname || $this->blockname==='filesystem') {
808
                $this->_filesystems();
809
            }
810
            if (!$this->blockname || $this->blockname==='network') {
811
                $this->_network();
812
            }
813
            break;
814
        case 'DrayOS':
815
            if (!$this->blockname || $this->blockname==='vitals') {
816
                $this->_distro();
817
                $this->_hostname();
818
//                $this->_kernel();
819
                $this->_uptime();
820
////                $this->_users();
821
                $this->_loadavg();
822
////                $this->_processes();
823
            }
824
            if (!$this->blockname || $this->blockname==='hardware') {
825
                $this->_machine();
826
                $this->_cpuinfo();
827
//                //$this->_virtualizer();
828
////                $this->_pci();
829
//                $this->_usb();
830
////                $this->_i2c();
831
            }
832
            if (!$this->blockname || $this->blockname==='memory') {
833
                $this->_memory();
834
            }
835
            if (!$this->blockname || $this->blockname==='filesystem') {
836
                $this->_filesystems();
837
            }
838
            if (!$this->blockname || $this->blockname==='network') {
839
                $this->_network();
840
            }
841
            break;
842
 
843
        case 'GNU':
844
        case 'Linux':
845
            parent::build();
846
        }
847
    }
848
}