Subversion Repositories ALCASAR

Rev

Rev 325 | Rev 3037 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * Darwin System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI Darwin 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.Darwin.inc.php 638 2012-08-24 09:40:48Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Darwin sysinfo class
17
 * get all the required information from Darwin system
18
 * information may be incomplete
19
 *
20
 * @category  PHP
21
 * @package   PSI Darwin OS class
22
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
23
 * @copyright 2009 phpSysInfo
24
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
25
 * @version   Release: 3.0
26
 * @link      http://phpsysinfo.sourceforge.net
27
 */
28
class Darwin extends BSDCommon
29
{
30
    /**
31
     * define the regexp for log parser
32
     */
33
    /* public function __construct($blockname = false)
34
    {
35
        parent::__construct($blockname);
36
        $this->error->addWarning("The Darwin version of phpSysInfo is a work in progress, some things currently don't work!");
37
        $this->setCPURegExp1("/CPU: (.*) \((.*)-MHz (.*)\)/");
38
        $this->setCPURegExp2("/(.*) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/");
39
        $this->setSCSIRegExp1("/^(.*): <(.*)> .*SCSI.*device/");
40
    } */
325 richard 41
 
2770 rexy 42
    /**
43
     * get a value from sysctl command
44
     *
45
     * @param string $key key of the value to get
46
     *
47
     * @return string
48
     */
49
    protected function grabkey($key)
50
    {
51
        if (CommonFunctions::executeProgram('sysctl', $key, $s, PSI_DEBUG)) {
52
            $s = preg_replace('/'.$key.': /', '', $s);
53
            $s = preg_replace('/'.$key.' = /', '', $s);
325 richard 54
 
2770 rexy 55
            return $s;
56
        } else {
57
            return '';
58
        }
59
    }
325 richard 60
 
2770 rexy 61
    /**
62
     * get a value from ioreg command
63
     *
64
     * @param string $key key of the value to get
65
     *
66
     * @return string
67
     */
68
    private function _grabioreg($key)
69
    {
70
        if (CommonFunctions::executeProgram('ioreg', '-c "'.$key.'"', $s, PSI_DEBUG)) {
71
            /* delete newlines */
72
            $s = preg_replace("/\s+/", " ", $s);
73
            /* new newlines */
74
            $s = preg_replace("/[\|\t ]*\+\-o/", "\n", $s);
75
            /* combine duplicate whitespaces and some chars */
76
            $s = preg_replace("/[\|\t ]+/", " ", $s);
325 richard 77
 
2770 rexy 78
            $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
79
            $out = "";
80
            foreach ($lines as $line) {
81
                if (preg_match('/^([^<]*) <class '.$key.',/', $line)) {
82
                    $out .= $line."\n";
83
                }
84
            }
325 richard 85
 
2770 rexy 86
            return $out;
87
        } else {
88
            return '';
89
        }
90
    }
325 richard 91
 
2770 rexy 92
    /**
93
     * UpTime
94
     * time the system is running
95
     *
96
     * @return void
97
     */
98
    private function _uptime()
99
    {
100
        if (CommonFunctions::executeProgram('sysctl', '-n kern.boottime', $a, PSI_DEBUG)) {
101
            $tmp = explode(" ", $a);
102
            if ($tmp[0]=="{") { /* kern.boottime= { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004 */
103
              $data = trim($tmp[3], ",");
104
              $this->sys->setUptime(time() - $data);
105
            } else { /* kern.boottime= 1096732600 */
106
              $this->sys->setUptime(time() - $a);
107
            }
108
        }
109
    }
325 richard 110
 
2770 rexy 111
    /**
112
     * get CPU information
113
     *
114
     * @return void
115
     */
116
    protected function cpuinfo()
117
    {
118
        $dev = new CpuDevice();
119
        if (CommonFunctions::executeProgram('hostinfo', '| grep "Processor type"', $buf, PSI_DEBUG)) {
120
            $dev->setModel(preg_replace('/Processor type: /', '', $buf));
121
            $buf=$this->grabkey('hw.model');
122
            if (!is_null($buf) && (trim($buf) != "")) {
123
                $this->sys->setMachine(trim($buf));
124
                if (CommonFunctions::rfts(PSI_APP_ROOT.'/data/ModelTranslation.txt', $buffer)) {
125
                    $buffer = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
126
                    foreach ($buffer as $line) {
127
                        $ar_buf = preg_split("/:/", $line, 3);
128
                        if (trim($buf) === trim($ar_buf[0])) {
129
                            $dev->setModel(trim($ar_buf[2]));
130
                            $this->sys->setMachine($this->sys->getMachine().' - '.trim($ar_buf[1]));
131
                            break;
132
                        }
133
                    }
134
                }
135
            }
136
            $buf=$this->grabkey('machdep.cpu.brand_string');
137
            if (!is_null($buf) && (trim($buf) != "") &&
138
                 ((trim($buf) != "i486 (Intel 80486)") || ($dev->getModel() == ""))) {
139
                $dev->setModel(trim($buf));
140
            }
141
            $buf=$this->grabkey('machdep.cpu.features');
142
            if (!is_null($buf) && (trim($buf) != "")) {
143
                if (preg_match("/ VMX/", $buf)) {
144
                    $dev->setVirt("vmx");
145
                } elseif (preg_match("/ SVM/", $buf)) {
146
                    $dev->setVirt("svm");
147
                }
148
            }
149
        }
150
        $dev->setCpuSpeed(round($this->grabkey('hw.cpufrequency') / 1000000));
151
        $dev->setBusSpeed(round($this->grabkey('hw.busfrequency') / 1000000));
152
        $bufn=$this->grabkey('hw.cpufrequency_min');
153
        $bufx=$this->grabkey('hw.cpufrequency_max');
154
        if (!is_null($bufn) && (trim($bufn) != "") && !is_null($bufx) && (trim($bufx) != "") && ($bufn != $bufx)) {
155
            $dev->setCpuSpeedMin(round($bufn / 1000000));
156
            $dev->setCpuSpeedMax(round($bufx / 1000000));
157
        }
158
        $buf=$this->grabkey('hw.l2cachesize');
159
        if (!is_null($buf) && (trim($buf) != "")) {
160
            $dev->setCache(round($buf));
161
        }
162
        $ncpu = $this->grabkey('hw.ncpu');
163
        if (is_null($ncpu) || (trim($ncpu) == "") || (!($ncpu >= 1)))
164
            $ncpu = 1;
165
        for ($ncpu ; $ncpu > 0 ; $ncpu--) {
166
            $this->sys->setCpus($dev);
167
        }
168
    }
325 richard 169
 
2770 rexy 170
    /**
171
     * get the pci device information out of ioreg
172
     *
173
     * @return void
174
     */
175
    protected function pci()
176
    {
177
        if (!$arrResults = Parser::lspci(false)) { //no lspci port
178
            $s = $this->_grabioreg('IOPCIDevice');
179
            $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
180
            foreach ($lines as $line) {
181
                $dev = new HWDevice();
182
                if (!preg_match('/"IOName" = "([^"]*)"/', $line, $ar_buf)) {
183
                    $ar_buf = preg_split("/[\s@]+/", $line, 19);
184
                }
185
                if (preg_match('/"model" = <?"([^"]*)"/', $line, $ar_buf2)) {
186
                    $dev->setName(trim($ar_buf[1]). ": ".trim($ar_buf2[1]));
187
                } else {
188
                    $dev->setName(trim($ar_buf[1]));
189
                }
190
                $this->sys->setPciDevices($dev);
191
            }
192
        } else {
193
            foreach ($arrResults as $dev) {
194
                $this->sys->setPciDevices($dev);
195
            }
196
        }
197
    }
325 richard 198
 
2770 rexy 199
    /**
200
     * get the ide device information out of ioreg
201
     *
202
     * @return void
203
     */
204
    protected function ide()
205
    {
206
        $s = $this->_grabioreg('IOATABlockStorageDevice');
207
        $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
208
        foreach ($lines as $line) {
209
                    $dev = new HWDevice();
210
                    if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf))
211
                       $ar_buf = preg_split("/[\s@]+/", $line, 19);
212
                    $dev->setName(trim($ar_buf[1]));
213
                    $this->sys->setIdeDevices($dev);
214
        }
325 richard 215
 
2770 rexy 216
        $s = $this->_grabioreg('IOAHCIBlockStorageDevice');
217
        $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
218
        foreach ($lines as $line) {
219
                    $dev = new HWDevice();
220
                    if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf))
221
                       $ar_buf = preg_split("/[\s@]+/", $line, 19);
222
                    $dev->setName(trim($ar_buf[1]));
223
                    $this->sys->setIdeDevices($dev);
224
        }
225
    }
325 richard 226
 
2770 rexy 227
    /**
228
     * get the usb device information out of ioreg
229
     *
230
     * @return void
231
     */
232
    protected function usb()
233
    {
234
        $s = $this->_grabioreg('IOUSBDevice');
235
        $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
236
        foreach ($lines as $line) {
237
                    $dev = new HWDevice();
238
                    if (!preg_match('/"USB Product Name" = "([^"]*)"/', $line, $ar_buf))
239
                       $ar_buf = preg_split("/[\s@]+/", $line, 19);
240
                    $dev->setName(trim($ar_buf[1]));
241
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
242
                        if (preg_match('/"USB Vendor Name" = "([^"]*)"/', $line, $ar_buf)) {
243
                            $dev->setManufacturer(trim($ar_buf[1]));
244
                        }
245
                        if (preg_match('/"USB Product Name" = "([^"]*)"/', $line, $ar_buf)) {
246
                            $dev->setProduct(trim($ar_buf[1]));
247
                        }
248
                        if (defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL
249
                           && preg_match('/"USB Serial Number" = "([^"]*)"/', $line, $ar_buf)) {
250
                            $dev->setSerial(trim($ar_buf[1]));
251
                        }
252
                    }
253
                    $this->sys->setUsbDevices($dev);
254
        }
255
    }
325 richard 256
 
2770 rexy 257
    /**
258
     * get the scsi device information out of ioreg
259
     *
260
     * @return void
261
     */
262
    protected function scsi()
263
    {
264
        $s = $this->_grabioreg('IOBlockStorageServices');
265
        $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
266
        foreach ($lines as $line) {
267
                    $dev = new HWDevice();
268
                    if (!preg_match('/"Product Name"="([^"]*)"/', $line, $ar_buf))
269
                       $ar_buf = preg_split("/[\s@]+/", $line, 19);
270
                    $dev->setName(trim($ar_buf[1]));
271
                    $this->sys->setScsiDevices($dev);
272
        }
273
    }
325 richard 274
 
2770 rexy 275
    /**
276
     * get memory and swap information
277
     *
278
     * @return void
279
     */
280
    protected function memory()
281
    {
282
        $s = $this->grabkey('hw.memsize');
283
        if (CommonFunctions::executeProgram('vm_stat', '', $pstat, PSI_DEBUG)) {
284
            // calculate free memory from page sizes (each page = 4096)
285
            if (preg_match('/^Pages free:\s+(\S+)/m', $pstat, $free_buf)) {
286
                if (preg_match('/^Anonymous pages:\s+(\S+)/m', $pstat, $anon_buf)
287
                   && preg_match('/^Pages wired down:\s+(\S+)/m', $pstat, $wire_buf)
288
                   && preg_match('/^File-backed pages:\s+(\S+)/m', $pstat, $fileb_buf)) {
289
                        // OS X 10.9 or never
290
                        $this->sys->setMemFree($free_buf[1] * 4 * 1024);
291
                        $this->sys->setMemApplication(($anon_buf[1]+$wire_buf[1]) * 4 * 1024);
292
                        $this->sys->setMemCache($fileb_buf[1] * 4 * 1024);
293
                        if (preg_match('/^Pages occupied by compressor:\s+(\S+)/m', $pstat, $compr_buf)) {
294
                            $this->sys->setMemBuffer($compr_buf[1] * 4 * 1024);
295
                        }
296
                } else {
297
                    if (preg_match('/^Pages speculative:\s+(\S+)/m', $pstat, $spec_buf)) {
298
                        $this->sys->setMemFree(($free_buf[1]+$spec_buf[1]) * 4 * 1024);
299
                    } else {
300
                        $this->sys->setMemFree($free_buf[1] * 4 * 1024);
301
                    }
302
                    $appMemory = 0;
303
                    if (preg_match('/^Pages wired down:\s+(\S+)/m', $pstat, $wire_buf)) {
304
                        $appMemory += $wire_buf[1] * 4 * 1024;
305
                    }
306
                    if (preg_match('/^Pages active:\s+(\S+)/m', $pstat, $active_buf)) {
307
                        $appMemory += $active_buf[1] * 4 * 1024;
308
                    }
309
                    $this->sys->setMemApplication($appMemory);
325 richard 310
 
2770 rexy 311
                    if (preg_match('/^Pages inactive:\s+(\S+)/m', $pstat, $inactive_buf)) {
312
                        $this->sys->setMemCache($inactive_buf[1] * 4 * 1024);
313
                    }
314
                }
315
            } else {
316
                $lines = preg_split("/\n/", $pstat, -1, PREG_SPLIT_NO_EMPTY);
317
                $ar_buf = preg_split("/\s+/", $lines[1], 19);
318
                $this->sys->setMemFree($ar_buf[2] * 4 * 1024);
319
            }
325 richard 320
 
2770 rexy 321
            $this->sys->setMemTotal($s);
322
            $this->sys->setMemUsed($this->sys->getMemTotal() - $this->sys->getMemFree());
325 richard 323
 
2770 rexy 324
            if (CommonFunctions::executeProgram('sysctl', 'vm.swapusage | colrm 1 22', $swapBuff, PSI_DEBUG)) {
325
                $swap1 = preg_split('/M/', $swapBuff);
326
                $swap2 = preg_split('/=/', $swap1[1]);
327
                $swap3 = preg_split('/=/', $swap1[2]);
328
                $dev = new DiskDevice();
329
                $dev->setName('SWAP');
330
                $dev->setMountPoint('SWAP');
331
                $dev->setFsType('swap');
332
                $dev->setTotal($swap1[0] * 1024 * 1024);
333
                $dev->setUsed($swap2[1] * 1024 * 1024);
334
                $dev->setFree($swap3[1] * 1024 * 1024);
335
                $this->sys->setSwapDevices($dev);
336
            }
337
        }
338
    }
325 richard 339
 
2770 rexy 340
    /**
341
     * get the thunderbolt device information out of ioreg
342
     *
343
     * @return void
344
     */
345
    protected function _tb()
346
    {
347
        $s = $this->_grabioreg('IOThunderboltPort');
348
        $lines = preg_split("/\n/", $s, -1, PREG_SPLIT_NO_EMPTY);
349
        foreach ($lines as $line) {
350
                    $dev = new HWDevice();
351
                    if (!preg_match('/"Description" = "([^"]*)"/', $line, $ar_buf))
352
                       $ar_buf = preg_split("/[\s@]+/", $line, 19);
353
                    $dev->setName(trim($ar_buf[1]));
354
                    $this->sys->setTbDevices($dev);
355
        }
356
    }
325 richard 357
 
2770 rexy 358
    /**
359
     * get network information
360
     *
361
     * @return void
362
     */
363
    private function _network()
364
    {
365
        if (CommonFunctions::executeProgram('netstat', '-nbdi | cut -c1-24,42- | grep Link', $netstat, PSI_DEBUG)) {
366
            $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
367
            foreach ($lines as $line) {
368
                $ar_buf = preg_split("/\s+/", $line, 10);
369
                if (!empty($ar_buf[0])) {
370
                    $dev = new NetDevice();
371
                    $dev->setName($ar_buf[0]);
372
                    $dev->setTxBytes($ar_buf[8]);
373
                    $dev->setRxBytes($ar_buf[5]);
374
                    $dev->setErrors($ar_buf[4] + $ar_buf[7]);
375
                    if (isset($ar_buf[10])) {
376
                        $dev->setDrops($ar_buf[10]);
377
                    }
378
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
379
                        $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
380
                        foreach ($bufe2 as $buf2) {
381
                            if (preg_match('/^\s+ether\s+(\S+)/i', $buf2, $ar_buf2)) {
382
                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
383
                            } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2)) {
384
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
385
                            } elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
386
                                  || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
387
                                  && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1])) {
388
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
389
                            } elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
390
                                if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
391
                                    $unit = "G";
392
                                } else {
393
                                    $unit = "M";
394
                                }
395
                                if (preg_match('/[<\s]([^\s<]+)-duplex/i', $buf2, $ar_buf3))
396
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]));
397
                                else
398
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s');
399
                            }
400
                        }
401
                    }
402
                    $this->sys->setNetDevices($dev);
403
                }
404
            }
405
        }
406
    }
325 richard 407
 
2770 rexy 408
    /**
409
     * get icon name
410
     *
411
     * @return void
412
     */
413
    protected function distro()
414
    {
415
        $this->sys->setDistributionIcon('Darwin.png');
416
        if (!CommonFunctions::executeProgram('system_profiler', 'SPSoftwareDataType', $buffer, PSI_DEBUG)) {
417
            parent::distro();
418
        } else {
419
            $arrBuff = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
420
            foreach ($arrBuff as $line) {
421
                $arrLine = preg_split("/:/", $line, -1, PREG_SPLIT_NO_EMPTY);
422
                if (trim($arrLine[0]) === "System Version") {
423
                    $distro = trim($arrLine[1]);
325 richard 424
 
2770 rexy 425
                    if (preg_match('/^Mac OS|^OS X|^macOS/', $distro)) {
426
                        $this->sys->setDistributionIcon('Apple.png');
427
                        if (preg_match('/(^Mac OS X Server|^Mac OS X|^OS X Server|^OS X|^macOS Server|^macOS) (\d+\.\d+)/', $distro, $ver)
428
                            && ($list = @parse_ini_file(PSI_APP_ROOT."/data/osnames.ini", true))
429
                            && isset($list['OS X'][$ver[2]])) {
430
                            $distro.=' '.$list['OS X'][$ver[2]];
431
                        }
432
                    }
325 richard 433
 
2770 rexy 434
                    $this->sys->setDistribution($distro);
325 richard 435
 
2770 rexy 436
                    return;
437
                }
438
            }
439
        }
440
    }
325 richard 441
 
2770 rexy 442
    /**
443
     * Processes
444
     *
445
     * @return void
446
     */
447
    protected function _processes()
448
    {
449
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
450
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
451
            $processes['*'] = 0;
452
            foreach ($lines as $line) {
453
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
454
                    $processes['*']++;
455
                    $state = $ar_buf[1];
456
                    if ($state == 'U') $state = 'D'; //linux format
457
                    elseif ($state == 'I') $state = 'S';
458
                    elseif ($state == 'D') $state = 'd'; //invalid
459
                    if (isset($processes[$state])) {
460
                        $processes[$state]++;
461
                    } else {
462
                        $processes[$state] = 1;
463
                    }
464
                }
465
            }
466
            if ($processes['*'] > 0) {
467
                $this->sys->setProcesses($processes);
468
            }
469
        }
470
    }
325 richard 471
 
2770 rexy 472
    /**
473
     * get the information
474
     *
475
     * @see PSI_Interface_OS::build()
476
     *
477
     * @return Void
478
     */
479
    public function build()
480
    {
481
        parent::build();
482
        if (!$this->blockname || $this->blockname==='vitals') {
483
            $this->_uptime();
484
            $this->_processes();
485
        }
486
        if (!$this->blockname || $this->blockname==='hardware') {
487
            $this->_tb();
488
        }
489
        if (!$this->blockname || $this->blockname==='network') {
490
            $this->_network();
491
        }
325 richard 492
    }
2770 rexy 493
}