Skip to content

Commit

Permalink
Load configuration from current or /etc directory.
Browse files Browse the repository at this point in the history
Signed-off-by: Volker Theile <[email protected]>
  • Loading branch information
votdev committed Mar 24, 2020
1 parent 132c499 commit 4104431
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 17 additions & 7 deletions prometheus_webhook_snmp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import ipaddress
import json
import logging
import os
import sys

import cherrypy
import dateutil.parser
Expand Down Expand Up @@ -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):
"""
Expand Down

0 comments on commit 4104431

Please sign in to comment.