SunOS::_distro()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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 <[email protected]>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
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 <[email protected]>
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class SunOS extends OS
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
28
{
29
    /**
30
     * Extract kernel values via kstat() interface
31
     *
32
     * @param string $key key for kstat programm
33
     *
34
     * @return string
35
     */
36
    private function _kstat($key)
37
    {
38
        if (CommonFunctions::executeProgram('kstat', '-p d '.$key, $m, PSI_DEBUG) && ($m!=="")) {
39
            list($key, $value) = preg_split("/\t/", $m, 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
40
41
            return trim($value);
42
        } else {
43
            return '';
44
        }
45
    }
46
47
    /**
48
     * Virtual Host Name
49
     *
50
     * @return void
51
     */
52 View Code Duplication
    private function _hostname()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        if (PSI_USE_VHOST === true) {
55
            $this->sys->setHostname(getenv('SERVER_NAME'));
56
        } else {
57
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
58
                $ip = gethostbyname($result);
59
                if ($ip != $result) {
60
                    $this->sys->setHostname(gethostbyaddr($ip));
61
                }
62
            }
63
        }
64
    }
65
66
    /**
67
     * Kernel Version
68
     *
69
     * @return void
70
     */
71
    private function _kernel()
72
    {
73
        if (CommonFunctions::executeProgram('uname', '-s', $os, PSI_DEBUG) && ($os!="")) {
74 View Code Duplication
            if (CommonFunctions::executeProgram('uname', '-r', $version, PSI_DEBUG) && ($version!="")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                $os.=' '.$version;
76
            }
77 View Code Duplication
            if (CommonFunctions::executeProgram('uname', '-v', $subversion, PSI_DEBUG) && ($subversion!="")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
                $os.=' ('.$subversion.')';
79
            }
80 View Code Duplication
            if (CommonFunctions::executeProgram('uname', '-i', $platform, PSI_DEBUG) && ($platform!="")) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                $os.=' '.$platform;
82
            }
83
            $this->sys->setKernel($os);
84
        }
85
    }
86
87
    /**
88
     * UpTime
89
     * time the system is running
90
     *
91
     * @return void
92
     */
93
    private function _uptime()
94
    {
95
        $this->sys->setUptime(time() - $this->_kstat('unix:0:system_misc:boot_time'));
0 ignored issues
show
Documentation introduced by
time() - $this->_kstat('...system_misc:boot_time') is of type integer|double, but the function expects a object<Interger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
    }
97
98
    /**
99
     * Processor Load
100
     * optionally create a loadbar
101
     *
102
     * @return void
103
     */
104
    private function _loadavg()
105
    {
106
        $load1 = $this->_kstat('unix:0:system_misc:avenrun_1min');
107
        $load5 = $this->_kstat('unix:0:system_misc:avenrun_5min');
108
        $load15 = $this->_kstat('unix:0:system_misc:avenrun_15min');
109
        $this->sys->setLoad(round($load1 / 256, 2).' '.round($load5 / 256, 2).' '.round($load15 / 256, 2));
110
    }
111
112
    /**
113
     * CPU information
114
     *
115
     * @return void
116
     */
117
    private function _cpuinfo()
118
    {
119
        if (CommonFunctions::executeProgram('kstat', '-p d cpu_info:*:cpu_info*:core_id', $m, PSI_DEBUG) && ($m!=="")) {
120
            $cpuc = count(preg_split('/\n/', $m, -1, PREG_SPLIT_NO_EMPTY));
121
            for ($cpu=0; $cpu < $cpuc; $cpu++) {
122
                $dev = new CpuDevice();
123
                if (($buf = $this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':clock_MHz')) !== "") {
124
                   $dev->setCpuSpeed($buf);
125
                }
126
                if (($buf = $this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':current_clock_Hz')) !== "") {
127
                    $dev->setCpuSpeedMax($buf/1000000);
128
                }
129
                if (($buf  =$this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':brand')) !== "") {
130
                    $dev->setModel($buf);
131
                } elseif (($buf  =$this->_kstat('cpu_info:'.$cpu.':cpu_info'.$cpu.':cpu_type')) !== "") {
132
                    $dev->setModel($buf);
133
                } elseif (CommonFunctions::executeProgram('uname', '-p', $buf, PSI_DEBUG) && ($buf!="")) {
134
                    $dev->setModel($buf);
135
                } elseif (CommonFunctions::executeProgram('uname', '-i', $buf, PSI_DEBUG) && ($buf!="")) {
136
                    $dev->setModel($buf);
137
                }
138
                $this->sys->setCpus($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<CpuDevice>, but the function expects a object<Cpu>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
139
            }
140
         }
141
    }
142
143
    /**
144
     * Network devices
145
     *
146
     * @return void
147
     */
148
    private function _network()
149
    {
150
        if (CommonFunctions::executeProgram('netstat', '-ni | awk \'(NF ==10){print;}\'', $netstat, PSI_DEBUG)) {
151
            $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
152
            foreach ($lines as $line) {
153
                $ar_buf = preg_split("/\s+/", $line);
154
                if (!empty($ar_buf[0]) && $ar_buf[0] !== 'Name') {
155
                    $dev = new NetDevice();
156
                    $dev->setName($ar_buf[0]);
157
                    $results[$ar_buf[0]]['errs'] = $ar_buf[5] + $ar_buf[7];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$results was never initialized. Although not strictly required by PHP, it is generally a good practice to add $results = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
158
                    if (preg_match('/^(\D+)(\d+)$/', $ar_buf[0], $intf)) {
159
                        $prefix = $intf[1].':'.$intf[2].':'.$intf[1].$intf[2].':';
160
                    } elseif (preg_match('/^(\D.*)(\d+)$/', $ar_buf[0], $intf)) {
161
                        $prefix = $intf[1].':'.$intf[2].':mac:';
162
                    } else {
163
                        $prefix = "";
164
                    }
165
                    if ($prefix !== "") {
166
                        $cnt = $this->_kstat($prefix.'drop');
167
                        if ($cnt > 0) {
168
                            $dev->setDrops($cnt);
169
                        }
170
                        $cnt = $this->_kstat($prefix.'obytes64');
171
                        if ($cnt > 0) {
172
                            $dev->setTxBytes($cnt);
173
                        }
174
                        $cnt = $this->_kstat($prefix.'rbytes64');
175
                        if ($cnt > 0) {
176
                            $dev->setRxBytes($cnt);
177
                        }
178
                    }
179
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
180
                        if (CommonFunctions::executeProgram('ifconfig', $ar_buf[0], $bufr2, PSI_DEBUG) && ($bufr2!=="")) {
181
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
182
                            foreach ($bufe2 as $buf2) {
183 View Code Duplication
                                if (preg_match('/^\s+ether\s+(\S+)/i', $buf2, $ar_buf2))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
185
                                elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2))
186
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
187
                            }
188
                        }
189
                        if (CommonFunctions::executeProgram('ifconfig', $ar_buf[0].' inet6', $bufr2, PSI_DEBUG) && ($bufr2!=="")) {
190
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
191
                            foreach ($bufe2 as $buf2) {
192 View Code Duplication
                                if (preg_match('/^\s+inet6\s+([^\s\/]+)/i', $buf2, $ar_buf2)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
                                   && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1]))
194
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
195
                            }
196
                        }
197
                    }
198
199
                    $this->sys->setNetDevices($dev);
200
                }
201
            }
202
        }
203
    }
204
205
    /**
206
     * Physical memory information and Swap Space information
207
     *
208
     * @return void
209
     */
210
    private function _memory()
211
    {
212
        $pagesize = $this->_kstat('unix:0:seg_cache:slab_size');
213
        $this->sys->setMemTotal($this->_kstat('unix:0:system_pages:pagestotal') * $pagesize);
214
        $this->sys->setMemUsed($this->_kstat('unix:0:system_pages:pageslocked') * $pagesize);
215
        $this->sys->setMemFree($this->_kstat('unix:0:system_pages:pagesfree') * $pagesize);
216
        $dev = new DiskDevice();
217
        $dev->setName('SWAP');
218
        $dev->setFsType('swap');
219
        $dev->setMountPoint('SWAP');
220
        $dev->setTotal($this->_kstat('unix:0:vminfo:swap_avail') / 1024);
221
        $dev->setUsed($this->_kstat('unix:0:vminfo:swap_alloc') / 1024);
222
        $dev->setFree($this->_kstat('unix:0:vminfo:swap_free') / 1024);
223
        $this->sys->setSwapDevices($dev);
224
    }
225
226
    /**
227
     * filesystem information
228
     *
229
     * @return void
230
     */
231
    private function _filesystems()
232
    {
233
        if (CommonFunctions::executeProgram('df', '-k', $df, PSI_DEBUG)) {
234
            $df = preg_replace('/\n\s/m', ' ', $df);
235
            $mounts = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
236
            foreach ($mounts as $mount) {
237
                $ar_buf = preg_split('/\s+/', $mount, 6);
238
                if (!empty($ar_buf[0]) && $ar_buf[0] !== 'Filesystem') {
239
                    $dev = new DiskDevice();
240
                    $dev->setName($ar_buf[0]);
241
                    $dev->setTotal($ar_buf[1] * 1024);
242
                    $dev->setUsed($ar_buf[2] * 1024);
243
                    $dev->setFree($ar_buf[3] * 1024);
244
                    $dev->setMountPoint($ar_buf[5]);
245
                    if (CommonFunctions::executeProgram('df', '-n', $dftypes, PSI_DEBUG)) {
246
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
247 View Code Duplication
                        foreach ($mounttypes as $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
                            $ty_buf = preg_split('/:/', $type, 2);
249
                            if (trim($ty_buf[0]) == $dev->getMountPoint()) {
250
                                $dev->setFsType($ty_buf[1]);
251
                                break;
252
                            }
253
                        }
254
                    } elseif (CommonFunctions::executeProgram('df', '-T', $dftypes, PSI_DEBUG)) {
255
                        $dftypes = preg_replace('/\n\s/m', ' ', $dftypes);
256
                        $mounttypes = preg_split("/\n/", $dftypes, -1, PREG_SPLIT_NO_EMPTY);
257 View Code Duplication
                        foreach ($mounttypes as $type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
                            $ty_buf = preg_split("/\s+/", $type, 3);
259
                            if ($ty_buf[0] == $dev->getName()) {
260
                                $dev->setFsType($ty_buf[1]);
261
                                break;
262
                            }
263
                        }
264
                    }
265
                    $this->sys->setDiskDevices($dev);
266
                }
267
            }
268
        }
269
    }
270
271
    /**
272
     * Distribution Icon
273
     *
274
     * @return void
275
     */
276
    private function _distro()
277
    {
278
        $this->sys->setDistribution('SunOS');
279
        $this->sys->setDistributionIcon('SunOS.png');
280
    }
281
282
    /**
283
     * Processes
284
     *
285
     * @return void
286
     */
287 View Code Duplication
    protected function _processes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
288
    {
289
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
290
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
291
            $processes['*'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$processes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $processes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
292
            foreach ($lines as $line) {
293
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
294
                    $processes['*']++;
295
                    $state = $ar_buf[1];
296
                    if ($state == 'O') $state = 'R'; //linux format
297
                    elseif ($state == 'W') $state = 'D';
298
                    elseif ($state == 'D') $state = 'd'; //invalid
299
                    if (isset($processes[$state])) {
300
                        $processes[$state]++;
301
                    } else {
302
                        $processes[$state] = 1;
303
                    }
304
                }
305
            }
306
            if ($processes['*'] > 0) {
307
                $this->sys->setProcesses($processes);
308
            }
309
        }
310
    }
311
312
    /**
313
     * get the information
314
     *
315
     * @see PSI_Interface_OS::build()
316
     *
317
     * @return Void
318
     */
319 View Code Duplication
    public function build()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320
    {
321
        $this->error->addError("WARN", "The SunOS version of phpSysInfo is a work in progress, some things currently don't work");
322
        $this->_distro();
323
        $this->_hostname();
324
        $this->_kernel();
325
        $this->_uptime();
326
        $this->_users();
327
        $this->_loadavg();
328
        $this->_cpuinfo();
329
        $this->_network();
330
        $this->_memory();
331
        $this->_filesystems();
332
        $this->_processes();
333
    }
334
}
335