Prometheus File Exporter is a Prometheus exporter to monitor file state.
PFE exposes 3 metrics:
- file_stat_count (File count for the service)
- file_stat_last_modification_time (Unix timestamp corresponding to the last file modification for the service)
- file_stat_last_file_size (Size in bytes of the latest modified file for the service)
PFE is useful to monitor your backup directory.
# HELP file_stat_count File count for the service
# TYPE file_stat_count gauge
file_stat_count{service="backup_for_my_service"} 1.0
file_stat_count{service="backup_for_my_other_service"} 2.0
# HELP file_stat_last_modification_time Unix timestamp corresponding to the last file modification for the service
# TYPE file_stat_last_modification_time gauge
file_stat_last_modification_time{service="backup_for_my_service"} 1.64916739e+09
file_stat_last_modification_time{service="backup_for_my_other_service"} 1.649167313e+09
# HELP file_stat_last_file_size Size in bytes of the latest modified file for the service
# TYPE file_stat_last_file_size gauge
file_stat_last_file_size{service="backup_for_my_service"} 0.0
file_stat_last_file_size{service="backup_for_my_other_service"} 9.0Use environnement variables to override default settings, you can edit them in env/pfe.env:
| Name | Default | Description |
|---|---|---|
| SCRAP_INTERVAL_SECONDS | 60 | Interval between two metrics update |
| EXPORTER_PORT | 9000 | Port to expose metrics in container |
| YAML_CONFIG_PATH | config.yml | PATH of PFE configuration file |
Create a docker-compose.override.yml with the following lines to expose the port 9000 on your system.
# docker-compose.override.yml
version: '3.6'
services:
pfe:
ports:
- 9000:9000extra_labels_definition is a dict that contains key value labels that will be assigned to all your exposed services.
A declared label value can be overrided on a particular service by declaring extra_labels.
NB: Extra labels in services need to be declared in
extra_labels_definition. It means that you have to declare all your extra_labels inextra_labels_definition
#config.yml
---
extra_labels_definition:
foo: default_bar
services:
backup_for_my_service:
driver:
name: DriverLocal
config:
path: ./test_data/backup_for_my_service/*.sql.gz
extra_labels:
foo: bar
backup_for_my_other_service:
driver:
name: DriverLocal
config:
path: ./test_data/backup_for_my_other_service/*.tar.bz2This will produce the following metrics:
# HELP file_stat_count File count for the service
# TYPE file_stat_count gauge
file_stat_count{foo="default_bar",service="backup_for_my_other_service"} 2.0
file_stat_count{foo="bar",service="backup_for_my_service"} 1.0A docker-compose.yml with Prometheus File Exporter is available.
For demo purpose, test.docker-compose.override.yml can be used to expose a Prometheus and Grafana service.
Dashboards and Alerts are in ./docker.
Let's try with:
docker-compose -f docker-compose.yml -f test.docker-compose.yml upGrafana is exposed on localhost:3000
Prometheus on localhost:9090
Exporter on localhost:9000
Use docker-compose.yml to override environnement variables and configure PFE
Let's try with:
pip3 install -r requirements.txt
python3 -m src.exporterUse
exportto set environnement variables and configure PFE
PFE has been designed to support multiple drivers. You can write your own driver to track file on a cloud provider, or whatever you want.
DriverLocal is for local file monitoring.
This config.yml will track all files ending with .tar.bz2 and .sql in the directory /home/backup/mybackups/
#config.yml
---
services:
backup_for_my_service:
driver:
name: DriverLocal
config:
path: ./test_data/backup_for_my_service/*.sql.gz
backup_for_my_other_service:
driver:
name: DriverLocal
config:
path: ./test_data/backup_for_my_other_service/*.tar.bz2
Create a file my_driver.py in src/drivers/. Starting with driver_quickstart.py is recommended.
cp src/drivers/driver_quickstart.py src/drivers/mydriver.pyFirst, rename your class in src/drivers/mydriver.py.
from src.drivers.driver_interface import DriverInterface
-class DriverQuickstart(DriverInterface):
+class MyDriver(DriverInterface):
# Overriding the constructor is optional
# def __init__(self, driver_config):Then, add it in src/drivers/__init__.py
from .driver_local import DriverLocal
+from .mydriver import MyDriverYou can now use MyDriver in your config.yml
NB: A Python logger is available with
self.logging
You can access all your driver's configuration with self.driver_config.
It means that if your configuration file is:
#config.yml
---
services:
backup_for_my_other_service:
driver:
name: MyDriver
config:
foo: barYou can get the value of foo in your driver with:
foo = self.driver_config['foo']Override the get_metrics method and make it return a dict with the following keys:
- latest_backup_timestamp
- latest_backup_size
- backup_count
Now, use your favorites library/API to gather your values.
def get_metrics(self):
metrics = dict()
metrics["backup_count"] = requests.get("MYAPI")
metrics["latest_backup_timestamp"] = datetime.datetime.now().timestamp()
metrics["latest_backup_size"] = len(requests.get("MYAPI"))
return metrics
