TIBCO LogLogic LMI - How to run REST API calls using 'curl' command

TIBCO LogLogic LMI - How to run REST API calls using 'curl' command

book

Article ID: KB0073074

calendar_today

Updated On:

Products Versions
TIBCO LogLogic Log Management Intelligence 6.1.0, 6.1.1, 6.2.1, 6.3.0 and 6.3.1

Description

REST API calls can be run from the Swagger page or using java / python to develop REST API clients. For this purpose - however - some users may prefer to develop a BASH script instead. In this case they can leverage the Linux command 'curl'.

Resolution

First of all, let us take a look at the options that 'curl' command offers:
-k, --insecure      Allow connections to SSL sites without certs (H)
-d, --data is the same as --data-ascii. To post data purely binary, you should instead use the --data-binary option.
-L, --location      Follow redirects (H)
     --location-trusted like --location and send auth to other hosts (H)
-X, --request COMMAND  Specify request command to use
     --resolve HOST:PORT:ADDRESS  Force resolve of HOST:PORT to ADDRESS
     --retry NUM   Retry request NUM times if transient problems occur
     --retry-delay SECONDS When retrying, wait this many seconds between each
     --retry-max-time SECONDS  Retry only within this period
-H, --header LINE   Custom header to pass to server (H)


Given the above, a template to run a query in LMI using 'curl' would be:
 
# curl --insecure --location --request POST 'https://<destination_ip>:<port>/path/to/rest/api' --header 'Authorization: Basic <encrypted_authorization_string>' --header 'Content-Type: application/json' -d <query_in_json_format>
 
This command will connect to the LMI using a generated '<encrypted_authorization_string>' and will run a query via REST API . Its results will be returned within a JSON string.

To build up the command string we need first to generate the authorization string. Given a couple 'username:password' like 'admin:admin' we can generate the authorization string with following command:

 
# echo -ne "admin:admin" | base64 --wrap 0
YWRtaW46YWRtaW4=

 
Having the authorization string, we can now build up the command. In this example we will connect to an IP address like 192.168.56.10 on port 9681, and will call the 'quickquery' API ('api/v2/quickquery') which will return a JSON string containing results:
 
# curl --insecure --location --request POST 'https://192.168.56.10:9681/api/v2/quickquery' --header 'Authorization: Basic YWRtaW46YWRtaW4=' --header 'Content-Type: application/json' -d '{  "query": "USE Microsoft_Windows_Events | COLUMNS ll_eventID, count(*) as TOTAL | GROUP BY ll_eventID | sys_eventTime in -5m"}'
 
The outcome of above command would be something like:
 
{"rows":[[1003,1],[4672,1],[1066,1],[900,1],[902,1],[4634,4],[4624,1]],"errorsOrWarnings":[{"text":"This aggregation query is not optimized. Click on optimize magic button to optimize this query","severity":"WARNING"},{"text":"Some period of time in your time range is not indexed, the result may not be complete.","severity":"INFOWARNING"}],"columns":[{"name":"ll_eventID","type":"INT"},{"name":"TOTAL","type":"LONG"}],"timeSpent":1365}
 
To get a more readable output we can format the JSON string with python by calling a module (json.tool) to format the output as follows:
 
# curl --insecure --location --request POST 'https://192.168.1.89:9681/api/v2/quickquery' --header 'Authorization: Basic YWRtaW46YWRtaW4zMjE=' --header 'Content-Type: application/json' -d '{  "query": "USE Microsoft_Windows_Events | COLUMNS ll_eventID, count(*) as TOTAL | GROUP BY ll_eventID | sys_eventTime in -5m"}' | python -m json.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   583  100   455  100   128    316     89  0:00:01  0:00:01 --:--:--   316
{
    "columns": [
        {
            "name": "ll_eventID",
            "type": "INT"
        },
        {
            "name": "TOTAL",
            "type": "LONG"
        }
    ],
    "errorsOrWarnings": [
        {
            "severity": "WARNING",
            "text": "This aggregation query is not optimized. Click on optimize magic button to optimize this query"
        },
        {
            "severity": "INFOWARNING",
            "text": "Some period of time in your time range is not indexed, the result may not be complete."
        }
    ],
    "rows": [
        [
            4672,
            2
        ],
        [
            1003,
            1
        ],
        [
            1066,
            1
        ],
        [
            900,
            1
        ],
        [
            4776,
            1
        ],
        [
            902,
            1
        ],
        [
            4634,
            4
        ],
        [
            4624,
            2
        ],
        [
            4648,
            1
        ]
    ],
    "timeSpent": 1303
}

 
Running this command within a BASH script will easily allow you to manipulate REST API calls data.
 

Issue/Introduction

This article explains how to use command 'curl' to run REST API calls in LMI