Skip to content

Commit 6de44a0

Browse files
bmfmanciniCopilotTheWitness
authored
Cacti Restuful API V0.1 (#6310)
* inital commit * remove composer files * Update api/public/index.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update api/include/db_functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Add API Versioning - Group Endpoint types together - Add initial V1 endpoint groups - Leverage the slim route collector class * Migrate db functions to Cacti core db functions * remove dedicated config file * Update README.md * Update db_functions.php - include global and not the lib/database - remove redudant db_connect function * Sanity checks - Ensure arrays are initialized before iterating * Update db_functions.php * Add logging * helper function for parameter validation * Update api/include/db_functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Sanitize error message * Update db_functions.php use the proper cacti db function Leverage the read_config_option function instead of standalone db query * Update api/include/db_functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Cleaning up from pre migration to core cacti functions * clean up * update tholds endpoint Call on the validate parameters helper function * call on db_fetch_cell for thold db_function * Update api/public/index.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Creat automation networks api endpoint Call the proper db prepared function for prepared statments * automation status endpoint * data source stats status endpoint * add validations (more to come) * validations * Update api/include/arrays.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update api/include/db_functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update api/include/arrays.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update api/include/db_functions.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Refactor validation error handling in API routes This changes adds a small bit of PSR formatting to the code, and reduces the length of the $validation_error by renaming to $verror for redability. * Clean up whitespace in arrays.php * Refactor SQL queries for improved readability --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: TheWitness <thewitness@cacti.net>
1 parent 697e231 commit 6de44a0

5 files changed

Lines changed: 832 additions & 0 deletions

File tree

api/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Cacti RESTful Slim API
2+
3+
This project provides a RESTful API for accessing Cacti monitoring data using the Slim PHP framework.
4+
5+
## Overview
6+
7+
The API exposes endpoints to retrieve information about hosts, host templates, poller status, graph lists, Cacti status, boost status, database connectivity, and plugin thresholds. All responses are in JSON format.
8+
9+
THIS IS NOT PROD READY!
10+
11+
To run the API php -S 127.0.0.1:8080 -t public ( Which for Prod will be replaced with a WSGI server)
12+
13+
## API Versioning
14+
15+
The API uses URL path versioning. All endpoints are prefixed with a version number:
16+
- `/v1/` - Current stable version
17+
18+
## Endpoints
19+
20+
- `GET /`
21+
Returns a welcome message.
22+
23+
### Version 1 (v1) Endpoints
24+
25+
#### Info Endpoints
26+
- `GET /v1/info/hosts`
27+
Returns a list of hosts.
28+
**Allowed query parameters:**
29+
- `host_id`
30+
- `poller_id`
31+
- `site_id`
32+
- `template_id`
33+
- `status`
34+
35+
- `GET /v1/info/host_templates`
36+
Returns host template information. Accepts `template_id` as a query parameter.
37+
38+
- `GET /v1/info/graph_list`
39+
Returns a list of graphs. Accepts `host_id` as a query parameter.
40+
41+
#### Status Endpoints
42+
- `GET /v1/status/poller_status`
43+
Returns poller status. Accepts `poller_id` as a query parameter.
44+
45+
- `GET /v1/status/cacti_status`
46+
Returns the status of the Cacti system.
47+
48+
- `GET /v1/status/boost_status`
49+
Returns the status of the Cacti Boost system.
50+
51+
- `GET /v1/status/api_db_ping`
52+
Checks database connectivity.
53+
54+
- `GET /v1/status/cacti_db_status`
55+
Returns some metrics of the Main cacti DB
56+
57+
#### Plugin Endpoints
58+
- `GET /v1/plugin/thold/thresholds`
59+
Returns threshold information from the thold plugin.
60+
61+
- `GET /v1/plugin/thold/status`
62+
Returns threshold status information.
63+
64+
## Usage
65+
66+
1. Install dependencies with Composer.
67+
2. Configure your web server to serve the `public/` directory.
68+
3. Access the API endpoints as described above using the versioned URLs (e.g., `/v1/info/hosts`).
69+
70+
## Requirements
71+
72+
- PHP 7.4 or higher
73+
- Composer
74+
- Cacti database and configuration
75+
76+
## TODO
77+
- Import Cacti base db_functions
78+
- Logging
79+
- Authentication/Authorization
80+
- Rate limiting
81+
- API documentation (OpenAPI/Swagger)

api/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"slim/slim": "^4.14",
4+
"slim/psr7": "^1.7"
5+
}
6+
}

api/include/arrays.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
$allowed_hosts_filter = [
4+
'host_id',
5+
'poller_id',
6+
'site_id',
7+
'template_id',
8+
'status',
9+
'snmp_location',
10+
'hostname',
11+
'description'
12+
];
13+
14+
$allowed_host_templates_filter = [
15+
'template_id'
16+
];
17+
18+
$allowed_thold_filter = [
19+
'host_id',
20+
'host_description',
21+
'hostname',
22+
'data_source_name',
23+
'template_id'
24+
];
25+
26+
$allowed_automation_networks_filter = [
27+
'network_id',
28+
'network_name',
29+
'subnet_range',
30+
];

0 commit comments

Comments
 (0)