Subversion Repositories ALCASAR

Rev

Rev 2770 | Rev 3037 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log

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