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
 * 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) {
2976 rexy 48
                        if (preg_match("/^\s+Data TLB:\s+(.*)K-byte/", $Line, $Line_buf) || preg_match("/^\s+L0 Data TLB:\s+(.*)K-byte/", $Line, $Line_buf)) {
49
                            $dev->setCache(max(intval($Line_buf[1])*1024, $dev->getCache()));
50
                        } elseif (preg_match("/^\s+Data TLB:\s+(.*)M-byte/", $Line, $Line_buf) || preg_match("/^\s+L0 Data TLB:\s+(.*)M-byte/", $Line, $Line_buf)) {
51
                            $dev->setCache(max(intval($Line_buf[1])*1024*1024, $dev->getCache()));
52
                        } elseif (preg_match("/^\s+Data TLB:\s+(.*)G-byte/", $Line, $Line_buf) || preg_match("/^\s+L0 Data TLB:\s+(.*)G-byte/", $Line, $Line_buf)) {
53
                            $dev->setCache(max(intval($Line_buf[1])*1024*1024*1024, $dev->getCache()));
2770 rexy 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
    {
2976 rexy 159
        if (CommonFunctions::executeProgram('uptime', '', $buf)) {
160
            if (preg_match("/up (\d+) day[s]?,[ ]+(\d+):(\d+),/", $buf, $ar_buf)) {
161
                $min = $ar_buf[3];
162
                $hours = $ar_buf[2];
163
                $days = $ar_buf[1];
164
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
165
            } elseif (preg_match("/up[ ]+(\d+):(\d+),/", $buf, $ar_buf)) {
2770 rexy 166
                $min = $ar_buf[2];
167
                $hours = $ar_buf[1];
168
                $this->sys->setUptime($hours * 3600 + $min * 60);
2976 rexy 169
            } elseif (preg_match("/up (\d+) day[s]?, (\d+) hour[s]?, (\d+) minute[s]?$/", $buf, $ar_buf)) {
2770 rexy 170
                $min = $ar_buf[3];
171
                $hours = $ar_buf[2];
172
                $days = $ar_buf[1];
173
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
2976 rexy 174
            } elseif (preg_match("/up (\d+) hour[s]?, (\d+) minute[s]?$/", $buf, $ar_buf)) {
175
                $min = $ar_buf[2];
176
                $hours = $ar_buf[1];
177
                $this->sys->setUptime($hours * 3600 + $min * 60);
178
            } elseif (preg_match("/up (\d+) minute[s]?$/", $buf, $ar_buf)) {
179
                $min = $ar_buf[1];
180
                $this->sys->setUptime($min * 60);
2770 rexy 181
            }
182
        }
183
    }
184
 
185
    /**
186
     * Processor Load
187
     * optionally create a loadbar
188
     *
189
     * @return void
190
     */
191
    private function _loadavg()
192
    {
193
        if (CommonFunctions::executeProgram('top', '-n 1 -i 1', $buf)) {
194
            if (preg_match("/\s+(\S+)%\s+TOTAL\s+\(\S+%\s+idle time/", $buf, $ar_buf)) {
195
                $this->sys->setLoad($ar_buf[1]);
196
                if (PSI_LOAD_BAR) {
197
                    $this->sys->setLoadPercent(round($ar_buf[1]));
198
                }
199
            }
200
        }
201
    }
202
 
203
    /**
204
     * Number of Users
205
     *
206
     * @return void
207
     */
208
    protected function _users()
209
    {
210
        $this->sys->setUsers(1);
211
    }
212
 
213
    /**
214
     * Virtual Host Name
215
     *
216
     * @return void
217
     */
218
    private function _hostname()
219
    {
3037 rexy 220
        if (PSI_USE_VHOST) {
2770 rexy 221
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
222
        } else {
223
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
224
                $ip = gethostbyname($result);
225
                if ($ip != $result) {
226
                    $this->sys->setHostname(gethostbyaddr($ip));
227
                }
228
            }
229
        }
230
    }
231
 
232
    /**
233
     *  Physical memory information and Swap Space information
234
     *
235
     *  @return void
236
     */
237
    private function _memory()
238
    {
239
        if (CommonFunctions::executeProgram('sysinfo', '-mem', $bufr, PSI_DEBUG)) {
240
            if (preg_match("/(.*)bytes free\s+\(used\/max\s+(.*)\s+\/\s+(.*)\)\s*\n\s+\(cached\s+(.*)\)/", $bufr, $ar_buf)) {
241
                $this->sys->setMemTotal($ar_buf[3]);
242
                $this->sys->setMemFree($ar_buf[1]);
2976 rexy 243
                $this->sys->setMemCache(min($ar_buf[4], $ar_buf[2]));
2770 rexy 244
                $this->sys->setMemUsed($ar_buf[2]);
245
            }
246
        }
247
        if (CommonFunctions::executeProgram('vmstat', '', $bufr, PSI_DEBUG)) {
248
            if (preg_match("/max swap space:\s+(.*)\nfree swap space:\s+(.*)\n/", $bufr, $ar_buf)) {
249
                if ($ar_buf[1]>0) {
250
                    $dev = new DiskDevice();
251
                    $dev->setMountPoint("/boot/common/var/swap");
252
                    $dev->setName("SWAP");
253
                    $dev->setTotal($ar_buf[1]);
254
                    $dev->setFree($ar_buf[2]);
255
                    $dev->setUSed($ar_buf[1]-$ar_buf[2]);
256
                    $this->sys->setSwapDevices($dev);
257
                }
258
            }
259
        }
260
    }
261
 
262
    /**
263
     * filesystem information
264
     *
265
     * @return void
266
     */
267
    private function _filesystems()
268
    {
269
        if (CommonFunctions::executeProgram('df', '-b', $df, PSI_DEBUG)) {
270
        $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
271
            foreach ($df as $df_line) {
272
                $ar_buf = preg_split("/\s+/", $df_line);
273
                if ((substr($df_line, 0, 1) == "/") && (count($ar_buf) == 6)) {
274
                    $dev = new DiskDevice();
275
                    $dev->setMountPoint($ar_buf[0]);
276
                    $dev->setName($ar_buf[5]);
277
                    $dev->setFsType($ar_buf[1]);
278
                    $dev->setOptions($ar_buf[4]);
279
                    $dev->setTotal($ar_buf[2] * 1024);
280
                    $dev->setFree($ar_buf[3] * 1024);
281
                    $dev->setUsed($dev->getTotal() - $dev->getFree());
282
                    $this->sys->setDiskDevices($dev);
283
                }
284
            }
285
        }
286
    }
287
 
288
    /**
289
     * network information
290
     *
291
     * @return void
292
     */
293
    private function _network()
294
    {
295
        if (CommonFunctions::executeProgram('ifconfig', '', $bufr, PSI_DEBUG)) {
296
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
297
            $was = false;
298
            $errors = 0;
299
            $drops = 0;
300
            $dev = null;
301
            foreach ($lines as $line) {
302
                if (preg_match("/^(\S+)/", $line, $ar_buf)) {
303
                    if ($was) {
304
                        $dev->setErrors($errors);
305
                        $dev->setDrops($drops);
306
                        $this->sys->setNetDevices($dev);
307
                    }
308
                    $errors = 0;
309
                    $drops = 0;
310
                    $dev = new NetDevice();
311
                    $dev->setName($ar_buf[1]);
312
                    $was = true;
313
                } else {
314
                    if ($was) {
315
                        if (preg_match('/\sReceive:\s\d+\spackets,\s(\d+)\serrors,\s(\d+)\sbytes,\s\d+\smcasts,\s(\d+)\sdropped/i', $line, $ar_buf2)) {
316
                            $errors +=$ar_buf2[1];
317
                            $drops +=$ar_buf2[3];
318
                            $dev->setRxBytes($ar_buf2[2]);
319
                        } elseif (preg_match('/\sTransmit:\s\d+\spackets,\s(\d+)\serrors,\s(\d+)\sbytes,\s\d+\smcasts,\s(\d+)\sdropped/i', $line, $ar_buf2)) {
320
                            $errors +=$ar_buf2[1];
321
                            $drops +=$ar_buf2[3];
322
                            $dev->setTxBytes($ar_buf2[2]);
323
                        }
324
 
325
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
326
                            if (preg_match('/\sEthernet,\s+Address:\s(\S*)/i', $line, $ar_buf2)) {
327
                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
328
                            } elseif (preg_match('/^\s+inet\saddr:\s(\S*),/i', $line, $ar_buf2)) {
329
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
330
                            } elseif (preg_match('/^\s+inet6\saddr:\s(\S*),/i', $line, $ar_buf2)
331
                                     && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
332
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
333
                            }
334
                        }
335
                    }
336
                }
337
            }
338
            if ($was) {
339
                $dev->setErrors($errors);
340
                $dev->setDrops($drops);
341
                $this->sys->setNetDevices($dev);
342
            }
343
        }
344
    }
345
 
346
    /**
347
     * Processes
348
     *
349
     * @return void
350
     */
351
    protected function _processes()
352
    {
353
        if (CommonFunctions::executeProgram('ps', '', $bufr, PSI_DEBUG)) {
354
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
355
            $processes['*'] = 0;
356
            foreach ($lines as $line) {
357
                if (preg_match("/^(kernel_team|\/)/", $line, $ar_buf)) {
358
                    $processes['*']++;
359
                }
360
            }
361
            if ($processes['*'] > 0) {
362
                $processes[' '] = $processes['*'];
363
                $this->sys->setProcesses($processes);
364
            }
365
        }
366
    }
367
 
368
    /**
369
     * get the information
370
     *
3037 rexy 371
     * @return void
2770 rexy 372
     */
373
    public function build()
374
    {
3037 rexy 375
        $this->error->addWarning("The Haiku version of phpSysInfo is a work in progress, some things currently don't work");
2770 rexy 376
        if (!$this->blockname || $this->blockname==='vitals') {
377
            $this->_distro();
378
            $this->_hostname();
379
            $this->_kernel();
380
            $this->_uptime();
381
            $this->_users();
382
            $this->_loadavg();
383
            $this->_processes();
384
        }
385
        if (!$this->blockname || $this->blockname==='hardware') {
386
           $this->_cpuinfo();
387
           $this->_pci();
388
           $this->_usb();
389
        }
390
        if (!$this->blockname || $this->blockname==='memory') {
391
            $this->_memory();
392
        }
393
        if (!$this->blockname || $this->blockname==='filesystem') {
394
            $this->_filesystems();
395
        }
3037 rexy 396
        if (!$this->blockname || $this->blockname==='network') {
397
            $this->_network();
398
        }
2770 rexy 399
    }
400
}