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
 * Basic OS Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI OS class
9
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
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
12
 * @version   SVN: $Id: class.OS.inc.php 699 2012-09-15 11:57:13Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Basic OS functions for all OS classes
17
 *
18
 * @category  PHP
19
 * @package   PSI OS class
20
 * @author    Michael Cramer <BigMichi1@users.sourceforge.net>
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
abstract class OS implements PSI_Interface_OS
27
{
28
    /**
29
     * object for error handling
30
     *
31
     * @var PSI_Error
32
     */
33
    protected $error;
34
 
35
    /**
36
     * block name
37
     *
38
     * @var string
39
     */
40
    protected $blockname = false;
41
 
42
    /**
43
     * @var System
44
     */
45
    protected $sys;
46
 
47
    /**
48
     * build the global Error object
49
     */
50
    public function __construct($blockname = false)
51
    {
52
        $this->error = PSI_Error::singleton();
53
        $this->sys = new System();
54
        $this->blockname = $blockname;
3100 rexy 55
        $this->sys->setOS(get_class($this));
2770 rexy 56
    }
57
 
58
    /**
59
     * get os specific encoding
60
     *
61
     * @see PSI_Interface_OS::getEncoding()
62
     *
63
     * @return string
64
     */
65
    public function getEncoding()
66
    {
67
        return PSI_SYSTEM_CODEPAGE;
68
    }
69
 
70
    /**
71
     * get os specific language
72
     *
73
     * @see PSI_Interface_OS::getLanguage()
74
     *
75
     * @return string
76
     */
77
    public function getLanguage()
78
    {
79
        return PSI_SYSTEM_LANG;
80
    }
81
 
82
    /**
83
     * get block name
84
     *
85
     * @see PSI_Interface_OS::getBlockName()
86
     *
87
     * @return string
88
     */
89
    public function getBlockName()
90
    {
91
        return $this->blockname;
92
    }
93
 
94
    /**
95
     * Number of Users
96
     *
97
     * @return void
98
     */
99
    protected function _users()
100
    {
101
        if (CommonFunctions::executeProgram('who', '', $strBuf, PSI_DEBUG)) {
102
            if (strlen($strBuf) > 0) {
103
                $lines = preg_split('/\n/', $strBuf);
104
                $this->sys->setUsers(count($lines));
105
            }
106
        } elseif (CommonFunctions::executeProgram('uptime', '', $buf, PSI_DEBUG) && preg_match("/,\s+(\d+)\s+user[s]?,/", $buf, $ar_buf)) {
107
        //} elseif (CommonFunctions::executeProgram('uptime', '', $buf) && preg_match("/,\s+(\d+)\s+user[s]?,\s+load average[s]?:\s+(.*),\s+(.*),\s+(.*)$/", $buf, $ar_buf)) {
108
            $this->sys->setUsers($ar_buf[1]);
109
        } else {
3037 rexy 110
            $processlist = CommonFunctions::findglob('/proc/*/cmdline', GLOB_NOSORT);
2770 rexy 111
            if (is_array($processlist) && (($total = count($processlist)) > 0)) {
112
                $count = 0;
113
                $buf = "";
114
                for ($i = 0; $i < $total; $i++) {
115
                    if (CommonFunctions::rfts($processlist[$i], $buf, 0, 4096, false)) {
116
                        $name = str_replace(chr(0), ' ', trim($buf));
117
                        if (preg_match("/^-/", $name)) {
118
                            $count++;
119
                        }
120
                    }
121
                }
122
                if ($count > 0) {
123
                    $this->sys->setUsers($count);
124
                }
125
            }
126
        }
127
    }
128
 
129
    /**
130
     * IP of the Host
131
     *
132
     * @return void
133
     */
134
    protected function _ip()
135
    {
3037 rexy 136
        if (PSI_USE_VHOST && !defined('PSI_EMU_HOSTNAME')) {
2770 rexy 137
           if ((CommonFunctions::readenv('SERVER_ADDR', $result) || CommonFunctions::readenv('LOCAL_ADDR', $result)) //is server address defined
2976 rexy 138
              && !strstr($result, '.') && strstr($result, ':')) { //is IPv6, quick version of preg_match('/\(([[0-9A-Fa-f\:]+)\)/', $result)
2770 rexy 139
                $dnsrec = dns_get_record($this->sys->getHostname(), DNS_AAAA);
140
                if (isset($dnsrec[0]['ipv6'])) { //is DNS IPv6 record
141
                    $this->sys->setIp($dnsrec[0]['ipv6']); //from DNS (avoid IPv6 NAT translation)
142
                } else {
143
                    $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result)); //from SERVER_ADDR or LOCAL_ADDR
144
                }
145
            } else {
146
                $this->sys->setIp(gethostbyname($this->sys->getHostname())); //IPv4 only
147
            }
2976 rexy 148
        } elseif (((PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME')) && (CommonFunctions::readenv('SERVER_ADDR', $result) || CommonFunctions::readenv('LOCAL_ADDR', $result))) {
149
            $this->sys->setIp(preg_replace('/^::ffff:/i', '', $result));
2770 rexy 150
        } else {
2976 rexy 151
            //$this->sys->setIp(gethostbyname($this->sys->getHostname()));
152
            $hn = $this->sys->getHostname();
153
            $ghbn = gethostbyname($hn);
154
            if (defined('PSI_EMU_HOSTNAME') && ($hn === $ghbn)) {
155
                $this->sys->setIp(PSI_EMU_HOSTNAME);
2770 rexy 156
            } else {
2976 rexy 157
                $this->sys->setIp($ghbn);
2770 rexy 158
            }
159
        }
160
    }
161
 
162
    /**
2976 rexy 163
     * MEM information from dmidecode
164
     *
165
     * @return void
166
     */
167
    protected function _dmimeminfo()
168
    {
3037 rexy 169
        $dmimd = CommonFunctions::readdmimemdata();
170
        if (!empty($dmimd)) {
171
            foreach ($dmimd as $mem) {
2976 rexy 172
                if (isset($mem['Size']) && preg_match('/^(\d+)\s(M|G)B$/', $mem['Size'], $size) && ($size[1] > 0)) {
173
                    $dev = new HWDevice();
174
                    $name = '';
175
                    if (isset($mem['Part Number']) && !preg_match("/^PartNum\d+$/", $part = $mem['Part Number']) && ($part != 'None') && ($part != 'N/A') && ($part != 'Not Specified') && ($part != 'NOT AVAILABLE')) {
176
                        $name = $part;
177
                    }
178
                    if (isset($mem['Locator']) && (($dloc = $mem['Locator']) != 'None') && ($dloc != 'N/A') && ($dloc != 'Not Specified')) {
179
                        if ($name != '') {
180
                            $name .= ' - '.$dloc;
181
                        } else {
182
                            $name = $dloc;
183
                        }
184
                    }
185
                    if (isset($mem['Bank Locator']) && (($bank = $mem['Bank Locator']) != 'None') && ($bank != 'N/A') && ($bank != 'Not Specified')) {
186
                        if ($name != '') {
187
                            $name .= ' in '.$bank;
188
                        } else {
189
                            $name = 'Physical Memory in '.$bank;
190
                        }
191
                    }
192
                    if ($name != '') {
193
                        $dev->setName(trim($name));
194
                    } else {
195
                        $dev->setName('Physical Memory');
196
                    }
197
                    if (defined('PSI_SHOW_DEVICES_INFOS') && PSI_SHOW_DEVICES_INFOS) {
198
                        if (isset($mem['Manufacturer']) && !preg_match("/^([A-F\d]{4}|[A-F\d]{12}|[A-F\d]{16})$/", $manufacturer = $mem['Manufacturer']) && !preg_match("/^Manufacturer\d+$/", $manufacturer) && !preg_match("/^Mfg \d+$/", $manufacturer) && !preg_match("/^JEDEC ID:/", $manufacturer) && ($manufacturer != 'None') && ($manufacturer != 'N/A') && ($manufacturer != 'Not Specified') && ($manufacturer != 'UNKNOWN')) {
199
                            $dev->setManufacturer($manufacturer);
200
                        }
201
                        if ($size[2] == 'G') {
202
                            $dev->setCapacity($size[1]*1024*1024*1024);
203
                        } else {
204
                            $dev->setCapacity($size[1]*1024*1024);
205
                        }
206
                        $memtype = '';
207
                        if (isset($mem['Type']) && (($type = $mem['Type']) != 'None') && ($type != 'N/A') && ($type != 'Not Specified') && ($type != 'Other') && ($type != 'Unknown') && ($type != '<OUT OF SPEC>')) {
208
                            if (isset($mem['Speed']) && preg_match('/^(\d+)\s(MHz|MT\/s)/', $mem['Speed'], $speed) && ($speed[1] > 0) && (preg_match('/^(DDR\d*)(.*)/', $type, $dr) || preg_match('/^(SDR)AM(.*)/', $type, $dr))) {
3037 rexy 209
                               if (isset($mem['Minimum Voltage']) && isset($mem['Maximum Voltage']) &&
2976 rexy 210
                                  preg_match('/^([\d\.]+)\sV$/', $mem['Minimum Voltage'], $minv) && preg_match('/^([\d\.]+)\sV$/', $mem['Maximum Voltage'], $maxv) &&
211
                                  ($minv[1]  > 0) && ($maxv[1] >0) && ($minv[1] < $maxv[1])) {
212
                                    $lv = 'L';
213
                                } else {
214
                                    $lv = '';
215
                                }
216
                                if (isset($dr[2])) {
217
                                    $memtype = $dr[1].$lv.'-'.$speed[1].' '.$dr[2];
218
                                } else {
219
                                    $memtype = $dr[1].$lv.'-'.$speed[1];
220
                                }
221
                            } else {
222
                                $memtype = $type;
223
                            }
224
                        }
225
                        if (isset($mem['Form Factor']) && (($form = $mem['Form Factor']) != 'None') && ($form != 'N/A') && ($form != 'Not Specified') && ($form != 'Other') && ($form != 'Unknown') && !preg_match('/ '.$form.'$/', $memtype)) {
226
                            $memtype .= ' '.$form;
227
                        }
228
                        if (isset($mem['Data Width']) && isset($mem['Total Width']) &&
229
                           preg_match('/^(\d+)\sbits$/', $mem['Data Width'], $dataw) && preg_match('/^(\d+)\sbits$/', $mem['Total Width'], $totalw) &&
230
                           ($dataw[1]  > 0) && ($totalw[1] >0) && ($dataw[1] < $totalw[1])) {
231
                            $memtype .= ' ECC';
232
                        }
233
                        if (isset($mem['Type Detail']) && preg_match('/Registered/', $mem['Type Detail'])) {
234
                            $memtype .= ' REG';
235
                        }
236
                        if (($memtype = trim($memtype)) != '') {
237
                            $dev->setProduct($memtype);
238
                        }
239
                        if (isset($mem['Configured Clock Speed']) && preg_match('/^(\d+)\s(MHz|MT\/s)$/', $mem['Configured Clock Speed'], $clock) && ($clock[1] > 0)) {
240
                            $dev->setSpeed($clock[1]);
241
                        }
242
                        if (isset($mem['Configured Voltage']) && preg_match('/^([\d\.]+)\sV$/', $mem['Configured Voltage'], $voltage) && ($voltage[1] > 0)) {
243
                            $dev->setVoltage($voltage[1]);
244
                        }
245
                        if (defined('PSI_SHOW_DEVICES_SERIAL') && PSI_SHOW_DEVICES_SERIAL &&
246
                           isset($mem['Serial Number']) && !preg_match("/^SerNum\d+$/", $serial = $mem['Serial Number']) && ($serial != 'None') && ($serial != 'Not Specified')) {
247
                            $dev->setSerial($serial);
248
                        }
249
                    }
250
                    $this->sys->setMemDevices($dev);
251
                }
252
            }
253
        }
254
    }
255
 
256
    /**
2770 rexy 257
     * get the filled or unfilled (with default values) System object
258
     *
259
     * @see PSI_Interface_OS::getSys()
260
     *
261
     * @return System
262
     */
263
    final public function getSys()
264
    {
265
        $this->build();
266
        if (!$this->blockname || $this->blockname==='vitals') {
267
            $this->_ip();
268
        }
2976 rexy 269
        if ((!$this->blockname || $this->blockname==='hardware') && (PSI_OS != 'WINNT') && !defined('PSI_EMU_HOSTNAME')) {
270
            $this->_dmimeminfo();
271
        }
2770 rexy 272
 
273
        return $this->sys;
274
    }
275
}