Skip to content

Commit

Permalink
Merge branch 'FlxPeters:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ekrichbaum authored May 9, 2024
2 parents 32df731 + 2315d1d commit b645211
Show file tree
Hide file tree
Showing 21 changed files with 527 additions and 174 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ updates:
schedule:
interval: weekly
commit-message:
prefix: fix
prefix: chore
prefix-development: chore
include: "scope"
- package-ecosystem: docker
Expand All @@ -23,4 +23,4 @@ updates:
commit-message:
prefix: fix
prefix-development: chore
include: "scope"
include: "scope"
1 change: 0 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@

- [ ] I have performed a self-review of my code
- [ ] If it is a core feature, I have added thorough tests.

8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ jobs:
matrix:
# Netbox git tags
netbox-version:
- v3.2.2
- v3.3.2
- v3.4.4
- v3.5.3
- v3.6.7
- v3.7.5
- master
- v3.7.8

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -56,7 +54,11 @@ jobs:
- name: Build test images
run: |
poetry run invoke build
env:
NETBOX_VER: ${{ matrix.netbox-version }}

- name: Run tests
run: |
poetry run invoke tests
env:
NETBOX_VER: ${{ matrix.netbox-version }}
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish
name: Publish on PyPi

on:
release:
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Release
on:
push:
branches:
- main
repository_dispatch:
types: [semantic-release]

permissions:
contents: read # for checkout

jobs:
release:
name: Release
runs-on: ubuntu-latest
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: "lts/*"
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release --dry-run --debug --ci
27 changes: 27 additions & 0 deletions .github/workflows/stale.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Close Stale Pull Requests

on:
schedule:
- cron: '0 0 * * *' # Run every day at midnight

permissions:
issues: write
pull-requests: write

jobs:
close_stale_prs:
runs-on: ubuntu-latest
steps:
- name: Close stale pull requests
uses: actions/stale@v9
with:
debug-only: true
stale-issue-message: |
This issue has been automatically closed because it has been inactive for more than 60 days.
Please reopen if you still intend to submit this pull request.
days-before-stale: 60
days-before-close: 7
stale-pr-message: |
This pull request has been marked as stale because it has been inactive for more than 60 days.
Please update this pull request or it will be automatically closed in 7 days.
stale-pr-label: stale
9 changes: 9 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ repos:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace

- repo: https://github.com/python-poetry/poetry
rev: "1.8"
hooks:
- id: poetry-check
- id: poetry-lock
- id: poetry-export
args: ["-f", "requirements.txt"]
verbose: true
8 changes: 8 additions & 0 deletions .releaserc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"branches": ["main"],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/github"
]
}
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@
[![CI](https://github.com/FlxPeters/netbox-plugin-prometheus-sd/workflows/CI/badge.svg?event=push)](https://github.com/FlxPeters/netbox-plugin-prometheus-sd/actions?query=workflow%3ACI)
[![PyPI](https://img.shields.io/pypi/v/netbox-plugin-prometheus-sd)](https://pypi.org/project/netbox-plugin-prometheus-sd/)

Provide Prometheus http_sd compatible API Endpoint with data from Netbox.
[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/flxpeters)

HTTP SD is a new feature in Prometheus 2.28.0 that allows hosts to be found via a URL instead of just files.
This plugin implements API endpoints in Netbox to make devices, IPs and virtual machines available to Prometheus.
Provide Prometheus `http_sd` compatible API Endpoint with data from Netbox.

HTTP SD is a feature since Prometheus 2.28.0 that allows hosts to be found via a URL instead of just files.
This plugin implements API endpoints in Netbox to make devices, services, IPs and virtual machines available to Prometheus.

## Compatibility

We aim to support the latest major versions of Netbox. For now we Support Netbox `3.2`, `3.3`, `3.4` and `3.5` including bugfix versions.
Check the `.github/workflows/ci.yml` pipeline for the current tested builds. Other versions may work, but we do not test them explicitly.
All relevant target versions are tested in CI. Have a look at the Github Actions definition for the current build targets.
We aim to support the latest major versions of Netbox. For now we support Netbox `>= 3.3` including bugfix versions.

Check the `.github/workflows/ci.yml` pipeline for the current tested builds.
Other versions may work, but we do not test them explicitly. All relevant target versions are tested in CI.

## Installation

The plugin is available as a Python package in pypi and can be installed with pip

pip install netbox-plugin-prometheus-sd
```bash
pip install netbox-plugin-prometheus-sd
```

Enable the plugin in /opt/netbox/netbox/netbox/configuration.py:

```python
PLUGINS = ['netbox_prometheus_sd']
```

The plugin has not further plugin configuration at the moment.
The plugin has not further plugin configuration.

## Usage

Expand All @@ -34,7 +41,7 @@ The plugin only provides a new API endpoint on the Netbox API. There is no furth
### API

The plugin reuses Netbox API view sets with new serializers for Prometheus.
This means that all filters that can be used on the Netbox api can also be used to filter Prometheus targets.
This means that all filters that can be used on the Netbox API can also be used to filter Prometheus targets.
Paging is disabled because Prometheus does not support paged results.

The plugin also reuses the Netbox authentication and permission model.
Expand All @@ -47,12 +54,17 @@ GET /api/plugins/prometheus-sd/services/ Get a list of servic
GET /api/plugins/prometheus-sd/ip-addresses/ Get a list of ip in a prometheus compatible format
```

#### Extended services filters

Apart from standard Netbox filters, services endpoint also supports `tenant=<slug>` or `tenant_id=<id>` parameters.
The lookup is only executed against the `tenant` attribute of the object associated with the service.

### Config context

The plugin can also discover extra config to inject in the HTTP SD JSON from the config context of the devices/virtual machines.
If you have a `prometheus-plugin-prometheus-sd` entry in your config context with the following schema it will be automatically picked up:

```
```yaml
prometheus-plugin-prometheus-sd:
- metrics_path: /not/metrics
port: 4242
Expand All @@ -65,7 +77,7 @@ config and filtering each scenario by a specific tag for instance.
If there is only one entry you can also use this form:
```
```yaml
prometheus-plugin-prometheus-sd:
metrics_path: /not/metrics
port: 4242
Expand All @@ -74,9 +86,11 @@ prometheus-plugin-prometheus-sd:
### Example
A working example on how to use this plugin with Prometheus is located at the `example` folder. Netbox content is created by using Netbox docker initializers.
A working example on how to use this plugin with Prometheus is located at the `example` folder.
Netbox content is created by using Netbox docker initializers.

The demo data doesn't make sense, but they are good enough for demonstrating how to configure Prometheus and get demo data to Prometheus service discovery.
The demo data doesn't make sense, but they are good enough for demonstrating how to configure Prometheus
and get demo data to Prometheus service discovery.

Go to the `example` folder and run `docker-compose up`. Prometheus should get available on `http://localhost:9090`.

Expand All @@ -99,7 +113,7 @@ All following commands are started inside this environment.

In order to run tests invoke the test steps

``` bash
```bash
# Build Docker images
invoke build
Expand All @@ -112,7 +126,7 @@ invoke unittest

Features should be covered by a unit test, but some times it's easier to develop on an running system.

``` bash
```bash
# Start a local Netbox with docker
invoke start
Expand Down
11 changes: 11 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Configuration = {
extends: ['@commitlint/config-conventional'],
rules: {
// Increase to 300 characters for max line length in body
"body-max-line-length": [2, "always", 300],
// Increase to 300 characters for max line length in footer
"footer-max-line-length": [2, "always", 300],
},
};

export default Configuration;
35 changes: 7 additions & 28 deletions develop/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
ARG netbox_ver=latest

FROM python:3.11
FROM netboxcommunity/netbox:${netbox_ver}

ENV PYTHONUNBUFFERED 1

RUN mkdir -p /opt

RUN pip install --upgrade pip\
&& pip install poetry

# -------------------------------------------------------------------------------------
# Install NetBox
# -------------------------------------------------------------------------------------
ARG netbox_ver=master

RUN git clone --single-branch --branch ${netbox_ver} https://github.com/netbox-community/netbox.git /opt/netbox/ && \
cd /opt/netbox/ && \
pip install -r /opt/netbox/requirements.txt

# Make the django-debug-toolbar always visible when DEBUG is enabled,
# except when we're running Django unit-tests.
RUN echo "import sys" >> /opt/netbox/netbox/netbox/settings.py && \
echo "TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'" >> /opt/netbox/netbox/netbox/settings.py && \
echo "DEBUG_TOOLBAR_CONFIG = {'SHOW_TOOLBAR_CALLBACK': lambda _: DEBUG and not TESTING }" >> /opt/netbox/netbox/netbox/settings.py

# -------------------------------------------------------------------------------------
# Install Netbox Plugin
# -------------------------------------------------------------------------------------
RUN mkdir -p /source
WORKDIR /source
COPY . /source
RUN poetry config virtualenvs.create false \
&& poetry install --no-interaction --no-ansi
COPY .. /source

# Install the plugin in netbox
RUN /opt/netbox/venv/bin/pip install -r /source/requirements.txt
RUN /opt/netbox/venv/bin/pip install --editable /source

WORKDIR /opt/netbox/netbox/
2 changes: 1 addition & 1 deletion example/initializers/custom_fields.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
text_field:
type: string
on_objects: ["dcim.models.DeviceType", "dcim.models.Device", "virtualization.models.VirtualMachine"]
on_objects: ["dcim.models.DeviceType", "dcim.models.Device", "virtualization.models.VirtualMachine"]
2 changes: 1 addition & 1 deletion example/plugin_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
netbox-plugin-prometheus-sd @ git+https://github.com/flxpeters/netbox-plugin-prometheus-sd.git@main
netbox-initializers==3.7.*
netbox-initializers==4.0.*
5 changes: 3 additions & 2 deletions netbox_prometheus_sd/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ class NetboxPrometheusSDModelViewSet( # pylint: disable=too-many-ancestors
# Filtersets have been renamed, we support both
# https://github.com/netbox-community/netbox/commit/1024782b9e0abb48f6da65f8248741227d53dbed#diff-d9224204dab475bbe888868c02235b8ef10f07c9201c45c90804d395dc161c40
try:
from ipam.filtersets import IPAddressFilterSet, ServiceFilterSet
from ipam.filtersets import IPAddressFilterSet
from dcim.filtersets import DeviceFilterSet
from virtualization.filtersets import VirtualMachineFilterSet
except ImportError:
from ipam.filters import IPAddressFilterSet, ServiceFilterSet
from ipam.filters import IPAddressFilterSet
from dcim.filters import DeviceFilterSet
from virtualization.filters import VirtualMachineFilterSet
# pylint: enable=ungrouped-imports


from ..filtersets import ServiceFilterSet
from .serializers import (
PrometheusIPAddressSerializer,
PrometheusDeviceSerializer,
Expand Down
49 changes: 49 additions & 0 deletions netbox_prometheus_sd/filtersets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Filtersets have been renamed, we support both
# https://github.com/netbox-community/netbox/commit/1024782b9e0abb48f6da65f8248741227d53dbed#diff-d9224204dab475bbe888868c02235b8ef10f07c9201c45c90804d395dc161c40
from django.db.models import Q
from django.utils.translation import gettext as _

from utilities.filters import MultiValueCharFilter, MultiValueNumberFilter

try:
from ipam.filtersets import ServiceFilterSet as NetboxServiceFilterSet
except ImportError:
from ipam.filters import ServiceFilterSet as NetboxServiceFilterSet


class ServiceFilterSet(NetboxServiceFilterSet):
"""Filter set to support tenancy over the device/VM foreign key.
Tenancy in Netbox is very incosistent and the relationship on its own is defined across many different models. This
means that supporting all layers is nearly impossible without a stronger upstream support. For this reason only the
"first level" tenancy is supported by this filter set.
"""

tenant_id = MultiValueNumberFilter(
method='filter_by_tenant_id',
label=_('Tenant (ID)'),
)

tenant = MultiValueCharFilter(
method='filter_by_tenant_slug',
label=_('Tenant (slug)'),
)

# pylint: disable=unused-argument
def filter_by_cluster_tenant_id(self, queryset, name, value):
return queryset.filter(
Q(device__cluster__tenant_id__in=value) |
Q(virtual_machine__cluster__tenant_id__in=value)
)

def filter_by_cluster_tenant_slug(self, queryset, name, value):
return queryset.filter(
Q(device__cluster__tenant__slug__in=value) |
Q(virtual_machine__cluster__tenant__slug__in=value)
)

def filter_by_tenant_id(self, queryset, name, value):
return queryset.filter(Q(device__tenant_id__in=value) | Q(virtual_machine__tenant_id__in=value))

def filter_by_tenant_slug(self, queryset, name, value):
return queryset.filter(Q(device__tenant__slug__in=value) | Q(virtual_machine__tenant__slug__in=value))
Loading

0 comments on commit b645211

Please sign in to comment.