Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

class.Parser.inc.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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
  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
  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. * @return Array
  32. */
  33. public static function lspci($debug = PSI_DEBUG)
  34. {
  35. $arrResults = array();
  36. if (CommonFunctions::executeProgram("lspci", "", $strBuf, $debug)) {
  37. $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
  38. foreach ($arrLines as $strLine) {
  39. $arrParams = preg_split('/ /', trim($strLine), 2);
  40. if (count($arrParams) == 2)
  41. $strName = $arrParams[1];
  42. else
  43. $strName = "unknown";
  44. $strName = preg_replace('/\(.*\)/', '', $strName);
  45. $dev = new HWDevice();
  46. $dev->setName($strName);
  47. $arrResults[] = $dev;
  48. }
  49. }
  50. return $arrResults;
  51. }
  52. /**
  53. * parsing the output of pciconf command
  54. *
  55. * @return Array
  56. */
  57. public static function pciconf()
  58. {
  59. $arrResults = array();
  60. $intS = 0;
  61. if (CommonFunctions::executeProgram("pciconf", "-lv", $strBuf, PSI_DEBUG)) {
  62. $arrTemp = array();
  63. $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
  64. foreach ($arrLines as $strLine) {
  65. if (preg_match("/(.*) = '(.*)'/", $strLine, $arrParts)) {
  66. if (trim($arrParts[1]) == "vendor") {
  67. $arrTemp[$intS] = trim($arrParts[2]);
  68. } elseif (trim($arrParts[1]) == "device") {
  69. $arrTemp[$intS] .= " - ".trim($arrParts[2]);
  70. $intS++;
  71. }
  72. }
  73. }
  74. foreach ($arrTemp as $name) {
  75. $dev = new HWDevice();
  76. $dev->setName($name);
  77. $arrResults[] = $dev;
  78. }
  79. }
  80. return $arrResults;
  81. }
  82. /**
  83. * parsing the output of df command
  84. *
  85. * @param string $df_param additional parameter for df command
  86. *
  87. * @return array
  88. */
  89. public static function df($df_param = "")
  90. {
  91. $arrResult = array();
  92. if (CommonFunctions::executeProgram('mount', '', $mount, PSI_DEBUG)) {
  93. $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
  94. foreach ($mount as $mount_line) {
  95. if (preg_match("/(\S+) on ([\S ]+) type (.*) \((.*)\)/", $mount_line, $mount_buf)) {
  96. $mount_parm[$mount_buf[2]]['fstype'] = $mount_buf[3];
  97. $mount_parm[$mount_buf[2]]['name'] = $mount_buf[1];
  98. if (PSI_SHOW_MOUNT_OPTION) $mount_parm[$mount_buf[2]]['options'] = $mount_buf[4];
  99. } elseif (preg_match("/(\S+) is (.*) mounted on (\S+) \(type (.*)\)/", $mount_line, $mount_buf)) {
  100. $mount_parm[$mount_buf[3]]['fstype'] = $mount_buf[4];
  101. $mount_parm[$mount_buf[3]]['name'] = $mount_buf[1];
  102. if (PSI_SHOW_MOUNT_OPTION) $mount_parm[$mount_buf[3]]['options'] = $mount_buf[2];
  103. } elseif (preg_match("/(\S+) (.*) on (\S+) \((.*)\)/", $mount_line, $mount_buf)) {
  104. $mount_parm[$mount_buf[3]]['fstype'] = $mount_buf[2];
  105. $mount_parm[$mount_buf[3]]['name'] = $mount_buf[1];
  106. if (PSI_SHOW_MOUNT_OPTION) $mount_parm[$mount_buf[3]]['options'] = $mount_buf[4];
  107. } elseif (preg_match("/(\S+) on ([\S ]+) \((\S+)(,\s(.*))?\)/", $mount_line, $mount_buf)) {
  108. $mount_parm[$mount_buf[2]]['fstype'] = $mount_buf[3];
  109. $mount_parm[$mount_buf[2]]['name'] = $mount_buf[1];
  110. if (PSI_SHOW_MOUNT_OPTION) $mount_parm[$mount_buf[2]]['options'] = isset($mount_buf[5]) ? $mount_buf[5] : '';
  111. }
  112. }
  113. } elseif (CommonFunctions::rfts("/etc/mtab", $mount)) {
  114. $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
  115. foreach ($mount as $mount_line) {
  116. if (preg_match("/(\S+) (\S+) (\S+) (\S+) ([0-9]+) ([0-9]+)/", $mount_line, $mount_buf)) {
  117. $mount_point = preg_replace("/\\\\040/i", ' ', $mount_buf[2]); //space as \040
  118. $mount_parm[$mount_point]['fstype'] = $mount_buf[3];
  119. $mount_parm[$mount_point]['name'] = $mount_buf[1];
  120. if (PSI_SHOW_MOUNT_OPTION) $mount_parm[$mount_point]['options'] = $mount_buf[4];
  121. }
  122. }
  123. }
  124. if (CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG)) {
  125. $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
  126. if (PSI_SHOW_INODES) {
  127. if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) {
  128. $df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY);
  129. // Store inode use% in an associative array (df_inodes) for later use
  130. foreach ($df2 as $df2_line) {
  131. if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) {
  132. $df_inodes[$inode_buf[1]] = $inode_buf[2];
  133. }
  134. }
  135. }
  136. }
  137. foreach ($df as $df_line) {
  138. $df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
  139. if (count($df_buf1) < 2) {
  140. continue;
  141. }
  142. if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
  143. if (count($df_buf1) == 3) {
  144. $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]);
  145. } else {
  146. $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
  147. }
  148. if (count($df_buf) == 6) {
  149. $df_buf[5] = trim($df_buf[5]);
  150. $dev = new DiskDevice();
  151. $dev->setName(trim($df_buf[0]));
  152. if ($df_buf[2] < 0) {
  153. $dev->setTotal($df_buf[3] * 1024);
  154. $dev->setUsed($df_buf[3] * 1024);
  155. } else {
  156. $dev->setTotal($df_buf[1] * 1024);
  157. $dev->setUsed($df_buf[2] * 1024);
  158. if ($df_buf[3]>0) {
  159. $dev->setFree($df_buf[3] * 1024);
  160. }
  161. }
  162. if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
  163. if (isset($mount_parm[$df_buf[5]])) {
  164. $dev->setFsType($mount_parm[$df_buf[5]]['fstype']);
  165. if (PSI_SHOW_MOUNT_OPTION) {
  166. if (PSI_SHOW_MOUNT_CREDENTIALS) {
  167. $dev->setOptions($mount_parm[$df_buf[5]]['options']);
  168. } else {
  169. $mpo=$mount_parm[$df_buf[5]]['options'];
  170. $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
  171. $mpo=preg_replace('/,guest,/i', ',', $mpo);
  172. $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
  173. $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
  174. $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
  175. $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
  176. $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
  177. $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
  178. $dev->setOptions($mpo);
  179. }
  180. }
  181. }
  182. if (PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) {
  183. $dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]);
  184. }
  185. $arrResult[] = $dev;
  186. }
  187. }
  188. }
  189. } else {
  190. if (isset($mount_parm)) {
  191. foreach ($mount_parm as $mount_point=>$mount_param) {
  192. $total = disk_total_space($mount_point);
  193. if (($mount_param['fstype'] != 'none') && ($total > 0)) {
  194. $dev = new DiskDevice();
  195. $dev->setName($mount_param['name']);
  196. $dev->setFsType($mount_param['fstype']);
  197. if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_point);
  198. $dev->setTotal($total);
  199. $free = disk_free_space($mount_point);
  200. if ($free > 0) {
  201. $dev->setFree($free);
  202. } else {
  203. $free = 0;
  204. }
  205. if ($total > $free) $dev->setUsed($total - $free);
  206. if (PSI_SHOW_MOUNT_OPTION) {
  207. if (PSI_SHOW_MOUNT_CREDENTIALS) {
  208. $dev->setOptions($mount_param['options']);
  209. } else {
  210. $mpo=$mount_param['options'];
  211. $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
  212. $mpo=preg_replace('/,guest,/i', ',', $mpo);
  213. $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
  214. $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
  215. $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
  216. $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
  217. $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
  218. $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
  219. $dev->setOptions($mpo);
  220. }
  221. }
  222. $arrResult[] = $dev;
  223. }
  224. }
  225. }
  226. }
  227. return $arrResult;
  228. }
  229. }