aboutsummaryrefslogtreecommitdiffstats
path: root/api/app/api/routes/prometheus.py
blob: 02be2c99683d93d9c1ea02c1d75934dfae2075fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from fastapi import APIRouter, Depends
import json

from app.api.deps import get_cache

router = APIRouter(prefix="/v2/prometheus", tags=["prometheus"])

@router.get("/ping")
async def devices(cache=Depends(get_cache)):
    output = []

    devices = (
        json.loads(cache.get("devices:data")) if cache.exists("devices:data") else {}
    )
    for sysname in devices:
        device = devices[sysname]
        if device["mgmt_v4_addr"]:
            output.append(
                {
                    "targets": [device["mgmt_v4_addr"]],
                    "labels": {"sysname": device["sysname"], "type": "v4"},
                }
            )
        if device["mgmt_v6_addr"]:
            output.append(
                {
                    "targets": [device["mgmt_v6_addr"]],
                    "labels": {"sysname": device["sysname"], "type": "v6"},
                }
            )
    return output


@router.get("/snmp")
async def devices(cache=Depends(get_cache)):
    output = []

    devices = (
        json.loads(cache.get("devices:data")) if cache.exists("devices:data") else {}
    )
    for sysname in devices:
        device = devices[sysname]
        if device["mgmt_v6_addr"]:
            output.append(
                {
                    "targets": [device["mgmt_v6_addr"]],
                    "labels": {
                        "sysname": device["sysname"],
                        "platform": device["platform"],
                    },
                }
            )
        elif device["mgmt_v4_addr"]:
            output.append(
                {
                    "targets": [device["mgmt_v4_addr"]],
                    "labels": {
                        "sysname": device["sysname"],
                        "platform": device["platform"],
                    },
                }
            )
    return output