Subversion Repositories ALCASAR

Rev

Rev 325 | Rev 2976 | 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
    {
150
        if (CommonFunctions::executeProgram('uname', '-s', $os, PSI_DEBUG) && ($os!="")) {
151
            if (CommonFunctions::executeProgram('uname', '-r', $version, PSI_DEBUG) && ($version!="")) {
152
                $os.=' '.$version;
153
            }
154
            if (CommonFunctions::executeProgram('uname', '-v', $subversion, PSI_DEBUG) && ($subversion!="")) {
155
                $os.=' ('.$subversion.')';
156
            }
157
            if (CommonFunctions::executeProgram('uname', '-i', $platform, PSI_DEBUG) && ($platform!="")) {
158
                $os.=' '.$platform;
159
            }
160
            $this->sys->setKernel($os);
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);
341
        $dev = new DiskDevice();
342
        $dev->setName('SWAP');
343
        $dev->setFsType('swap');
344
        $dev->setMountPoint('SWAP');
345
        $dev->setTotal($this->_kstat('unix:0:vminfo:swap_avail') / 1024);
346
        $dev->setUsed($this->_kstat('unix:0:vminfo:swap_alloc') / 1024);
347
        $dev->setFree($this->_kstat('unix:0:vminfo:swap_free') / 1024);
348
        $this->sys->setSwapDevices($dev);
349
    }
325 richard 350
 
2770 rexy 351
    /**
352
     * filesystem information
353
     *
354
     * @return void
355
     */
356
    private function _filesystems()
357
    {
358
        if (CommonFunctions::executeProgram('df', '-k', $df, PSI_DEBUG)) {
359
            $df = preg_replace('/\n\s/m', ' ', $df);
360
            $mounts = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
361
            foreach ($mounts as $mount) {
362
                $ar_buf = preg_split('/\s+/', $mount, 6);
363
                if (!empty($ar_buf[0]) && $ar_buf[0] !== 'Filesystem') {
364
                    $dev = new DiskDevice();
365
                    $dev->setName($ar_buf[0]);
366
                    $dev->setTotal($ar_buf[1] * 1024);
367
                    $dev->setUsed($ar_buf[2] * 1024);
368
                    $dev->setFree($ar_buf[3] * 1024);
369
                    $dev->setMountPoint($ar_buf[5]);
370
                    if (CommonFunctions::executeProgram('df', '-n', $dftypes, PSI_DEBUG)) {
371
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
372
                        foreach ($mounttypes as $type) {
373
                            $ty_buf = preg_split('/:/', $type, 2);
374
                            if (trim($ty_buf[0]) == $dev->getMountPoint()) {
375
                                $dev->setFsType($ty_buf[1]);
376
                                break;
377
                            }
378
                        }
379
                    } elseif (CommonFunctions::executeProgram('df', '-T', $dftypes, PSI_DEBUG)) {
380
                        $dftypes = preg_replace('/\n\s/m', ' ', $dftypes);
381
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
382
                        foreach ($mounttypes as $type) {
383
                            $ty_buf = preg_split("/\s+/", $type, 3);
384
                            if ($ty_buf[0] == $dev->getName()) {
385
                                $dev->setFsType($ty_buf[1]);
386
                                break;
387
                            }
388
                        }
389
                    }
390
                    $this->sys->setDiskDevices($dev);
391
                }
392
            }
393
        }
394
    }
325 richard 395
 
2770 rexy 396
    /**
397
     * Distribution Icon
398
     *
399
     * @return void
400
     */
401
    private function _distro()
402
    {
403
        if (CommonFunctions::rfts('/etc/release', $buf, 1, 4096, false) && (trim($buf)!="")) {
404
            $this->sys->setDistribution(trim($buf));
405
            $list = @parse_ini_file(PSI_APP_ROOT."/data/distros.ini", true);
406
            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'])) {
407
                $this->sys->setDistributionIcon($list[$distid]['Image']);
408
                if (isset($list[trim($distid)]['Name'])) {
409
                    $this->sys->setDistribution(trim($list[$distid]['Name']).' '.$this->sys->getDistribution());
410
                }
411
            } else {
412
                $this->sys->setDistributionIcon('SunOS.png');
413
            }
414
        } else {
415
            $this->sys->setDistribution('SunOS');
416
            $this->sys->setDistributionIcon('SunOS.png');
417
        }
418
    }
325 richard 419
 
2770 rexy 420
    /**
421
     * Processes
422
     *
423
     * @return void
424
     */
425
    protected function _processes()
426
    {
427
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
428
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
429
            $processes['*'] = 0;
430
            foreach ($lines as $line) {
431
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
432
                    $processes['*']++;
433
                    $state = $ar_buf[1];
434
                    if ($state == 'O') $state = 'R'; //linux format
435
                    elseif ($state == 'W') $state = 'D';
436
                    elseif ($state == 'D') $state = 'd'; //invalid
437
                    if (isset($processes[$state])) {
438
                        $processes[$state]++;
439
                    } else {
440
                        $processes[$state] = 1;
441
                    }
442
                }
443
            }
444
            if ($processes['*'] > 0) {
445
                $this->sys->setProcesses($processes);
446
            }
447
        }
448
    }
325 richard 449
 
2770 rexy 450
    /**
451
     * get the information
452
     *
453
     * @see PSI_Interface_OS::build()
454
     *
455
     * @return Void
456
     */
457
    public function build()
458
    {
459
        $this->error->addError("WARN", "The SunOS version of phpSysInfo is a work in progress, some things currently don't work");
460
        if (!$this->blockname || $this->blockname==='vitals') {
461
            $this->_distro();
462
            $this->_hostname();
463
            $this->_kernel();
464
            $this->_uptime();
465
            $this->_users();
466
            $this->_loadavg();
467
            $this->_processes();
325 richard 468
        }
2770 rexy 469
        if (!$this->blockname || $this->blockname==='hardware') {
470
            $this->_cpuinfo();
471
            $this->_pci();
472
        }
473
        if (!$this->blockname || $this->blockname==='network') {
474
            $this->_network();
475
        }
476
        if (!$this->blockname || $this->blockname==='memory') {
477
            $this->_memory();
478
        }
479
        if (!$this->blockname || $this->blockname==='filesystem') {
480
            $this->_filesystems();
481
        }
482
    }
483
}