Skip to content

Commit

Permalink
Add Dockerfile and CI builds, general config overhaul (janw#18)
Browse files Browse the repository at this point in the history
* Refactor into class-based daemon
* Add Dockerfile
* Add GitLab CI file for Docker image builds
* Integrate dynaconf
* Ensure testsuite passing for py3.5
* Codestyle fixes
  • Loading branch information
janw authored May 1, 2019
1 parent fccff36 commit 2432d9e
Show file tree
Hide file tree
Showing 19 changed files with 379 additions and 196 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[flake8]
ignore = E203, E266, E501, W503, D100, D107, D401, D202
max-line-length = 88
max-complexity = 18
exclude = .git,__pycache__,docs/source/conf.py,old,build,dist,.venv
134 changes: 132 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,134 @@
config.ini
.coverage

# Created by https://www.gitignore.io/api/python
# Edit at https://www.gitignore.io/?templates=python

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don’t work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# End of https://www.gitignore.io/api/python

.vscode
config.ini
user.toml
34 changes: 34 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
image: docker:stable

services:
- docker:dind

variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2

stages:
- build
- tag

before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
stage: build
script:
- docker pull $CI_REGISTRY_IMAGE:latest || true
- docker build
--cache-from $CI_REGISTRY_IMAGE:latest
--tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA .
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA

tag latest:
stage: tag
script:
- docker pull
- docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:latest

only:
- master
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ install:
pip install coveralls; fi
- if [[ $TEST_CMD == black* ]]; then pip install black; fi
- if [[ $TEST_CMD == flake8* ]]; then pip install flake8 flake8-bugbear
flake8-docstrings flake8-todo flake8-eradicate; fi
flake8-todo; fi
- pip install -r requirements.txt
- pip install -r requirements-tests.txt

Expand Down
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM python:3.6-alpine

LABEL maintainer="Jan Willhaus <[email protected]>"

WORKDIR /app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY user.toml.example ./user.toml
COPY default.toml ./
COPY piholeinflux.py ./

CMD [ "python", "./piholeinflux.py" ]
57 changes: 36 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,42 @@

A simple daemonized script to report Pi-Hole stats to an InfluxDB, ready to be displayed via Grafana.

**Heads-up: the configuration options changed fundamentally in recent versions. Please read up on the current state below.**

![Example Grafana Dashboard](.readme-assets/dashboard.png)

## Requirements and Setup
## Setup (Using Docker)

To use docker for running the daemon, use the following command:

```bash
docker run \
-e PIHOLE_INFLUXDB_HOST="myhostname" \
-e PIHOLE_INFLUXDB_PORT="8086" \
-e PIHOLE_INFLUXDB_USERNAME="myusername" \
-e PIHOLE_INFLUXDB_PASSWORD="mysupersecretpassword" \
-e PIHOLE_INFLUXDB_DATABASE="pihole" \
-e PIHOLE_INSTANCES="localhost=http://127.0.0.1/admin/api.php" \
registry.gitlab.com/janw/pi-hole-influx
```

The following values are the defaults and will be used if not set:

* PIHOLE_INFLUXDB_PORT="8086"
* PIHOLE_INFLUXDB_HOST="127.0.0.1"
* PIHOLE_INFLUXDB_DATABASE="pihole"
* PIHOLE_INSTANCES="localhost=http://127.0.0.1/admin/api.php"

`PIHOLE_INSTANCES` contains the Pi-hole instances that are to be reported. Multiple instances can given in a dict-like boxed syntax, known as [Inline Tables in TOML](https://github.com/toml-lang/toml#inline-table):

```bash
PIHOLE_INSTANCES="{first_one="http://127.0.0.1/admin/api.php",second_pihole="http://192.168.42.79/admin/api.php"[,…]}"
```

Note that instances are prefixed by a custom name.


## Setup (Traditional Way)

As Pi-hole (as the name suggests) is built specifically with the Raspberry Pi in mind (and I run it on there as well), the following steps assume an instance of Pi-hole on Raspbian Strech Lite, with no additional modifications so far. Piholestatus will be configured to run on the same Pi.

Expand All @@ -30,8 +63,8 @@ cd ~/pi-hole-influx
pip3 install -r requirements.txt

# Copy config.example and modify it (should be self-explanatory)
cp config.ini.example config.ini
vi config.ini
cp user.toml.example user.toml
vi user.toml
```

Before starting the daemon for the first time, symlink the systemd service into place, reload, and enable the service.
Expand Down Expand Up @@ -66,24 +99,6 @@ The status should look as follows. Note the `Status:` line showing the last time

The example dashboard seen [at the top](#pi-hole-influx) uses the collected data and displays it in concise and sensible graphs and single stats. The dashboard can be imported into your Grafana instance from the `dashboard.json` file included in the repo, or by using ID `6603` to [import it from Grafana's Dashboard Directory](https://grafana.com/dashboards/6603).

## Monitoring multiple Pi-holes

As shown in the example configuration, it is possibe to add more than one Pi-hole instance to be monitored. Simply duplicate the `[pihole]` section and its config entries, and rename it to another unique name, like so:

```ini
[pihole]
api_location = http://127.0.0.1/admin/api.php
instance_name = pihole
timeout = 10

[pihole_2]
api_location = http://192.168.27.42/admin/api.php
instance_name = second_pihole
timeout = 10
```

The config entries for `instance_name`, and `timeout` are optional; the instance name defaults to the config section name (`pihole_2` in this case), the connection timeout will be 10 seconds by default.

## Attributions

The script originally [created by Jon Hayward](https://fattylewis.com/Graphing-pi-hole-stats/), adapted to work with InfluxDB [by /u/tollsjo in December 2016](https://github.com/sco01/piholestatus), and [improved and extended by @johnappletree](https://github.com/johnappletree/piholestatus). "If I have seen further it is by standing on the shoulders of giants". 🤓
21 changes: 0 additions & 21 deletions config.ini.example

This file was deleted.

10 changes: 10 additions & 0 deletions default.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]

influxdb_host = "127.0.0.1"
influxdb_port = "8086"
influxdb_database = "pihole"

request_timeout = 10
reporting_interval = 30

instances="localhost=http://127.0.0.1/admin/api.php"
Loading

0 comments on commit 2432d9e

Please sign in to comment.