1

I need to find a way to detect whether a website (a joseki end point) is overloaded or not. http://128.250.202.125:7001/joseki/oracle is always up, but when I submit a query, sometimes it is idling. (i.e. overloaded, rather than it is down)

My approach so far is to simulate a form submission using curl. if curl_exec return false, I know the website is overloaded.

The major problem is that I am not sure whether website overloading triggers 'FALSE return' or not. I can log the curl_exec's return using this method, but this the website going down.

<?php

$is_run = true;
if($is_run) {
    $url = "http://128.250.202.125:7001/joseki/oracle";

    $the_query = "
PREFIX dc:    <http://purl.org/dc/elements/1.1/>
PREFIX rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd:   <http://www.w3.org/2001/XMLSchema#>
PREFIX owl:   <http://www.w3.org/2002/07/owl#>
PREFIX fn:    <http://www.w3.org/2005/xpath-functions#>
PREFIX ouext: <http://oracle.com/semtech/jena-adaptor/ext/user-def-function#>
PREFIX oext:  <http://oracle.com/semtech/jena-adaptor/ext/function#>
PREFIX ORACLE_SEM_FS_NS: <http://oracle.com/semtech#timeout=100,qid=123>
SELECT ?sc ?c
WHERE 
  { ?sc rdfs:subClassOf ?c} 
    ";

    // Simulate form submission.    
  $postdata = http_build_query(array('query' => $the_query));
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $tmp_condi = curl_exec($curl);

    // After I submit a simulated form submission, and http://128.250.202.125:7001/joseki/oracle is
    // not responding (i.g. idling), does it definitely returns FALSE????
  if($tmp_condi === FALSE) {
    die('not responding');  
  }
    else {

    }

  curl_close($curl);
}

Solution

Able to solve it by adding the following, based on this: Setting Curl's Timeout in PHP

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
curl_setopt($ch, CURLOPT_TIMEOUT, 400); //timeout in seconds
Community
  • 1
  • 1
kenpeter
  • 7,404
  • 14
  • 64
  • 95
  • you can use `curl_error($curl)` after curl_exec to see if an error happened. – Asenar Oct 10 '14 at 00:03
  • @Asenar I already put logging into the code and I am waiting for the site to go down. Have you seen any site which is not responding, and curl_exec returns false? – kenpeter Oct 10 '14 at 00:19

2 Answers2

1

I need to find a way to detect whether a website is responding or not. My approach so far is to simulate a form submission using curl.

I'd rather do HTTP HEAD request (see docs) and check return code. You do not need any data returned so no point of sending POST request or fetching response. I'd also set shorten timeout for the request:

$ch = curl_init();
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);

curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$content = curl_exec($ch);
curl_close($ch);

if $http_status is 200 (OK) then remote end can perhaps be considered live.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
1

Yes, if the website doesnt respond for some time (set in CURLOPT_CONNECTIONTIMEOUT) it will trigger an error and curl_exec() will return false, in fact it will return false on any other error either, so your code will not actualy tell if the site is down or not.

dimention
  • 147
  • 10