OpenBSD::_uptime()   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
 * OpenBSD System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI OpenBSD 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.OpenBSD.inc.php 621 2012-07-29 18:49:04Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * OpenBSD sysinfo class
17
 * get all the required information from OpenBSD systems
18
 *
19
 * @category  PHP
20
 * @package   PSI OpenBSD 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 OpenBSD extends BSDCommon
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
     * define the regexp for log parser
31
     */
32 View Code Duplication
    public function __construct()
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...
33
    {
34
        parent::__construct();
35
//        $this->setCPURegExp1("/^cpu(.*) (.*) MHz/");
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
36
        $this->setCPURegExp2("/(.*),(.*),(.*),(.*),(.*)/");
37
        $this->setSCSIRegExp1("/^(.*) at scsibus.*: <(.*)> .*/");
38
        $this->setSCSIRegExp2("/^(da[0-9]+): (.*)MB /");
39
        $this->setPCIRegExp1("/(.*) at pci[0-9]+ .* \"(.*)\"/");
40
        $this->setPCIRegExp2("/\"(.*)\" (.*).* at [.0-9]+ irq/");
41
    }
42
43
    /**
44
     * UpTime
45
     * time the system is running
46
     *
47
     * @return void
48
     */
49
    private function _uptime()
50
    {
51
        $a = $this->grabkey('kern.boottime');
52
        $this->sys->setUptime(time() - $a);
0 ignored issues
show
Documentation introduced by
time() - $a 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...
53
    }
54
55
    /**
56
     * get network information
57
     *
58
     * @return void
59
     */
60 View Code Duplication
    private function _network()
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...
61
    {
62
        CommonFunctions::executeProgram('netstat', '-nbdi | cut -c1-25,44- | grep Link | grep -v \'* \'', $netstat_b, PSI_DEBUG);
63
        CommonFunctions::executeProgram('netstat', '-ndi | cut -c1-25,44- | grep Link | grep -v \'* \'', $netstat_n, PSI_DEBUG);
64
        $lines_b = preg_split("/\n/", $netstat_b, -1, PREG_SPLIT_NO_EMPTY);
65
        $lines_n = preg_split("/\n/", $netstat_n, -1, PREG_SPLIT_NO_EMPTY);
66
        for ($i = 0, $max = sizeof($lines_b); $i < $max; $i++) {
67
            $ar_buf_b = preg_split("/\s+/", $lines_b[$i]);
68
            $ar_buf_n = preg_split("/\s+/", $lines_n[$i]);
69
            if (!empty($ar_buf_b[0]) && (!empty($ar_buf_n[3]) || ($ar_buf_n[3] === "0"))) {
70
                $dev = new NetDevice();
71
                $dev->setName($ar_buf_b[0]);
72
                $dev->setTxBytes($ar_buf_b[4]);
73
                $dev->setRxBytes($ar_buf_b[3]);
74
                $dev->setErrors($ar_buf_n[4] + $ar_buf_n[6]);
75
                $dev->setDrops($ar_buf_n[8]);
76
                if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf_b[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
77
                    $speedinfo = "";
78
                    $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
79
                    foreach ($bufe2 as $buf2) {
80
                        if (preg_match('/^\s+lladdr\s+(\S+)/i', $buf2, $ar_buf2))
81
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
82
                        elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2))
83
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
84
                        elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
85
                              || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
86
                              && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1]))
87
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
88
                        elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
89
                            if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
90
                                $unit = "G";
91
                            } else {
92
                                $unit = "M";
93
                            }
94
                            if (preg_match('/\s(\S+)-duplex/i', $buf2, $ar_buf3))
95
                                $speedinfo = $ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]);
96
                            else
97
                                $speedinfo = $ar_buf2[1].$unit.'b/s';
98
                        }
99
                    }
100
                    if ($speedinfo != "") $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
101
                }
102
                $this->sys->setNetDevices($dev);
103
            }
104
        }
105
    }
106
107
    /**
108
     * IDE information
109
     *
110
     * @return void
111
     */
112
    protected function ide()
113
    {
114
        foreach ($this->readdmesg() as $line) {
115
            if (preg_match('/^(.*) at pciide[0-9]+ (.*): <(.*)>/', $line, $ar_buf)) {
116
                $dev = new HWDevice();
117
                $dev->setName($ar_buf[0]);
118
                // now loop again and find the capacity
119
                foreach ($this->readdmesg() as $line2) {
120 View Code Duplication
                    if (preg_match("/^(".$ar_buf[0]."): (.*), (.*), (.*)MB, .*$/", $line2, $ar_buf_n)) {
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...
121
                        $dev->setCapacity($ar_buf_n[4] * 2048 * 1.049);
122
                    }
123
                }
124
                $this->sys->setIdeDevices($dev);
125
            }
126
        }
127
    }
128
129
    /**
130
     * get CPU information
131
     *
132
     * @return void
133
     */
134
    protected function cpuinfo()
135
    {
136
        $dev = new CpuDevice();
137
        $dev->setModel($this->grabkey('hw.model'));
138
        $dev->setCpuSpeed($this->grabkey('hw.cpuspeed'));
139
        $was = false;
140
        foreach ($this->readdmesg() as $line) {
141
            if (preg_match("/^cpu[0-9]+: (.*)/", $line, $ar_buf)) {
142
                $was = true;
143
                if (preg_match("/^cpu[0-9]+: (\d+)([KM])B (.*) L2 cache/", $line, $ar_buf2)) {
144
                    if ($ar_buf2[2]=="M") {
145
                        $dev->setCache($ar_buf2[1]*1024*1024);
146
                    } elseif ($ar_buf2[2]=="K") {
147
                        $dev->setCache($ar_buf2[1]*1024);
148
                    }
149
                } else {
150
                    $feats = preg_split("/,/", strtolower(trim($ar_buf[1])), -1, PREG_SPLIT_NO_EMPTY);
151
                    foreach ($feats as $feat) {
152
                        if (($feat=="vmx") || ($feat=="svm")) {
153
                            $dev->setVirt($feat);
154
                        }
155
                    }
156
                }
157
            } elseif ($was) {
158
                break;
159
            }
160
        }
161
        $ncpu = $this->grabkey('hw.ncpu');
162 View Code Duplication
        if (is_null($ncpu) || (trim($ncpu) == "") || (!($ncpu >= 1)))
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...
163
            $ncpu = 1;
164
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
165
            $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...
166
        }
167
    }
168
169
    /**
170
     * get icon name
171
     *
172
     * @return void
173
     */
174
    private function _distroicon()
175
    {
176
        $this->sys->setDistributionIcon('OpenBSD.png');
177
    }
178
179
    /**
180
     * Processes
181
     *
182
     * @return void
183
     */
184 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...
185
    {
186
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
187
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
188
            $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...
189
            foreach ($lines as $line) {
190
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
191
                    $processes['*']++;
192
                    $state = $ar_buf[1];
193
                    if ($state == 'I') $state = 'S'; //linux format
194
                    if (isset($processes[$state])) {
195
                        $processes[$state]++;
196
                    } else {
197
                        $processes[$state] = 1;
198
                    }
199
                }
200
            }
201
            if ($processes['*'] > 0) {
202
                $this->sys->setProcesses($processes);
203
            }
204
        }
205
    }
206
207
    /**
208
     * get the information
209
     *
210
     * @see BSDCommon::build()
211
     *
212
     * @return Void
213
     */
214
    public function build()
215
    {
216
        parent::build();
217
        $this->_distroicon();
218
        $this->_network();
219
        $this->_uptime();
220
        $this->_processes();
221
    }
222
}
223