Come join our live training webinar every other Wednesday at 11am PST and hear LogicMonitor experts explain best practices and answer common questions. We understand these are uncertain times, and we are here to help!
The LogicMonitor Collector has a number of helper methods to assist with using HTTP within Groovy. You might need to use Groovy / HTTP methods to:
Note that if you're writing a DataSource to pull data from a single webpage, you can do so with the Webpage collection mechanism.
Object Instantiation
HTTP.open(host, port, [isSSL]) instantiate an HTTP session object for use across multiple HTTP transactions
Object Methods
setAuthentication(user,pass) Set the authentication information for basic authentication for get and post methods
setConnectTimeout(milliseconds) sets the connection timeout for the initial TCP connection. Defaults to settings in the collector.conf file.
setReadTimeout(milliseconds) sets the read timeout (time to complete the request) Defaults to settings in the collector.conf file.
setFollowRedirect(follow) Normally the httpclient will transparently follow redirects. You can set this to false to not follow, and return the redirect headers.
setHTTPProxy(host, port [,user, pass]) Specify an HTTP proxy to use for object get and post requests. If user and password are passed with credentials, only support BASIC authentication method
get(url, [headers]) do an HTTP GET on the provided url and return the entire HTTP response
e.g. response=httpClient.get(url,["Accept" : "text/plain, text/html"])
post(url, payload, headers] do an HTTP POST to the provided url and return the entire response
getHeaders() return only the HTTP headers of the last call
getHeader(string) return a specific header named string from the last call. e.g. println httpClient.getHeader("Set-Cookie")
getResponseBody() return only the HTTP body of the last call
getStatusCode() return only the HTTP status code of the last call as an integer.
getTimeStatistics() returns an array of two longs, containing the connect time (the time to complete the TCP and HTTPS establishment – this is different than the time set in setConnectTimeout) and read time (total time to complete the request) of the last request in milliseconds.
close() close and release the HTTP object. If you fail to do so, the collector will clean up and release the resource for you – but it's always good practice to release resources
In this example, we do an HTTP POST to a server to authenticate a session. The session cookie will be automatically used in subsequent GET calls that use the same HTTP object.
// instantiate an http client object for the target system ip="mydevice.com" httpClient = HTTP.open(ip, 443); // use an authentication API call to initiate a session // specify the url to which we want to post url = "https://"+ip+"/api/v310/session/login"; def payload = '{"username":"myusername","typeId":"com.tintri.api.rest.vcommon.dto.rbac. RestApiCredentials","password":"mypassword"}'; // do the post def postResponse = httpClient.post(url, payload,["Content-Type":"application/json"]); // does the response indicate a successful authentication? if ( !(httpClient.getStatusCode() =~ /200/) ) { // no -- report an error, and return a non-zero exit code println "authentication failure"; return(1); } // we are now authenticated. Subsequent GETs with the httpClient will pass in the session cookie url="https://"+ip+"/api/info"; def getResponse=httpClient.get(url); // print some data println httpClient.getResponseBody();
import com.santaba.agent.groovyapi.http.*; // instantiate an http client object for the target system httpClient = HTTP.open("www.mysite.com",80); // set basic authentication credentials httpClient.setAuthentication("myusername","mypassword"); url = "https://www.mysite.com/httpgallery/authentication/authenticatedimage/figure1.gif"; def getResponse=httpClient.get(url) headers=httpClient.getHeaders(); println headers; times=httpClient.getTimeStatistics(); println "Connect Time "+times[0]; println "Read Time "+times[1]; httpClient.close(); return 0;
In This Article