feat: add option to set output file mode (#184)

This commit is contained in:
Robert Kaussow 2022-03-27 13:36:26 +02:00 committed by GitHub
parent 65afd4a6e5
commit 3ccb789bdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 27 additions and 2 deletions

View File

@ -6,16 +6,18 @@ You can get all available CLI options by running `prometheus-pve-sd --help`:
```Shell ```Shell
$ prometheus-pve-sd --help $ prometheus-pve-sd --help
usage: prometheus-pve-sd [-h] [-c CONFIG_FILE] [-o OUTPUT_FILE] [-d LOOP_DELAY] [--no-service] [-f LOG_FORMAT] [-v] [-q] [--version] usage: prometheus-pve-sd [-h] [-c CONFIG_FILE] [-o OUTPUT_FILE] [-m OUTPUT_FILE_MODE] [-d LOOP_DELAY] [--no-service] [-f LOG_FORMAT] [-v] [-q] [--version]
Prometheus Service Discovery for Proxmox VE Prometheus Service Discovery for Proxmox VE
optional arguments: options:
-h, --help show this help message and exit -h, --help show this help message and exit
-c CONFIG_FILE, --config CONFIG_FILE -c CONFIG_FILE, --config CONFIG_FILE
location of configuration file location of configuration file
-o OUTPUT_FILE, --output OUTPUT_FILE -o OUTPUT_FILE, --output OUTPUT_FILE
output file output file
-m OUTPUT_FILE_MODE, --mode OUTPUT_FILE_MODE
output file mode
-d LOOP_DELAY, --loop-delay LOOP_DELAY -d LOOP_DELAY, --loop-delay LOOP_DELAY
delay between discovery runs delay between discovery runs
--no-service run discovery only once --no-service run discovery only once

View File

@ -16,6 +16,8 @@ metrics:
port: 8000 port: 8000
output_file: output_file:
output_file_mode: "0640"
loop_delay: 300 loop_delay: 300
# Run pve sd in a loop and discover hosts every n seconds (as defined in loop_delay). # Run pve sd in a loop and discover hosts every n seconds (as defined in loop_delay).
# Can be disabled to run disovery only once. # Can be disabled to run disovery only once.

View File

@ -15,6 +15,8 @@ PROMETHEUS_PVE_SD_METRICS_ADDRESS=127.0.01
PROMETHEUS_PVE_SD_METRICS_PORT=8000 PROMETHEUS_PVE_SD_METRICS_PORT=8000
PROMETHEUS_PVE_SD_OUTPUT_FILE= PROMETHEUS_PVE_SD_OUTPUT_FILE=
PROMETHEUS_PVE_SD_OUTPUT_FILE_MODE=0640
PROMETHEUS_PVE_SD_LOOP_DELAY=300 PROMETHEUS_PVE_SD_LOOP_DELAY=300
# Run PVE SD in a loop and discover hosts every n seconds (as defined in PROMETHEUS_PVE_SD_LOOP_DELAY). # Run PVE SD in a loop and discover hosts every n seconds (as defined in PROMETHEUS_PVE_SD_LOOP_DELAY).

View File

@ -6,6 +6,7 @@ import json
import shutil import shutil
import signal import signal
import tempfile import tempfile
from os import chmod
from time import sleep from time import sleep
from prometheus_client import start_http_server from prometheus_client import start_http_server
@ -65,6 +66,9 @@ class PrometheusSD:
parser.add_argument( parser.add_argument(
"-o", "--output", dest="output_file", action="store", help="output file" "-o", "--output", dest="output_file", action="store", help="output file"
) )
parser.add_argument(
"-m", "--mode", dest="output_file_mode", action="store", help="output file mode"
)
parser.add_argument( parser.add_argument(
"-d", "-d",
"--loop-delay", "--loop-delay",
@ -170,6 +174,7 @@ class PrometheusSD:
json.dump(output, tf, indent=4) json.dump(output, tf, indent=4)
shutil.move(temp_file.name, self.config.config["output_file"]) shutil.move(temp_file.name, self.config.config["output_file"])
chmod(self.config.config["output_file"], self.config.config["output_file_mode"])
def _terminate(self, signal, frame): def _terminate(self, signal, frame):
self.log.sysexit_with_message("Terminating", code=0) self.log.sysexit_with_message("Terminating", code=0)

View File

@ -70,6 +70,12 @@ class Config():
"file": True, "file": True,
"type": environs.Env().str "type": environs.Env().str
}, },
"output_file_mode": {
"default": "0644",
"env": "OUTPUT_FILE_MODE",
"file": True,
"type": environs.Env().str
},
"loop_delay": { "loop_delay": {
"default": 300, "default": 300,
"env": "LOOP_DELAY", "env": "LOOP_DELAY",

View File

@ -9,6 +9,7 @@ metrics:
port: 8000 port: 8000
output_file: dummy output_file: dummy
output_file_mode: "0640"
loop_delay: 300 loop_delay: 300
service: true service: true

View File

@ -45,6 +45,12 @@ def builtins():
"file": True, "file": True,
"type": environs.Env().str "type": environs.Env().str
}, },
"output_file_mode": {
"default": "0640",
"env": "OUTPUT_FILE_MODE",
"file": True,
"type": environs.Env().str
},
"loop_delay": { "loop_delay": {
"default": 300, "default": 300,
"env": "LOOP_DELAY", "env": "LOOP_DELAY",
@ -125,6 +131,7 @@ def defaults():
"port": 8000 "port": 8000
}, },
"output_file": "dummy", "output_file": "dummy",
"output_file_mode": "0640",
"pve": { "pve": {
"auth_timeout": 5, "auth_timeout": 5,
"password": "", "password": "",