Overview
This KB article provides examples on how to use the application programming interface (API) available in Nagios Network Analyzer. The API has many features that extend the functionality of Nagios Network Analyzer and will be demonstrated here.
API Access & Key/Token
To use the API your user account needs to be granted API access. A Nagios Network Analyzer administrator is able to grant this via Administration > Authentication > User Management.
A key will then exist for that user account. The user needs to login to Nagios Network Analyzer and in the top right corner click their name. This will present them with their profile and the API Access Key will be displayed, for example db44c379a5e13b62ce0bb528266dcd07424ab1f0.
This key is commonly referred to as the token and will for part of the URL request, for example:
http://10.25.5.70/nagiosna/index.php/api/system/memory_status?token=db44c379a5e13b62ce0bb528266dcd07424ab1f0
To make this KB article easy to read, the token XXXX will be used.
Usage
It's best to start off with a simple example. An API call is as simple as accessing a web page. If I point my web browser to the API URL it will return the information in a JSON object.
Here is an example that will get the product version information:
http://10.25.5.70/nagiosna/index.php/api/system/get_product_info?token=XXXX
The web browser will display something like this:
{"product":"Nagios Network Analyzer","release":231,"version":"2.3.1","version_major":"2","version_minor":"3.1","build_id":"BUILD_ID"}
Some web browser may detect that it is a JSON object and display the information in an easy to read format:
{
"product": "Nagios Network Analyzer",
"release": 231,
"version": "2.3.1",
"version_major": "2",
"version_minor": "3.1",
"build_id": "BUILD_ID"
}
However if you are making API calls then it's most likely you require the data in a script so you can manipulate the returned JSON object. This is most commonly done using a CURL XGET request, for example:
curl -XGET 'http://10.25.5.70/nagiosna/index.php/api/system/get_product_info?token=XXXX'
The output will be the JSON object:
{"product":"Nagios Network Analyzer","release":231,"version":"2.3.1","version_major":"2","version_minor":"3.1","build_id":"BUILD_ID"}
If you are using SSL/TLS with self signed certificates then you might receive this error when performing a CURL request:
curl: (60) server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
You receive this error because the certificate authority that generated this certificate is unknown and untrusted. You can ignore this by using the -k argument:
curl -k -XGET 'https://10.25.5.70/nagiosna/index.php/api/system/get_product_info?token=XXXX'
Endpoints
Status
CPU Status
URL
http://10.25.5.70/nagiosna/index.php/api/system/cpu_status?token=XXXX
Output:
{"cpu_usage":0.1}
Memory Status
URL
http://10.25.5.70/nagiosna/index.php/api/system/memory_status?token=XXXX
Output:
{"used_percent":80.750407830343,"swap_percent":0.91519219035997}
Root Drive
URL
http://10.25.5.70/nagiosna/index.php/api/system/root_drive_status?token=XXXX
Output:
{"filesystem":0,"size":28715008000,"used":5928940000,"available":22786068000,"percent":20.647530378539}
Product Info
URL
http://10.25.5.70/nagiosna/index.php/api/system/get_product_info?token=XXXX
Output:
{"product":"Nagios Network Analyzer","release":231,"version":"2.3.1","version_major":"2","version_minor":"3.1","build_id":"BUILD_ID"}
Do Update Check
URL
http://10.25.5.70/nagiosna/index.php/api/system/do_update_check?token=XXXX
Output:
{"update_available":0}
Sources
List All Sources
URL
http://10.25.5.70/nagiosna/index.php/api/sources/read?token=XXXX
Output:
[{"sid":"2","port":"9912","addresses":"10.25.1.254","name":"pfSense IPv4","flowtype":"netflow","directory":"\/usr\/local\/nagiosna\/var\/pfSenseIPv4","lifetime":"8w","disable_abnormal":"0"},
{"sid":"3","port":"9913","addresses":"2001:44b8:3132:25:10:25:1:253","name":"pfSense IPv6","flowtype":"netflow","directory":"\/usr\/local\/nagiosna\/var\/pfSenseIPv6","lifetime":"8w","disable_abnormal":"0"},
{"sid":"4","port":"9914","addresses":"192.168.25.1","name":"Firewall Public","flowtype":"netflow","directory":"\/usr\/local\/nagiosna\/var\/FirewallPublic","lifetime":"8w","disable_abnormal":"0"},
{"sid":"5","port":"9915","addresses":"10.25.13.33","name":"centos15","flowtype":"netflow","directory":"\/usr\/local\/nagiosna\/var\/centos15","lifetime":"8w","disable_abnormal":"0"}]
Stop Source
This requires a source id (sid), please refer to the List All Sources section above to identify the sid.
URL
http://10.25.5.70/nagiosna/index.php/api/system/stop?sid=5&token=XXXX
Output:
{"status":"success"}
Start Source
This requires a source id (sid), please refer to the List All Sources section above to identify the sid.
URL
http://10.25.5.70/nagiosna/index.php/api/system/start?sid=5&token=XXXX
Output:
{"status":"success"}
Source Status
This requires a source id (sid), please refer to the List All Sources section above to identify the sid. You can also omit the sid= argument and it will return the status of all sources.
URL
http://10.25.5.70/nagiosna/index.php/api/system/source_status?sid=2&token=XXXX
Output:
{"diskusage":"1.5G","icon":"<img src=\"\/nagiosna\/media\/icons\/accept.png\" class=\"status-icon\" title=\"Running\" \/>","status":"Running","pid":"12501","owner":"nna"}
Backups
Backup Cron Status
URL
http://10.25.5.70/nagiosna/index.php/api/system/backup_cron_status?token=XXXX
Output:
{"status":0}
Do Backup
URL
http://10.25.5.70/nagiosna/index.php/api/system/do_backup?token=XXXX
Output:
{"command_id":"5a94a132ca617","name":"1519690034.tar.gz"}
Users
Get All Users
URL
http://10.25.5.70/nagiosna/index.php/api/system/get_users?token=XXXX
Output:
[
{
"id": "1",
"username": "nagiosadmin",
"password": "97f84dfb5184561aa67f3a1193eeb24sad4ff265",
"salt": "",
"email": "xxx@xxx.xxx",
"activation_code": "",
"forgotten_password_code": null,
"forgotten_password_time": null,
"remember_code": null,
"created_on": "1268889823",
"last_login": "1519688898",
"active": "1",
"first_name": "",
"last_name": "",
"company": "",
"phone": "",
"apiaccess": "1",
"apikey": "5e9a61b074315a3bf3246710090adccf256c3e4e",
"lang": "default",
"type": "local",
"auth_server_id": null,
"auth_server_data": null,
"user_id": "1"
},
{
"id": "2",
"username": "readonly",
"password": "bbdc9dc6e684bd925c5215a508c27a49s8063fbb",
"salt": null,
"email": "yyy@yyy.yyyy",
"activation_code": null,
"forgotten_password_code": null,
"forgotten_password_time": null,
"remember_code": null,
"created_on": "1494910284",
"last_login": "1512453257",
"active": "1",
"first_name": "Read",
"last_name": "Only",
"company": "",
"phone": "",
"apiaccess": "1",
"apikey": "667328cf72d0521c98bc0fc31759990351dbc41b",
"lang": "default",
"type": "local",
"auth_server_id": null,
"auth_server_data": null,
"user_id": "2"
},
]
Final Thoughts
For any support related questions please visit the Nagios Support Forums at: