Subversion Repositories ALCASAR

Rev

Rev 2976 | Go to most recent revision | Details | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * Haiku System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI Haiku OS class
9
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
10
 * @copyright 2012 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.Haiku.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Haiku sysinfo class
17
 * get all the required information from Haiku system
18
 *
19
 * @category  PHP
20
 * @package   PSI Haiku OS class
21
 * @author    Mieczyslaw Nalewaj <namiltd@users.sourceforge.net>
22
 * @copyright 2012 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 Haiku extends OS
28
{
29
    /**
30
     * get the cpu information
31
     *
32
     * @return void
33
     */
34
    protected function _cpuinfo()
35
    {
36
 
37
        if (CommonFunctions::executeProgram('sysinfo', '-cpu', $bufr, PSI_DEBUG)) {
38
            $cpus = preg_split("/\nCPU #\d+/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
39
            $cpuspeed = "";
40
            foreach ($cpus as $cpu) {
41
                if (preg_match("/^.*running at (\d+)MHz/", $cpu, $ar_buf)) {
42
                    $cpuspeed = $ar_buf[1];
43
                } elseif (preg_match("/^: \"(.*)\"/", $cpu, $ar_buf)) {
44
                    $dev = new CpuDevice();
45
                    $dev->setModel($ar_buf[1]);
46
                    $arrLines = preg_split("/\n/", $cpu, -1, PREG_SPLIT_NO_EMPTY);
47
                    foreach ($arrLines as $Line) {
48
                        if (preg_match("/^\s+Data TLB:\s+(.*)K-byte/", $Line, $Line_buf)) {
49
                            $dev->setCache(max($Line_buf[1]*1024, $dev->getCache()));
50
                        } elseif (preg_match("/^\s+Data TLB:\s+(.*)M-byte/", $Line, $Line_buf)) {
51
                            $dev->setCache(max($Line_buf[1]*1024*1024, $dev->getCache()));
52
                        } elseif (preg_match("/^\s+Data TLB:\s+(.*)G-byte/", $Line, $Line_buf)) {
53
                            $dev->setCache(max($Line_buf[1]*1024*1024*1024, $dev->getCache()));
54
                        } elseif (preg_match("/\s+VMX/", $Line, $Line_buf)) {
55
                            $dev->setVirt("vmx");
56
                        } elseif (preg_match("/\s+SVM/", $Line, $Line_buf)) {
57
                            $dev->setVirt("svm");
58
                        }
59
                    }
60
                    if ($cpuspeed != "") {
61
                        $dev->setCpuSpeed($cpuspeed);
62
                    }
63
                    $this->sys->setCpus($dev);
64
                }
65
            }
66
        }
67
    }
68
 
69
    /**
70
     * PCI devices
71
     * get the pci device information
72
     *
73
     * @return void
74
     */
75
    protected function _pci()
76
    {
77
        if (CommonFunctions::executeProgram('listdev', '', $bufr, PSI_DEBUG)) {
78
//            $devices = preg_split("/^device |\ndevice /", $bufr, -1, PREG_SPLIT_NO_EMPTY);
79
            $devices = preg_split("/^device /m", $bufr, -1, PREG_SPLIT_NO_EMPTY);
80
            foreach ($devices as $device) {
81
                $ar_buf = preg_split("/\n/", $device);
82
                if (count($ar_buf) >= 3) {
83
                    if (preg_match("/^([^\(\[\n]*)/", $device, $ar_buf2)) {
84
                        if (preg_match("/^[^\(]*\((.*)\)/", $device, $ar_buf3)) {
85
                            $ar_buf2[1] = $ar_buf3[1];
86
                        }
87
                        $name = trim($ar_buf2[1]).": ";
88
 
89
                        if (preg_match("/^\s+vendor\s+[0-9a-fA-F]{4}:\s+(.*)/", $ar_buf[1], $ar_buf3)) {
90
                            $name .=$ar_buf3[1]." ";
91
                        }
92
                        if (preg_match("/^\s+device\s+[0-9a-fA-F]{4}:\s+(.*)/", $ar_buf[2], $ar_buf3)) {
93
                            $name .=$ar_buf3[1]." ";
94
                        }
95
                        $dev = new HWDevice();
96
                        $dev->setName(trim($name));
97
                        $this->sys->setPciDevices($dev);
98
                    }
99
                }
100
            }
101
        }
102
    }
103
 
104
    /**
105
     * USB devices
106
     * get the usb device information
107
     *
108
     * @return void
109
     */
110
    protected function _usb()
111
    {
112
        if (CommonFunctions::executeProgram('listusb', '', $bufr, PSI_DEBUG)) {
113
            $devices = preg_split("/\n/", $bufr);
114
            foreach ($devices as $device) {
115
                if (preg_match("/^\S+\s+\S+\s+\"(.*)\"\s+\"(.*)\"/", $device, $ar_buf)) {
116
                    $dev = new HWDevice();
117
                    $dev->setName(trim($ar_buf[1]." ".$ar_buf[2]));
118
                    $this->sys->setUSBDevices($dev);
119
                }
120
            }
121
        }
122
    }
123
 
124
    /**
125
     * Haiku Version
126
     *
127
     * @return void
128
     */
129
    private function _kernel()
130
    {
131
        if (CommonFunctions::executeProgram('uname', '-rvm', $ret)) {
132
            $this->sys->setKernel($ret);
133
        }
134
    }
135
 
136
    /**
137
     * Distribution
138
     *
139
     * @return void
140
     */
141
    protected function _distro()
142
    {
143
        if (CommonFunctions::executeProgram('uname', '-sr', $ret))
144
            $this->sys->setDistribution($ret);
145
        else
146
            $this->sys->setDistribution('Haiku');
147
 
148
        $this->sys->setDistributionIcon('Haiku.png');
149
    }
150
 
151
    /**
152
     * UpTime
153
     * time the system is running
154
     *
155
     * @return void
156
     */
157
    private function _uptime()
158
    {
159
        if (CommonFunctions::executeProgram('uptime', '-u', $buf)) {
160
            if (preg_match("/^up (\d+) minute[s]?/", $buf, $ar_buf)) {
161
                $min = $ar_buf[1];
162
                $this->sys->setUptime($min * 60);
163
            } elseif (preg_match("/^up (\d+) hour[s]?, (\d+) minute[s]?/", $buf, $ar_buf)) {
164
                $min = $ar_buf[2];
165
                $hours = $ar_buf[1];
166
                $this->sys->setUptime($hours * 3600 + $min * 60);
167
            } elseif (preg_match("/^up (\d+) day[s]?, (\d+) hour[s]?, (\d+) minute[s]?/", $buf, $ar_buf)) {
168
                $min = $ar_buf[3];
169
                $hours = $ar_buf[2];
170
                $days = $ar_buf[1];
171
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
172
            }
173
        }
174
    }
175
 
176
    /**
177
     * Processor Load
178
     * optionally create a loadbar
179
     *
180
     * @return void
181
     */
182
    private function _loadavg()
183
    {
184
        if (CommonFunctions::executeProgram('top', '-n 1 -i 1', $buf)) {
185
            if (preg_match("/\s+(\S+)%\s+TOTAL\s+\(\S+%\s+idle time/", $buf, $ar_buf)) {
186
                $this->sys->setLoad($ar_buf[1]);
187
                if (PSI_LOAD_BAR) {
188
                    $this->sys->setLoadPercent(round($ar_buf[1]));
189
                }
190
            }
191
        }
192
    }
193
 
194
    /**
195
     * Number of Users
196
     *
197
     * @return void
198
     */
199
    protected function _users()
200
    {
201
        $this->sys->setUsers(1);
202
    }
203
 
204
    /**
205
     * Virtual Host Name
206
     *
207
     * @return void
208
     */
209
    private function _hostname()
210
    {
211
        if (PSI_USE_VHOST === true) {
212
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
213
        } else {
214
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
215
                $ip = gethostbyname($result);
216
                if ($ip != $result) {
217
                    $this->sys->setHostname(gethostbyaddr($ip));
218
                }
219
            }
220
        }
221
    }
222
 
223
    /**
224
     *  Physical memory information and Swap Space information
225
     *
226
     *  @return void
227
     */
228
    private function _memory()
229
    {
230
        if (CommonFunctions::executeProgram('sysinfo', '-mem', $bufr, PSI_DEBUG)) {
231
            if (preg_match("/(.*)bytes free\s+\(used\/max\s+(.*)\s+\/\s+(.*)\)\s*\n\s+\(cached\s+(.*)\)/", $bufr, $ar_buf)) {
232
                $this->sys->setMemTotal($ar_buf[3]);
233
                $this->sys->setMemFree($ar_buf[1]);
234
                $this->sys->setMemCache($ar_buf[4]);
235
                $this->sys->setMemUsed($ar_buf[2]);
236
            }
237
        }
238
        if (CommonFunctions::executeProgram('vmstat', '', $bufr, PSI_DEBUG)) {
239
            if (preg_match("/max swap space:\s+(.*)\nfree swap space:\s+(.*)\n/", $bufr, $ar_buf)) {
240
                if ($ar_buf[1]>0) {
241
                    $dev = new DiskDevice();
242
                    $dev->setMountPoint("/boot/common/var/swap");
243
                    $dev->setName("SWAP");
244
                    $dev->setTotal($ar_buf[1]);
245
                    $dev->setFree($ar_buf[2]);
246
                    $dev->setUSed($ar_buf[1]-$ar_buf[2]);
247
                    $this->sys->setSwapDevices($dev);
248
                }
249
            }
250
        }
251
    }
252
 
253
    /**
254
     * filesystem information
255
     *
256
     * @return void
257
     */
258
    private function _filesystems()
259
    {
260
        if (CommonFunctions::executeProgram('df', '-b', $df, PSI_DEBUG)) {
261
        $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
262
            foreach ($df as $df_line) {
263
                $ar_buf = preg_split("/\s+/", $df_line);
264
                if ((substr($df_line, 0, 1) == "/") && (count($ar_buf) == 6)) {
265
                    $dev = new DiskDevice();
266
                    $dev->setMountPoint($ar_buf[0]);
267
                    $dev->setName($ar_buf[5]);
268
                    $dev->setFsType($ar_buf[1]);
269
                    $dev->setOptions($ar_buf[4]);
270
                    $dev->setTotal($ar_buf[2] * 1024);
271
                    $dev->setFree($ar_buf[3] * 1024);
272
                    $dev->setUsed($dev->getTotal() - $dev->getFree());
273
                    $this->sys->setDiskDevices($dev);
274
                }
275
            }
276
        }
277
    }
278
 
279
    /**
280
     * network information
281
     *
282
     * @return void
283
     */
284
    private function _network()
285
    {
286
        if (CommonFunctions::executeProgram('ifconfig', '', $bufr, PSI_DEBUG)) {
287
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
288
            $was = false;
289
            $errors = 0;
290
            $drops = 0;
291
            $dev = null;
292
            foreach ($lines as $line) {
293
                if (preg_match("/^(\S+)/", $line, $ar_buf)) {
294
                    if ($was) {
295
                        $dev->setErrors($errors);
296
                        $dev->setDrops($drops);
297
                        $this->sys->setNetDevices($dev);
298
                    }
299
                    $errors = 0;
300
                    $drops = 0;
301
                    $dev = new NetDevice();
302
                    $dev->setName($ar_buf[1]);
303
                    $was = true;
304
                } else {
305
                    if ($was) {
306
                        if (preg_match('/\sReceive:\s\d+\spackets,\s(\d+)\serrors,\s(\d+)\sbytes,\s\d+\smcasts,\s(\d+)\sdropped/i', $line, $ar_buf2)) {
307
                            $errors +=$ar_buf2[1];
308
                            $drops +=$ar_buf2[3];
309
                            $dev->setRxBytes($ar_buf2[2]);
310
                        } elseif (preg_match('/\sTransmit:\s\d+\spackets,\s(\d+)\serrors,\s(\d+)\sbytes,\s\d+\smcasts,\s(\d+)\sdropped/i', $line, $ar_buf2)) {
311
                            $errors +=$ar_buf2[1];
312
                            $drops +=$ar_buf2[3];
313
                            $dev->setTxBytes($ar_buf2[2]);
314
                        }
315
 
316
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
317
                            if (preg_match('/\sEthernet,\s+Address:\s(\S*)/i', $line, $ar_buf2)) {
318
                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
319
                            } elseif (preg_match('/^\s+inet\saddr:\s(\S*),/i', $line, $ar_buf2)) {
320
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
321
                            } elseif (preg_match('/^\s+inet6\saddr:\s(\S*),/i', $line, $ar_buf2)
322
                                     && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
323
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
324
                            }
325
                        }
326
                    }
327
                }
328
            }
329
            if ($was) {
330
                $dev->setErrors($errors);
331
                $dev->setDrops($drops);
332
                $this->sys->setNetDevices($dev);
333
            }
334
        }
335
    }
336
 
337
    /**
338
     * Processes
339
     *
340
     * @return void
341
     */
342
    protected function _processes()
343
    {
344
        if (CommonFunctions::executeProgram('ps', '', $bufr, PSI_DEBUG)) {
345
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
346
            $processes['*'] = 0;
347
            foreach ($lines as $line) {
348
                if (preg_match("/^(kernel_team|\/)/", $line, $ar_buf)) {
349
                    $processes['*']++;
350
                }
351
            }
352
            if ($processes['*'] > 0) {
353
                $processes[' '] = $processes['*'];
354
                $this->sys->setProcesses($processes);
355
            }
356
        }
357
    }
358
 
359
    /**
360
     * get the information
361
     *
362
     * @return Void
363
     */
364
    public function build()
365
    {
366
        $this->error->addError("WARN", "The Haiku version of phpSysInfo is a work in progress, some things currently don't work");
367
        if (!$this->blockname || $this->blockname==='vitals') {
368
            $this->_distro();
369
            $this->_hostname();
370
            $this->_kernel();
371
            $this->_uptime();
372
            $this->_users();
373
            $this->_loadavg();
374
            $this->_processes();
375
        }
376
        if (!$this->blockname || $this->blockname==='hardware') {
377
           $this->_cpuinfo();
378
           $this->_pci();
379
           $this->_usb();
380
        }
381
        if (!$this->blockname || $this->blockname==='network') {
382
            $this->_network();
383
        }
384
        if (!$this->blockname || $this->blockname==='memory') {
385
            $this->_memory();
386
        }
387
        if (!$this->blockname || $this->blockname==='filesystem') {
388
            $this->_filesystems();
389
        }
390
    }
391
}