Subversion Repositories ALCASAR

Rev

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

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * NetBSD System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI NetBSD 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.NetBSD.inc.php 287 2009-06-26 12:11:59Z bigmichi1 $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * NetBSD sysinfo class
17
 * get all the required information from NetBSD systems
18
 *
19
 * @category  PHP
20
 * @package   PSI NetBSD 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 NetBSD extends BSDCommon
28
{
29
    /**
30
     * define the regexp for log parser
31
     */
32
    public function __construct($blockname = false)
33
    {
34
        parent::__construct($blockname);
3037 rexy 35
        //$this->setCPURegExp1("/^cpu(.*)\, (.*) MHz/");
2770 rexy 36
        $this->setCPURegExp2("/user = (.*), nice = (.*), sys = (.*), intr = (.*), idle = (.*)/");
37
        $this->setSCSIRegExp1("/^(.*) at scsibus.*: <(.*)> .*/");
38
        $this->setSCSIRegExp2("/^(sd[0-9]+): (.*)([MG])B,/");
39
        $this->setPCIRegExp1("/(.*) at pci[0-9]+ dev [0-9]* function [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 "^[a-z]*[0-9][ \t].*Link"', $netstat_b);
51
        CommonFunctions::executeProgram('netstat', '-ndi | cut -c1-25,44- | grep "^[a-z]*[0-9][ \t].*Link"', $netstat_n);
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]);
62
                $dev->setDrops($ar_buf_n[8]);
63
                $dev->setErrors($ar_buf_n[4] + $ar_buf_n[6]);
64
                if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf_b[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
65
                    $speedinfo = "";
66
                    $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
67
                    foreach ($bufe2 as $buf2) {
68
                        if (preg_match('/^\s+address:\s+(\S+)/i', $buf2, $ar_buf2)) {
69
                            if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
70
                        } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)) {
71
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
72
                        } elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
73
                              || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
74
                              && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
75
                            $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
76
                        } elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
77
                            if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
78
                                $unit = "G";
79
                            } else {
80
                                $unit = "M";
81
                            }
82
                            if (preg_match('/\s(\S+)-duplex/i', $buf2, $ar_buf3))
83
                                $speedinfo = $ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]);
84
                            else
85
                                $speedinfo = $ar_buf2[1].$unit.'b/s';
86
                        }
87
                    }
88
                    if ($speedinfo != "") $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$speedinfo);
89
                }
90
                $this->sys->setNetDevices($dev);
91
            }
92
        }
93
    }
325 richard 94
 
2770 rexy 95
    /**
96
     * IDE information
97
     *
98
     * @return void
99
     */
100
    protected function ide()
101
    {
102
        foreach ($this->readdmesg() as $line) {
103
            if (preg_match('/^(.*) at (pciide|wdc|atabus|atapibus)[0-9]+ (.*): <(.*)>/', $line, $ar_buf)
104
               || preg_match('/^(.*) at (pciide|wdc|atabus|atapibus)[0-9]+ /', $line, $ar_buf)) {
105
                $dev = new HWDevice();
106
                if (isset($ar_buf[4])) {
107
                    $dev->setName($ar_buf[4]);
108
                } else {
109
                    $dev->setName($ar_buf[1]);
110
                    // now loop again and find the name
111
                    foreach ($this->readdmesg() as $line2) {
112
                        if (preg_match("/^(".$ar_buf[1]."): <(.*)>$/", $line2, $ar_buf_n)) {
113
                            $dev->setName($ar_buf_n[2]);
114
                            break;
115
                        }
116
                    }
117
                }
118
                if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
119
                    // now loop again and find the capacity
120
                    foreach ($this->readdmesg() as $line2) {
121
                        if (preg_match("/^(".$ar_buf[1]."): (.*), (.*), (.*)MB, .*$/", $line2, $ar_buf_n)) {
122
                            $dev->setCapacity($ar_buf_n[4] * 1024 * 1024);
123
                            break;
124
                        } elseif (preg_match("/^(".$ar_buf[1]."): (.*) MB, (.*), (.*), .*$/", $line2, $ar_buf_n)) {
125
                            $dev->setCapacity($ar_buf_n[2] * 1024 * 1024);
126
                            break;
127
                        } elseif (preg_match("/^(".$ar_buf[1]."): (.*) GB, (.*), (.*), .*$/", $line2, $ar_buf_n)) {
128
                            $dev->setCapacity($ar_buf_n[2] * 1024 * 1024 * 1024);
129
                            break;
130
                        }
131
                    }
132
                }
133
                $this->sys->setIdeDevices($dev);
134
            }
135
        }
136
    }
325 richard 137
 
2770 rexy 138
    /**
3037 rexy 139
     * CPU information
140
     *
141
     * @return void
142
     */
143
    protected function cpuinfo()
144
    {
145
        $was = false;
146
        $cpuarray = array();
147
        foreach ($this->readdmesg() as $line) {
148
            if (preg_match("/^cpu([0-9])+: (.*)/", $line, $ar_buf)) {
149
                $was = true;
150
                $ar_buf[2] = trim($ar_buf[2]);
151
                if (preg_match("/^(.+), ([\d\.]+) MHz/", $ar_buf[2], $ar_buf2)) {
152
                    if (($model = trim($ar_buf2[1])) !== "") {
153
                        $cpuarray[$ar_buf[1]]['model'] = $model;
154
                    }
155
                    if (($speed = trim($ar_buf2[2])) > 0) {
156
                        $cpuarray[$ar_buf[1]]['speed'] = $speed;
157
                    }
158
                } elseif (preg_match("/^L2 cache (\d+) ([KM])B /", $ar_buf[2], $ar_buf2)) {
159
                    if ($ar_buf2[2]=="M") {
160
                        $cpuarray[$ar_buf[1]]['cache'] = $ar_buf2[1]*1024*1024;
161
                    } elseif ($ar_buf2[2]=="K") {
162
                        $cpuarray[$ar_buf[1]]['cache'] = $ar_buf2[1]*1024;
163
                    }
164
                }
165
            } elseif (!preg_match("/^cpu[0-9]+ /", $line) && $was) {
166
                break;
167
            }
168
        }
169
 
170
        $ncpu = $this->grabkey('hw.ncpu');
171
        if (($ncpu === "") || !($ncpu >= 1)) {
172
            $ncpu = 1;
173
        }
174
        $ncpu = max($ncpu, count($cpuarray));
175
 
176
        $model = $this->grabkey('machdep.cpu_brand');
177
        $model2 = $this->grabkey('hw.model');
178
        if ($cpuspeed = $this->grabkey('machdep.tsc_freq')) {
179
            $speed = round($cpuspeed / 1000000);
180
        } else {
181
            $speed = "";
182
        }
183
 
184
        for ($cpu = 0 ; $cpu < $ncpu ; $cpu++) {
185
            $dev = new CpuDevice();
186
 
187
            if (isset($cpuarray[$cpu]['model'])) {
188
                $dev->setModel($cpuarray[$cpu]['model']);
189
            } elseif ($model !== "") {
190
                $dev->setModel($model);
191
            } elseif ($model2 !== "") {
192
                $dev->setModel($model2);
193
            }
194
            if (isset($cpuarray[$cpu]['speed'])) {
195
                $dev->setCpuSpeed($cpuarray[$cpu]['speed']);
196
            } elseif ($speed !== "") {
197
                $dev->setCpuSpeed($speed);
198
            }
199
            if (isset($cpuarray[$cpu]['cache'])) {
200
                $dev->setCache($cpuarray[$cpu]['cache']);
201
            }
202
            if (($ncpu == 1) && PSI_LOAD_BAR) {
203
                $dev->setLoad($this->cpuusage());
204
            }
205
            $this->sys->setCpus($dev);
206
        }
207
    }
208
 
209
    /**
2770 rexy 210
     * get icon name
211
     *
212
     * @return void
213
     */
214
    private function _distroicon()
215
    {
216
        $this->sys->setDistributionIcon('NetBSD.png');
217
    }
325 richard 218
 
2770 rexy 219
    /**
220
     * Processes
221
     *
222
     * @return void
223
     */
224
    protected function _processes()
225
    {
226
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
227
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
228
            $processes['*'] = 0;
229
            foreach ($lines as $line) {
230
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
231
                    $processes['*']++;
232
                    $state = $ar_buf[1];
233
                    if ($state == 'O') $state = 'R'; //linux format
234
                    elseif ($state == 'I') $state = 'S';
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
    }
247
 
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
    }
325 richard 266
}