Skip to content

Commit

Permalink
Merge pull request #30 from pdreker/develop
Browse files Browse the repository at this point in the history
Make nvironment configuration more kubernetes friendly
  • Loading branch information
pdreker authored Mar 7, 2021
2 parents a67a863 + 81e200d commit ec1b26f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 17 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-push-develop.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Build Dev Docker image

on:
pull_request:
push:
branches:
- master
- develop

jobs:
build-docker:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ __pycache__
.vscode
launch.json
settings.json
.idea
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Configuration is done via environment vars.

The main configuration is done inside the environment variable `FRITZ_EXPORTER_CONFIG`. This variable must contain the comma-separated address, username and password for the device. If you need multiple devices simply repeat the three values for the other devices.

An alternative it to use The three environment variables `FRITZ_HOSTNAME`, `FRITZ_USERNAME` and `FRITZ_PASSWORD`, This way you lose the ability to monitor multiple devices with one exporter but gain the ability To put `FRITZ_PASSWORD` into a kubernetes secret like shown in [Kubernetes deployment](#kubernetes-deployment).

Example for a single device (at 192.168.178.1 username monitoring and the password "mysupersecretpassword"):

```bash
Expand All @@ -125,6 +127,53 @@ NOTE: If you are using WiFi Mesh all your devices should have the same username
| FRITZ_EXPORTER_CONFIG | Comma separated "hostname","user","password" triplets | none |
| FRITZ_EXPORTER_PORT | Listening port for the exporter | 9787 |

## Kubernetes deployment

Put the Fritz!Box password into a kubernetes secret
```bash
kubectl create secret generic fritzbox-password --from-literal=password='mysupersecretpassword'
```
then deploy the exporter with a deployment
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: fritzbox-exporter
labels:
app: fritzbox-exporter
spec:
selector:
matchLabels:
app: fritzbox-exporter
template:
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: /
prometheus.io/port: "9787"
labels:
app: fritzbox-exporter
spec:
containers:
- name: fritzbox-exporter
image: pdreker/fritz_exporter:latest
imagePullPolicy: Always
env:
- name: FRITZ_HOST
value: "192.168.178.1"
- name: FRITZ_USER
value: "monitoring"
- name: FRITZ_EXPORTER_PORT
value: "9787"
- name: FRITZ_PASS
valueFrom:
secretKeyRef:
name: fritzbox-password
key: password
ports:
- containerPort: 9787
```
## Helping out
If your device delivers some metrics which are not yet scraped by this exporter you can either create a Pull Request, which will be gladly accepted ;-)
Expand Down
35 changes: 20 additions & 15 deletions fritz_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import logging
import os
import sys
import time
from pprint import pprint

from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY
Expand All @@ -28,20 +26,27 @@
logger = logging.getLogger(__name__)
logger.setLevel(logging.WARN)

def main():
fritz_config_env = os.getenv('FRITZ_EXPORTER_CONFIG')
if fritz_config_env == None:
logger.critical('FRITZ_EXPORTER_CONFIG is not set. Exiting.')
sys.exit(1)
else:
fritz_config = [ x.strip() for x in fritz_config_env.split(',') ]
# if the next idiom looks like magic: https://stackoverflow.com/questions/23286254/how-to-convert-a-list-to-a-list-of-tuples
conf_it = [iter(fritz_config)] * 3
device_config = zip(*conf_it) # now tuples of (host, user, password)

def main():
fritzcollector = FritzCollector()
for device in device_config:
fritzcollector.register(FritzDevice(device[0], device[1], device[2]))

if 'FRITZ_EXPORTER_CONFIG' in os.environ:
fritz_config_env = os.getenv('FRITZ_EXPORTER_CONFIG')
fritz_config = [x.strip() for x in fritz_config_env.split(',')]
configs = int(len(fritz_config) / 3)
for device in range(configs):
address = fritz_config.pop(0)
username = fritz_config.pop(0)
password = fritz_config.pop(0)
fritzcollector.register(FritzDevice(address, username, password))
elif 'FRITZ_HOSTNAME' and 'FRITZ_USERNAME' and 'FRITZ_PASSWORD' in os.environ:
address = os.getenv('FRITZ_HOSTNAME')
username = os.getenv('FRITZ_USERNAME')
password = os.getenv('FRITZ_PASSWORD')
fritzcollector.register(FritzDevice(address, username, password))
else:
logger.critical('no ENV variables set. Exiting.')
sys.exit(1)

REGISTRY.register(fritzcollector)

Expand All @@ -55,6 +60,6 @@ def main():
finally:
loop.close()


if __name__ == '__main__':
main()

0 comments on commit ec1b26f

Please sign in to comment.