Skip to content

Usage examples

The reference says which parameters each operation accepts; this chapter shows how to use them together. The examples have really been run, from start to finish, against a development console (version 8.0NG.805) with test data that was deleted afterwards. The responses are trimmed to the essentials, and your identifiers and dates will be different.

A lot of space goes to list filters, because the reference describes the fields but cannot show how they combine, and that is where most time is lost.

Before you start

Requirement Detail
Credential An API token or the access_token of a session (chapter 13.1).
Tools curl and jq to format the responses.
Permissions A user who can create and delete agents, modules, users and alerts. The write examples were run with admin.

All the examples share these variables and this function, which adds the authentication header and the content type:

export PANDORA="http://localhost/pandora_console/api/v2"
export TOKEN="<API token or access_token>"
api() { curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" "$@"; }

Creating an agent and seeing the full response

The first call is also the most revealing one: create an agent and look at everything it returns. Only alias and idGroup are needed (further down you can see what happens if one is missing).

api -X POST "$PANDORA/agent" -d '{"alias": "docs-test-first", "idGroup": 13}'

The response is a 200 with the full agent, not just its identifier. This is it as is, unfiltered. The API returns it on a single line; here it is indented to make it readable:

{
  "idAgent": 10,
  "idAgentNode": null,
  "serverId": null,
  "name": "4fac860064de6c3ea0cc7294c7e0993fd6a407ab31b58db194bc4e3a4ae90d1b",
  "address": "",
  "comments": "",
  "idGroup": 13,
  "lastContact": null,
  "mode": "LEARNING",
  "interval": 300,
  "idOs": 0,
  "osVersion": "",
  "osName": null,
  "osImagePath": null,
  "agentVersion": "",
  "lastRemoteContact": null,
  "disabled": 0,
  "remote": 0,
  "idParent": 0,
  "customId": "",
  "serverName": "",
  "cascadeProtection": 0,
  "cascadeProtectionModule": 0,
  "timezoneOffset": 0,
  "iconPath": null,
  "updateGisData": 1,
  "urlAddress": null,
  "quiet": 0,
  "normalCount": null,
  "warningCount": null,
  "criticalCount": null,
  "unknownCount": null,
  "notinitCount": null,
  "totalCount": null,
  "firedCount": null,
  "updateModuleCount": 0,
  "updateAlertCount": 0,
  "updateSecondaryGroups": 0,
  "alias": "docs-test-first",
  "aliasAsName": 0,
  "safeModeModule": 0,
  "cps": 0,
  "cpsInhibit": 0,
  "satelliteServer": 0,
  "fixedIp": 0,
  "disabledByDowntime": 0,
  "vulScanEnabled": "DEFAULT",
  "ignoreUnknown": 0,
  "balanceType": "BALANCED",
  "autoHa": 1,
  "extraData": "",
  "quietByDowntime": -1,
  "idFimPolicy": 0
}

All 53 fields of the agent always come back. What you did not set takes its default value (interval is 300, mode is LEARNING) and what does not exist yet comes back as null (lastContact and the status counters, because the agent has not sent any data). The name is an internal identifier (a hash); the name you see in the console is alias.

The other examples only need the identifier, or a few fields, so they filter the response with jq. The full response is always this one.

Preparing the data for the filters

The filter examples need a few agents. Create these five; all of them carry docs-test in the alias so you can find and delete them easily:

for spec in "docs-test-web-01 13 300" "docs-test-web-02 13 60" "docs-test-db-01 8 300" \
            "docs-test-net-01 9 120" "docs-test-old-01 2 300"; do
  set -- $spec
  api -X POST "$PANDORA/agent" -d "{\"alias\": \"$1\", \"idGroup\": $2, \"interval\": $3}" \
    | jq -c '{idAgent, alias, idGroup}'
done
{"idAgent":9,"alias":"docs-test-web-01","idGroup":13}
{"idAgent":10,"alias":"docs-test-web-02","idGroup":13}
{"idAgent":11,"alias":"docs-test-db-01","idGroup":8}
{"idAgent":12,"alias":"docs-test-net-01","idGroup":9}
{"idAgent":13,"alias":"docs-test-old-01","idGroup":2}

Listing with pagination

The simple list is a GET. With sizePage you ask for pages of that size, and page starts at 0:

api "$PANDORA/agent/list?page=0&sizePage=2" \
  | jq -c '.paginationData, (.data[] | {idAgent, alias, idGroup})'
{"totalPages":4,"sizePage":2,"currentPage":0,"totalRegisters":7,"totalRegistersPage":2,"unfilteredTotalRegisters":7}
{"idAgent":13,"alias":"docs-test-old-01","idGroup":2}
{"idAgent":12,"alias":"docs-test-net-01","idGroup":9}

There are seven agents (the five new ones plus the two the console comes with) and, with pages of two, totalPages is 4. Unsorted, this list returns the newest agent first.

Filtering by a field and choosing which fields to receive

To filter you use POST with the filter in the body. Every field you write is an exact-equality condition. With requestedFields you receive only the fields you ask for, and the response weighs much less than the 53 fields of a full agent:

api -X POST "$PANDORA/agent/list?page=0&sizePage=10" \
  -d '{"idGroup": 13, "requestedFields": ["idAgent", "alias", "idGroup"]}' \
  | jq -c '.paginationData, .data[]'
{"totalPages":1,"sizePage":10,"currentPage":0,"totalRegisters":2,"totalRegistersPage":2,"unfilteredTotalRegisters":7}
{"idAgent":10,"alias":"docs-test-web-02","idGroup":13}
{"idAgent":9,"alias":"docs-test-web-01","idGroup":13}

totalRegisters counts what matches the filter (2) and unfilteredTotalRegisters what there would be without filtering (7).

Combining conditions with operators

When equality is not enough, fieldConditions accepts operators. All the conditions are joined with AND. This example looks for the agents whose alias contains docs-test and that are in groups 8 or 9, sorted by alias:

api -X POST "$PANDORA/agent/list?page=0&sizePage=10&sortField=alias&sortDirection=ASC" \
  -d '{"requestedFields": ["idAgent", "alias", "idGroup"],
       "fieldConditions": [
         {"field": "alias", "operator": "LIKE", "value": "docs-test"},
         {"field": "idGroup", "operator": "IN", "value": [8, 9]}
       ]}' | jq -c '.paginationData, .data[]'
{"totalPages":1,"sizePage":10,"currentPage":0,"totalRegisters":2,"totalRegistersPage":2,"unfilteredTotalRegisters":7}
{"idAgent":11,"alias":"docs-test-db-01","idGroup":8}
{"idAgent":12,"alias":"docs-test-net-01","idGroup":9}

Which fields do conditions accept

Not every field of an agent works in fieldConditions (nor in requestedFields, multipleSearch or sortField): only a subset. interval, for example, does not. If you use one that is not valid, the response is a 400 that lists the valid ones:

api -X POST "$PANDORA/agent/list" \
  -d '{"fieldConditions": [{"field": "interval", "operator": "=", "value": 300}]}'
{"error":"Field interval is not a valid filter field. Valid fields: idAgent, name, alias, address, idGroup, disabled, idOs, lastContact, osName, osImagePath, normalCount, warningCount, criticalCount, unknownCount, notinitCount, totalCount, firedCount"}

It is a good trick to discover the fields of any list: ask for one that does not exist and read the message. By contrast, the simple filter of the body ({"interval": 300}) does accept any field of the entity, and one that does not exist gives Field: <name> is not a valid parameter.

How each filter behaves

The table collects what each filter returned over the five test agents. Look at the first two rows: the simple filter does not search by pieces.

Request body Agents returned
{"alias": "docs-test-web-01"} docs-test-web-01
{"alias": "docs-test-web"} None: it is exact equality, not a prefix.
{"idGroup": 13} docs-test-web-01, docs-test-web-02
{"freeSearch": "web"} docs-test-web-01, docs-test-web-02 (partial match across several fields)
{"multipleSearch": {"field": "idGroup", "data": [8, 9]}} docs-test-db-01, docs-test-net-01 (any of the values)
{"multipleSearchString": {"field": "alias", "data": ["docs-test-web-01", "docs-test-net-01"]}} docs-test-net-01, docs-test-web-01
Condition alias LIKE web docs-test-web-01, docs-test-web-02: it contains the text.
Condition alias LIKE docs-test-web% None: the % is not a wildcard, it is searched as is.
Condition alias REGEXP ^docs-test-web docs-test-web-01, docs-test-web-02
Condition idGroup IN [8, 9] docs-test-db-01, docs-test-net-01
Condition idGroup BETWEEN [8, 9] docs-test-db-01, docs-test-net-01
Condition idGroup gt 9 (and alias LIKE docs-test) docs-test-web-01, docs-test-web-02

LIKE is the point that confuses most: it means "contains", not the SQL LIKE, and there is no need (nor use) to wrap the text in %.

Sorting and paging

sortField and sortDirection (ASC or DESC, case-insensitive) go in the address. This request asks for the second page (page=1) of two items, sorted by alias from highest to lowest:

api -X POST "$PANDORA/agent/list?page=1&sizePage=2&sortField=alias&sortDirection=DESC" \
  -d '{"requestedFields": ["alias"],
       "fieldConditions": [{"field": "alias", "operator": "LIKE", "value": "docs-test"}]}' \
  | jq -c '.paginationData, .data[]'
{"totalPages":3,"sizePage":2,"currentPage":1,"totalRegisters":5,"totalRegistersPage":2,"unfilteredTotalRegisters":7}
{"alias":"docs-test-old-01"}
{"alias":"docs-test-net-01"}

To walk through a set in arbitrary windows, use the offset mode (limit and offset, inside the body). Here, two items starting from the second; note that currentPage becomes null and limit and offset appear:

api -X POST "$PANDORA/agent/list" \
  -d '{"limit": 2, "offset": 1, "requestedFields": ["alias"],
       "fieldConditions": [{"field": "alias", "operator": "LIKE", "value": "docs-test"}]}' \
  | jq -c '.paginationData, .data[]'
{"totalPages":3,"sizePage":2,"currentPage":null,"totalRegisters":5,"totalRegistersPage":2,"unfilteredTotalRegisters":7,"limit":2,"offset":1}
{"alias":"docs-test-net-01"}
{"alias":"docs-test-db-01"}

Filtering events

Events do not use fieldConditions: they have their own filters (severity, status, search, idAgent, eventType…), and an option of their own, dateRange. This one looks for the critical, new events of the last 24 hours, from newest to oldest:

api -X POST "$PANDORA/event/list?page=0&sizePage=2&sortField=idEvent&sortDirection=DESC" \
  -d '{"severity": [4], "status": "new", "dateRange": "{\"preset\": {\"value\": \"last_24_hours\"}}"}' \
  | jq -c '.paginationData, (.data[] | {idEvent, severity, status, event})'
{"totalPages":7,"sizePage":2,"currentPage":0,"totalRegisters":13,"totalRegistersPage":2,"unfilteredTotalRegisters":13}
{"idEvent":30,"severity":"CRITICAL","status":"NEW","event":"Pandora DB maintenance tool has never been run."}
{"idEvent":29,"severity":"CRITICAL","status":"NEW","event":"Pandora DB maintenance tool has never been run."}

Three details you cannot guess:

  • dateRange is a text that contains JSON, not a date. Hence the escaped quotes in the example. A value that is not JSON gives Invalid date range format. Must be valid JSON.
  • Severity is filtered with a list of numbers ([4] is critical, [0] maintenance); when reading them, the event carries the name (CRITICAL).
  • The status accepts new, validated, inprocess, not_validated, not_in_process and all.

dateRange accepts a preset period or a start and an end:

Form Example
Preset {"preset": {"value": "last_24_hours"}}. Values: today, this_week, this_month, last_15_minutes, last_30_minutes, last_1_hours, last_24_hours, last_7_days, last_30_days, last_60_days, last_90_days, last_1_year.
Relative {"start": {"mode": "relative", "value": 2, "unit": "hours"}, "end": {"mode": "now"}}. Units: seconds, minutes, hours, days, weeks, months, years.
Absolute {"start": {"mode": "absolute", "value": "2020-01-01 00:00:00"}, "end": {"mode": "absolute", "value": "2020-12-31 23:59:59"}}

An unknown preset value gives Invalid date range values.

Creating, reading, changing and deleting an agent

Creating an agent requires alias and idGroup; if one is missing, the 400 says which:

api -X POST "$PANDORA/agent" -d '{"idGroup": 13}'
api -X POST "$PANDORA/agent" -d '{"alias": "docs-test-crud"}'
{"error":"Agent alias is missing"}
{"error":"Agent group is missing"}

With both, the agent is created. The response is the one you already saw in the first example, the full agent; here only its identifier is kept for the following calls:

ID=$(api -X POST "$PANDORA/agent" -d '{"alias": "docs-test-crud", "idGroup": 13}' | jq -r .idAgent)

PUT changes only the fields you send; the rest stays as it was:

api -X PUT "$PANDORA/agent/$ID" -d '{"comments": "created from the docs", "interval": 120}' \
  | jq -c '{idAgent, alias, comments, interval, idGroup}'
{"idAgent":14,"alias":"docs-test-crud","comments":"created from the docs","interval":120,"idGroup":13}

Deleting it answers 200 with the body null, and asking for it again gives a 404:

api -X DELETE "$PANDORA/agent/$ID" -w 'HTTP %{http_code}\n'
api "$PANDORA/agent/$ID" -w ' HTTP %{http_code}\n'
nullHTTP 200
{"error":" not found"} HTTP 404

Creating a module

A module hangs off an agent. The required fields are idAgent, name, idModule (the server type of the module: 1 in this example) and idModuleType (the data type: 1 is generic_data; GET /moduleType/list shows the others). Without idModule the response is Unsupported module and module type combination.

AG=$(api -X POST "$PANDORA/agent" -d '{"alias": "docs-test-mod-agent", "idGroup": 13}' | jq -r .idAgent)
api -X POST "$PANDORA/agentModule" \
  -d "{\"idAgent\": $AG, \"name\": \"docs-test-module\", \"idModule\": 1, \"idModuleType\": 1}" \
  | jq -c '{idAgentModule, idAgent, idModuleType, name}'
{"idAgentModule":92,"idAgent":15,"idModuleType":1,"name":"docs-test-module"}

To find the modules of an agent, a filtered list; and to know how it is doing, its state:

MOD=$(api -X POST "$PANDORA/agentModule/list" \
  -d "{\"idAgent\": $AG, \"requestedFields\": [\"idAgentModule\"]}" | jq -r '.data[0].idAgentModule')
api "$PANDORA/agentModule/$MOD/agentModuleState" | jq -c '{idAgentModule, status, data}'
{"idAgentModule":92,"status":4,"data":""}

Status 4 means "no data", which is normal in a newly created module that has not received any yet. Deleting the agent (DELETE /agent/$AG) also deletes its modules.

Creating a user

This shows clearly that the swagger does not mark every required field: the reference only points out fullName, but the server demands more. The messages ask for each one in turn (the first, without idUser; the second, without password; the third, without passwordValidate):

api -X POST "$PANDORA/user" -d '{"fullName": "Docs Test"}'
api -X POST "$PANDORA/user" -d '{"idUser": "docs-test-user", "fullName": "Docs Test"}'
api -X POST "$PANDORA/user" \
  -d '{"idUser": "docs-test-user", "fullName": "Docs Test", "password": "Docs-test-Pass-1"}'
{"error":"IdUser is missing"}
{"error":"Password is missing"}
{"error":"PasswordValidate is missing"}

Four fields are needed: idUser, fullName, password and passwordValidate (the repeated password). The reference marks only one; the server is the one that tells the truth:

api -X POST "$PANDORA/user" \
  -d '{"idUser": "docs-test-user", "fullName": "Docs Test", "password": "Docs-test-Pass-1", "passwordValidate": "Docs-test-Pass-1"}' \
  | jq -c '{idUser, fullName, isAdmin}'
{"idUser":"docs-test-user","fullName":"Docs Test","isAdmin":false}

Creating an alert action

An alert action needs a command. The command is created first and then the action that uses it:

CMD=$(api -X POST "$PANDORA/alert/command" -d '{"name": "docs-test-command", "command": "echo _field1_"}' | jq -r .idAlertCommand)
ACT=$(api -X POST "$PANDORA/alert/action" -d "{\"name\": \"docs-test-action\", \"idAlertCommand\": $CMD}" | jq -r .idAlertAction)

Since both return their identifier, they can be chained. To clean up, delete the action first and then the command.

Cleaning up

The examples leave test data behind. All of it carries docs-test in the name, so it can be found with a filter and deleted:

for id in $(api -X POST "$PANDORA/agent/list" \
  -d '{"requestedFields": ["idAgent"], "fieldConditions": [{"field": "alias", "operator": "LIKE", "value": "docs-test"}]}' \
  | jq -r '.data[].idAgent'); do api -X DELETE "$PANDORA/agent/$id"; done

Creating or deleting agents also generates system events (Agent [...] created by API). Search for them with {"search": "docs-test"} in POST /event/list and delete them with DELETE /event/{idEvent}.

The errors you have seen

Code Message Cause
400 Agent alias is missing A required field is missing (the message says which).
400 Field interval is not a valid filter field. Valid fields: … A field that does not support that option; the message lists the valid ones.
400 Field: nope is not a valid parameter A body field that does not exist in the entity.
400 Operator foo is not a valid filter operator Unknown operator.
400 Field condition operator IN expects an array value IN and NOT IN need a list.
400 Field sizePage must be an integer greater than or equal to 1 Invalid pagination.
400 limit/offset cannot be combined with page/sizePage The two pagination modes were mixed.
400 Node identity filters can only be used on a metaconsole serverId was used on a standalone console.
404 not found The item does not exist (the message starts with a space).

The rest of the codes (401, 403, 429, 500) are in API conventions.