netbox-plugin-prometheus-http-sd 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. netbox_plugin_prometheus_http_sd-0.1.0/.dockerignore +8 -0
  2. netbox_plugin_prometheus_http_sd-0.1.0/.github/workflows/publish.yml +35 -0
  3. netbox_plugin_prometheus_http_sd-0.1.0/.gitignore +19 -0
  4. netbox_plugin_prometheus_http_sd-0.1.0/LICENSE +201 -0
  5. netbox_plugin_prometheus_http_sd-0.1.0/PKG-INFO +92 -0
  6. netbox_plugin_prometheus_http_sd-0.1.0/README.md +78 -0
  7. netbox_plugin_prometheus_http_sd-0.1.0/docker-compose.yml +84 -0
  8. netbox_plugin_prometheus_http_sd-0.1.0/example/configuration/plugins.py +1 -0
  9. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/cluster_types.yml +2 -0
  10. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/clusters.yml +2 -0
  11. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/custom_fields.yml +8 -0
  12. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/device_roles.yml +3 -0
  13. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/device_types.yml +3 -0
  14. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/devices.yml +27 -0
  15. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/interfaces.yml +12 -0
  16. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/ip_addresses.yml +24 -0
  17. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/manufacturers.yml +2 -0
  18. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/sites.yml +4 -0
  19. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/tenants.yml +2 -0
  20. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/virtual_machines.yml +33 -0
  21. netbox_plugin_prometheus_http_sd-0.1.0/example/initializers/virtualization_interfaces.yml +9 -0
  22. netbox_plugin_prometheus_http_sd-0.1.0/example/prometheus.yml +17 -0
  23. netbox_plugin_prometheus_http_sd-0.1.0/netbox_plugin_prometheus_http_sd.egg-info/PKG-INFO +92 -0
  24. netbox_plugin_prometheus_http_sd-0.1.0/netbox_plugin_prometheus_http_sd.egg-info/SOURCES.txt +32 -0
  25. netbox_plugin_prometheus_http_sd-0.1.0/netbox_plugin_prometheus_http_sd.egg-info/dependency_links.txt +1 -0
  26. netbox_plugin_prometheus_http_sd-0.1.0/netbox_plugin_prometheus_http_sd.egg-info/top_level.txt +1 -0
  27. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/__init__.py +15 -0
  28. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/_version.py +24 -0
  29. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/api/__init__.py +0 -0
  30. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/api/urls.py +6 -0
  31. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/api/views.py +101 -0
  32. netbox_plugin_prometheus_http_sd-0.1.0/netbox_prometheus_sd/urls.py +1 -0
  33. netbox_plugin_prometheus_http_sd-0.1.0/pyproject.toml +24 -0
  34. netbox_plugin_prometheus_http_sd-0.1.0/setup.cfg +4 -0
@@ -0,0 +1,8 @@
1
+ # VCS
2
+ .git/
3
+ .gitignore
4
+ .gitattributes
5
+
6
+ # Docker
7
+ .dockerignore
8
+ docker-compose.yml
@@ -0,0 +1,35 @@
1
+ name: Publish on PyPi
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - "*"
7
+
8
+ jobs:
9
+ publish:
10
+ name: Publish on PyPi
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - name: Checkout code
15
+ uses: actions/checkout@v4
16
+ with:
17
+ fetch-depth: 0
18
+
19
+ - name: Set up Python
20
+ uses: actions/setup-python@v5
21
+ with:
22
+ python-version: "3.12"
23
+
24
+ - name: Install dependencies
25
+ run: |
26
+ python -m pip install --upgrade pip
27
+ pip install build twine setuptools_scm
28
+
29
+ - name: Build package
30
+ run: python -m build
31
+
32
+ - name: Publish
33
+ uses: pypa/gh-action-pypi-publish@release/v1
34
+ with:
35
+ password: ${{ secrets.PYPI_API_TOKEN }}
@@ -0,0 +1,19 @@
1
+ # Python
2
+ .venv
3
+ __pycache__/
4
+ *.py[cod]
5
+ *.egg-info/
6
+ *.egg
7
+ dist/
8
+ build/
9
+ *.whl
10
+
11
+ # IDE
12
+ .idea/
13
+ .vscode/
14
+ *.swp
15
+ *.swo
16
+ *~
17
+
18
+ # setuptools
19
+ netbox_prometheus_sd/_version.py
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.4
2
+ Name: netbox-plugin-prometheus-http-sd
3
+ Version: 0.1.0
4
+ Summary: A Netbox plugin to export targets for Prometheus HTTP service discovery
5
+ Author-email: Artem Tuchinsky <tuchinsky@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Repository, https://github.com/tuchinsky/netbox-plugin-prometheus-http-sd
8
+ Keywords: netbox,prometheus
9
+ Classifier: Programming Language :: Python :: 3
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Dynamic: license-file
14
+
15
+ # netbox-plugin-prometheus-http-sd
16
+
17
+ ## Demo
18
+
19
+ ```bash
20
+ docker-compose up -d
21
+
22
+ docker exec netbox-plugin-prometheus-http-sd-netbox-1 /opt/netbox/netbox/manage.py load_initializer_data --path /etc/netbox/initializer-data
23
+ ```
24
+
25
+ Login into Netbox (http://localhost:8080) and create **Config Context** named `prometheus_role_map` with the following JSON structure:
26
+ ```json
27
+ {
28
+ "prometheus_exporters": {
29
+ "default": [
30
+ {
31
+ "job": "node-exporter",
32
+ "port": 9100
33
+ }
34
+ ],
35
+ "etcd": [
36
+ {
37
+ "job": "node-exporter",
38
+ "port": 9100
39
+ },
40
+ {
41
+ "job": "etcd-exporter",
42
+ "port": 2379
43
+ }
44
+ ],
45
+ "postgresql": [
46
+ {
47
+ "job": "node-exporter",
48
+ "port": 9100
49
+ },
50
+ {
51
+ "job": "postgresql-exporter",
52
+ "port": 9187
53
+ }
54
+ ],
55
+ "vault": [
56
+ {
57
+ "job": "vault-exporter",
58
+ "scheme": "https",
59
+ "metrics_path": "/v1/sys/metrics",
60
+ "params": {
61
+ "format": [
62
+ "prometheus"
63
+ ]
64
+ },
65
+ "port": 8200
66
+ }
67
+ ]
68
+ }
69
+ }
70
+ ```
71
+ Each role can have multiple jobs (one target per job is generated)
72
+
73
+ If role is not found, the `default` entry is used
74
+
75
+ ## Usage
76
+
77
+ The plugin exposes a single API endpoint (no web UI):
78
+ ```
79
+ GET /api/plugins/prometheus-sd/targets/
80
+ ```
81
+
82
+ Device or Virtual Machine is included only if all of the following are true:
83
+ 1. It has status `active`
84
+ 2. The custom field `role` is present and non‑empty
85
+
86
+ ## Metrics labels
87
+
88
+ Labels:
89
+ * `job` – Prometheus job name defined in the mapping
90
+ * `__meta_netbox_name` – object name
91
+ * `__meta_netbox_type` – `device` or `vm`
92
+ * `__meta_netbox_cf_<custom_field>` – every custom field prefixed with `__meta_netbox_cf_`
@@ -0,0 +1,78 @@
1
+ # netbox-plugin-prometheus-http-sd
2
+
3
+ ## Demo
4
+
5
+ ```bash
6
+ docker-compose up -d
7
+
8
+ docker exec netbox-plugin-prometheus-http-sd-netbox-1 /opt/netbox/netbox/manage.py load_initializer_data --path /etc/netbox/initializer-data
9
+ ```
10
+
11
+ Login into Netbox (http://localhost:8080) and create **Config Context** named `prometheus_role_map` with the following JSON structure:
12
+ ```json
13
+ {
14
+ "prometheus_exporters": {
15
+ "default": [
16
+ {
17
+ "job": "node-exporter",
18
+ "port": 9100
19
+ }
20
+ ],
21
+ "etcd": [
22
+ {
23
+ "job": "node-exporter",
24
+ "port": 9100
25
+ },
26
+ {
27
+ "job": "etcd-exporter",
28
+ "port": 2379
29
+ }
30
+ ],
31
+ "postgresql": [
32
+ {
33
+ "job": "node-exporter",
34
+ "port": 9100
35
+ },
36
+ {
37
+ "job": "postgresql-exporter",
38
+ "port": 9187
39
+ }
40
+ ],
41
+ "vault": [
42
+ {
43
+ "job": "vault-exporter",
44
+ "scheme": "https",
45
+ "metrics_path": "/v1/sys/metrics",
46
+ "params": {
47
+ "format": [
48
+ "prometheus"
49
+ ]
50
+ },
51
+ "port": 8200
52
+ }
53
+ ]
54
+ }
55
+ }
56
+ ```
57
+ Each role can have multiple jobs (one target per job is generated)
58
+
59
+ If role is not found, the `default` entry is used
60
+
61
+ ## Usage
62
+
63
+ The plugin exposes a single API endpoint (no web UI):
64
+ ```
65
+ GET /api/plugins/prometheus-sd/targets/
66
+ ```
67
+
68
+ Device or Virtual Machine is included only if all of the following are true:
69
+ 1. It has status `active`
70
+ 2. The custom field `role` is present and non‑empty
71
+
72
+ ## Metrics labels
73
+
74
+ Labels:
75
+ * `job` – Prometheus job name defined in the mapping
76
+ * `__meta_netbox_name` – object name
77
+ * `__meta_netbox_type` – `device` or `vm`
78
+ * `__meta_netbox_cf_<custom_field>` – every custom field prefixed with `__meta_netbox_cf_`
@@ -0,0 +1,84 @@
1
+ services:
2
+ netbox:
3
+ image: netboxcommunity/netbox:${VERSION-v4.5-4.0.2}
4
+ user: "root:root" # only for pip
5
+ environment:
6
+ CORS_ORIGIN_ALLOW_ALL: True
7
+ DB_HOST: postgres
8
+ DB_NAME: netbox
9
+ DB_PASSWORD: netbox
10
+ DB_USER: netbox
11
+ DEBUG: 1
12
+ DB_WAIT_DEBUG: 1
13
+ HOUSEKEEPING_INTERVAL: 86400
14
+ MEDIA_ROOT: /opt/netbox/netbox/media
15
+ METRICS_ENABLED: true
16
+ WEBHOOKS_ENABLED: true
17
+ REDIS_CACHE_DATABASE: 1
18
+ REDIS_CACHE_HOST: redis
19
+ REDIS_CACHE_SSL: false
20
+ REDIS_DATABASE: 0
21
+ REDIS_HOST: redis
22
+ REDIS_SSL: false
23
+ RELEASE_CHECK_URL: https://api.github.com/repos/netbox-community/netbox/releases
24
+ SECRET_KEY: 'r(m)9nLGnz$(_q3N4z1k(EFsMCjjjzx08x9VhNVcfd%6RF#r!6DE@+V5Zk2X'
25
+ SKIP_STARTUP_SCRIPTS: false
26
+ SKIP_SUPERUSER: false
27
+ SUPERUSER_API_TOKEN: 0123456789abcdef0123456789abcdef01234567
28
+ SUPERUSER_EMAIL: admin@example.com
29
+ SUPERUSER_NAME: admin
30
+ SUPERUSER_PASSWORD: admin
31
+ command: >
32
+ sh -c "/usr/local/bin/uv pip install netbox-initializers==4.5.1 &&
33
+ uv pip install -e /opt/netbox/netbox/netbox_prometheus_sd &&
34
+ /opt/netbox/docker-entrypoint.sh /opt/netbox/launch-netbox.sh"
35
+ volumes:
36
+ - ./example/configuration/plugins.py:/etc/netbox/config/plugins.py:z,ro
37
+ - ./example/initializers:/etc/netbox/initializer-data:z,ro
38
+ - ./netbox_prometheus_sd:/opt/netbox/netbox/netbox_prometheus_sd
39
+ ports:
40
+ - "8080:8080"
41
+ healthcheck:
42
+ test: curl -f http://localhost:8080/login/ || exit 1
43
+ start_period: 20s
44
+ timeout: 5s
45
+ interval: 10s
46
+ retries: 5
47
+ depends_on:
48
+ postgres:
49
+ condition: service_healthy
50
+ redis:
51
+ condition: service_started
52
+
53
+ postgres:
54
+ image: postgres:15-alpine
55
+ environment:
56
+ POSTGRES_DB: netbox
57
+ POSTGRES_PASSWORD: netbox
58
+ POSTGRES_USER: netbox
59
+ volumes:
60
+ - netbox-postgres-data:/var/lib/postgresql/data
61
+ healthcheck:
62
+ test: pg_isready -q -t 2 -d $$POSTGRES_DB -U $$POSTGRES_USER
63
+ start_period: 20s
64
+ timeout: 30s
65
+ interval: 10s
66
+ retries: 5
67
+
68
+ redis:
69
+ image: redis:7-alpine
70
+
71
+ prometheus:
72
+ image: prom/prometheus:v3.11.3
73
+ volumes:
74
+ - ./example/prometheus.yml:/etc/prometheus/prometheus.yml:ro,z
75
+ command:
76
+ - "--config.file=/etc/prometheus/prometheus.yml"
77
+ ports:
78
+ - 9090:9090
79
+ depends_on:
80
+ netbox:
81
+ condition: service_started
82
+
83
+ volumes:
84
+ netbox-postgres-data:
@@ -0,0 +1 @@
1
+ PLUGINS = ["netbox_initializers", "netbox_prometheus_sd"]
@@ -0,0 +1,2 @@
1
+ - name: Proxmox
2
+ slug: proxmox
@@ -0,0 +1,2 @@
1
+ - name: Default
2
+ type: Proxmox
@@ -0,0 +1,8 @@
1
+ service:
2
+ type: string
3
+ weight: 100
4
+ on_objects: ["dcim.models.Device", "virtualization.models.VirtualMachine"]
5
+ role:
6
+ type: string
7
+ weight: 101
8
+ on_objects: ["dcim.models.Device", "virtualization.models.VirtualMachine"]
@@ -0,0 +1,3 @@
1
+ - name: Server
2
+ slug: server
3
+ color: Grey
@@ -0,0 +1,3 @@
1
+ - model: Default
2
+ manufacturer: No Name
3
+ slug: default
@@ -0,0 +1,27 @@
1
+ - name: Server01
2
+ role: Server
3
+ device_type: Default
4
+ site: Default
5
+ primary_ip4: 192.168.1.1/24
6
+ status: active
7
+ custom_field_data:
8
+ service: proxmox
9
+ role: pve
10
+ - name: Server02
11
+ role: Server
12
+ device_type: Default
13
+ site: Default
14
+ primary_ip4: 192.168.1.2/24
15
+ status: staged
16
+ custom_field_data:
17
+ service: proxmox
18
+ role: pve
19
+ - name: Server03
20
+ role: Server
21
+ device_type: Default
22
+ site: Default
23
+ primary_ip4: 192.168.1.3/24
24
+ status: offline
25
+ custom_field_data:
26
+ service: proxmox
27
+ role: pve
@@ -0,0 +1,12 @@
1
+ - device: Server01
2
+ enabled: true
3
+ type: 1000base-t
4
+ name: eth0
5
+ - device: Server02
6
+ enabled: true
7
+ type: 1000base-t
8
+ name: eth0
9
+ - device: Server03
10
+ enabled: true
11
+ type: 1000base-t
12
+ name: eth0
@@ -0,0 +1,24 @@
1
+ - address: 192.168.1.1/24
2
+ device: Server01
3
+ interface: eth0
4
+ status: active
5
+ - address: 192.168.1.2/24
6
+ device: Server02
7
+ interface: eth0
8
+ status: active
9
+ - address: 192.168.1.3/24
10
+ device: Server03
11
+ interface: eth0
12
+ status: active
13
+ - address: 192.168.1.4/24
14
+ virtual_machine: VM01
15
+ interface: eth0
16
+ status: active
17
+ - address: 192.168.1.5/24
18
+ virtual_machine: VM02
19
+ interface: eth0
20
+ status: active
21
+ - address: 192.168.1.6/24
22
+ virtual_machine: VM02
23
+ interface: eth0
24
+ status: active
@@ -0,0 +1,2 @@
1
+ - name: No Name
2
+ slug: no-name
@@ -0,0 +1,4 @@
1
+ - name: Default
2
+ slug: default
3
+ status: active
4
+ tenant: Default
@@ -0,0 +1,2 @@
1
+ - name: Default
2
+ slug: default
@@ -0,0 +1,33 @@
1
+ - name: VM01
2
+ cluster: Default
3
+ vcpus: 2
4
+ memory: 4096
5
+ disk: 100
6
+ primary_ip4: 192.168.1.4/24
7
+ status: active
8
+ tenant: Default
9
+ custom_field_data:
10
+ service: billing-postgresql
11
+ role: postgresql
12
+ - name: VM02
13
+ cluster: Default
14
+ vcpus: 4
15
+ memory: 8192
16
+ disk: 200
17
+ primary_ip4: 192.168.1.5/24
18
+ status: active
19
+ tenant: Default
20
+ custom_field_data:
21
+ service: billing-postgresql
22
+ role: etcd
23
+ - name: VM03
24
+ cluster: Default
25
+ vcpus: 8
26
+ memory: 16384
27
+ disk: 300
28
+ primary_ip4: 192.168.1.6/24
29
+ status: active
30
+ tenant: Default
31
+ custom_field_data:
32
+ service: vault
33
+ role: vault
@@ -0,0 +1,9 @@
1
+ - virtual_machine: VM01
2
+ name: eth0
3
+ enabled: true
4
+ - virtual_machine: VM02
5
+ name: eth0
6
+ enabled: true
7
+ - virtual_machine: VM03
8
+ name: eth0
9
+ enabled: true
@@ -0,0 +1,17 @@
1
+ global:
2
+ scrape_interval: 15s
3
+ evaluation_interval: 15s
4
+
5
+ scrape_configs:
6
+ - job_name: "prometheus"
7
+ scrape_interval: 5s
8
+ static_configs:
9
+ - targets: ["localhost:9090"]
10
+
11
+ - job_name: "netbox-sd"
12
+ http_sd_configs:
13
+ - url: http://netbox:8080/api/plugins/prometheus-sd/targets/
14
+ refresh_interval: 15s
15
+ relabel_configs:
16
+ - source_labels: [__meta_netbox_name]
17
+ target_label: instance
@@ -0,0 +1,92 @@
1
+ Metadata-Version: 2.4
2
+ Name: netbox-plugin-prometheus-http-sd
3
+ Version: 0.1.0
4
+ Summary: A Netbox plugin to export targets for Prometheus HTTP service discovery
5
+ Author-email: Artem Tuchinsky <tuchinsky@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Repository, https://github.com/tuchinsky/netbox-plugin-prometheus-http-sd
8
+ Keywords: netbox,prometheus
9
+ Classifier: Programming Language :: Python :: 3
10
+ Requires-Python: >=3.8
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Dynamic: license-file
14
+
15
+ # netbox-plugin-prometheus-http-sd
16
+
17
+ ## Demo
18
+
19
+ ```bash
20
+ docker-compose up -d
21
+
22
+ docker exec netbox-plugin-prometheus-http-sd-netbox-1 /opt/netbox/netbox/manage.py load_initializer_data --path /etc/netbox/initializer-data
23
+ ```
24
+
25
+ Login into Netbox (http://localhost:8080) and create **Config Context** named `prometheus_role_map` with the following JSON structure:
26
+ ```json
27
+ {
28
+ "prometheus_exporters": {
29
+ "default": [
30
+ {
31
+ "job": "node-exporter",
32
+ "port": 9100
33
+ }
34
+ ],
35
+ "etcd": [
36
+ {
37
+ "job": "node-exporter",
38
+ "port": 9100
39
+ },
40
+ {
41
+ "job": "etcd-exporter",
42
+ "port": 2379
43
+ }
44
+ ],
45
+ "postgresql": [
46
+ {
47
+ "job": "node-exporter",
48
+ "port": 9100
49
+ },
50
+ {
51
+ "job": "postgresql-exporter",
52
+ "port": 9187
53
+ }
54
+ ],
55
+ "vault": [
56
+ {
57
+ "job": "vault-exporter",
58
+ "scheme": "https",
59
+ "metrics_path": "/v1/sys/metrics",
60
+ "params": {
61
+ "format": [
62
+ "prometheus"
63
+ ]
64
+ },
65
+ "port": 8200
66
+ }
67
+ ]
68
+ }
69
+ }
70
+ ```
71
+ Each role can have multiple jobs (one target per job is generated)
72
+
73
+ If role is not found, the `default` entry is used
74
+
75
+ ## Usage
76
+
77
+ The plugin exposes a single API endpoint (no web UI):
78
+ ```
79
+ GET /api/plugins/prometheus-sd/targets/
80
+ ```
81
+
82
+ Device or Virtual Machine is included only if all of the following are true:
83
+ 1. It has status `active`
84
+ 2. The custom field `role` is present and non‑empty
85
+
86
+ ## Metrics labels
87
+
88
+ Labels:
89
+ * `job` – Prometheus job name defined in the mapping
90
+ * `__meta_netbox_name` – object name
91
+ * `__meta_netbox_type` – `device` or `vm`
92
+ * `__meta_netbox_cf_<custom_field>` – every custom field prefixed with `__meta_netbox_cf_`
@@ -0,0 +1,32 @@
1
+ .dockerignore
2
+ .gitignore
3
+ LICENSE
4
+ README.md
5
+ docker-compose.yml
6
+ pyproject.toml
7
+ .github/workflows/publish.yml
8
+ example/prometheus.yml
9
+ example/configuration/plugins.py
10
+ example/initializers/cluster_types.yml
11
+ example/initializers/clusters.yml
12
+ example/initializers/custom_fields.yml
13
+ example/initializers/device_roles.yml
14
+ example/initializers/device_types.yml
15
+ example/initializers/devices.yml
16
+ example/initializers/interfaces.yml
17
+ example/initializers/ip_addresses.yml
18
+ example/initializers/manufacturers.yml
19
+ example/initializers/sites.yml
20
+ example/initializers/tenants.yml
21
+ example/initializers/virtual_machines.yml
22
+ example/initializers/virtualization_interfaces.yml
23
+ netbox_plugin_prometheus_http_sd.egg-info/PKG-INFO
24
+ netbox_plugin_prometheus_http_sd.egg-info/SOURCES.txt
25
+ netbox_plugin_prometheus_http_sd.egg-info/dependency_links.txt
26
+ netbox_plugin_prometheus_http_sd.egg-info/top_level.txt
27
+ netbox_prometheus_sd/__init__.py
28
+ netbox_prometheus_sd/_version.py
29
+ netbox_prometheus_sd/urls.py
30
+ netbox_prometheus_sd/api/__init__.py
31
+ netbox_prometheus_sd/api/urls.py
32
+ netbox_prometheus_sd/api/views.py
@@ -0,0 +1,15 @@
1
+ from netbox.plugins import PluginConfig
2
+
3
+ try:
4
+ from ._version import version as __version__
5
+ except ImportError:
6
+ __version__ = "0.0.0.dev0"
7
+
8
+ class NetBoxPrometheusSDConfig(PluginConfig):
9
+ name = 'netbox_prometheus_sd'
10
+ verbose_name = 'NetBox Prometheus SD'
11
+ description = 'A Netbox plugin to export targets for Prometheus HTTP service discovery'
12
+ version = __version__
13
+ base_url = 'prometheus-sd'
14
+
15
+ config = NetBoxPrometheusSDConfig
@@ -0,0 +1,24 @@
1
+ # file generated by vcs-versioning
2
+ # don't change, don't track in version control
3
+ from __future__ import annotations
4
+
5
+ __all__ = [
6
+ "__version__",
7
+ "__version_tuple__",
8
+ "version",
9
+ "version_tuple",
10
+ "__commit_id__",
11
+ "commit_id",
12
+ ]
13
+
14
+ version: str
15
+ __version__: str
16
+ __version_tuple__: tuple[int | str, ...]
17
+ version_tuple: tuple[int | str, ...]
18
+ commit_id: str | None
19
+ __commit_id__: str | None
20
+
21
+ __version__ = version = '0.1.0'
22
+ __version_tuple__ = version_tuple = (0, 1, 0)
23
+
24
+ __commit_id__ = commit_id = 'gffa0468d8'
@@ -0,0 +1,6 @@
1
+ from django.urls import path
2
+ from . import views
3
+
4
+ urlpatterns = [
5
+ path('targets/', views.PrometheusTargetsView.as_view(), name='prometheus-target-list'),
6
+ ]
@@ -0,0 +1,101 @@
1
+ from rest_framework.views import APIView
2
+ from rest_framework.response import Response
3
+ from dcim.models import Device
4
+ from virtualization.models import VirtualMachine
5
+ from extras.models import ConfigContext
6
+
7
+ # 'ip' or 'dns'
8
+ TARGET_SOURCE = 'ip'
9
+
10
+ class PrometheusTargetsView(APIView):
11
+ permission_classes = []
12
+
13
+ def get(self, request, format=None):
14
+ mapping = self._get_prometheus_mapping()
15
+ targets = []
16
+
17
+ for device in Device.objects.filter(status='active'):
18
+ if not device.custom_field_data or 'role' not in device.custom_field_data:
19
+ continue
20
+
21
+ ip = device.primary_ip4 or device.primary_ip6
22
+ if TARGET_SOURCE == 'ip' and not ip:
23
+ continue
24
+
25
+ self._add_targets(
26
+ targets, ip, device.name, 'device', mapping,
27
+ device.custom_field_data
28
+ )
29
+
30
+ for vm in VirtualMachine.objects.filter(status='active'):
31
+ if not vm.custom_field_data or 'role' not in vm.custom_field_data:
32
+ continue
33
+
34
+ ip = vm.primary_ip4 or vm.primary_ip6
35
+ if TARGET_SOURCE == 'ip' and not ip:
36
+ continue
37
+
38
+ self._add_targets(
39
+ targets, ip, vm.name, 'vm', mapping,
40
+ vm.custom_field_data
41
+ )
42
+
43
+ return Response(targets)
44
+
45
+ def _get_prometheus_mapping(self):
46
+ context = ConfigContext.objects.filter(name='prometheus_role_map').first()
47
+ if context and context.data:
48
+ return context.data.get('prometheus_exporters', {})
49
+ return {
50
+ 'default': [{'job': 'node-exporter', 'port': 9100}]
51
+ }
52
+
53
+ def _add_targets(self, targets, ip, name, obj_type, mapping, cf_data):
54
+ role = cf_data['role']
55
+ exporters = mapping.get(role, mapping.get('default', []))
56
+
57
+ for exporter in exporters:
58
+ job = exporter['job']
59
+ port = exporter['port']
60
+ scheme = exporter.get('scheme', 'http')
61
+ metrics_path = exporter.get('metrics_path', '/metrics')
62
+ params = exporter.get('params', {})
63
+
64
+ if TARGET_SOURCE == 'dns':
65
+ address = name
66
+ elif TARGET_SOURCE == 'ip':
67
+ address = str(ip.address.ip)
68
+ else:
69
+ raise ValueError(
70
+ f"Incorrect value TARGET_SOURCE: '{TARGET_SOURCE}'. "
71
+ "Acceptable values: 'ip', 'dns'."
72
+ )
73
+
74
+ # default labels
75
+ labels = {
76
+ "job": job,
77
+ "__meta_netbox_name": name,
78
+ "__meta_netbox_type": obj_type,
79
+ }
80
+
81
+ # change scheme if it not default (http)
82
+ if scheme != 'http':
83
+ labels['__scheme__'] = scheme
84
+
85
+ # change metrics path if it not default (/metrics)
86
+ if metrics_path != '/metrics':
87
+ labels['__metrics_path__'] = metrics_path
88
+
89
+ # params to scrape request
90
+ for param_name, param_values in params.items():
91
+ if param_values:
92
+ labels[f'__param_{param_name}'] = param_values[0]
93
+
94
+ # add all custom fields as labels with prefix '__meta_netbox_cf_'
95
+ for cf_key, cf_value in cf_data.items():
96
+ labels[f"__meta_netbox_cf_{cf_key}"] = str(cf_value)
97
+
98
+ targets.append({
99
+ "targets": [f"{address}:{port}"],
100
+ "labels": labels
101
+ })
@@ -0,0 +1 @@
1
+ urlpatterns = []
@@ -0,0 +1,24 @@
1
+ [project]
2
+ name = "netbox-plugin-prometheus-http-sd"
3
+ dynamic = ["version"]
4
+ description = "A Netbox plugin to export targets for Prometheus HTTP service discovery"
5
+ readme = "README.md"
6
+ license = "Apache-2.0"
7
+ authors = [{ name = "Artem Tuchinsky", email = "tuchinsky@gmail.com" }]
8
+ keywords = ["netbox", "prometheus"]
9
+ classifiers = [
10
+ "Programming Language :: Python :: 3",
11
+ ]
12
+ requires-python = ">=3.8"
13
+ dependencies = []
14
+
15
+ [project.urls]
16
+ Repository = "https://github.com/tuchinsky/netbox-plugin-prometheus-http-sd"
17
+
18
+ [build-system]
19
+ requires = ["setuptools>=61", "wheel", "setuptools-scm>=8"]
20
+ build-backend = "setuptools.build_meta"
21
+
22
+ [tool.setuptools_scm]
23
+ version_file = "netbox_prometheus_sd/_version.py"
24
+ local_scheme = "no-local-version"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+