1<?php
2/**
3 * QNX System Class
4 *
5 * PHP version 5
6 *
7 * @category  PHP
8 * @package   PSI QNX 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.QNX.inc.php 687 2012-09-06 20:54:49Z namiltd $
13 * @link      http://phpsysinfo.sourceforge.net
14 */
15 /**
16 * QNX sysinfo class
17 * get all the required information from QNX system
18 *
19 * @category  PHP
20 * @package   PSI QNX 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 */
27class QNX extends OS
28{
29    /**
30     * get the cpu information
31     *
32     * @return void
33     */
34    protected function _cpuinfo()
35    {
36        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
37           && preg_match('/^Processor\d+: (.*)/m', $buf)) {
38            $lines = preg_split("/\n/", $buf, -1, PREG_SPLIT_NO_EMPTY);
39            foreach ($lines as $line) {
40                if (preg_match('/^Processor\d+: (.+)/', $line, $proc)) {
41                    $dev = new CpuDevice();
42                    $dev->SetModel(trim($proc[1]));
43                    if (preg_match('/(\d+)MHz/', $proc[1], $mhz)) {
44                        $dev->setCpuSpeed($mhz[1]);
45                    }
46                    $this->sys->setCpus($dev);
47                }
48            }
49        }
50    }
51
52    /**
53     * QNX Version
54     *
55     * @return void
56     */
57    private function _kernel()
58    {
59        if (CommonFunctions::executeProgram('uname', '-rvm', $ret)) {
60            $this->sys->setKernel($ret);
61        }
62    }
63
64    /**
65     * Distribution
66     *
67     * @return void
68     */
69    protected function _distro()
70    {
71        if (CommonFunctions::executeProgram('uname', '-sr', $ret))
72            $this->sys->setDistribution($ret);
73        else
74            $this->sys->setDistribution('QNX');
75
76        $this->sys->setDistributionIcon('QNX.png');
77    }
78
79    /**
80     * UpTime
81     * time the system is running
82     *
83     * @return void
84     */
85    private function _uptime()
86    {
87
88        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
89           && preg_match('/^.* BootTime:(.*)/', $buf, $bstart)
90           && CommonFunctions::executeProgram('date', '', $bstop)) {
91            /* default error handler */
92            if (function_exists('errorHandlerPsi')) {
93                restore_error_handler();
94            }
95            /* fatal errors only */
96            $old_err_rep = error_reporting();
97            error_reporting(E_ERROR);
98
99            $uptime = strtotime($bstop)-strtotime($bstart[1]);
100            if ($uptime > 0) $this->sys->setUptime($uptime);
101
102            /* restore error level */
103            error_reporting($old_err_rep);
104            /* restore error handler */
105            if (function_exists('errorHandlerPsi')) {
106                set_error_handler('errorHandlerPsi');
107            }
108        }
109    }
110
111    /**
112     * Number of Users
113     *
114     * @return void
115     */
116    protected function _users()
117    {
118        $this->sys->setUsers(1);
119    }
120
121    /**
122     * Virtual Host Name
123     *
124     * @return void
125     */
126    private function _hostname()
127    {
128        if (PSI_USE_VHOST === true) {
129            if (CommonFunctions::readenv('SERVER_NAME', $hnm)) $this->sys->setHostname($hnm);
130        } else {
131            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
132                $ip = gethostbyname($result);
133                if ($ip != $result) {
134                    $this->sys->setHostname(gethostbyaddr($ip));
135                }
136            }
137        }
138    }
139
140    /**
141     *  Physical memory information and Swap Space information
142     *
143     *  @return void
144     */
145    private function _memory()
146    {
147        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
148           && preg_match('/^.* FreeMem:(\S+)Mb\/(\S+)Mb/', $buf, $memm)) {
149            $this->sys->setMemTotal(1024*1024*$memm[2]);
150            $this->sys->setMemFree(1024*1024*$memm[1]);
151            $this->sys->setMemUsed(1024*1024*($memm[2]-$memm[1]));
152        }
153    }
154
155    /**
156     * filesystem information
157     *
158     * @return void
159     */
160    private function _filesystems()
161    {
162        $arrResult = Parser::df("-P 2>/dev/null");
163        foreach ($arrResult as $dev) {
164            $this->sys->setDiskDevices($dev);
165        }
166    }
167
168    /**
169     * network information
170     *
171     * @return void
172     */
173    private function _network()
174    {
175        if (CommonFunctions::executeProgram('ifconfig', '', $bufr, PSI_DEBUG)) {
176            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
177            $was = false;
178            $dev = null;
179            foreach ($lines as $line) {
180                if (preg_match("/^([^\s:]+)/", $line, $ar_buf)) {
181                    if ($was) {
182                        $this->sys->setNetDevices($dev);
183                    }
184                    $dev = new NetDevice();
185                    $dev->setName($ar_buf[1]);
186                    $was = true;
187                } else {
188                    if ($was) {
189                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
190                            if (preg_match('/^\s+address:\s*(\S+)/i', $line, $ar_buf2)) {
191                                if (!defined('PSI_HIDE_NETWORK_MACADDR') || !PSI_HIDE_NETWORK_MACADDR) $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
192                            } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $line, $ar_buf2))
193                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
194
195                        }
196                    }
197                }
198            }
199            if ($was) {
200                $this->sys->setNetDevices($dev);
201            }
202        }
203    }
204
205    /**
206     * get the information
207     *
208     * @return Void
209     */
210    public function build()
211    {
212        $this->error->addError("WARN", "The QNX version of phpSysInfo is a work in progress, some things currently don't work");
213        if (!$this->blockname || $this->blockname==='vitals') {
214            $this->_distro();
215            $this->_hostname();
216            $this->_kernel();
217            $this->_uptime();
218            $this->_users();
219        }
220        if (!$this->blockname || $this->blockname==='hardware') {
221            $this->_cpuinfo();
222        }
223        if (!$this->blockname || $this->blockname==='network') {
224            $this->_network();
225        }
226        if (!$this->blockname || $this->blockname==='memory') {
227            $this->_memory();
228        }
229        if (!$this->blockname || $this->blockname==='filesystem') {
230            $this->_filesystems();
231        }
232    }
233}
234