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!
In addition to the settings field configuration for Internal Web Checks, you can also execute your check using a Groovy script to collect and process HTTP data. Scripted Web Checks tend to be more flexible and are particularly useful for sites that use form-based authentication with dynamic tokens.
Web Checks that are executed via Groovy script will have at least one step, which will contain a Step Request script and a Step Response processor. What makes scripted checks unique is the ability to share contexts (i.e. a dynamic token for authentication) between steps. In this way, Step One can send a GET request to collect a site’s authenticating token and share the token in Step Two, which will authenticate into the site.
When creating or editing an Internal Web Check, you’ll notice that an additional tab titled Script is available under both the Request and Response sections. You can choose to manually add your request and response Groovy scripts directly into the text boxes found under both of these Script tabs or, as shown next, you can choose to first complete the fields under the Settings tab (e.g. HTTP Version, Method, Follow redirect, Authentication Required, Headers, HTTP Response Format, etc.) and then click the Generate Script from Settings button to have LogicMonitor auto-generate request and response scripts based on those settigs. The latter option produces a basic template for your Groovy scripts. (For more information on completing the fields found under the Settings tab for both the HTTP request and response, see Adding a Web Check).
The request script will use the Groovy API to get an HTTP response.
The following table lists the API commands you may use in your request Groovy script.
import com.logicmonitor.service.groovyapi.AuthType; import com.logicmonitor.service.groovyapi.LMRequest; LMRequest request = new LMRequest(); request.setAuthType(AuthType.BASIC) .setUsername("username") .setPassword("password") .followRedirect(false) .useHttp1_0() .needFullpageLoad(true); String jsonData = "{\"name\": \"value\"}" return LMHttpClient.request(request) .post(JSON, jsonData);
StatusCode status = LMResponse.statusMatch(200); LMResponse.setContext("body", Response.getBody()); return status;
return LMResponse.plainTextmatch(Response.getContext("exampletext"))
import static com.logicmonitor.service.groovyapi.StatusCode.*; import static com.logicmonitor.service.groovyapi.PostDataType.*; String jsonData = "{\"name\": \"value\"}"; return LMHttpClient.post(JSON, jsonData);
import com.logicmonitor.service.groovyapi.AuthType; import com.logicmonitor.service.groovyapi.LMRequest; LMRequest request = new LMRequest(); request.setAuthType(AuthType.BASIC) .setUsername("username") .setPassword("password") .followRedirect(false) .useHttp1_0() .needLoadFullPage(false); String jsonData = "{\"name\": \"value\"}" return LMHttpClient.request(request) .post(JSON, jsonData);
The response script will parse the HTTP response from your request script and apply any post processing methods (e.g. check status, check the HTTP response body, etc.).
The following table lists the API calls you may use in your request Groovy script.
In the event that you wanted to verify a response status of “302” for a site, use the following request:
import com.logicmonitor.service.groovyapi.LMRequest; LMRequest request = new LMRequest(); return LMHttpClient.request(request.followRedirect(false) .get());
You would write your response script as follows:
return LMResponse.statusMatch(302);
Below is a full two-step script used for verifying the availability of a messaging service. This particular script uses a dynamic token shared between steps one and two of the Internal Web Check via the setContext and getContext commands in order to authenticate into the site. The post-processing method looks for the presence of “Welcome” in the HTTP response as a means of verifying the site’s availability.
In This Article