Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * SunOS System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI SunOS OS class
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
10
 * @copyright 2009 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.SunOS.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * SunOS sysinfo class
17
 * get all the required information from SunOS systems
18
 *
19
 * @category  PHP
20
 * @package   PSI SunOS OS class
21
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
22
 * @copyright 2009 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 SunOS extends OS
28
{
325 richard 29
 
2770 rexy 30
    /**
31
     * content of prtconf -v
32
     *
33
     * @var array
34
     */
35
    private $_prtconf = null;
325 richard 36
 
2770 rexy 37
    /**
38
     * Execute prtconf -v and save ass array
39
     *
40
     * @return array
41
     */
42
    protected function prtconf()
43
    {
44
        if ($this->_prtconf === null) {
45
            $this->_prtconf = array();
46
            if (CommonFunctions::executeProgram('prtconf', '-v', $buf, PSI_DEBUG) && ($buf!="")) {
47
                $blocks = preg_split( '/\n(?=    \S)/', $buf, -1, PREG_SPLIT_NO_EMPTY);
48
                if (!empty($blocks) && (count($blocks)>2)) {
49
                    array_shift($blocks);
50
                    foreach ($blocks as $block) {
51
                        if (preg_match('/^    (\S+) /',$block, $ar_buf)) {
52
                            $group = trim($ar_buf[1], ',');
53
                            $grouparr = array();
54
                            $blocks1 = preg_split( '/\n(?=        \S)/', $block, -1, PREG_SPLIT_NO_EMPTY);
55
                            if (!empty($blocks1) && count($blocks1)) {
56
                                array_shift($blocks1);
57
                                foreach ($blocks1 as $block1) {
58
                                    if (!preg_match('/^        name=\'([^\']+)\'/',$block1)
59
                                       && preg_match('/^        (\S+) /',$block1, $ar_buf)) {
60
                                        $device = trim($ar_buf[1], ',');
61
                                        $devicearr = array();
62
                                        $blocks2 = preg_split( '/\n(?=            \S)/', $block1, -1, PREG_SPLIT_NO_EMPTY);
63
                                        if (!empty($blocks2) && count($blocks2)) {
64
                                            array_shift($blocks2);
65
                                            foreach ($blocks2 as $block2) {
66
                                                if (!preg_match('/^            name=\'([^\']+)\'/',$block2)
67
                                                   && preg_match('/^            (\S+) /',$block2, $ar_buf)) {
68
                                                    $subdev = trim($ar_buf[1], ',');
69
                                                    $subdevarr = array();
70
                                                    $blocks3 = preg_split( '/\n(?=                \S)/', $block2, -1, PREG_SPLIT_NO_EMPTY);
71
                                                    if (!empty($blocks3) && count($blocks3)) {
72
                                                        array_shift($blocks3);
73
                                                        foreach ($blocks3 as $block3) {
74
                                                            if (preg_match('/^                name=\'([^\']+)\' [\s\S]+ value=\'?([^\']+)\'?/m',$block3, $ar_buf)) {
75
                                                                if ($subdev==='Hardware') {
76
                                                                    $subdevarr[$ar_buf[1]] = $ar_buf[2];
77
                                                                    $subdevarr['device'] = $device;
78
                                                                }
79
                                                            }
80
                                                        }
81
                                                        if (count($subdevarr)) {
82
                                                            $devicearr = $subdevarr;
83
                                                        }
84
                                                    }
85
                                                }
86
                                            }
87
                                        }
88
                                        if (count($devicearr)) {
89
                                            $grouparr[$device][] = $devicearr;
90
                                        }
91
                                    }
92
                                }
93
                            }
94
                            if (count($grouparr)) {
95
                                $this->_prtconf[$group][] = $grouparr;
96
                            }
97
                        }
98
                    }
99
                }
100
            }
101
        }
325 richard 102
 
2770 rexy 103
        return $this->_prtconf;
104
    }
325 richard 105
 
2770 rexy 106
    /**
107
     * Extract kernel values via kstat() interface
108
     *
109
     * @param string $key key for kstat programm
110
     *
111
     * @return string
112
     */
113
    private function _kstat($key)
114
    {
115
        if (CommonFunctions::executeProgram('kstat', '-p d '.$key, $m, PSI_DEBUG) && ($m!=="")) {
116
            list($key, $value) = preg_split("/\t/", $m, 2);
325 richard 117
 
2770 rexy 118
            return trim($value);
119
        } else {
120
            return '';
121
        }
122
    }
325 richard 123
 
2770 rexy 124
    /**
125
     * Virtual Host Name
126
     *
127
     * @return void
128
     */
129
    private function _hostname()
130
    {
131
        if (PSI_USE_VHOST === true) {
132
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
133
        } else {
134
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
135
                $ip = gethostbyname($result);
136
                if ($ip != $result) {
137
                    $this->sys->setHostname(gethostbyaddr($ip));
138
                }
139
            }
140
        }
141
    }
325 richard 142
 
2770 rexy 143
    /**
144
     * Kernel Version
145
     *
146
     * @return void
147
     */
148
    private function _kernel()
149
    {
2976 rexy 150
        if (CommonFunctions::executeProgram('uname', '-s', $kernel, PSI_DEBUG) && ($kernel != "")) {
151
            if (CommonFunctions::executeProgram('uname', '-r', $version, PSI_DEBUG) && ($version != "")) {
152
                $kernel.=' '.$version;
2770 rexy 153
            }
2976 rexy 154
            if (CommonFunctions::executeProgram('uname', '-v', $subversion, PSI_DEBUG) && ($subversion != "")) {
155
                $kernel.=' ('.$subversion.')';
2770 rexy 156
            }
2976 rexy 157
            if (CommonFunctions::executeProgram('uname', '-i', $platform, PSI_DEBUG) && ($platform != "")) {
158
                $kernel.=' '.$platform;
2770 rexy 159
            }
2976 rexy 160
            $this->sys->setKernel($kernel);
2770 rexy 161
        }
162
    }
325 richard 163
 
2770 rexy 164
    /**
165
     * UpTime
166
     * time the system is running
167
     *
168
     * @return void
169
     */
170
    private function _uptime()
171
    {
172
        $this->sys->setUptime(time() - $this->_kstat('unix:0:system_misc:boot_time'));
173
    }
325 richard 174
 
2770 rexy 175
    /**
176
     * Processor Load
177
     * optionally create a loadbar
178
     *
179
     * @return void
180
     */
181
    private function _loadavg()
182
    {
183
        $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
184
        $load5 = $this->_kstat('unix:0:system_misc:avenrun_5min');
185
        $load15 = $this->_kstat('unix:0:system_misc:avenrun_15min');
186
        $this->sys->setLoad(round($load1 / 256, 2).' '.round($load5 / 256, 2).' '.round($load15 / 256, 2));
187
    }
325 richard 188
 
2770 rexy 189
    /**
190
     * CPU information
191
     *
192
     * @return void
193
     */
194
    private function _cpuinfo()
195
    {
196
        if (CommonFunctions::executeProgram('kstat', '-p d cpu_info:*:cpu_info*:core_id', $m, PSI_DEBUG) && ($m!=="")) {
197
            $cpuc = count(preg_split('/\n/', $m, -1, PREG_SPLIT_NO_EMPTY));
198
            for ($cpu=0; $cpu < $cpuc; $cpu++) {
199
                $dev = new CpuDevice();
200
                if (($buf = $this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':current_clock_Hz')) !== "") {
201
                    $dev->setCpuSpeed($buf/1000000);
202
                } elseif (($buf = $this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':clock_MHz')) !== "") {
203
                    $dev->setCpuSpeed($buf);
204
                }
205
                if (($buf = $this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':supported_frequencies_Hz')) !== "") {
206
                    $cpuarr = preg_split('/:/', $buf, -1, PREG_SPLIT_NO_EMPTY);
207
                    if (($cpuarrc=count($cpuarr))>1) {
208
                        $dev->setCpuSpeedMin($cpuarr[0]/1000000);
209
                        $dev->setCpuSpeedMax($cpuarr[$cpuarrc-1]/1000000);
210
                    }
211
                }
212
                if (($buf  =$this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':brand')) !== "") {
213
                    $dev->setModel($buf);
214
                } elseif (($buf  =$this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':cpu_type')) !== "") {
215
                    $dev->setModel($buf);
216
                } elseif (CommonFunctions::executeProgram('uname', '-p', $buf, PSI_DEBUG) && ($buf!="")) {
217
                    $dev->setModel($buf);
218
                } elseif (CommonFunctions::executeProgram('uname', '-i', $buf, PSI_DEBUG) && ($buf!="")) {
219
                    $dev->setModel($buf);
220
                }
221
                $this->sys->setCpus($dev);
222
            }
223
         }
224
    }
325 richard 225
 
2770 rexy 226
    /**
227
     * PCI devices
228
     *
229
     * @return void
230
     */
231
    protected function _pci()
232
    {
233
        $prtconf = $this->prtconf();
234
        if ((count($prtconf)>1) && isset($prtconf['pci'])) {
235
            foreach ($prtconf['pci'] as $prt) {
236
                foreach ($prt as $pci) {
237
                    foreach ($pci as $pcidev) {
238
                        if (isset($pcidev['device'])) {
239
                            $dev = new HWDevice();
240
                            if (isset($pcidev['model'])) {
241
                                $name = $pcidev['model'];
242
                            } else {
243
                                $name = $pcidev['device'];
244
                            }
245
                            if (isset($pcidev['device-name'])) {
246
                                $name .= ': '.$pcidev['device-name'];
247
                            }
248
                            $dev->setName($name);
325 richard 249
 
2770 rexy 250
                            if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
251
                                if (isset($pcidev['subsystem-name']) && ($pcidev['subsystem-name']!=='unknown subsystem')) {
252
                                    $dev->setProduct($pcidev['subsystem-name']);
253
                                }
254
                                if (isset($pcidev['vendor-name'])) {
255
                                    $dev->setManufacturer($pcidev['vendor-name']);
256
                                }
257
                            }
325 richard 258
 
2770 rexy 259
                            $this->sys->setPciDevices($dev);
260
                        }
261
                    }
262
                }
263
            }
264
        }
265
    }
325 richard 266
 
2770 rexy 267
    /**
268
     * Network devices
269
     *
270
     * @return void
271
     */
272
    private function _network()
273
    {
274
        if (CommonFunctions::executeProgram('netstat', '-ni | awk \'(NF ==10){print;}\'', $netstat, PSI_DEBUG)) {
275
            $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
276
            foreach ($lines as $line) {
277
                $ar_buf = preg_split("/\s+/", $line);
278
                if (!empty($ar_buf[0]) && $ar_buf[0] !== 'Name') {
279
                    $dev = new NetDevice();
280
                    $dev->setName($ar_buf[0]);
281
                    $results[$ar_buf[0]]['errs'] = $ar_buf[5] + $ar_buf[7];
282
                    if (preg_match('/^(\D+)(\d+)$/', $ar_buf[0], $intf)) {
283
                        $prefix = $intf[1].':'.$intf[2].':'.$intf[1].$intf[2].':';
284
                    } elseif (preg_match('/^(\D.*)(\d+)$/', $ar_buf[0], $intf)) {
285
                        $prefix = $intf[1].':'.$intf[2].':mac:';
286
                    } else {
287
                        $prefix = "";
288
                    }
289
                    if ($prefix !== "") {
290
                        $cnt = $this->_kstat($prefix.'drop');
291
                        if ($cnt > 0) {
292
                            $dev->setDrops($cnt);
293
                        }
294
                        $cnt = $this->_kstat($prefix.'obytes64');
295
                        if ($cnt > 0) {
296
                            $dev->setTxBytes($cnt);
297
                        }
298
                        $cnt = $this->_kstat($prefix.'rbytes64');
299
                        if ($cnt > 0) {
300
                            $dev->setRxBytes($cnt);
301
                        }
302
                    }
303
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
304
                        if (CommonFunctions::executeProgram('ifconfig', $ar_buf[0], $bufr2, PSI_DEBUG) && ($bufr2!=="")) {
305
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
306
                            foreach ($bufe2 as $buf2) {
307
                                if (preg_match('/^\s+ether\s+(\S+)/i', $buf2, $ar_buf2)) {
308
                                    if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
309
                                } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)) {
310
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
311
                                }
312
                            }
313
                        }
314
                        if (CommonFunctions::executeProgram('ifconfig', $ar_buf[0].' inet6', $bufr2, PSI_DEBUG) && ($bufr2!=="")) {
315
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
316
                            foreach ($bufe2 as $buf2) {
317
                                if (preg_match('/^\s+inet6\s+([^\s\/]+)/i', $buf2, $ar_buf2)
318
                                   && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1]))
319
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
320
                            }
321
                        }
322
                    }
325 richard 323
 
2770 rexy 324
                    $this->sys->setNetDevices($dev);
325
                }
326
            }
327
        }
328
    }
325 richard 329
 
2770 rexy 330
    /**
331
     * Physical memory information and Swap Space information
332
     *
333
     * @return void
334
     */
335
    private function _memory()
336
    {
337
        $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
338
        $this->sys->setMemTotal($this->_kstat('unix:0:system_pages:pagestotal') * $pagesize);
339
        $this->sys->setMemUsed($this->_kstat('unix:0:system_pages:pageslocked') * $pagesize);
340
        $this->sys->setMemFree($this->_kstat('unix:0:system_pages:pagesfree') * $pagesize);
2976 rexy 341
        if (($swap=$this->_kstat('unix:0:vminfo:swap_avail')) > 0) {
342
            $dev = new DiskDevice();
343
            $dev->setName('SWAP');
344
            $dev->setFsType('swap');
345
            $dev->setMountPoint('SWAP');
346
            $dev->setTotal($swap / 1024);
347
            $dev->setUsed($this->_kstat('unix:0:vminfo:swap_alloc') / 1024);
348
            $dev->setFree($this->_kstat('unix:0:vminfo:swap_free') / 1024);
349
            $this->sys->setSwapDevices($dev);
350
        }
2770 rexy 351
    }
325 richard 352
 
2770 rexy 353
    /**
354
     * filesystem information
355
     *
356
     * @return void
357
     */
358
    private function _filesystems()
359
    {
360
        if (CommonFunctions::executeProgram('df', '-k', $df, PSI_DEBUG)) {
361
            $df = preg_replace('/\n\s/m', ' ', $df);
362
            $mounts = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
363
            foreach ($mounts as $mount) {
364
                $ar_buf = preg_split('/\s+/', $mount, 6);
365
                if (!empty($ar_buf[0]) && $ar_buf[0] !== 'Filesystem') {
366
                    $dev = new DiskDevice();
367
                    $dev->setName($ar_buf[0]);
368
                    $dev->setTotal($ar_buf[1] * 1024);
369
                    $dev->setUsed($ar_buf[2] * 1024);
370
                    $dev->setFree($ar_buf[3] * 1024);
371
                    $dev->setMountPoint($ar_buf[5]);
372
                    if (CommonFunctions::executeProgram('df', '-n', $dftypes, PSI_DEBUG)) {
373
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
374
                        foreach ($mounttypes as $type) {
375
                            $ty_buf = preg_split('/:/', $type, 2);
376
                            if (trim($ty_buf[0]) == $dev->getMountPoint()) {
377
                                $dev->setFsType($ty_buf[1]);
378
                                break;
379
                            }
380
                        }
381
                    } elseif (CommonFunctions::executeProgram('df', '-T', $dftypes, PSI_DEBUG)) {
382
                        $dftypes = preg_replace('/\n\s/m', ' ', $dftypes);
383
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
384
                        foreach ($mounttypes as $type) {
385
                            $ty_buf = preg_split("/\s+/", $type, 3);
386
                            if ($ty_buf[0] == $dev->getName()) {
387
                                $dev->setFsType($ty_buf[1]);
388
                                break;
389
                            }
390
                        }
391
                    }
392
                    $this->sys->setDiskDevices($dev);
393
                }
394
            }
395
        }
396
    }
325 richard 397
 
2770 rexy 398
    /**
399
     * Distribution Icon
400
     *
401
     * @return void
402
     */
403
    private function _distro()
404
    {
405
        if (CommonFunctions::rfts('/etc/release', $buf, 1, 4096, false) && (trim($buf)!="")) {
406
            $this->sys->setDistribution(trim($buf));
407
            $list = @parse_ini_file(PSI_APP_ROOT."/data/distros.ini", true);
408
            if ($list && preg_match('/^(\S+)\s*/', preg_replace('/^Open\s+/', 'Open', preg_replace('/^Oracle\s+/', 'Oracle', trim($buf))), $id_buf) && isset($list[$distid=(trim($id_buf[1].' SunOS'))]['Image'])) {
409
                $this->sys->setDistributionIcon($list[$distid]['Image']);
410
                if (isset($list[trim($distid)]['Name'])) {
411
                    $this->sys->setDistribution(trim($list[$distid]['Name']).' '.$this->sys->getDistribution());
412
                }
413
            } else {
414
                $this->sys->setDistributionIcon('SunOS.png');
415
            }
416
        } else {
417
            $this->sys->setDistribution('SunOS');
418
            $this->sys->setDistributionIcon('SunOS.png');
419
        }
420
    }
325 richard 421
 
2770 rexy 422
    /**
423
     * Processes
424
     *
425
     * @return void
426
     */
427
    protected function _processes()
428
    {
429
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
430
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
431
            $processes['*'] = 0;
432
            foreach ($lines as $line) {
433
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
434
                    $processes['*']++;
435
                    $state = $ar_buf[1];
436
                    if ($state == 'O') $state = 'R'; //linux format
437
                    elseif ($state == 'W') $state = 'D';
438
                    elseif ($state == 'D') $state = 'd'; //invalid
439
                    if (isset($processes[$state])) {
440
                        $processes[$state]++;
441
                    } else {
442
                        $processes[$state] = 1;
443
                    }
444
                }
445
            }
446
            if ($processes['*'] > 0) {
447
                $this->sys->setProcesses($processes);
448
            }
449
        }
450
    }
325 richard 451
 
2770 rexy 452
    /**
453
     * get the information
454
     *
455
     * @see PSI_Interface_OS::build()
456
     *
457
     * @return Void
458
     */
459
    public function build()
460
    {
461
        $this->error->addError("WARN", "The SunOS version of phpSysInfo is a work in progress, some things currently don't work");
462
        if (!$this->blockname || $this->blockname==='vitals') {
463
            $this->_distro();
464
            $this->_hostname();
465
            $this->_kernel();
466
            $this->_uptime();
467
            $this->_users();
468
            $this->_loadavg();
469
            $this->_processes();
325 richard 470
        }
2770 rexy 471
        if (!$this->blockname || $this->blockname==='hardware') {
472
            $this->_cpuinfo();
473
            $this->_pci();
474
        }
475
        if (!$this->blockname || $this->blockname==='network') {
476
            $this->_network();
477
        }
478
        if (!$this->blockname || $this->blockname==='memory') {
479
            $this->_memory();
480
        }
481
        if (!$this->blockname || $this->blockname==='filesystem') {
482
            $this->_filesystems();
483
        }
484
    }
485
}