Subversion Repositories ALCASAR

Rev

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

Rev Author Line No. Line
2770 rexy 1
<?php
2
/**
3
 * Minix System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI Minix 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.Minix.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Minix sysinfo class
17
 * get all the required information from Minix system
18
 *
19
 * @category  PHP
20
 * @package   PSI Minix 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 Minix extends OS
28
{
29
    /**
3037 rexy 30
     * uptime command result.
31
     */
32
    private $_uptime = null;
33
 
34
    /**
2770 rexy 35
     * content of the syslog
36
     *
37
     * @var array
38
     */
39
    private $_dmesg = null;
40
 
41
    /**
42
     * read /var/log/messages, but only if we haven't already
43
     *
44
     * @return array
45
     */
46
    protected function readdmesg()
47
    {
48
        if ($this->_dmesg === null) {
49
            if (CommonFunctions::rfts('/var/log/messages', $buf)) {
50
                    $blocks = preg_replace("/\s(kernel: MINIX \d+\.\d+\.\d+\.)/", '<BLOCK>$1', $buf);
51
                    $parts = preg_split("/<BLOCK>/", $blocks, -1, PREG_SPLIT_NO_EMPTY);
52
                    $this->_dmesg = preg_split("/\n/", $parts[count($parts) - 1], -1, PREG_SPLIT_NO_EMPTY);
53
            } else {
54
                $this->_dmesg = array();
55
            }
56
        }
57
 
58
        return $this->_dmesg;
59
    }
60
 
61
    /**
62
     * get the cpu information
63
     *
64
     * @return void
65
     */
66
    protected function _cpuinfo()
67
    {
68
        if (CommonFunctions::rfts('/proc/cpuinfo', $bufr, 0, 4096, false)) {
69
            $processors = preg_split('/\s?\n\s?\n/', trim($bufr));
70
            foreach ($processors as $processor) {
71
                $_n = ""; $_f = ""; $_m = ""; $_s = "";
72
                $dev = new CpuDevice();
73
                $details = preg_split("/\n/", $processor, -1, PREG_SPLIT_NO_EMPTY);
74
                foreach ($details as $detail) {
3287 rexy 75
                    if (preg_match('/^([^:]+):(.+)$/', trim($detail) , $arrBuff) && (($arrBuff2 = trim($arrBuff[2])) !== '')) {
76
                        switch (strtolower(trim($arrBuff[1]))) {
2770 rexy 77
                        case 'model name':
3287 rexy 78
                            $_n = $arrBuff2;
2770 rexy 79
                            break;
80
                        case 'cpu mhz':
3287 rexy 81
                            $dev->setCpuSpeed($arrBuff2);
2770 rexy 82
                            break;
83
                        case 'cpu family':
3287 rexy 84
                            $_f = $arrBuff2;
2770 rexy 85
                            break;
86
                        case 'model':
3287 rexy 87
                            $_m = $arrBuff2;
2770 rexy 88
                            break;
89
                        case 'stepping':
3287 rexy 90
                            $_s = $arrBuff2;
2770 rexy 91
                            break;
92
                        case 'flags':
3287 rexy 93
                            if (preg_match("/ vmx/", $arrBuff2)) {
2770 rexy 94
                                $dev->setVirt("vmx");
3287 rexy 95
                            } elseif (preg_match("/ svm/", $arrBuff2)) {
2770 rexy 96
                                $dev->setVirt("svm");
97
                            }
98
                            break;
99
                        case 'vendor_id':
3287 rexy 100
                            $dev->setVendorId($arrBuff2);
2770 rexy 101
                        }
102
                    }
103
                }
104
                if ($_n == "") $_n="CPU";
105
                if ($_f != "") $_n.=" Family ".$_f;
106
                if ($_m != "") $_n.=" Model ".$_m;
107
                if ($_s != "") $_n.=" Stepping ".$_s;
108
                $dev->SetModel($_n);
109
                $this->sys->setCpus($dev);
110
            }
111
        } else
112
        foreach ($this->readdmesg() as $line) {
113
            if (preg_match('/kernel: (CPU .*) freq (.*) MHz/', $line, $ar_buf)) {
114
                $dev = new CpuDevice();
115
                $dev->setModel($ar_buf[1]);
116
                $dev->setCpuSpeed($ar_buf[2]);
117
                $this->sys->setCpus($dev);
118
            }
119
        }
120
    }
121
 
122
    /**
123
     * PCI devices
124
     * get the pci device information out of dmesg
125
     *
126
     * @return void
127
     */
128
    protected function _pci()
129
    {
130
        if (CommonFunctions::rfts('/proc/pci', $strBuf, 0, 4096, false)) {
131
            $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
132
            $arrResults = array();
133
            foreach ($arrLines as $strLine) {
134
               $arrParams = preg_split('/\s+/', trim($strLine), 4);
135
               if (count($arrParams) == 4)
136
                  $strName = $arrParams[3];
137
               else
138
                  $strName = "unknown";
139
               $strName = preg_replace('/\(.*\)/', '', $strName);
140
               $dev = new HWDevice();
141
               $dev->setName($strName);
142
               $arrResults[] = $dev;
143
            }
144
            foreach ($arrResults as $dev) {
145
                $this->sys->setPciDevices($dev);
146
            }
147
        }
148
        if (!(isset($arrResults) && is_array($arrResults)) && ($results = Parser::lspci())) {
149
            /* if access error: chmod 4755 /usr/bin/lspci */
150
            foreach ($results as $dev) {
151
                $this->sys->setPciDevices($dev);
152
            }
153
        }
154
    }
155
 
156
    /**
157
     * Minix Version
158
     *
159
     * @return void
160
     */
161
    private function _kernel()
162
    {
163
        if (CommonFunctions::executeProgram('uname', '-rvm', $ret)) {
164
            foreach ($this->readdmesg() as $line) {
165
                if (preg_match('/kernel: MINIX (\d+\.\d+\.\d+)\. \((.+)\)/', $line, $ar_buf)) {
166
                    $branch = $ar_buf[2];
167
                    break;
168
                }
169
            }
170
            if (isset($branch))
171
               $this->sys->setKernel($ret.' ('.$branch.')');
172
            else
173
               $this->sys->setKernel($ret);
174
        }
175
    }
176
 
177
    /**
178
     * Distribution
179
     *
180
     * @return void
181
     */
182
    protected function _distro()
183
    {
184
        if (CommonFunctions::executeProgram('uname', '-sr', $ret))
185
            $this->sys->setDistribution($ret);
186
        else
187
            $this->sys->setDistribution('Minix');
188
 
189
        $this->sys->setDistributionIcon('Minix.png');
190
    }
191
 
192
    /**
193
     * UpTime
194
     * time the system is running
195
     *
196
     * @return void
197
     */
198
    private function _uptime()
199
    {
3037 rexy 200
        if (($this->_uptime !== null) || CommonFunctions::executeProgram('uptime', '', $this->_uptime)) {
201
            if (preg_match("/up (\d+) day[s]?,\s*(\d+):(\d+),/", $this->_uptime, $ar_buf)) {
2770 rexy 202
                $min = $ar_buf[3];
203
                $hours = $ar_buf[2];
204
                $days = $ar_buf[1];
205
                $this->sys->setUptime($days * 86400 + $hours * 3600 + $min * 60);
3037 rexy 206
            } elseif (preg_match("/up (\d+):(\d+),/", $this->_uptime, $ar_buf)) {
2770 rexy 207
                $min = $ar_buf[2];
208
                $hours = $ar_buf[1];
209
                $this->sys->setUptime($hours * 3600 + $min * 60);
210
            }
211
        }
212
    }
213
 
214
    /**
215
     * Processor Load
216
     * optionally create a loadbar
217
     *
218
     * @return void
219
     */
220
    private function _loadavg()
221
    {
3037 rexy 222
        if (($this->_uptime !== null) || CommonFunctions::executeProgram('uptime', '', $this->_uptime)) {
223
            if (preg_match("/load averages: (.*), (.*), (.*)$/", $this->_uptime, $ar_buf)) {
2770 rexy 224
                $this->sys->setLoad($ar_buf[1].' '.$ar_buf[2].' '.$ar_buf[3]);
225
            }
226
        }
227
    }
228
 
229
    /**
230
     * Virtual Host Name
231
     *
232
     * @return void
233
     */
234
    private function _hostname()
235
    {
3037 rexy 236
        if (PSI_USE_VHOST) {
2770 rexy 237
            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
238
        } else {
239
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
240
                $ip = gethostbyname($result);
241
                if ($ip != $result) {
242
                    $this->sys->setHostname(gethostbyaddr($ip));
243
                }
244
            }
245
        }
246
    }
247
 
248
 
249
    /**
250
     *  Physical memory information and Swap Space information
251
     *
252
     *  @return void
253
     */
254
    private function _memory()
255
    {
256
        if (CommonFunctions::rfts('/proc/meminfo', $bufr, 1, 4096, false)) {
257
            $ar_buf = preg_split('/\s+/', trim($bufr));
258
            if (count($ar_buf) >= 5) {
259
                    $this->sys->setMemTotal($ar_buf[0]*$ar_buf[1]);
260
                    $this->sys->setMemFree($ar_buf[0]*$ar_buf[2]);
261
                    $this->sys->setMemCache($ar_buf[0]*$ar_buf[4]);
262
                    $this->sys->setMemUsed($ar_buf[0]*($ar_buf[1]-$ar_buf[2]));
263
            }
264
        }
265
    }
266
 
267
    /**
268
     * filesystem information
269
     *
270
     * @return void
271
     */
272
    private function _filesystems()
273
    {
274
        $arrResult = Parser::df("-P 2>/dev/null");
275
        foreach ($arrResult as $dev) {
276
            $this->sys->setDiskDevices($dev);
277
        }
278
    }
279
 
280
    /**
281
     * network information
282
     *
283
     * @return void
284
     */
285
    private function _network()
286
    {
287
        if (CommonFunctions::executeProgram('ifconfig', '-a', $bufr, PSI_DEBUG)) {
288
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
289
            foreach ($lines as $line) {
290
                if (preg_match("/^([^\s:]+):\saddress\s(\S+)\snetmask/", $line, $ar_buf)) {
291
                    $dev = new NetDevice();
292
                    $dev->setName($ar_buf[1]);
293
                    if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
294
                            $dev->setInfo($ar_buf[2]);
295
                    }
296
                    $this->sys->setNetDevices($dev);
297
                }
298
            }
299
        }
300
    }
301
 
302
    /**
303
     * Processes
304
     *
305
     * @return void
306
     */
307
    protected function _processes()
308
    {
309
        if (CommonFunctions::executeProgram('ps', 'alx', $bufr, PSI_DEBUG)) {
310
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
311
            $processes['*'] = 0;
312
            foreach ($lines as $line) {
313
                if (preg_match("/^\s(\w)\s/", $line, $ar_buf)) {
314
                    $processes['*']++;
315
                    $state = $ar_buf[1];
316
                    if ($state == 'W') $state = 'D'; //linux format
317
                    elseif ($state == 'D') $state = 'd'; //invalid
318
                    if (isset($processes[$state])) {
319
                        $processes[$state]++;
320
                    } else {
321
                        $processes[$state] = 1;
322
                    }
323
                }
324
            }
325
            if ($processes['*'] > 0) {
326
                $this->sys->setProcesses($processes);
327
            }
328
        }
329
    }
330
 
331
    /**
332
     * get the information
333
     *
3037 rexy 334
     * @return void
2770 rexy 335
     */
336
    public function build()
337
    {
3037 rexy 338
        $this->error->addWarning("The Minix version of phpSysInfo is a work in progress, some things currently don't work");
2770 rexy 339
        if (!$this->blockname || $this->blockname==='vitals') {
340
            $this->_distro();
341
            $this->_hostname();
342
            $this->_kernel();
343
            $this->_uptime();
344
            $this->_users();
345
            $this->_loadavg();
346
            $this->_processes();
347
        }
348
        if (!$this->blockname || $this->blockname==='hardware') {
349
            $this->_pci();
350
            $this->_cpuinfo();
351
        }
352
        if (!$this->blockname || $this->blockname==='memory') {
353
            $this->_memory();
354
        }
355
        if (!$this->blockname || $this->blockname==='filesystem') {
356
            $this->_filesystems();
357
        }
3037 rexy 358
        if (!$this->blockname || $this->blockname==='network') {
359
            $this->_network();
360
        }
2770 rexy 361
    }
362
}