├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── api.php ├── build ├── README.md ├── build ├── build.bat ├── build.sh └── holy_lance.php ├── common.php ├── index.php ├── init.php ├── screenshot ├── screencapture-ipv4-94cb-Holy-Lance-1474339395845.png ├── screencapture-ipv4-94cb-Holy-Lance-1474339413531.png ├── screencapture-ipv4-94cb-Holy-Lance-1474339422092.png ├── screencapture-ipv4-94cb-Holy-Lance-1474339435898.png ├── screencapture-ipv4-94cb-Holy-Lance-1474339450668.png ├── screencapture-ipv4-94cb-Holy-Lance-1474339466029.png ├── screencapture-ipv4-94cb-Holy-Lance-1477340144069.png ├── screencapture-ipv4-94cb-Holy-Lance-1477958681909.png ├── screencapture-ipv4-94cb-Holy-Lance-1479270760348.png └── screencapture-ipv4-94cb-Holy-Lance-1479270760349.png ├── static ├── css │ └── style.css └── js │ ├── common.js │ ├── easyResponsiveTabs.js │ ├── echarts.min.js │ ├── jquery.jsontotable.min.js │ └── jquery.min.js ├── test_disk.php ├── test_pi.php └── test_ping.php /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - '5.4' 4 | - '5.5' 5 | - '5.6' 6 | - '7.0' 7 | - '7.1' 8 | - '7.2' 9 | - nightly 10 | # - hhvm 11 | 12 | # https://docs.travis-ci.com/user/reference/trusty#PHP-images 13 | matrix: 14 | include: 15 | - php: 5.2 16 | dist: precise 17 | - php: 5.3.3 18 | dist: precise 19 | # matrix: 20 | # allow_failures: 21 | # - php: hhvm 22 | 23 | before_script: 24 | - chmod 777 -R ./ 25 | - php -m 26 | - cd build 27 | 28 | script: 29 | - php build -n holy_lance 30 | - php -l holy_lance.php 31 | # - php -S 0.0.0.0:8080 & 32 | # - curl -s http://localhost:8080/holy_lance.php?file=init.php 33 | # - curl -s http://localhost:8080/holy_lance.php?file=api.php 34 | - php-cgi -f holy_lance.php file=init.php 35 | - php-cgi -f holy_lance.php file=api.php 36 | - php-cgi -f holy_lance.php file=test_disk.php 37 | - php-cgi -f holy_lance.php file=test_pi.php 38 | - php-cgi -f holy_lance.php file=test_ping.php ip=8.8.8.8 39 | - php-cgi -f holy_lance.php -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Canbin Lin (lincanbin@hotmail.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Holy Lance [](https://travis-ci.org/lincanbin/Holy-Lance) 2 | A Linux Resource / Performance Monitor based on PHP. 3 | 4 | ## Demo/Official Website 5 | 6 | * [Demo](http://ipv4.94cb.com/Holy-Lance) 7 | * [中文官方支持论坛](https://www.94cb.com) 8 | * [English official support forum](https://en.94cb.com) 9 | 10 | ## Requirements 11 | 12 | * PHP version 5.2.0 or higher. 13 | * If your PHP version is lower than 5.4, you must disable [safe_mode](http://php.net/manual/en/features.safe-mode.php). 14 | 15 | ## Install 16 | 17 | upload `build/holy_lance.php` to your server. 18 | 19 | *__OR__* 20 | 21 | ```bash 22 | wget 'https://raw.githubusercontent.com/lincanbin/Holy-Lance/master/build/holy_lance.php' --no-check-certificate 23 | ``` 24 | 25 | ## Build 26 | 27 | If you want to modify the file name, you must rebuild it: 28 | 29 | * run `build\build.sh` under Linux. 30 | 31 | * run `build\build.bat` under Windows. 32 | 33 | ## Screenshot 34 | 35 |  36 | 37 |  38 | 39 |  40 | 41 |  42 | 43 |  44 | 45 |  46 | 47 |  48 | 49 |  50 | 51 |  52 | 53 |  54 | 55 | ## Donate for Holy Lance 56 | 57 | * [Donation list](https://www.94cb.com/t/4288) 58 | 59 | * Alipay: 60 | 61 |  62 | 63 | * Wechat: 64 | 65 |  66 | 67 | * Paypal: 68 | 69 | https://www.paypal.me/lincanbin 70 | 71 | ## License 72 | 73 | ``` 74 | The MIT License (MIT) 75 | 76 | Copyright (c) 2016 Canbin Lin (lincanbin@hotmail.com) 77 | 78 | Permission is hereby granted, free of charge, to any person obtaining a copy 79 | of this software and associated documentation files (the "Software"), to deal 80 | in the Software without restriction, including without limitation the rights 81 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 82 | copies of the Software, and to permit persons to whom the Software is 83 | furnished to do so, subject to the following conditions: 84 | 85 | The above copyright notice and this permission notice shall be included in all 86 | copies or substantial portions of the Software. 87 | 88 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 89 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 90 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 91 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 92 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 93 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 94 | SOFTWARE. 95 | ``` 96 | -------------------------------------------------------------------------------- /api.php: -------------------------------------------------------------------------------- 1 | true, 25 | 'load' => array(0, 0, 0), 26 | 'uptime' => '0:0:0:0', 27 | 'cpu_usage' => 0, 28 | 'logic_cpu_usage' => array(), 29 | 'network_service' => array(), 30 | 'connection' => array( 31 | 'ESTABLISHED' => 0, 32 | 'SYN_SENT' => 0, 33 | 'SYN_RECV' => 0, 34 | 'FIN_WAIT1' => 0, 35 | 'FIN_WAIT2' => 0, 36 | 'TIME_WAIT' => 0, 37 | 'CLOSE' => 0, 38 | 'CLOSE_WAIT' => 0, 39 | 'LAST_ACK' => 0, 40 | 'LISTEN' => 0, 41 | 'CLOSING' => 0, 42 | 'UNKNOWN' => 0 43 | ), 44 | 'process' => array(), 45 | 'disk_free' => array() 46 | ); 47 | $system_info['load'] = sys_getloadavg(); 48 | 49 | //uptime 50 | $uptime = array(); 51 | exec("cat /proc/uptime | awk '{print $1}'", $uptime); 52 | if (!empty($uptime)){ 53 | $uptime[0] = intval($uptime[0]); 54 | $system_info['uptime'] = convert_timestamp_2_string($uptime[0]); 55 | } 56 | unset($uptime); 57 | 58 | // cpu: % and disk and network 59 | $cpu_usage1 = array(); 60 | $cpu_usage2 = array(); 61 | $disk_usage1 = array(); 62 | $disk_usage2 = array(); 63 | $network_status1 = array(); 64 | $network_status2 = array(); 65 | // Deprecated Code: Low performance 66 | // exec("top -b -n2 | grep \"Cpu(s)\"|tail -n 1 | awk '{print $2 + $4}'", $cpu_usage); 67 | exec("cat /proc/stat | grep 'cpu' | awk '{print $2+$3+$4+$5+$6+$7+$8+$9+$10\"\\n\" $5}'", $cpu_usage1); 68 | exec("cat /proc/diskstats | awk '{print $3 \"\\n\" $6 \"\\n\" $7 \"\\n\" $10 \"\\n\" $11}'", $disk_usage1); 69 | exec("cat /proc/net/dev | grep \":\" | awk '{gsub(\":\", \" \");print $1 \":\" $2 \":\" $3 \":\" $10 \":\" $11}'", $network_status1); 70 | usleep(SAMPLING_TIME); 71 | exec("cat /proc/diskstats | awk '{print $3 \"\\n\" $6 \"\\n\" $7 \"\\n\" $10 \"\\n\" $11}'", $disk_usage2); 72 | exec("cat /proc/net/dev | grep \":\" | awk '{gsub(\":\", \" \");print $1 \":\" $2 \":\" $3 \":\" $10 \":\" $11}'", $network_status2); 73 | exec("cat /proc/stat | grep 'cpu' | awk '{print $2+$3+$4+$5+$6+$7+$8+$9+$10\"\\n\" $5}'", $cpu_usage2); 74 | // CPU 75 | if (!empty($cpu_usage1)) { 76 | foreach (range(0, count($cpu_usage1) / 2 - 1) as $offset) { 77 | $cpu_usage = round((($cpu_usage2[0 + $offset * 2] - $cpu_usage1[0 + $offset * 2]) - ($cpu_usage2[1 + $offset * 2] - $cpu_usage1[1 + $offset * 2])) / ($cpu_usage2[0 + $offset * 2] - $cpu_usage1[0 + $offset * 2]) * 100, 1); 78 | if ($offset === 0) { 79 | $system_info['cpu_usage'] = $cpu_usage; 80 | } else { 81 | $system_info['logic_cpu_usage'][$offset - 1] = $cpu_usage; 82 | } 83 | } 84 | } 85 | 86 | //Disk: KiB per second 87 | if (!empty($disk_usage1)) { 88 | foreach (range(0, count($disk_usage1) / 5 - 1) as $offset) { 89 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_read_speed'] = round(($disk_usage2[1 + $offset * 5] - $disk_usage1[1 + $offset * 5]) / 2 / (SAMPLING_TIME / 1000000), 1); 90 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_read_active_time'] = round(($disk_usage2[2 + $offset * 5] - $disk_usage1[2 + $offset * 5]) / (SAMPLING_TIME / 1000000), 1); 91 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_read_kibibytes'] = round($disk_usage2[1 + $offset * 5] / 2); 92 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_write_speed'] = round(($disk_usage2[3 + $offset * 5] - $disk_usage1[3 + $offset * 5]) / 2 / (SAMPLING_TIME / 1000000), 1); 93 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_write_active_time'] = round(($disk_usage2[4 + $offset * 5] - $disk_usage1[4 + $offset * 5]) / (SAMPLING_TIME / 1000000), 1); 94 | $system_info['disk'][$disk_usage2[0 + $offset * 5]]['disk_write_kibibytes'] = round($disk_usage2[3 + $offset * 5] / 2); 95 | } 96 | } 97 | unset($cpu_usage1); 98 | unset($cpu_usage2); 99 | unset($disk_usage1); 100 | unset($disk_usage2); 101 | 102 | //Network: Bytes per second 103 | $network_usage = array(); 104 | if (!empty($network_status2)) { 105 | foreach ($network_status2 as $key => $eth) { 106 | $eth_previous = explode(":", $network_status1[$key]); 107 | $eth = explode(":", $eth); 108 | $network_card_name = $eth[0]; 109 | unset($eth_previous[0]); 110 | unset($eth[0]); 111 | $network_usage[$network_card_name] = array( 112 | "receive_bytes" => $eth[1], 113 | "receive_packets" => $eth[2], 114 | "receive_speed" => ($eth[1] - $eth_previous[1]) / (SAMPLING_TIME / 1000000), 115 | "transmit_bytes" => $eth[3], 116 | "transmit_packets" => $eth[4], 117 | "transmit_speed" => ($eth[3] - $eth_previous[3]) / (SAMPLING_TIME / 1000000) 118 | ); 119 | } 120 | $system_info['network'] = $network_usage; 121 | } 122 | unset($network_status1); 123 | unset($network_status2); 124 | unset($network_usage); 125 | 126 | // memory: KiB 127 | $system_info['memory_usage_total'] = trim(shell_exec("free | grep \"Mem\" | awk '{print $2}'")); 128 | $system_info['memory_usage_used'] = trim(shell_exec("free | grep \"Mem\" | awk '{print $3}'")); 129 | $system_info['memory_usage_free'] = trim(shell_exec("free | grep \"Mem\" | awk '{print $4}'")); 130 | $system_info['memory_usage_buff'] = trim(shell_exec("cat /proc/meminfo | grep Buffers: | awk '{print $2}'")); 131 | $system_info['memory_usage_cache'] = trim(shell_exec("cat /proc/meminfo | grep Cached: | head -n1 | awk '{print $2}'")); 132 | 133 | $system_info['memory_usage_swap_total'] = trim(shell_exec("free | grep \"Swap\" | awk '{print $2}'")); 134 | $system_info['memory_usage_swap_used'] = trim(shell_exec("free | grep \"Swap\" | awk '{print $3}'")); 135 | $system_info['memory_usage_swap_free'] = trim(shell_exec("free | grep \"Swap\" | awk '{print $4}'")); 136 | 137 | // process 138 | $process_number = array(); 139 | exec("ps -ef|wc -l", $process_number); 140 | $system_info['process_number'] = $process_number[0]; 141 | unset($process_number); 142 | 143 | $process_list = array(); 144 | exec("ps auxw", $process_list); // --sort=time 145 | if (!empty($process_list)) { 146 | unset($process_list[0]); 147 | $process_map = array(); 148 | foreach ($process_list as $key => $value) { 149 | $process_map[] = explode(" ", preg_replace("/\s(?=\s)/","\\1", $value), 11); 150 | } 151 | $system_info['process'] = $process_map; 152 | } 153 | unset($process_list); 154 | 155 | // disk_free 156 | $disk_free_list = array(); 157 | exec("df -T", $disk_free_list); // --sort=time 158 | if (!empty($disk_free_list)) { 159 | unset($disk_free_list[0]); 160 | $disk_free_map = array(); 161 | foreach ($disk_free_list as $key => $value) { 162 | $disk_free_map[] = explode(" ", preg_replace("/\s(?=\s)/","\\1", $value), 7); 163 | } 164 | $system_info['disk_free'] = $disk_free_map; 165 | } 166 | unset($disk_free_list); 167 | 168 | // network service 169 | $temp_network_service_list = array(); 170 | exec("netstat -lntp | tail -n +3", $temp_network_service_list); 171 | if (!empty($temp_network_service_list)) { 172 | $network_service_list = array(); 173 | foreach ($temp_network_service_list as $key => $value) { 174 | $network_service_list[] = explode(" ", preg_replace("/\s(?=\s)/","\\1", $value), 7); 175 | } 176 | $system_info['network_service'] = $network_service_list; 177 | } 178 | unset($temp_network_service_list); 179 | 180 | // connections 181 | $temp_connection = array(); 182 | exec("netstat -nat| tail -n +3 | awk '{print $6}'|sort|uniq -c", $temp_connection); 183 | if (!empty($temp_connection)) { 184 | $connection = array(); 185 | foreach ($temp_connection as $key => $value) { 186 | $cur_connection = explode(" ", trim($value), 2); 187 | $connection[$cur_connection[1]] = intval($cur_connection[0]); 188 | } 189 | $system_info['connection'] = array_merge($system_info['connection'], $connection); 190 | } 191 | unset($temp_connection); 192 | 193 | if (version_compare(PHP_VERSION, '5.4.0') < 0) { 194 | echo json_encode($system_info); 195 | } else { 196 | echo json_encode($system_info, JSON_PRETTY_PRINT); 197 | } 198 | ?> -------------------------------------------------------------------------------- /build/README.md: -------------------------------------------------------------------------------- 1 | # Holy Lance [](https://travis-ci.org/lincanbin/Holy-Lance) 2 | A Linux Resource / Performance Monitor based on PHP. 3 | 4 | ## Build 5 | 6 | ```shell 7 | php build -n holy_lance 8 | ``` -------------------------------------------------------------------------------- /build/build: -------------------------------------------------------------------------------- 1 | $old_file_name_value) { 43 | $file_buffer[$old_file_name]['content'] = str_replace($old_file_name_value, $new_file_name_list[$file_name_key], $file_buffer[$old_file_name]['content']); 44 | } 45 | $file_buffer[$old_file_name]['content'] = str_replace(" ", "", $file_buffer[$old_file_name]['content']); 46 | } 47 | } 48 | 49 | function listDir($dir) 50 | { 51 | global $ignore_dir, $ignore_file, $old_file_name_list, $new_file_name_list; 52 | if (is_dir($dir)) { 53 | if ($dh = opendir($dir)) { 54 | while (($file = readdir($dh)) !== false) { 55 | if ((is_dir($dir . "/" . $file)) && $file != "." && $file != "..") { 56 | if (!in_array($file, $ignore_dir)) { 57 | // echo $file . "\n"; 58 | listDir($dir . $file . "/"); 59 | } 60 | } else { 61 | if ($file != "." && $file != "..") { 62 | $file_name = $dir . $file; 63 | if (!in_array($file_name, $ignore_file)) { 64 | $old_file_name_list[$file_name] = str_replace("../", "", $file_name); 65 | $new_file_name_list[$file_name] = BUILD_FILE_NAME . "?file=" . urlencode($old_file_name_list[$file_name]); 66 | echo $file_name . "\n"; 67 | } 68 | } 69 | } 70 | } 71 | closedir($dh); 72 | } 73 | } 74 | } 75 | 76 | 77 | listDir("../"); 78 | 79 | foreach ($old_file_name_list as $file_name => $old_file_name) { 80 | add_file_to_buffer($file_name); 81 | } 82 | 83 | $fp = fopen(BUILD_FILE_NAME, 'w'); 84 | $entry_file = !empty($file_buffer[ENTER_FILE_NAME]) ? $file_buffer[ENTER_FILE_NAME] : ''; 85 | if ($entry_file === '') { 86 | echo 'Entry file not found! '; 87 | exit(1); 88 | } 89 | unset($file_buffer[ENTER_FILE_NAME]); 90 | $common_file = !empty($file_buffer[COMMON_FILE_NAME]) ? $file_buffer[COMMON_FILE_NAME] : ''; 91 | if ($common_file === '') { 92 | echo 'Common file not found! '; 93 | exit(2); 94 | } 95 | fwrite($fp, ''); 98 | $common_file['content'] = str_replace("define('HOLY_LANCE_PASSWORD', '');", "define('HOLY_LANCE_PASSWORD', '" . $password . "');", $common_file['content']); 99 | fwrite($fp, $common_file['content']); 100 | unset($file_buffer[COMMON_FILE_NAME]); 101 | foreach ($file_buffer as $file_name => $file) { 102 | fwrite($fp, ''); 112 | fwrite($fp, $file['content']); 113 | fwrite($fp, ''); 117 | } 118 | 119 | fwrite($fp, $entry_file['content']); 120 | fclose($fp); 121 | 122 | echo "\n\n" . BUILD_FILE_NAME . " has been generated! \npssword: " . $password . "\n\n"; -------------------------------------------------------------------------------- /build/build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | set/p param=Enter the file name of the Lance Holy you want(Default: holy_lance): 3 | if "%param%" equ "" set "param=holy_lance" 4 | set/p pw=Enter the password of the Lance Holy you want(No password is required by default): 5 | php build -n %param% -p %pw% 6 | pause -------------------------------------------------------------------------------- /build/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | echo "Enter the file name of the Holy Lance you want (Default: holy_lance): " 3 | read file_name 4 | file_name="${file_name:=holy_lance}" 5 | echo "Enter the password of the Holy Lance you want (No password is required by default): " 6 | read pw 7 | pw="${pw:=}" 8 | php build -n $file_name -p $pw -------------------------------------------------------------------------------- /common.php: -------------------------------------------------------------------------------- 1 | = 1024 && $i < 4; $i++) 34 | $size /= 1024; 35 | return round($size, $precision) . $units[$i]; 36 | } 37 | 38 | function format_number($number) 39 | { 40 | return number_format($number, '0', '.', ' '); 41 | } 42 | 43 | 44 | function check_permission($file_name) 45 | { 46 | $fp = @fopen($file_name, 'w'); 47 | if (!$fp) { 48 | return false; 49 | } else { 50 | fclose($fp); 51 | @unlink($file_name); 52 | return true; 53 | } 54 | } 55 | 56 | function get_cpu_info_map($cpu_info_val) 57 | { 58 | $result = array(); 59 | foreach (explode("\n", $cpu_info_val) as $value) { 60 | if ($value) { 61 | $item = array_map("trim", explode(":", $value)); 62 | $result[str_replace(" ", "_", $item[0])] = $item[1]; 63 | } 64 | } 65 | return $result; 66 | } 67 | 68 | function get_mem_info_map($mem_info) 69 | { 70 | $result = array(); 71 | foreach ($mem_info as $value) { 72 | $value = str_ireplace(")", "", str_ireplace("(", "_", str_ireplace("kB", "", $value))); 73 | $item = array_map("trim", explode(":", $value)); 74 | $result[str_replace(" ", "_", $item[0])] = $item[1]; 75 | } 76 | return $result; 77 | } 78 | 79 | function convert_timestamp_2_string($timestamp) 80 | { 81 | $timestamp = intval($timestamp); 82 | return intval($timestamp / 86400) . ":" 83 | . sprintf("%02d", $timestamp % 86400 / 3600) . ":" 84 | . sprintf("%02d", $timestamp % 3600 / 60) . ":" 85 | . sprintf("%02d", $timestamp % 60); 86 | } 87 | 88 | function check_password() 89 | { 90 | if (HOLY_LANCE_PASSWORD !== '' && (!isset($_POST['password']) || $_POST['password'] !== HOLY_LANCE_PASSWORD)) { 91 | echo '{"status":false}'; 92 | exit(1); 93 | } 94 | return true; 95 | } 96 | 97 | 98 | // 创建row socket 需要root权限,所以用root账户在CLI下运行可以成功,用www用户在fpm下运行可能会失败,但是不会报错 99 | // 需要root权限运行则要php-fpm -R运行 100 | // 目前针对没有root权限做了一套临时的兼容方案 101 | function ping($host, $port = 80) 102 | { 103 | $protocolNumber = getprotobyname('icmp'); 104 | $socket = socket_create(AF_INET, SOCK_RAW, $protocolNumber); 105 | if ($socket === false) {// 没有Root权限,开启Raw socket失败,用TCP协议ping 106 | return ping_without_root($host, $port); 107 | } 108 | socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0)); 109 | socket_connect($socket, $host, 0); 110 | $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67"; 111 | socket_send($socket, $package, strlen($package), 0); 112 | $ts1 = microtime(true); 113 | if (socket_read($socket, 255) !== false) { 114 | $ts2 = microtime(true); 115 | $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; 116 | } else { 117 | $result = socket_strerror(socket_last_error($socket)); 118 | } 119 | socket_close($socket); 120 | return $result; 121 | } 122 | 123 | function ping_without_root($host, $port) 124 | { 125 | try { 126 | $err_no = null; 127 | $err_str = null; 128 | $ts1 = microtime(true); 129 | $fp = stream_socket_client("tcp://" . $host . ":" . $port, $err_no, $err_str, 3); 130 | $ts2 = microtime(true); 131 | $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; 132 | if ($fp === false) { 133 | $result = 'Timeout'; 134 | } 135 | fclose($fp); 136 | } catch (Exception $exception) { 137 | $result = 'Timeout'; 138 | } 139 | return $result; 140 | } 141 | ?> -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 |588 |
589 | MIT License 590 | 591 | Copyright (c) 2016 Canbin Lin (lincanbin@hotmail.com) 592 | 593 | Permission is hereby granted, free of charge, to any person obtaining a copy 594 | of this software and associated documentation files (the "Software"), to deal 595 | in the Software without restriction, including without limitation the rights 596 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 597 | copies of the Software, and to permit persons to whom the Software is 598 | furnished to do so, subject to the following conditions: 599 | 600 | The above copyright notice and this permission notice shall be included in all 601 | copies or substantial portions of the Software. 602 | 603 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 604 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 605 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 606 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 607 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 608 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 609 | SOFTWARE. 610 | 611 |612 | 613 |
614 | GitHub地址:https://github.com/lincanbin/Holy-Lance 615 |
616 |617 | 618 |
619 |t |