Skip to content

Using Adagios WEB API

T.J. Yang edited this page Feb 5, 2018 · 10 revisions

Adagios has a number of "rest style" web interfaces that are designed to make it easy for remote programs to interact with nagios.

To browse what it available open up the following urls in browser:

  • /rest/okconfig - wrapper around okconfig api
  • /rest/pynag - useful methods for viewing and editing configuration files
  • /rest/status - Good for interactive with status interface and sending commands to nagios

Formats supported include:

  • Text
  • JSON
  • XML

In general the following quirks apply

  • Access variables by using HTTP GET without querystrings
  • Access python methods by using HTTP POST and parameters as querystrings

Exploring the available REST modules and their GET/POST actions

  1. Browse to "/rest"

Adagios Rest page

  1. Choose one of the loaded REST modules f.ex. pynag

Adagios Rest Page for pynag

  1. Choose one operation to test f.ex. dnslookup

Adagios Rest API dnslookup

Example using curl

pynag REST API

  • dnslookup:

    • curl
    • output:
[root@nagios01 ~]# ./dnslookup.py
{
    "addresslist": [
        "192.168.1.1"
    ],
    "aliaslist": [],
    "host": "gateway.test.lan"
}
[root@nagios01 ~]#

  • Print all hosts:
import requests
import json

url = "http://localhost:8000/rest/pynag/json/get_objects?object_type=host"

result = requests.get(url)
json_data = result.content
hosts = json.loads( json_data )

for i in hosts:
  print i
  • Add a host:
curl -X POST \
-d "host_name=ipa1.test.net" \
-d "address=None" \
-d "group_name=None" \
-d "templates=None" \
-d "use=None" \
-d "alias=None" \
-d "host_template=host" \
-d "force=True" \
http://nagios01.test.lan:8000/rest/okconfig/json/addhost
  • Delete a host:
    • Before
    • curl command
curl -X POST \
-d "host_name=ipa1.test.lan" \
-d "recursive=True" \
http://nagios01:8000/rest/okconfig/json/removehost
  • Trigger an alert on a host ping:
TBC
  • Trigger an alert on a service check:
TBC

Example using python

pynag REST API

  • dnslookup:

    • python code
    #! /usr/bin/python
    import requests
    import json
    
    url = "http://192.168.1.31:8000/rest/pynag/json/dnslookup?host_name=gateway.test.lan"
    
    result = requests.get(url)
    json_data = result.content
    print json_data
    
    • output:
[root@nagios01 ~]# ./dnslookup.py
{
    "addresslist": [
        "192.168.1.1"
    ],
    "aliaslist": [],
    "host": "gateway.test.lan"
}
[root@nagios01 ~]#

  • Print all hosts:
import requests
import json

url = "http://localhost:8000/rest/pynag/json/get_objects?object_type=host"

result = requests.get(url)
json_data = result.content
hosts = json.loads( json_data )

for i in hosts:
  print i
  • Add a host:
TBC
  • Delete a host:
TBC
  • Trigger an alert on a host ping:
TBC
  • Trigger an alert on a service check:
TBC

Example using javascript

The web api includes a javascript library that can be used as such:

  • /rest/status.js
  • /rest/pynag.js
  • /rest/okconfig.js

Example code:

    <script src="/rest/status.js"></script>
    <script src="/rest/pynag.js"></script>
    <script src="/rest/adagios.js"></script>

    /* get a list of top_alert_producers */
    $(document).ready(function() {
        top_alert_producers()
        .done( function(data) {
           console.log("Top alert producers:");
           console.log(data);
        })
        .fail( function(data) {
            alert("Error from rest api: " + data);
        });

Extending the API

Server-side the methods are written in pure python, and REST calls and javascript api's are generated by inspecting the correct modules.

Adding a new method should be as simple as creating function in the appropriate module.

The only caveat is that functions should return data that is more or less serializable with JSON and XML (i.e. use strings, arrays, dicts, avoid complex objects)

Files to edit are as follows:

  • /rest/pynag: ./adagios/misc/helpers.py
  • /rest/okconfig: init.py in okconfig
  • /rest/status: ./adagios/status/rest.py