PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/config/cacti/http-response-time/scripts/ss_bwtest_curl.php

http://timoseven.googlecode.com/
PHP | 189 lines | 108 code | 22 blank | 59 comment | 16 complexity | 15ed0f7c7355beb04a2f2cd15b733538 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0
  1. <?php
  2. //////////////////////////////////////////////////////////////////////////////
  3. // ss_bwtest_curl.php
  4. // @author: yreddy (melk0r101@yahoo.com)
  5. // @version: 1.0 - 20100830
  6. // CURL script to fetch a web page and print out statistics from the query. Can
  7. // be called either from command line or cacti script server.
  8. // IMPORTANT: NEED PHP CURL MODULE INSTALLED (aptitude install php5-curl)
  9. //////////////////////////////////////////////////////////////////////////////
  10. //////////////////////////////////////////////////////////////////////////////
  11. // GLOBAL VARIABLES AND CONSTANT DEFINITIONS
  12. //////////////////////////////////////////////////////////////////////////////
  13. define("BWTEST_TIMEOUT", 3);
  14. //////////////////////////////////////////////////////////////////////////////
  15. // FUNCTIONS DEFINITIONS
  16. //////////////////////////////////////////////////////////////////////////////
  17. // FUNTION usage()
  18. // Show script usage
  19. function usage(){
  20. printf("Usage (command line): php ss_bwtest_curl.php -U URL [-P proxy_ip:proxy_port] [-u proxy_username] [-p proxy_password] [-t timeout] [-A useragent]\n");
  21. printf("Usage (Script Server): ss_bwtest_curl.php URL [proxy_ip:proxy_port] [proxy_username] [proxy_password] [timeout] [useragent]\n");
  22. }
  23. // FUNTION help()
  24. // Show script usage and show details of all output fields
  25. function help (){
  26. usage();
  27. printf("\nOutput Fields:
  28. CURLINFO_TOTAL_TIME - Total transaction time in seconds for last transfer
  29. CURLINFO_NAMELOOKUP_TIME - Time in seconds until name resolving was complete
  30. CURLINFO_CONNECT_TIME - Time in seconds it took to establish the connection
  31. CURLINFO_PRETRANSFER_TIME - Time in seconds from start until just before file transfer begins
  32. CURLINFO_STARTTRANSFER_TIME - Time in seconds until the first byte is about to be transferred
  33. CURLINFO_REDIRECT_TIME - Time in seconds of all redirection steps before final transaction was started
  34. CURLINFO_SIZE_UPLOAD - Total number of bytes uploaded
  35. CURLINFO_SIZE_DOWNLOAD - Total number of bytes downloaded
  36. CURLINFO_SPEED_DOWNLOAD- Average download speed
  37. CURLINFO_SPEED_UPLOAD - Average upload speed
  38. CURLINFO_HEADER_SIZE - Total size of all headers received
  39. CURLINFO_REQUEST_SIZE - Total size of issued requests, currently only for HTTP requests
  40. CURLINFO_CONTENT_LENGTH_DOWNLOAD - content-length of download, read from Content-Length: field
  41. CURLINFO_CONTENT_LENGTH_DOWNLOAD - Specified size of upload\n");
  42. }
  43. // FUNTION ss_bwtest_curl()
  44. // Main function. Create CURL object with appropriate variables, access web page and return statistics
  45. function ss_bwtest_curl($URL, $proxy=null, $proxy_username=null, $proxy_password=null, $timeout=BWTEST_TIMEOUT, $useragent=null){
  46. //init CURL OBJECT
  47. $ch=curl_init();
  48. //set CURL options
  49. //curl_setopt - CURLOPT_URL - The URL to fetch. This can also be set when initializing a session with curl_init().
  50. curl_setopt($ch, CURLOPT_URL, $URL);
  51. if ((!is_null($proxy))&&(preg_match("/http:\/\/[^:]+:[0-9]+/", $proxy))){
  52. //curl_setopt - CURLOPT_PROXY - The HTTP proxy to tunnel requests through.
  53. curl_setopt($ch, CURLOPT_PROXY, $proxy);
  54. if ((!is_null($proxy_username))&&(!is_null($proxy_username))){
  55. //curl_setopt - CURLOPT_PROXYUSERPWD - A username and password formatted as "[username]:[password]" to use for the connection to the proxy.
  56. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxy_username.":".$proxy_password);
  57. }
  58. }
  59. //curl_setopt - CURLOPT_DNS_USE_GLOBAL_CACHE - TRUE to use a global DNS cache. This option is not thread-safe and is enabled by default.
  60. curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
  61. //curl_setopt - CURLOPT_FOLLOWLOCATION - TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).
  62. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  63. //curl_setopt - CURLOPT_FORBID_REUSE - TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.
  64. curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
  65. //curl_setopt - CURLOPT_FRESH_CONNECT - TRUE to force the use of a new connection instead of a cached one.
  66. curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  67. //curl_setopt - CURLOPT_HEADER - TRUE to include the header in the output.
  68. curl_setopt($ch, CURLOPT_HEADER, false);
  69. //curl_setopt - CURLOPT_RETURNTRANSFER - TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. //curl_setopt - CURLOPT_TIMEOUT - The maximum number of seconds to allow cURL functions to execute.
  72. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  73. //curl_setopt - CURLOPT_USERAGENT - The contents of the "User-Agent: " header to be used in a HTTP request.
  74. if (!is_null($useragent)){
  75. curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  76. }
  77. //execute HTTP GET
  78. $webpage = curl_exec($ch);
  79. // Fetch statistic from download through available variables
  80. //CURLINFO_TOTAL_TIME - Total transaction time in seconds for last transfer
  81. $CURLINFO_TOTAL_TIME = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
  82. //CURLINFO_NAMELOOKUP_TIME - Time in seconds until name resolving was complete
  83. $CURLINFO_NAMELOOKUP_TIME = curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME);
  84. //CURLINFO_CONNECT_TIME - Time in seconds it took to establish the connection
  85. $CURLINFO_CONNECT_TIME = curl_getinfo($ch, CURLINFO_CONNECT_TIME);
  86. //CURLINFO_PRETRANSFER_TIME - Time in seconds from start until just before file transfer begins
  87. $CURLINFO_PRETRANSFER_TIME = curl_getinfo($ch, CURLINFO_PRETRANSFER_TIME);
  88. //CURLINFO_STARTTRANSFER_TIME - Time in seconds until the first byte is about to be transferred
  89. $CURLINFO_STARTTRANSFER_TIME = curl_getinfo($ch, CURLINFO_STARTTRANSFER_TIME);
  90. //CURLINFO_REDIRECT_TIME - Time in seconds of all redirection steps before final transaction was started
  91. $CURLINFO_REDIRECT_TIME = curl_getinfo($ch, CURLINFO_REDIRECT_TIME);
  92. //CURLINFO_SIZE_UPLOAD - Total number of bytes uploaded
  93. $CURLINFO_SIZE_UPLOAD = curl_getinfo($ch, CURLINFO_SIZE_UPLOAD);
  94. //CURLINFO_SIZE_DOWNLOAD - Total number of bytes downloaded
  95. $CURLINFO_SIZE_DOWNLOAD = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
  96. //CURLINFO_SPEED_DOWNLOAD - Average download speed
  97. $CURLINFO_SPEED_DOWNLOAD = curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD);
  98. //CURLINFO_SPEED_UPLOAD - Average upload speed
  99. $CURLINFO_SPEED_UPLOAD = curl_getinfo($ch, CURLINFO_SPEED_UPLOAD);
  100. //CURLINFO_HEADER_SIZE - Total size of all headers received
  101. $CURLINFO_HEADER_SIZE = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  102. //CURLINFO_REQUEST_SIZE - Total size of issued requests, currently only for HTTP requests
  103. $CURLINFO_REQUEST_SIZE = curl_getinfo($ch, CURLINFO_REQUEST_SIZE);
  104. //CURLINFO_CONTENT_LENGTH_DOWNLOAD - content-length of download, read from Content-Length: field
  105. $CURLINFO_CONTENT_LENGTH_DOWNLOAD = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
  106. //CURLINFO_CONTENT_LENGTH_UPLOAD - Specified size of upload
  107. $CURLINFO_CONTENT_LENGTH_UPLOAD = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_UPLOAD);
  108. // CLOSE CURL OBJECT
  109. curl_close($ch);
  110. // PRINT OUT STATISTICS
  111. $ret=sprintf("CURLINFO_TOTAL_TIME:%0.6f CURLINFO_NAMELOOKUP_TIME:%0.6f CURLINFO_CONNECT_TIME:%0.6f CURLINFO_PRETRANSFER_TIME:%0.6f CURLINFO_STARTTRANSFER_TIME:%0.6f CURLINFO_REDIRECT_TIME:%0.6f CURLINFO_SIZE_UPLOAD:%d CURLINFO_SIZE_DOWNLOAD:%d CURLINFO_SPEED_DOWNLOAD:%d CURLINFO_SPEED_UPLOAD:%d CURLINFO_HEADER_SIZE:%d CURLINFO_REQUEST_SIZE:%d CURLINFO_CONTENT_LENGTH_DOWNLOAD:%d CURLINFO_CONTENT_LENGTH_UPLOAD:%d\n",$CURLINFO_TOTAL_TIME,$CURLINFO_NAMELOOKUP_TIME,$CURLINFO_CONNECT_TIME,$CURLINFO_PRETRANSFER_TIME,$CURLINFO_STARTTRANSFER_TIME,$CURLINFO_REDIRECT_TIME,$CURLINFO_SIZE_UPLOAD,$CURLINFO_SIZE_DOWNLOAD,$CURLINFO_SPEED_DOWNLOAD,$CURLINFO_SPEED_UPLOAD,$CURLINFO_HEADER_SIZE,$CURLINFO_REQUEST_SIZE,$CURLINFO_CONTENT_LENGTH_DOWNLOAD,$CURLINFO_CONTENT_LENGTH_UPLOAD);
  112. return $ret;
  113. }
  114. //////////////////////////////////////////////////////////////////////////////
  115. // MAIN PROGRAM
  116. //////////////////////////////////////////////////////////////////////////////
  117. /* do NOT run this script through a web browser */
  118. if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
  119. die("<br><strong>This script is only meant to run at the command line.</strong>");
  120. }
  121. $no_http_headers = true;
  122. /* display No errors */
  123. error_reporting(0);
  124. include_once(dirname(__FILE__) . "/../include/global.php");
  125. // If called directly from command line
  126. if (!isset($called_by_script_server)) {
  127. // read command line argumet with getopt
  128. $arrArgv = getopt("hU:P:u:p:A:t:c:");
  129. if (isset($arrArgv["h"])){
  130. help();
  131. return 1;
  132. }
  133. if (isset($arrArgv["U"])){
  134. $URL=$arrArgv["U"];
  135. }else{
  136. usage();
  137. return 1;
  138. }
  139. if (isset($arrArgv["P"])){
  140. $proxy=$arrArgv["P"];
  141. }else{
  142. $proxy=null;
  143. }
  144. if (isset($arrArgv["u"])){
  145. $proxy_username=$arrArgv["u"];
  146. }else{
  147. $proxy_username=null;
  148. }
  149. if (isset($arrArgv["p"])){
  150. $proxy_password=$arrArgv["p"];
  151. }else{
  152. $proxy_password=null;
  153. }
  154. if (isset($arrArgv["t"])){
  155. $timeout=$arrArgv["t"];
  156. }else{
  157. $timeout=BWTEST_TIMEOUT;
  158. }
  159. if (isset($arrArgv["A"])){
  160. $useragent=$arrArgv["A"];
  161. }else{
  162. $useragent=null;
  163. }
  164. // call appropriate function
  165. print call_user_func_array("ss_bwtest_curl", array ($URL,$proxy,$proxy_username,$proxy_password,$timeout,$useragent));
  166. }
  167. ?>