Subversion Repositories ALCASAR

Rev

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