https://github.com/amallek/prometheus-api
Lightweight Prometheus API serves as a powerful tool for managing your Prometheus configuration remotely
https://github.com/amallek/prometheus-api
api automation prometheus rest rest-api rust
Last synced: 6 months ago
JSON representation
Lightweight Prometheus API serves as a powerful tool for managing your Prometheus configuration remotely
- Host: GitHub
- URL: https://github.com/amallek/prometheus-api
- Owner: amallek
- License: mit
- Created: 2023-11-01T09:26:38.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-06T20:23:06.000Z (over 1 year ago)
- Last Synced: 2025-10-26T01:57:13.790Z (8 months ago)
- Topics: api, automation, prometheus, rest, rest-api, rust
- Language: Rust
- Homepage:
- Size: 12.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Prometheus API
## Overview
Lightweight Prometheus API serves as a powerful tool for managing your Prometheus configuration remotely. This code was developed as part of one of our latest projects, and I've decided to publish it because I find it incredibly useful. Written in Rust, the project offers basic authentication and various API endpoints for versatile management of your Prometheus setup.
## Features
- **Flexible Endpoints**: Numerous API calls available for managing Prometheus settings.
- **Basic Authentication**: Secure your Prometheus configurations with a configurable `x-auth` token.
- **Rust-powered**: Built with the speed and safety of Rust.
## Prerequisites
- Rust (latest stable version)
- Prometheus installed and running
## Getting started
To get started, follow the instructions below.
##### Step 1 - Clone this repository
```shell
git clone https://github.com/amallek/prometheus-api.git
```
##### Step 2 - Navigate to the project directory
```shell
cd prometheus-api
```
##### Step 3 - Build the project
```shell
# Uncomment the next line if Rust is not installed
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
cargo build --release
```
##### Step 4 - Run the application
```shell
# Uncomment to generate self-signed TLS certs
# ./helpers/create-cert.sh
./target/release/prometheus-api
```
##### Step 5 - Install as a service (optional)
If desired, you can install prometheus-api as a service. An example script is available in helpers/create-service.sh.
```shell
# Uncomment the next line to execute the script
# ./helpers/create-service.sh
systemctl start prometheus-api
```
## 🛠API Endpoints
The API provides a range of functionalities through the following endpoints:
### `prometheus::info()`
- **Endpoint**: `GET /info`
- **Description**: Retrieves basic information about the Prometheus API
- **Authentication**: Required (`x-auth` token)
### `prometheus::config_get()`
- **Endpoint**: `GET /config`
- **Description**: Fetches the current `prometheus.yml` configuration in JSON format.
- **Authentication**: Required (`x-auth` token)
### `prometheus::endpoint_get()`
- **Endpoint**: `GET /endpoint/{endpoint}`
- **Description**: Obtains details about a specified Prometheus scraping endpoint.
- **Authentication**: Required (`x-auth` token)
### `prometheus::endpoint_add()`
- **Endpoint**: `PUT /endpoint`
- **Description**: Adds a new scraping endpoint to the Prometheus configuration.
- **Payload**: JSON object with endpoint details.
- **Authentication**: Required (`x-auth` token)
### `prometheus::endpoint_delete()`
- **Endpoint**: `DELETE /endpoint/{endpoint}`
- **Description**: Removes a specified scraping endpoint from the Prometheus configuration.
- **Authentication**: Required (`x-auth` token)
### `prometheus::endpoint_update()`
- **Endpoint**: `POST /endpoint`
- **Description**: Updates an existing scraping endpoint in the Prometheus configuration.
- **Payload**: JSON object with updated endpoint details.
- **Authentication**: Required (`x-auth` token)
The payload required consists of literally what you would see in the prometheus.yaml file, but JSON encoded. For instance:
##### Add
```shell
curl -ikX PUT \
'https://localhost:9090/endpoint' \
-H 'x-auth: my_password' \
-d '{
"job_name": "my-job-name",
"scrape_interval": "60",
"scrape_timeout": "5",
"scheme": "https",
"static_configs": [
{
"targets": ["my.machine.example.com:9091"],
"labels": {"label1":"val","label2":"val"}
}
]
}'
#{"success":true}
```
##### Get
```shell
curl -ik 'https://localhost:9090/endpoint/my-job-name' \
-H 'x-auth: my_password'
```
##### Remove
```shell
curl -ikX DELETE \
'https://localhost:9090/endpoint/my-job-name' \
-H 'x-auth: my_password'
```
## Authentication
Include an `x-auth` token in every API call to ensure authentication. The token can be updated or replaced by custom logic in the `verify_token()` method (see `src/security.rs`).
## License
This project is licensed under the terms of the MIT License (see LICENSE.)