Subversion Repositories ALCASAR

Rev

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