Subversion Repositories ALCASAR

Rev

Rev 2976 | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 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 <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.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 <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 OpenBSD extends BSDCommon
28
{
29
    /**
30
     * define the regexp for log parser
31
     */
32
    public function __construct($blockname = false)
33
    {
34
        parent::__construct($blockname);
35
//        $this->setCPURegExp1("/^cpu(.*) (.*) MHz/");
36
        $this->setCPURegExp2("/(.*),(.*),(.*),(.*),(.*)/");
37
        $this->setSCSIRegExp1("/^(.*) at scsibus.*: <(.*)> .*/");
38
        $this->setSCSIRegExp2("/^(sd[0-9]+): (.*)MB,/");
39
        $this->setPCIRegExp1("/(.*) at pci[0-9]+ .* \"(.*)\"/");
40
        $this->setPCIRegExp2("/\"(.*)\" (.*).* at [.0-9]+ irq/");
41
    }
325 richard 42
 
2770 rexy 43
    /**
44
     * get network information
45
     *
46
     * @return void
47
     */
48
    private function _network()
49
    {
50
        CommonFunctions::executeProgram('netstat', '-nbdi | cut -c1-25,44- | grep Link | grep -v \'* \'', $netstat_b, PSI_DEBUG);
51
        CommonFunctions::executeProgram('netstat', '-ndi | cut -c1-25,44- | grep Link | grep -v \'* \'', $netstat_n, PSI_DEBUG);
52
        $lines_b = preg_split("/\n/", $netstat_b, -1, PREG_SPLIT_NO_EMPTY);
53
        $lines_n = preg_split("/\n/", $netstat_n, -1, PREG_SPLIT_NO_EMPTY);
54
        for ($i = 0, $max = sizeof($lines_b); $i < $max; $i++) {
55
            $ar_buf_b = preg_split("/\s+/", $lines_b[$i]);
56
            $ar_buf_n = preg_split("/\s+/", $lines_n[$i]);
57
            if (!empty($ar_buf_b[0]) && (!empty($ar_buf_n[3]) || ($ar_buf_n[3] === "0"))) {
58
                $dev = new NetDevice();
59
                $dev->setName($ar_buf_b[0]);
60
                $dev->setTxBytes($ar_buf_b[4]);
61
                $dev->setRxBytes($ar_buf_b[3]);
2976 rexy 62
                if (sizeof($ar_buf_n) == 9) {
63
                    $dev->setErrors($ar_buf_n[4] + $ar_buf_n[6]);
64
                    $dev->setDrops($ar_buf_n[8]);
65
                } elseif (sizeof($ar_buf_n) == 8) {
66
                    $dev->setDrops($ar_buf_n[4] + $ar_buf_n[6]);
67
                }
2770 rexy 68
                if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf_b[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
69
                    $speedinfo = "";
70
                    $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
71
                    foreach ($bufe2 as $buf2) {
72
                        if (preg_match('/^\s+lladdr\s+(\S+)/i', $buf2, $ar_buf2)) {
73
                            if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
74
                        } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)) {
75
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
76
                        } elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
77
                              || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
78
                              && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
79
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
80
                        } elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
81
                            if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
82
                                $unit = "G";
83
                            } else {
84
                                $unit = "M";
85
                            }
86
                            if (preg_match('/\s(\S+)-duplex/i', $buf2, $ar_buf3))
87
                                $speedinfo = $ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]);
88
                            else
89
                                $speedinfo = $ar_buf2[1].$unit.'b/s';
90
                        }
91
                    }
92
                    if ($speedinfo != "") $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
93
                }
94
                $this->sys->setNetDevices($dev);
95
            }
96
        }
97
    }
325 richard 98
 
2770 rexy 99
    /**
100
     * IDE information
101
     *
102
     * @return void
103
     */
104
    protected function ide()
105
    {
106
        foreach ($this->readdmesg() as $line) {
107
            if (preg_match('/^(.*) at pciide[0-9]+ (.*): <(.*)>/', $line, $ar_buf)) {
108
                $dev = new HWDevice();
109
                $dev->setName($ar_buf[3]);
110
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
111
                    // now loop again and find the capacity
112
                    foreach ($this->readdmesg() as $line2) {
113
                        if (preg_match("/^(".$ar_buf[1]."): (.*), (.*), (.*)MB, .*$/", $line2, $ar_buf_n)) {
114
                            $dev->setCapacity($ar_buf_n[4] * 1024 * 1024);
115
                            break;
116
                        }
117
                    }
118
                }
119
                $this->sys->setIdeDevices($dev);
120
            }
121
        }
122
    }
325 richard 123
 
2770 rexy 124
    /**
125
     * get CPU information
126
     *
127
     * @return void
128
     */
129
    protected function cpuinfo()
130
    {
131
        $was = false;
3037 rexy 132
        $cpuarray = array();
2770 rexy 133
        foreach ($this->readdmesg() as $line) {
3037 rexy 134
            if (preg_match("/^cpu([0-9])+: (.*)/", $line, $ar_buf)) {
2770 rexy 135
                $was = true;
3037 rexy 136
                $ar_buf[2] = trim($ar_buf[2]);
137
                if (preg_match("/^(.+), ([\d\.]+) MHz/", $ar_buf[2], $ar_buf2)) {
138
                    if (($model = trim($ar_buf2[1])) !== "") {
139
                        $cpuarray[$ar_buf[1]]['model'] = $model;
140
                    }
141
                    if (($speed = trim($ar_buf2[2])) > 0) {
142
                        $cpuarray[$ar_buf[1]]['speed'] = $speed;
143
                    }
144
                } elseif (preg_match("/(\d+)([KM])B \S+ \S+ L[23] cache$/", $ar_buf[2], $ar_buf2)) {
2770 rexy 145
                    if ($ar_buf2[2]=="M") {
3037 rexy 146
                        $cpuarray[$ar_buf[1]]['cache'] = $ar_buf2[1]*1024*1024;
2770 rexy 147
                    } elseif ($ar_buf2[2]=="K") {
3037 rexy 148
                        $cpuarray[$ar_buf[1]]['cache'] = $ar_buf2[1]*1024;
2770 rexy 149
                    }
150
                } else {
3037 rexy 151
                    $feats = preg_split("/,/", strtolower($ar_buf[2]), -1, PREG_SPLIT_NO_EMPTY);
2770 rexy 152
                    foreach ($feats as $feat) {
153
                        if (($feat=="vmx") || ($feat=="svm")) {
3037 rexy 154
                            $cpuarray[$ar_buf[1]]['virt'] = $feat;
155
                        } elseif ($feat=="hv") {
156
                            if (!isset($cpuarray[$ar_buf[1]]['virt'])) {
157
                                $cpuarray[$ar_buf[1]]['virt'] = 'hypervisor';
158
                            }
159
                            if (defined('PSI_SHOW_VIRTUALIZER_INFO') && PSI_SHOW_VIRTUALIZER_INFO) {
160
                                $this->sys->setVirtualizer("hypervisor", false);
161
                            }
2770 rexy 162
                        }
163
                    }
164
                }
3037 rexy 165
            } elseif (!preg_match("/^cpu[0-9]+|^mtrr: |^acpitimer[0-9]+: /", $line) && $was) {
2770 rexy 166
                break;
167
            }
168
        }
3037 rexy 169
 
2770 rexy 170
        $ncpu = $this->grabkey('hw.ncpu');
3037 rexy 171
        if (($ncpu === "") || !($ncpu >= 1)) {
2770 rexy 172
            $ncpu = 1;
3037 rexy 173
        }
174
        $ncpu = max($ncpu, count($cpuarray));
175
 
176
        $model = $this->grabkey('hw.model');
177
        $speed = $this->grabkey('hw.cpuspeed');
178
        $vendor = $this->grabkey('machdep.cpuvendor');
179
 
180
        for ($cpu = 0 ; $cpu < $ncpu ; $cpu++) {
181
            $dev = new CpuDevice();
182
 
183
            if (isset($cpuarray[$cpu]['model'])) {
184
                $dev->setModel($cpuarray[$cpu]['model']);
185
            } elseif ($model !== "") {
186
                $dev->setModel($model);
187
            }
188
            if (isset($cpuarray[$cpu]['speed'])) {
189
                $dev->setCpuSpeed($cpuarray[$cpu]['speed']);
190
            } elseif ($speed !== "") {
191
                $dev->setCpuSpeed($speed);
192
            }
193
            if (isset($cpuarray[$cpu]['cache'])) {
194
                $dev->setCache($cpuarray[$cpu]['cache']);
195
            }
196
            if (isset($cpuarray[$cpu]['virt'])) {
197
                $dev->setVirt($cpuarray[$cpu]['virt']);
198
            }
199
            if ($vendor !== "") {
200
                $dev->setVendorId($vendor);
201
            }
202
            if (($ncpu == 1) && PSI_LOAD_BAR) {
203
                $dev->setLoad($this->cpuusage());
204
            }
205
 
2770 rexy 206
            $this->sys->setCpus($dev);
207
        }
208
    }
325 richard 209
 
2770 rexy 210
    /**
211
     * get icon name
212
     *
213
     * @return void
214
     */
215
    private function _distroicon()
216
    {
217
        $this->sys->setDistributionIcon('OpenBSD.png');
218
    }
325 richard 219
 
2770 rexy 220
    /**
221
     * Processes
222
     *
223
     * @return void
224
     */
225
    protected function _processes()
226
    {
227
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
228
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
229
            $processes['*'] = 0;
230
            foreach ($lines as $line) {
231
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
232
                    $processes['*']++;
233
                    $state = $ar_buf[1];
234
                    if ($state == 'I') $state = 'S'; //linux format
235
                    if (isset($processes[$state])) {
236
                        $processes[$state]++;
237
                    } else {
238
                        $processes[$state] = 1;
239
                    }
240
                }
241
            }
242
            if ($processes['*'] > 0) {
243
                $this->sys->setProcesses($processes);
244
            }
245
        }
246
    }
325 richard 247
 
2770 rexy 248
    /**
249
     * get the information
250
     *
251
     * @see BSDCommon::build()
252
     *
3037 rexy 253
     * @return void
2770 rexy 254
     */
255
    public function build()
256
    {
257
        parent::build();
258
        if (!$this->blockname || $this->blockname==='vitals') {
259
            $this->_distroicon();
260
            $this->_processes();
261
        }
262
        if (!$this->blockname || $this->blockname==='network') {
263
            $this->_network();
264
        }
265
    }
266
}