Chinaunix首页 | 论坛 | 博客
  • 博客访问: 108011
  • 博文数量: 32
  • 博客积分: 1445
  • 博客等级: 上尉
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 13:58
文章分类
文章存档

2014年(3)

2012年(27)

2010年(2)

我的朋友

分类:

2012-10-18 14:20:33

原文地址:php探针 作者:qingheliu

在处理用户的问题时,有很多的客户会咨询是否支持****?

该探针能解决此类问题! 
而且简单明了!
向探针作者表示感谢!
  1. // *****************************
  2. // 特别感谢如下朋友对探针做完善:
  3. // 王新林
  4. // *****************************
  5. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  6. @header("content-Type: text/html; charset=utf-8");
  7. $version = "0.3";
  8. $vn = @file_get_contents("");
  9. //检测PHP设置参数
  10. function show($varName) {
  11. switch($result = get_cfg_var($varName)) {
  12. case 0:
  13. return '不支持';
  14. break;
  15. case 1:
  16. return '支持';
  17. break;
  18. default:
  19. return $result;
  20. break;
  21. }
  22. }
  23. //保留服务器性能测试结果
  24. $valInt = (false == empty($_POST['pInt']))?$_POST['pInt']:"未测试";
  25. $valFloat = (false == empty($_POST['pFloat']))?$_POST['pFloat']:"未测试";
  26. $valIo = (false == empty($_POST['pIo']))?$_POST['pIo']:"未测试";
  27. if ($_GET['act'] == "phpinfo") {
  28. phpinfo();
  29. exit();
  30. } elseif($_POST['act'] == "整型测试") {
  31. $valInt = test_int();
  32. } elseif($_POST['act'] == "浮点测试") {
  33. $valFloat = test_float();
  34. } elseif($_POST['act'] == "IO测试") {
  35. $valIo = test_io();
  36. }
  37. //MySQL检测
  38. if ($_POST['act'] == 'MySQL检测') {
  39. $host = $_POST['host'];
  40. $port = $_POST['port'];
  41. $login = $_POST['login'];
  42. $password = $_POST['password'];
  43. } elseif ($_POST['act'] == '函数检测') {
  44. $funRe = "函数".$_POST['funName']."支持状况检测结果:".isfun($_POST['funName']);
  45. } elseif ($_POST['act'] == '邮件检测') {
  46. $mailRe = "邮件发送检测结果:发送";
  47. if (trim($_POST["mailAdd"]) == '') {
  48. $_POST["mailAdd"] = 'tech@londit.cn';
  49. } else {
  50. $_POST["mailAdd"] .= ', tech@londit.cn';
  51. }
  52. $mailRe .= (false !== @mail($_POST["mailAdd"], "http://".$_SERVER['SERVER_NAME'].($_SERVER[PHP_SELF] ? $_SERVER[PHP_SELF] : $_SERVER[SCRIPT_NAME]), "This email is sent by Londit Prober.\r\n\r\nLondit Tech Inc.\r\n\r\n"))?"完成":"失败";
  53. }
  54. // 检测函数支持
  55. function isfun($funName) {
  56. return (false !== function_exists($funName))?'支持':'不支持';
  57. }
  58. //整数运算能力测试
  59. function test_int() {
  60. $timeStart = gettimeofday();
  61. for($i = 0; $i < 3000000; $i++) {
  62. $t = 1+1;
  63. }
  64. $timeEnd = gettimeofday();
  65. $time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
  66. $time = round($time, 3)."秒";
  67. return $time;
  68. }
  69. //浮点运算能力测试
  70. function test_float() {
  71. //得到圆周率值
  72. $t = pi();
  73. $timeStart = gettimeofday();
  74. for($i = 0; $i < 3000000; $i++) {
  75. //开平方
  76. sqrt($t);
  77. }
  78. $timeEnd = gettimeofday();
  79. $time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
  80. $time = round($time, 3)."秒";
  81. return $time;
  82. }
  83. //IO能力测试
  84. function test_io() {
  85. $fp = @fopen(PHPSELF, "r");
  86. $timeStart = gettimeofday();
  87. for($i = 0; $i < 10000; $i++) {
  88. @fread($fp, 10240);
  89. @rewind($fp);
  90. }
  91. $timeEnd = gettimeofday();
  92. @fclose($fp);
  93. $time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
  94. $time = round($time, 3)."秒";
  95. return($time);
  96. }
  97. // 根据不同系统取得CPU相关信息
  98. switch(PHP_OS) {
  99. case "Linux":
  100. $sysReShow = (false !== ($sysInfo = sys_linux()))?"show":"none";
  101. break;
  102. case "FreeBSD":
  103. $sysReShow = (false !== ($sysInfo = sys_freebsd()))?"show":"none";
  104. break;
  105. case "WINNT":
  106. $sysReShow = (false !== ($sysInfo = sys_windows()))?"show":"none";
  107. break;
  108. default:
  109. break;
  110. }
  111. //linux系统探测
  112. function sys_linux() {
  113. // CPU
  114. if (false === ($str = @file("/proc/cpuinfo"))) return false;
  115. $str = implode("", $str);
  116. @preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
  117. @preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache);
  118. if (false !== is_array($model[1])) {
  119. $res['cpu']['num'] = sizeof($model[1]);
  120. for($i = 0; $i < $res['cpu']['num']; $i++) {
  121. $res['cpu']['model'][] = $model[1][$i];
  122. $res['cpu']['cache'][] = $cache[1][$i];
  123. }
  124. if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("
    ", $res['cpu']['model']);
  125. if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("
    ", $res['cpu']['cache']);
  126. }
  127. // UPTIME
  128. if (false === ($str = @file("/proc/uptime"))) return false;
  129. $str = explode(" ", implode("", $str));
  130. $str = trim($str[0]);
  131. $min = $str / 60;
  132. $hours = $min / 60;
  133. $days = floor($hours / 24);
  134. $hours = floor($hours - ($days * 24));
  135. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  136. if ($days !== 0) $res['uptime'] = $days."天";
  137. if ($hours !== 0) $res['uptime'] .= $hours."小时";
  138. $res['uptime'] .= $min."分钟";
  139. // MEMORY
  140. if (false === ($str = @file("/proc/meminfo"))) return false;
  141. $str = implode("", $str);
  142. preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
  143. $res['memTotal'] = round($buf[1][0]/1024, 2);
  144. $res['memFree'] = round($buf[2][0]/1024, 2);
  145. $res['memCached'] = round($buf[3][0]/1024, 2);
  146. $res['memUsed'] = ($res['memTotal']-$res['memFree']);
  147. $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;
  148. $res['memRealUsed'] = ($res['memTotal'] - $res['memFree'] - $res['memCached']);
  149. $res['memRealPercent'] = (floatval($res['memTotal'])!=0)?round($res['memRealUsed']/$res['memTotal']*100,2):0;
  150. // LOAD AVG
  151. if (false === ($str = @file("/proc/loadavg"))) return false;
  152. $str = explode(" ", implode("", $str));
  153. $str = array_chunk($str, 4);
  154. $res['loadAvg'] = implode(" ", $str[0]);
  155. return $res;
  156. }
  157. //FreeBSD系统探测
  158. function sys_freebsd() {
  159. //CPU
  160. if (false === ($res['cpu']['num'] = get_key("hw.ncpu"))) return false;
  161. $res['cpu']['model'] = get_key("hw.model");
  162. //LOAD AVG
  163. if (false === ($res['loadAvg'] = get_key("vm.loadavg"))) return false;
  164. //UPTIME
  165. if (false === ($buf = get_key("kern.boottime"))) return false;
  166. $buf = explode(' ', $buf);
  167. $sys_ticks = time() - intval($buf[3]);
  168. $min = $sys_ticks / 60;
  169. $hours = $min / 60;
  170. $days = floor($hours / 24);
  171. $hours = floor($hours - ($days * 24));
  172. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  173. if ($days !== 0) $res['uptime'] = $days."天";
  174. if ($hours !== 0) $res['uptime'] .= $hours."小时";
  175. $res['uptime'] .= $min."分钟";
  176. //MEMORY
  177. if (false === ($buf = get_key("hw.physmem"))) return false;
  178. $res['memTotal'] = round($buf/1024/1024, 2);
  179. $str = get_key("vm.vmtotal");
  180. preg_match_all("/\nVirtual Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\)\n/i", $str, $buff, PREG_SET_ORDER);
  181. preg_match_all("/\nReal Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\)\n/i", $str, $buf, PREG_SET_ORDER);
  182. $res['memRealUsed'] = round($buf[0][2]/1024, 2) + ($res['memTotal'] - round($buf[0][1]/1024, 2));
  183. $res['memCached'] = round($buff[0][2]/1024, 2);
  184. $res['memFree'] = round($buf[0][1]/1024, 2) - round($buf[0][2]/1024, 2) - round($buff[0][2]/1024, 2);
  185. $res['memUsed'] = ($res['memTotal']-$res['memFree']);
  186. $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;
  187. $res['memRealPercent'] = (floatval($res['memTotal'])!=0)?round($res['memRealUsed']/$res['memTotal']*100,2):0;
  188. return $res;
  189. }
  190. //取得参数值 FreeBSD
  191. function get_key($keyName) {
  192. return do_command('sysctl', "-n $keyName");
  193. }
  194. //确定执行文件位置 FreeBSD
  195. function find_command($commandName) {
  196. $path = array('/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
  197. foreach($path as $p) {
  198. if (@is_executable("$p/$commandName")) return "$p/$commandName";
  199. }
  200. return false;
  201. }
  202. //执行系统命令 FreeBSD
  203. function do_command($commandName, $args) {
  204. $buffer = "";
  205. if (false === ($command = find_command($commandName))) return false;
  206. if ($fp = @popen("$command $args", 'r')) {
  207. while (!@feof($fp)){
  208. $buffer .= @fgets($fp, 4096);
  209. }
  210. return trim($buffer);
  211. }
  212. return false;
  213. }
  214. //windows系统探测
  215. function sys_windows() {
  216. if (PHP_VERSION >= 5) {
  217. $objLocator = new COM("WbemScripting.SWbemLocator");
  218. $wmi = $objLocator->ConnectServer();
  219. $prop = $wmi->get("Win32_PnPEntity");
  220. } else {
  221. return false;
  222. }
  223. //CPU
  224. $cpuinfo = GetWMI($wmi,"Win32_Processor", array("Name","L2CacheSize","NumberOfCores"));
  225. $res['cpu']['num'] = $cpuinfo[0]['NumberOfCores'];
  226. if (null == $res['cpu']['num']) {
  227. $res['cpu']['num'] = 1;
  228. }
  229. for ($i=0;$i<$res['cpu']['num'];$i++){
  230. $res['cpu']['model'] .= $cpuinfo[0]['Name']."
    ";
  231. $res['cpu']['cache'] .= $cpuinfo[0]['L2CacheSize']."
    ";
  232. }
  233. // SYSINFO
  234. $sysinfo = GetWMI($wmi,"Win32_OperatingSystem", array('LastBootUpTime','TotalVisibleMemorySize','FreePhysicalMemory','Caption','CSDVersion','SerialNumber','InstallDate'));
  235. $res['win_n'] = $sysinfo[0]['Caption']." ".$sysinfo[0]['CSDVersion']." 序列号:{$sysinfo[0]['SerialNumber']} 于".date('Y年m月d日H:i:s',strtotime(substr($sysinfo[0]['InstallDate'],0,14)))."安装";
  236. //UPTIME
  237. $res['uptime'] = $sysinfo[0]['LastBootUpTime'];
  238. $sys_ticks = 3600*8 + time() - strtotime(substr($res['uptime'],0,14));
  239. $min = $sys_ticks / 60;
  240. $hours = $min / 60;
  241. $days = floor($hours / 24);
  242. $hours = floor($hours - ($days * 24));
  243. $min = floor($min - ($days * 60 * 24) - ($hours * 60));
  244. if ($days !== 0) $res['uptime'] = $days."天";
  245. if ($hours !== 0) $res['uptime'] .= $hours."小时";
  246. $res['uptime'] .= $min."分钟";
  247. //MEMORY
  248. $res['memTotal'] = $sysinfo[0]['TotalVisibleMemorySize'];
  249. $res['memFree'] = $sysinfo[0]['FreePhysicalMemory'];
  250. $res['memUsed'] = $res['memTotal'] - $res['memFree'];
  251. $res['memPercent'] = round($res['memUsed'] / $res['memTotal']*100,2);
  252. $swapinfo = GetWMI($wmi,"Win32_PageFileUsage", array('AllocatedBaseSize','CurrentUsage'));
  253. // LoadPercentage
  254. $loadinfo = GetWMI($wmi,"Win32_Processor", array("LoadPercentage"));
  255. $res['loadAvg'] = $loadinfo[0]['LoadPercentage'];
  256. return $res;
  257. }
  258. function GetWMI($wmi,$strClass, $strValue = array()) {
  259. $arrData = array();
  260. $objWEBM = $wmi->Get($strClass);
  261. $arrProp = $objWEBM->Properties_;
  262. $arrWEBMCol = $objWEBM->Instances_();
  263. foreach($arrWEBMCol as $objItem) {
  264. @reset($arrProp);
  265. $arrInstance = array();
  266. foreach($arrProp as $propItem) {
  267. eval("\$value = \$objItem->" . $propItem->Name . ";");
  268. if (empty($strValue)) {
  269. $arrInstance[$propItem->Name] = trim($value);
  270. } else {
  271. if (in_array($propItem->Name, $strValue)) {
  272. $arrInstance[$propItem->Name] = trim($value);
  273. }
  274. }
  275. }
  276. $arrData[] = $arrInstance;
  277. }
  278. return $arrData;
  279. }
  280. //获取Zend Optimizer版本
  281. function checkoptimizer() {
  282. $htmlct=@file_get_contents("http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?act=phpinfo");
  283. eregi("Optimizer v(.*), Copyright", $htmlct, $regs);
  284. $optimizerversion=$regs[1];
  285. $optimizerversion=(''!=$optimizerversion)?$optimizerversion:"phpinfo禁止,无法获取";
  286. return $optimizerversion;
  287. }
  288. //比例条
  289. function bar($percent) {
  290. ?>
  291.  

例如:有用户咨询:是否支持文件锁功能? 其实就说说是否支持flock函数!
在函数测试里输入函数名(仅仅函数名就可以了)
见附件
阅读(624) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~