mirror of
https://github.com/thegeeklab/prometheus-pve-sd.git
synced 2024-06-02 16:09:41 +02:00
Add __meta_pve_ipv4
and __meta_pve_ipv6
labels
- Remove prefer_ipv6 config flag - Keep `__meta_pve_ip` label for backwards compatibility
This commit is contained in:
parent
4749d257d9
commit
7eeb7533d2
|
@ -24,9 +24,6 @@ loop_delay: 300
|
|||
# Can be disabled to run disovery only once.
|
||||
service: true
|
||||
|
||||
# By default ipv4 addresses are preferred
|
||||
prefer_ipv6: false
|
||||
|
||||
exclude_state: []
|
||||
exclude_vmid: []
|
||||
|
||||
|
|
|
@ -82,12 +82,6 @@ class Config():
|
|||
"file": True,
|
||||
"type": environs.Env().bool
|
||||
},
|
||||
"prefer_ipv6": {
|
||||
"default": False,
|
||||
"env": "PREFER_IPV6",
|
||||
"file": True,
|
||||
"type": environs.Env().bool
|
||||
},
|
||||
"exclude_state": {
|
||||
"default": [],
|
||||
"env": "EXCLUDE_STATE",
|
||||
|
|
|
@ -85,7 +85,7 @@ class Discovery():
|
|||
|
||||
return variables
|
||||
|
||||
def _get_ip_address(self, pve_type, pve_node, vmid):
|
||||
def _get_ip_addresses(self, pve_type, pve_node, vmid):
|
||||
|
||||
def validate_ipv4(address: object) -> object:
|
||||
try:
|
||||
|
@ -96,12 +96,15 @@ class Discovery():
|
|||
|
||||
def validate_ipv6(address: object) -> object:
|
||||
try:
|
||||
if ipaddress.ip_address(address) not in ipaddress.ip_network("::1/128"):
|
||||
# Make sure to skip loopback addresses and local-link addresses
|
||||
if ipaddress.ip_address(address) not in ipaddress.ip_network("::1/128") \
|
||||
and ipaddress.ip_address(address) not in ipaddress.ip_network("fe80::/10"):
|
||||
return address
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
address = False
|
||||
ipv4_address = False
|
||||
ipv6_address = False
|
||||
networks = False
|
||||
if pve_type == "qemu":
|
||||
# If qemu agent is enabled, try to gather the IP address
|
||||
|
@ -119,26 +122,45 @@ class Discovery():
|
|||
for network in networks:
|
||||
for ip_address in network["ip-addresses"]:
|
||||
if ip_address["ip-address-type"] == "ipv4":
|
||||
address = validate_ipv4(ip_address["ip-address"])
|
||||
elif ip_address["ip-address-type"] == "ipv6" \
|
||||
and self.config.config["prefer_ipv6"]:
|
||||
address = validate_ipv6(ip_address["ip-address"])
|
||||
ipv4_address = validate_ipv4(ip_address["ip-address"])
|
||||
elif ip_address["ip-address-type"] == "ipv6":
|
||||
ipv6_address = validate_ipv6(ip_address["ip-address"])
|
||||
|
||||
if not address:
|
||||
if not ipv4_address:
|
||||
try:
|
||||
PVE_REQUEST_COUNT_TOTAL.inc()
|
||||
config = self.client.nodes(pve_node).get(pve_type, vmid, "config")
|
||||
sources = [config["net0"], config["ipconfig0"]]
|
||||
if "ipconfig0" in config.keys():
|
||||
sources = [config["net0"], config["ipconfig0"]]
|
||||
else:
|
||||
sources = [config["net0"]]
|
||||
|
||||
for s in sources:
|
||||
find = re.search(r"ip=(\d*\.\d*\.\d*\.\d*)", str(sources))
|
||||
find = re.search(r"ip=(\d*\.\d*\.\d*\.\d*)", str(s))
|
||||
if find and find.group(1):
|
||||
address = find.group(1)
|
||||
ipv4_address = find.group(1)
|
||||
break
|
||||
except Exception: # noqa # nosec
|
||||
pass
|
||||
|
||||
return address
|
||||
if not ipv6_address:
|
||||
try:
|
||||
PVE_REQUEST_COUNT_TOTAL.inc()
|
||||
config = self.client.nodes(pve_node).get(pve_type, vmid, "config")
|
||||
if "ipconfig0" in config.keys():
|
||||
sources = [config["net0"], config["ipconfig0"]]
|
||||
else:
|
||||
sources = [config["net0"]]
|
||||
|
||||
for s in sources:
|
||||
find = re.search(r"ip=(\d*:\d*:\d*:\d*:\d*:\d*)", str(s))
|
||||
if find and find.group(1):
|
||||
ipv6_address = find.group(1)
|
||||
break
|
||||
except Exception: # noqa # nosec
|
||||
pass
|
||||
|
||||
return ipv4_address, ipv6_address
|
||||
|
||||
def _exclude(self, pve_list):
|
||||
filtered = []
|
||||
|
@ -204,9 +226,9 @@ class Discovery():
|
|||
except ValueError:
|
||||
metadata = {"notes": description}
|
||||
|
||||
address = self._get_ip_address(pve_type, node, vmid) or host
|
||||
ipv4_address, ipv6_address = self._get_ip_addresses(pve_type, node, vmid) or host
|
||||
|
||||
prom_host = Host(vmid, host, address, pve_type)
|
||||
prom_host = Host(vmid, host, ipv4_address, ipv6_address, pve_type)
|
||||
|
||||
config_flags = [("cpu", "sockets"), ("cores", "cores"), ("memory", "memory")]
|
||||
meta_flags = [("status", "proxmox_status")]
|
||||
|
|
|
@ -5,19 +5,23 @@
|
|||
class Host:
|
||||
"""Represents a virtual machine or container in PVE."""
|
||||
|
||||
def __init__(self, vmid, hostname, ip_address, pve_type):
|
||||
def __init__(self, vmid, hostname, ipv4_address, ipv6_address, pve_type):
|
||||
self.hostname = hostname
|
||||
self.ip_address = ip_address
|
||||
self.ipv4_address = ipv4_address
|
||||
self.ipv6_address = ipv6_address
|
||||
self.vmid = vmid
|
||||
self.pve_type = pve_type
|
||||
self.labels = {}
|
||||
self.labels["__meta_pve_ip"] = ip_address
|
||||
# kept for backwards compatibility
|
||||
self.labels["__meta_pve_ip"] = ipv4_address
|
||||
self.labels["__meta_pve_ipv4"] = ipv4_address
|
||||
self.labels["__meta_pve_ipv6"] = ipv6_address
|
||||
self.labels["__meta_pve_name"] = hostname
|
||||
self.labels["__meta_pve_type"] = pve_type
|
||||
self.labels["__meta_pve_vmid"] = str(vmid)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.hostname}({self.vmid}): {self.pve_type} {self.ip_address}"
|
||||
return f"{self.hostname}({self.vmid}): {self.pve_type} {self.ipv4_address}"
|
||||
|
||||
def add_label(self, key, value):
|
||||
key = key.replace("-", "_").replace(" ", "_")
|
||||
|
|
Loading…
Reference in New Issue
Block a user