diff --git a/CHANGELOG.md b/CHANGELOG.md index 55c34ca..fbb6afb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ v1.2 * Show used configuration settings in debug mode. + * Load configuration file from the directory in which the prometheus-webhook-snmp + command is located. + * Automatically convert hyphens to underscores in configuration file parameters. v1.1 diff --git a/README.md b/README.md index c890b69..5a03068 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,13 @@ scrape_configs: ``` # Global configuration file -The Prometheus Alertmanager receiver can be configured via configuration file, too. The file ``/etc/prometheus-webhook-snmp.conf`` is written in YAML format. Parameters in this file have precedence over default configuration settings. Please replace hyphens in parameter names with underscores. +The Prometheus Alertmanager receiver can be configured via configuration file, too. +Command line arguments have precedence over the settings in the configuration file. +The configuration file is written in YAML format. The file will be loaded in the +following order and precedence from ``/etc/prometheus-webhook-snmp.conf`` and the +directory in which the prometheus-webhook-snmp command is located. +Parameters in these files have precedence over default configuration settings. +Please replace hyphens in parameter names with underscores. Example configuration: diff --git a/prometheus_webhook_snmp/utils.py b/prometheus_webhook_snmp/utils.py index 7e8afc9..b1c8e48 100755 --- a/prometheus_webhook_snmp/utils.py +++ b/prometheus_webhook_snmp/utils.py @@ -2,6 +2,8 @@ import ipaddress import json import logging +import os +import sys import cherrypy import dateutil.parser @@ -209,13 +211,21 @@ def load(self, prog_name): :param prog_name: The name of the program. :type prog_name: str """ - file_name = '/etc/{}.conf'.format(prog_name) - try: - with open(file_name, 'r') as stream: - config = yaml.safe_load(stream) - self.update(config) - except (IOError, FileNotFoundError): - pass + file_name = '{}.conf'.format(prog_name) + path_names = [ + os.path.join('/etc', file_name), + os.path.join(sys.path[0], file_name) + ] + for path_name in path_names: + try: + with open(path_name, 'r') as stream: + config = yaml.safe_load(stream) + # Automatically convert hyphens to underscores. + for key in list(config.keys()): + config[key.replace('-', '_')] = config.pop(key) + self.update(config) + except (IOError, FileNotFoundError): + pass def __setitem__(self, key, value): """