Subversion Repositories ALCASAR

Rev

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