Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Docker support, set hardcoded values to ENV variables #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM debian:bullseye-slim

RUN apt-get update && apt-get -y install supervisor nginx python3-pip procps

COPY netbox_prometheus.py /netbox_prometheus.py
COPY requirements.txt /requirements.txt

ADD conf/default.conf /etc/nginx/sites-enabled/default
ADD poll.sh /

COPY conf/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN pip3 install -r /requirements.txt \
&& chmod +x /netbox_prometheus.py \
&& mkdir -p /etc/prometheus/targets.d /var/www/html/metrics \
&& chmod +x /poll.sh

EXPOSE 80
CMD ["/usr/bin/supervisord"]
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,25 @@ SITE_TAG = "prometheus2"
Then in Netbox, tag sites A, B and C with "prometheus1", and sites A, D and
E with "prometheus2". The correct targets will be generated for each
prometheus instance.

# Docker Support

If run Prometheus in Docker or have Docker running locally. The `Dockerfile` included should make it quicker to get started.

## Building the image
```
docker build -t netbox-promethus .
```

## Running the container
```
docker run -d -p 80:80 --name netbox-promethus -e NETBOX_URL="https://netbox.example.net" -e API_TOKEN="XXXXXXXX"
```

## Overriding the poll interval

`Poll.sh` contains an `ENV` var `SLEEP_INT` to override the default poll interval of `300` seconds. This is optional, here we are setting it to poll every minute. The default value is `300` seconds.

```
docker run -d -p 80:80 --name netbox-promethus -e NETBOX_URL="https://netbox.example.net" -e API_TOKEN="XXXXXXXX" -e SLEEP_INT=60 netbox-promethus
```
10 changes: 10 additions & 0 deletions conf/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Default Nginx endpoint to expose metrics
server {
server_name _;
listen *:80 default_server deferred;
location / {
add_header Content-Type text/plain;
root /var/www/html;
}

}
21 changes: 21 additions & 0 deletions conf/supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx -g "daemon off;"
priority=900
stdout_logfile= /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
username=www-data
autorestart=true

[program:netbox_prometheus]
directory=/
command=/poll.sh
stdout_logfile= /dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
autostart=true
7 changes: 5 additions & 2 deletions netbox_prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def write_metrics(self, filename):
self.replace_file(filename, content)

if __name__ == "__main__":
API_URL = "https://netbox.example.net"
API_TOKEN = "XXXXXXXX"
API_URL = os.getenv('NETBOX_URL', "https://netbox.example.net")
API_TOKEN = os.getenv('API_TOKEN', "XXXXXXXX")
SITE_TAG = "prometheus" # we will poll devices in all sites with this tag
DIR = "/etc/prometheus/targets.d"
METRICS = "/var/www/html/metrics/netbox"
Expand All @@ -134,6 +134,9 @@ def write_metrics(self, filename):
#METRICS = "/tmp/netbox.prom"

nb = pynetbox.api(API_URL, token=API_TOKEN)
# Wether or not to validate the TLS certificate of API_URL
nb.http_session.verify = True
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAICS, this is true by default (which is what I'd expect):

root@prometheus:~# python3 -i nb
>>> nb.http_session
<requests.sessions.Session object at 0x7f072cbf9160>
>>> nb.http_session.verify
True

However, I guess it's useful to have it as documentation on how to disable HTTPS verification if required.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, we had a bit of an issue when we started playing with this with one of our internally signed certificates. Python request just did not like it so I was digging around to figure out how to disable it. So I found that. The plan was to set another ENV var and set it to true by default for us to disable but I couldn't get it to work. So I just left it True in the commit so I could toggle it off in our internal environment. Getting an ENV variable such as SSL_VALIDATE would be ideal.


builder = ConfigBuilder(
nb=nb,
filter={
Expand Down
8 changes: 8 additions & 0 deletions poll.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
set -e

# Sleep interval for how often to poll (300s) This can be controlled by setting SLEEP_INT env var.
sleep_duration="${SLEEP_INT:-300}"

while true; do (python3 netbox_prometheus.py; sleep $sleep_duration); done

2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pynetbox
pyyaml