2770 |
rexy |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* parser Class
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PSI
|
|
|
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.Parser.inc.php 604 2012-07-10 07:31:34Z namiltd $
|
|
|
13 |
* @link http://phpsysinfo.sourceforge.net
|
|
|
14 |
*/
|
|
|
15 |
/**
|
|
|
16 |
* parser class with common used parsing metods
|
|
|
17 |
*
|
|
|
18 |
* @category PHP
|
|
|
19 |
* @package PSI
|
|
|
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 |
class Parser
|
|
|
27 |
{
|
|
|
28 |
/**
|
|
|
29 |
* parsing the output of lspci command
|
|
|
30 |
*
|
|
|
31 |
* @param bool $debug
|
|
|
32 |
* @return array
|
|
|
33 |
*/
|
|
|
34 |
public static function lspci($debug = PSI_DEBUG)
|
|
|
35 |
{
|
|
|
36 |
$arrResults = array();
|
|
|
37 |
if (CommonFunctions::executeProgram("lspci", "", $strBuf, $debug)) {
|
|
|
38 |
$arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
39 |
foreach ($arrLines as $strLine) {
|
|
|
40 |
$arrParams = preg_split('/ /', trim($strLine), 2);
|
|
|
41 |
if (count($arrParams) == 2)
|
|
|
42 |
$strName = $arrParams[1];
|
|
|
43 |
else
|
|
|
44 |
$strName = "unknown";
|
|
|
45 |
$strName = preg_replace('/\(.*\)/', '', $strName);
|
|
|
46 |
$dev = new HWDevice();
|
|
|
47 |
$dev->setName($strName);
|
|
|
48 |
$arrResults[] = $dev;
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
return $arrResults;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* parsing the output of df command
|
|
|
57 |
*
|
|
|
58 |
* @param string $df_param additional parameter for df command
|
|
|
59 |
* @param bool $get_inodes
|
|
|
60 |
*
|
|
|
61 |
* @return array
|
|
|
62 |
*/
|
|
|
63 |
public static function df($df_param = "", $get_inodes = true)
|
|
|
64 |
{
|
|
|
65 |
$arrResult = array();
|
|
|
66 |
if (CommonFunctions::executeProgram('mount', '', $mount, PSI_DEBUG)) {
|
|
|
67 |
$mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
68 |
foreach ($mount as $mount_line) {
|
|
|
69 |
if (preg_match("/(\S+) on ([\S ]+) type (.*) \((.*)\)/", $mount_line, $mount_buf)) {
|
|
|
70 |
$parm = array();
|
|
|
71 |
$parm['mountpoint'] = trim($mount_buf[2]);
|
|
|
72 |
$parm['fstype'] = $mount_buf[3];
|
|
|
73 |
$parm['name'] = $mount_buf[1];
|
|
|
74 |
if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
|
|
|
75 |
$mount_parm[] = $parm;
|
|
|
76 |
} elseif (preg_match("/(\S+) is (.*) mounted on (\S+) \(type (.*)\)/", $mount_line, $mount_buf)) {
|
|
|
77 |
$parm = array();
|
|
|
78 |
$parm['mountpoint'] = trim($mount_buf[3]);
|
|
|
79 |
$parm['fstype'] = $mount_buf[4];
|
|
|
80 |
$parm['name'] = $mount_buf[1];
|
|
|
81 |
if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[2];
|
|
|
82 |
$mount_parm[] = $parm;
|
|
|
83 |
} elseif (preg_match("/(\S+) (.*) on (\S+) \((.*)\)/", $mount_line, $mount_buf)) {
|
|
|
84 |
$parm = array();
|
|
|
85 |
$parm['mountpoint'] = trim($mount_buf[3]);
|
|
|
86 |
$parm['fstype'] = $mount_buf[2];
|
|
|
87 |
$parm['name'] = $mount_buf[1];
|
|
|
88 |
if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
|
|
|
89 |
$mount_parm[] = $parm;
|
|
|
90 |
} elseif (preg_match("/(\S+) on ([\S ]+) \((\S+)(,\s(.*))?\)/", $mount_line, $mount_buf)) {
|
|
|
91 |
$parm = array();
|
|
|
92 |
$parm['mountpoint'] = trim($mount_buf[2]);
|
|
|
93 |
$parm['fstype'] = $mount_buf[3];
|
|
|
94 |
$parm['name'] = $mount_buf[1];
|
|
|
95 |
if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = isset($mount_buf[5]) ? $mount_buf[5] : '';
|
|
|
96 |
$mount_parm[] = $parm;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
} elseif (CommonFunctions::rfts("/etc/mtab", $mount)) {
|
|
|
100 |
$mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
101 |
foreach ($mount as $mount_line) {
|
|
|
102 |
if (preg_match("/(\S+) (\S+) (\S+) (\S+) ([0-9]+) ([0-9]+)/", $mount_line, $mount_buf)) {
|
|
|
103 |
$parm = array();
|
|
|
104 |
$mount_point = preg_replace("/\\\\040/i", ' ', $mount_buf[2]); //space as \040
|
|
|
105 |
$parm['mountpoint'] = $mount_point;
|
|
|
106 |
$parm['fstype'] = $mount_buf[3];
|
|
|
107 |
$parm['name'] = $mount_buf[1];
|
|
|
108 |
if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
|
|
|
109 |
$mount_parm[] = $parm;
|
|
|
110 |
}
|
|
|
111 |
}
|
|
|
112 |
}
|
|
|
113 |
if (CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG) && ($df!=="")) {
|
|
|
114 |
$df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
115 |
if ($get_inodes && PSI_SHOW_INODES) {
|
|
|
116 |
if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) {
|
|
|
117 |
$df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
118 |
// Store inode use% in an associative array (df_inodes) for later use
|
|
|
119 |
foreach ($df2 as $df2_line) {
|
|
|
120 |
if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) {
|
|
|
121 |
$df_inodes[$inode_buf[1]] = $inode_buf[2];
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
foreach ($df as $df_line) {
|
|
|
127 |
$df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
|
|
|
128 |
if (count($df_buf1) < 2) {
|
|
|
129 |
continue;
|
|
|
130 |
}
|
|
|
131 |
if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
|
|
|
132 |
if (count($df_buf1) == 3) {
|
|
|
133 |
$df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]);
|
|
|
134 |
} else {
|
|
|
135 |
$df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
|
|
|
136 |
}
|
|
|
137 |
if (count($df_buf) == 6) {
|
|
|
138 |
$df_buf[5] = trim($df_buf[5]);
|
|
|
139 |
$dev = new DiskDevice();
|
|
|
140 |
$dev->setName(trim($df_buf[0]));
|
|
|
141 |
if ($df_buf[2] < 0) {
|
|
|
142 |
$dev->setTotal($df_buf[3] * 1024);
|
|
|
143 |
$dev->setUsed($df_buf[3] * 1024);
|
|
|
144 |
} else {
|
|
|
145 |
$dev->setTotal($df_buf[1] * 1024);
|
|
|
146 |
$dev->setUsed($df_buf[2] * 1024);
|
|
|
147 |
if ($df_buf[3]>0) {
|
|
|
148 |
$dev->setFree($df_buf[3] * 1024);
|
|
|
149 |
}
|
|
|
150 |
}
|
|
|
151 |
if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
|
|
|
152 |
|
|
|
153 |
$notwas = true;
|
|
|
154 |
if (isset($mount_parm)) {
|
|
|
155 |
foreach ($mount_parm as $mount_param) { //name and mountpoint find
|
|
|
156 |
if (($mount_param['name']===trim($df_buf[0])) && ($mount_param['mountpoint']===$df_buf[5])) {
|
|
|
157 |
$dev->setFsType($mount_param['fstype']);
|
|
|
158 |
if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
|
|
|
159 |
if (PSI_SHOW_MOUNT_CREDENTIALS) {
|
|
|
160 |
$dev->setOptions($mount_param['options']);
|
|
|
161 |
} else {
|
|
|
162 |
$mpo=$mount_param['options'];
|
|
|
163 |
|
|
|
164 |
$mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
|
|
|
165 |
$mpo=preg_replace('/,guest,/i', ',', $mpo);
|
|
|
166 |
|
|
|
167 |
$mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
|
|
|
168 |
$mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
|
|
|
169 |
|
|
|
170 |
$mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
|
|
|
171 |
$mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
|
|
|
172 |
|
|
|
173 |
$mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
|
|
|
174 |
$mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
|
|
|
175 |
|
|
|
176 |
$dev->setOptions($mpo);
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
$notwas = false;
|
|
|
180 |
break;
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
if ($notwas) foreach ($mount_parm as $mount_param) { //mountpoint find
|
|
|
184 |
if ($mount_param['mountpoint']===$df_buf[5]) {
|
|
|
185 |
$dev->setFsType($mount_param['fstype']);
|
|
|
186 |
if (PSI_SHOW_MOUNT_OPTION && (trim($mount_param['options'])!=="")) {
|
|
|
187 |
if (PSI_SHOW_MOUNT_CREDENTIALS) {
|
|
|
188 |
$dev->setOptions($mount_param['options']);
|
|
|
189 |
} else {
|
|
|
190 |
$mpo=$mount_param['options'];
|
|
|
191 |
|
|
|
192 |
$mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
|
|
|
193 |
$mpo=preg_replace('/,guest,/i', ',', $mpo);
|
|
|
194 |
|
|
|
195 |
$mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
|
|
|
196 |
$mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
|
|
|
197 |
|
|
|
198 |
$mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
|
|
|
199 |
$mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
|
|
|
200 |
|
|
|
201 |
$mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
|
|
|
202 |
$mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
|
|
|
203 |
|
|
|
204 |
$dev->setOptions($mpo);
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
$notwas = false;
|
|
|
208 |
break;
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
if ($notwas) {
|
|
|
214 |
$dev->setFsType('unknown');
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
if ($get_inodes && PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) {
|
|
|
218 |
$dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]);
|
|
|
219 |
}
|
|
|
220 |
$arrResult[] = $dev;
|
|
|
221 |
}
|
|
|
222 |
}
|
|
|
223 |
}
|
|
|
224 |
} else {
|
|
|
225 |
if (isset($mount_parm)) {
|
|
|
226 |
foreach ($mount_parm as $mount_param) {
|
|
|
227 |
if (is_dir($mount_param['mountpoint'])) {
|
|
|
228 |
$total = disk_total_space($mount_param['mountpoint']);
|
|
|
229 |
if (($mount_param['fstype'] != 'none') && ($total > 0)) {
|
|
|
230 |
$dev = new DiskDevice();
|
|
|
231 |
$dev->setName($mount_param['name']);
|
|
|
232 |
$dev->setFsType($mount_param['fstype']);
|
|
|
233 |
|
|
|
234 |
if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_param['mountpoint']);
|
|
|
235 |
|
|
|
236 |
$dev->setTotal($total);
|
|
|
237 |
$free = disk_free_space($mount_param['mountpoint']);
|
|
|
238 |
if ($free > 0) {
|
|
|
239 |
$dev->setFree($free);
|
|
|
240 |
} else {
|
|
|
241 |
$free = 0;
|
|
|
242 |
}
|
|
|
243 |
if ($total > $free) $dev->setUsed($total - $free);
|
|
|
244 |
|
|
|
245 |
if (PSI_SHOW_MOUNT_OPTION) {
|
|
|
246 |
if (PSI_SHOW_MOUNT_CREDENTIALS) {
|
|
|
247 |
$dev->setOptions($mount_param['options']);
|
|
|
248 |
} else {
|
|
|
249 |
$mpo=$mount_param['options'];
|
|
|
250 |
|
|
|
251 |
$mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
|
|
|
252 |
$mpo=preg_replace('/,guest,/i', ',', $mpo);
|
|
|
253 |
|
|
|
254 |
$mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
|
|
|
255 |
$mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
|
|
|
256 |
|
|
|
257 |
$mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
|
|
|
258 |
$mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
|
|
|
259 |
|
|
|
260 |
$mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
|
|
|
261 |
$mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
|
|
|
262 |
|
|
|
263 |
$dev->setOptions($mpo);
|
|
|
264 |
}
|
|
|
265 |
}
|
|
|
266 |
$arrResult[] = $dev;
|
|
|
267 |
}
|
|
|
268 |
}
|
|
|
269 |
}
|
|
|
270 |
}
|
|
|
271 |
}
|
|
|
272 |
|
|
|
273 |
return $arrResult;
|
|
|
274 |
}
|
|
|
275 |
}
|