Add mac metrics
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending
ci/woodpecker/manual/woodpecker Pipeline is pending

This commit is contained in:
Madis Mägi 2024-09-23 12:53:20 +03:00
parent 282c904088
commit ede6c14e37
2 changed files with 40 additions and 0 deletions

View File

@ -2,6 +2,7 @@
import hashlib import hashlib
import json import json
import secrets import secrets
import threading
import unicodedata import unicodedata
import urllib import urllib
from collections import Counter from collections import Counter
@ -10,6 +11,7 @@ from datetime import datetime, timedelta
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
from functools import wraps from functools import wraps
from http.server import BaseHTTPRequestHandler, HTTPServer
from time import sleep from time import sleep
import bleach import bleach
@ -17,6 +19,7 @@ import flask
import jinja2 import jinja2
import ldap import ldap
import markdown import markdown
import prometheus_client
import pymongo import pymongo
import requests import requests
import safe import safe
@ -26,6 +29,7 @@ from flask_wtf import FlaskForm, RecaptchaField
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
from ldap import modlist from ldap import modlist
from markupsafe import Markup from markupsafe import Markup
from prometheus_client import start_http_server, Gauge, CollectorRegistry, generate_latest
from prometheus_flask_exporter import PrometheusMetrics from prometheus_flask_exporter import PrometheusMetrics
from pymongo import MongoClient from pymongo import MongoClient
from werkzeug import exceptions from werkzeug import exceptions
@ -224,9 +228,44 @@ def view_profile():
def view_hello(): def view_hello():
return "Hello!" return "Hello!"
def get_mac_metrics():
registry = CollectorRegistry()
inventory_item = Gauge('inventory_item_info', 'Information about inventory item', ['mac', 'slug', 'owner', 'user', 'name'], registry=registry)
items = mongodb.inventory.find(
filter = { "mac": { "$exists": True } },
projection = {
"mac": 1,
"name": 1,
"shortener.slug": 1,
"inventory.owner": 1,
"inventory.user": 1,
}
)
for item in items:
inventory_item.labels(mac=item.mac, owner=item.get("owner", {}).get("username"), user=item.get("user", {}).get("username"), slug=item.slug, name=item.name).set(1)
return generate_latest(registry)
class MetricsHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/metrics':
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
self.wfile.write(get_mac_metrics())
else:
self.send_response(404)
self.end_headers()
class MultiCheckboxField(SelectMultipleField): class MultiCheckboxField(SelectMultipleField):
widget = widgets.ListWidget(prefix_label=False) widget = widgets.ListWidget(prefix_label=False)
option_widget = widgets.CheckboxInput() option_widget = widgets.CheckboxInput()
if __name__ == '__main__': if __name__ == '__main__':
if not devenv:
metrics_address = ('', 8000)
httpd = HTTPServer(metrics_address, MetricsHandler)
metrics_thread = threading.Thread(target=httpd.serve_forever)
metrics_thread.daemon = True
metrics_thread.start()
app.run(debug=devenv, host='::') app.run(debug=devenv, host='::')

View File

@ -13,6 +13,7 @@ safe
segno segno
sepa sepa
Flask-WTF Flask-WTF
prometheus-client
prometheus-flask-exporter prometheus-flask-exporter
pymongo pymongo
python-dateutil python-dateutil