Blog Post

Tech Talk
2 MIN READ

Retrieving data from an external API via a Groovy Scripted module

kendall's avatar
kendall
Icon for LM Champion rankLM Champion
2 years ago

1) Using Expert mode, define a resource as the hostname of the api in question, i.e. api.someapinamehere.com. For the purposes of this example, I'm going to make a call to worldtimeapi.org for data on the timezone America/Chicago to determine if daylight savings time is in effect. 

2) Next, choose an available collector and group (optional) and click save. Note: normally you would also add your api user name (if any) and api token as properties, but in this case, it's not necessary. 

0rlpuhzNWHt2XvZibVtqkxkiis4jorq_Pxs5LvrhyByJ-hx3AZBJINduUIfnk-Gv3_XTIn42xr7M_FyDem814NPymv6U798Wrt3EWsmMG5n6AIy8jaZED4EvbK9kOi6A1Zhu-gXidREG-KK3uyUb91Q

2) Next, go to Settings -> LogicModules -> DataSources and click Add -> Datasource

pd97Y17j-NF8VOAwJExEOm-SCmkWyRTNmYK7m8UHygD4t2HkccBbty80CN6PuxInKYiW1Wvmvojo5yQNrqanxzf3Ajk91zKd-Ls_SDsn6R4IaHU7ePtFENdyuCDqGDC6KSwumxGtCxMLnO4xmwpY3c0

3) Follow the steps below to adjust the DataSource:

WpSqEj71PuVl1UXkFJaRXf52zD_E7HiJkosNo5k_O1MYO0LvTomMPI12cSCaWJSwnIqLK3wLF3Wst_bySLsjquOP6bvzR4kf1oZlUq_aIHBqzEj6PjqeL1OWukW6-gEApRL8ETNX_TmgTBQrILZaFbA

16z0YbQebaYSjM71egZidooHWqJvbVYKNrTrdWhcVKWwsQ8CpdqY9TNiU049dtp20ArXMyCZoRfGxVR-FWC-7uAp_55dEgFD2vMMUIXNaG3N0MPZ1ksMI1AkFdbeeXHdh_p0Hpx4Y4_cW1ADOHvmLKA

Y1NSRJZ4IBEBHe_sESiQjX6wZ1bhD7oAnqxhVcEmmu7Q_DBWtALuBonS_YyvdQuMgpO6F6rbIoIR-cieQa2T2lBOhRTTJgXfoKDk7SNi3nuEayeV1g4-7ldF_4JWCLLbgA_ZkOBwtSTwUEBMnASP9bM

4) Insert this script in the text box entitled Groovy Script under Collector Attributes

import com.santaba.agent.groovyapi.expect.Expect;

import com.santaba.agent.groovyapi.snmp.Snmp;

import com.santaba.agent.groovyapi.http.*;

import com.santaba.agent.groovyapi.jmx.*;

import org.xbill.DNS.*;

import groovy.json.*;

//Defines host as the name of the resource added, in this case worldtimeapi.org

def host = hostProps.get("system.hostname");

//Defines the path to endpoint

def endpointUrl = "/api/timezone/America/Chicago"

//Defines port. SSL would require 443.

def port = 80

//Opens connection

def httpClient = HTTP.open(host, port)

//Get response

def response = httpClient.get(endpointUrl)

//Define status code

def statusCode = httpClient.getStatusCode()

//Close connection

httpClient.close()

//Extract values or handle error

if(statusCode == 200){

    response = new JsonSlurper().parseText(httpClient.getResponseBody())

    if(response['dst'] == true){

        status = 1

        println("dstStatus=${status}")

        } else {

            status = 0

            println("dstStatus=${status}")    

            }

    } else {

        println("Your HTTP get request was not successful. StatusCode=${statusCode}")

        }


 

wflIGyTKQ8xLj2rlTB_Mt_xVI7WyHo4d_CB7X6z41RK66jJj3Hr-s-NnWVt8xob-ARMEwGAwt_5hbAe0b93nIDCxTZ2M747x8iiCMPLSKk7Ggu8S-aSrcoO5dg9hwtlWX0NBzal8GPWRtcIa36LWVYM

5) Add Normal datapoint named dstStatus

xTYZ6Ry7hGRQPITD1Inm9KwP9xCYOW4hcUNNhS5loMXr3QZqz6pxoHNelGWvd-O-MgDo4edbEFXqNz6ZH57Oz3aYK15M0PAxGB1xLVe8Sg8YWtsko1723ymXB8BTqUIVRh00BJXp3JEICEoukH7-qXY

6) Save Module

1yRiMNvEOMMI8_86Ec3de689pp9O-HmX06cdgSpZwF0T3Hnxc7PmkhfYo6ZLpO6EtQXdwpADp4zLI5N5LUxJ_gmBtUKCGbDMlPRk3DIbCOfI1murV6pJUEiRi2oCp5viVlPl0n6JrVz1FOnd-rlaQ5E

7) You will see data painted in your portal for the device worldtimeapi.org under the name of the DataSource you created.

tcw_ymld1nMEGqOlrauTPXynjWVn3UGQEgTwUrWFHgt61wkIA1xBNxnfyb1wScCp4fGS1UKvaK_aXFqznNhmZIwU3jRTbMlljhwpm31Rn6HISaxYzHWki_ykRIBSNZ289ZrmJsyEYl-Ae5D7L9UmR5g

You can learn more about this on this recently updated support doc: https://www.logicmonitor.com/support/terminology-syntax/scripting-support/access-a-website-from-groovy# 

Published 2 years ago
Version 1.0
No CommentsBe the first to comment