2021-06-09 18:44:10 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Entrypoint and CLI handler."""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import shutil
|
|
|
|
import signal
|
|
|
|
import tempfile
|
2022-03-27 11:36:26 +00:00
|
|
|
from os import chmod
|
2021-06-09 18:44:10 +00:00
|
|
|
from time import sleep
|
|
|
|
|
2021-06-15 20:48:11 +00:00
|
|
|
from prometheus_client import start_http_server
|
|
|
|
|
2021-06-09 18:44:10 +00:00
|
|
|
import prometheuspvesd.exception
|
|
|
|
from prometheuspvesd import __version__
|
|
|
|
from prometheuspvesd.config import SingleConfig
|
|
|
|
from prometheuspvesd.discovery import Discovery
|
2021-06-09 20:57:48 +00:00
|
|
|
from prometheuspvesd.exception import APIError
|
2021-06-12 14:59:46 +00:00
|
|
|
from prometheuspvesd.logger import SingleLog
|
2021-06-09 18:44:10 +00:00
|
|
|
from prometheuspvesd.model import HostList
|
|
|
|
|
|
|
|
|
|
|
|
class PrometheusSD:
|
|
|
|
"""Main Prometheus SD object."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.log = SingleLog()
|
|
|
|
self.logger = self.log.logger
|
|
|
|
self.args = self._cli_args()
|
|
|
|
self.config = self._get_config()
|
|
|
|
|
2021-06-09 20:57:48 +00:00
|
|
|
signal.signal(signal.SIGINT, self._terminate)
|
|
|
|
signal.signal(signal.SIGTERM, self._terminate)
|
2021-06-09 21:18:14 +00:00
|
|
|
|
2021-06-09 20:57:48 +00:00
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.discovery = Discovery()
|
|
|
|
except APIError as e:
|
2021-06-09 21:18:14 +00:00
|
|
|
if not self.config.config["service"]:
|
2023-02-12 14:11:15 +00:00
|
|
|
self.log.sysexit_with_message(f"Proxmoxer API error: {str(e).strip()}")
|
2021-06-09 21:18:14 +00:00
|
|
|
|
2023-02-12 14:11:15 +00:00
|
|
|
self.logger.error(f"Proxmoxer API error: {str(e).strip()}")
|
2021-06-09 21:18:14 +00:00
|
|
|
sleep(60)
|
2021-06-09 20:57:48 +00:00
|
|
|
continue
|
2021-06-09 21:18:14 +00:00
|
|
|
else:
|
|
|
|
break
|
2021-06-09 20:57:48 +00:00
|
|
|
|
2021-06-09 18:44:10 +00:00
|
|
|
self._fetch()
|
|
|
|
|
|
|
|
def _cli_args(self):
|
|
|
|
"""
|
|
|
|
Use argparse for parsing CLI arguments.
|
|
|
|
|
|
|
|
:return: args objec
|
|
|
|
"""
|
|
|
|
parser = argparse.ArgumentParser(description="Prometheus Service Discovery for Proxmox VE")
|
|
|
|
parser.add_argument(
|
2021-06-12 14:59:46 +00:00
|
|
|
"-c",
|
|
|
|
"--config",
|
|
|
|
dest="config_file",
|
|
|
|
action="store",
|
|
|
|
help="location of configuration file"
|
2021-06-09 18:44:10 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-o", "--output", dest="output_file", action="store", help="output file"
|
|
|
|
)
|
2022-03-27 11:36:26 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-m", "--mode", dest="output_file_mode", action="store", help="output file mode"
|
|
|
|
)
|
2021-06-09 19:10:51 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-d",
|
|
|
|
"--loop-delay",
|
|
|
|
dest="loop_delay",
|
|
|
|
action="store",
|
2021-06-09 21:18:14 +00:00
|
|
|
type=int,
|
2021-06-09 19:10:51 +00:00
|
|
|
help="delay between discovery runs"
|
|
|
|
)
|
2021-06-09 19:38:06 +00:00
|
|
|
parser.add_argument(
|
2021-07-25 14:01:23 +00:00
|
|
|
"--no-service",
|
|
|
|
dest="service",
|
|
|
|
action="store_false",
|
|
|
|
default=None,
|
|
|
|
help="run discovery only once"
|
2021-06-12 14:59:46 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-f",
|
|
|
|
"--log-format",
|
|
|
|
dest="logging.format",
|
|
|
|
metavar="LOG_FORMAT",
|
|
|
|
action="store",
|
|
|
|
help="used log format"
|
2021-06-09 19:38:06 +00:00
|
|
|
)
|
2021-06-09 18:44:10 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-v", dest="logging.level", action="append_const", const=-1, help="increase log level"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-q", dest="logging.level", action="append_const", const=1, help="decrease log level"
|
|
|
|
)
|
2023-02-12 14:11:15 +00:00
|
|
|
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
2021-06-09 18:44:10 +00:00
|
|
|
|
|
|
|
return parser.parse_args().__dict__
|
|
|
|
|
|
|
|
def _get_config(self):
|
|
|
|
try:
|
|
|
|
config = SingleConfig(args=self.args)
|
|
|
|
except prometheuspvesd.exception.ConfigError as e:
|
|
|
|
self.log.sysexit_with_message(e)
|
|
|
|
|
|
|
|
try:
|
2021-06-12 14:59:46 +00:00
|
|
|
self.log.update_logger(
|
|
|
|
config.config["logging"]["level"], config.config["logging"]["format"]
|
|
|
|
)
|
2021-06-09 18:44:10 +00:00
|
|
|
except ValueError as e:
|
2023-02-12 14:11:15 +00:00
|
|
|
self.log.sysexit_with_message(f"Can not set log level.\n{str(e)}")
|
2021-06-09 18:44:10 +00:00
|
|
|
|
|
|
|
required = [("pve.server", config.config["pve"]["server"]),
|
|
|
|
("pve.user", config.config["pve"]["user"]),
|
|
|
|
("pve.password", config.config["pve"]["password"])]
|
|
|
|
for name, value in required:
|
|
|
|
if not value:
|
2023-02-12 14:11:15 +00:00
|
|
|
self.log.sysexit_with_message(f"Option '{name}' is required but not set")
|
2021-06-09 18:44:10 +00:00
|
|
|
|
2023-02-12 14:11:15 +00:00
|
|
|
self.logger.info(f"Using config file {config.config_file}")
|
2021-06-09 18:44:10 +00:00
|
|
|
|
|
|
|
return config
|
|
|
|
|
|
|
|
def _fetch(self):
|
2021-06-15 20:48:11 +00:00
|
|
|
self.logger.info("Writes targets to {}".format(self.config.config["output_file"]))
|
2021-06-09 19:38:06 +00:00
|
|
|
self.logger.debug("Propagate from PVE")
|
2021-06-09 18:44:10 +00:00
|
|
|
|
2021-06-15 20:48:11 +00:00
|
|
|
if self.config.config["service"] and self.config.config["metrics"]["enabled"]:
|
|
|
|
self.logger.info(
|
|
|
|
"Starting metrics http endpoint on port {}".format(
|
|
|
|
self.config.config["metrics"]["port"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
start_http_server(
|
|
|
|
self.config.config["metrics"]["port"],
|
|
|
|
addr=self.config.config["metrics"]["address"]
|
|
|
|
)
|
|
|
|
|
2021-06-09 18:44:10 +00:00
|
|
|
while True:
|
2021-06-09 20:57:48 +00:00
|
|
|
try:
|
|
|
|
inventory = self.discovery.propagate()
|
|
|
|
except APIError as e:
|
2023-02-12 14:11:15 +00:00
|
|
|
self.logger.error(f"Proxmoxer API error: {str(e).strip()}")
|
2021-06-09 20:57:48 +00:00
|
|
|
except Exception as e: # noqa
|
2023-02-12 14:11:15 +00:00
|
|
|
self.logger.error(f"Unknown error: {str(e).strip()}")
|
2021-06-09 20:57:48 +00:00
|
|
|
else:
|
|
|
|
self._write(inventory)
|
2021-06-09 18:44:10 +00:00
|
|
|
|
2021-06-09 19:38:06 +00:00
|
|
|
if not self.config.config["service"]:
|
|
|
|
break
|
|
|
|
|
2021-06-15 20:48:11 +00:00
|
|
|
self.logger.info(
|
|
|
|
"Waiting {} seconds for next discovery loop".format(
|
|
|
|
self.config.config["loop_delay"]
|
|
|
|
)
|
|
|
|
)
|
2021-06-09 18:44:10 +00:00
|
|
|
sleep(self.config.config["loop_delay"])
|
|
|
|
|
|
|
|
def _write(self, host_list: HostList):
|
|
|
|
output = []
|
|
|
|
for host in host_list.hosts:
|
|
|
|
output.append(host.to_sd_json())
|
|
|
|
|
|
|
|
# Write to tmp file and move after write
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(mode="w", prefix="prometheus-pve-sd", delete=False)
|
|
|
|
with temp_file as tf:
|
|
|
|
json.dump(output, tf, indent=4)
|
|
|
|
|
|
|
|
shutil.move(temp_file.name, self.config.config["output_file"])
|
2022-03-28 21:10:38 +00:00
|
|
|
chmod(self.config.config["output_file"], int(self.config.config["output_file_mode"], 8))
|
2021-06-09 18:44:10 +00:00
|
|
|
|
2023-02-12 14:11:15 +00:00
|
|
|
def _terminate(self, signal, frame): # noqa
|
2021-06-09 18:44:10 +00:00
|
|
|
self.log.sysexit_with_message("Terminating", code=0)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
PrometheusSD()
|