» Access Website from groovy

There are two approaches to access websites via Groovy.

  • Simple access - pass a URL in to fetch the webpage
  • Transactional access - multi-step web transactions support (e.g. log into your Yahoo! mail account, download new emails, then log out).

Simple Website Access

There are two methods you can use to do simple website access:

  • HTTP.get(url) - pass an URL in. It returns the whole HTTP response (include the headers) as a string.
  • HTTP.post(url, payload, headers - pass an URL, the payload data, and HTTP headers in. It returns the whole HTTP response (include the headers) as a string.

Example 1. Retrieve the Yahoo! homepage

Ln 1  import com.santaba.collector.groovyapi.http.*;

Ln 2  def response = HTTP.get("http://www.yahoo.com");
Ln 3  println response

Ln 1 - To access Collector's HTTP capability, you need to put "import com.santaba.collector.groovyapi.http.*" in the begining of the script file.

Ln 2 - Use HTTP GET method to fetch the URL. HTTP.get() returns the whole HTTP response (include headers) from the server.

The figure above shows the output from the script.

Example 2. Use HTTP.post() to send form data to the server

Ln 1  import com.santaba.collector.groovyapi.http.*;

Ln 2  def headers = ["Content-Type": "application/x-www-form-urlencoded"]
Ln 3  response = HTTP.post("http://test.logicmonitor.com/santaba/rpc/getCollector",
         "c=demo&u=foo&p=bar&id=1", headers)
Ln 4  println response

The example above obtains the collector information from a LogicMonitor server.

Ln 1 -  "import com.santaba.collector.groovyapi.http.*" allows the script to access the collector's HTTP methods.

Ln 2 - Set HTTP header Content-Type to application/x-www.form-urlencoded because we want to pass the query as the HTTP POST payload.

Ln 3 - Sends the HTTP POST request to the server and get the response.

The figure above shows the output from the script.

Example 3. Retrieve RabbitMQ statistics with HTTP authentication

Ln 1 import com.santaba.collector.groovyapi.http.*;

Ln 2 def response = HTTP.get("http://127.0.0.1:55672/api/overview", 'guest', 'guest')
Ln 3 print response

RabbitMQ's management plugin lets users retrieve various metrics, but it requires the client authenticate using HTTP authentication mechanisms.

Ln 2 - we provide URL, username, and password.

Transactional Website Access

In some situations, to adequately monitor website performance, we need to send multiple requests to the web server in one session. For example, to download the new mails from your Yahoo!Mail account, you need to login, download email, then logout.

To support multi-step transactions, collectors export a Client object (can be obtained via HTTP.open()) that corresponds to a HTTP session. By calling Client.get()/post() methods (same parameters as HTTP.get()/post() methods), you can send multiple requests to the server in one session.

Example 1. Get LogicMonitor collector information

Ln 1  import com.santaba.collector.groovyapi.http.*;

Ln 2  cli = HTTP.open( "test.logicmonitor.com", 80)

Ln 3  response = cli.get("/santaba/rpc/signIn?c=demo&u=foo&p=bar");
Ln 4  if ( !(response =~ /status":200/) ) {
Ln 5      println "authentication fail"
Ln 6      return 1
Ln 7  }

Ln 8  response = cli.get("/santaba/rpc/getCollector?id=1");
Ln 9  println response

Ln 10 cli.get("/santaba/rpc/signOut")

Ln 11 return 0

Ln 1 - To access Collector's HTTP capability, you need to put "import com.santaba.collector.groovyapi.http.*" in the begining of the script file.

Ln 2 - call HTTP.open to get a Client object that corresponds to a HTTP session. There are two ways to call HTTP.open().

  • HTTP.open(host, port) - the collector will use HTTPS if port is 443. Otherwise, it will use normal HTTP.
  • HTTP.open(host, port, isSSL)

Ln 3 - send the first request to log into LogicMonitor server. Usually, the server will set up a cookie to identify the session.

Ln 4 - Ln 7 - check if we logged in successfully.

Ln 8 - send the second request to get the collector information

Ln 10 - send the third request to log out from the server.