Skip to content

Commit

Permalink
feat: add filter option exclude_tags (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
peschmae authored Mar 3, 2022
1 parent 7f2dc5a commit b5d3624
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/content/configuration/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ exclude_state: []
# needs to be a list of strings
exclude_vmid: []

# can be used to exclude vms by tags (proxmox 6+)
exclude_tags: []

pve:
server:
user:
Expand Down
6 changes: 6 additions & 0 deletions prometheuspvesd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class Config():
"file": True,
"type": environs.Env().list
},
"exclude_tags": {
"default": [],
"env": "EXCLUDE_TAGS",
"file": True,
"type": environs.Env().list
},
"pve.server": {
"default": "",
"env": "PVE_SERVER",
Expand Down
6 changes: 6 additions & 0 deletions prometheuspvesd/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ def _exclude(self, pve_list):
if str(obj["vmid"]) in self.config.config["exclude_vmid"]:
continue

if (
isinstance(obj["tags"], str)
and not set(obj["tags"].split(",")).isdisjoint(self.config.config["exclude_tags"])
):
continue

filtered.append(item.copy())
return filtered

Expand Down
22 changes: 21 additions & 1 deletion prometheuspvesd/test/fixtures/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def qemus():
"disk": 0,
"status": "running",
"netout": 12159205236,
"mem": 496179157
"mem": 496179157,
"tags": "unmonitored,excluded"
},
{
"diskwrite": 0,
Expand All @@ -54,6 +55,25 @@ def qemus():
"netout": 12159205236,
"mem": 496179157
},
{
"diskwrite": 0,
"vmid": "102",
"name": "102.example.com",
"cpu": 0.0202130478509556,
"diskread": 0,
"template": "",
"uptime": 3101505,
"maxdisk": 26843545600,
"maxmem": 1073741824,
"pid": "1765",
"cpus": 1,
"netin": 2856071643,
"disk": 0,
"status": "prelaunch",
"netout": 12159205236,
"mem": 496179157,
"tags": "monitored"
},
]


Expand Down
22 changes: 20 additions & 2 deletions prometheuspvesd/test/unit/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,31 @@ def get_mock(*args):
return False


def test_exclude(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_vmid"] = ["100", "101"]
def test_exclude_vmid(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_vmid"] = ["100", "101", "102"]

expected = []
filtered = discovery_fixture._exclude(qemus)

assert filtered == expected
discovery_fixture.config.config["exclude_vmid"] = []


def test_exclude_state(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_state"] = ["prelaunch"]
filtered = discovery_fixture._exclude(qemus)

assert len(filtered) == 2
discovery_fixture.config.config["exclude_state"] = []


def test_exclude_tags(discovery_fixture, qemus):
discovery_fixture.config.config["exclude_tags"] = ["unmonitored"]

filtered = discovery_fixture._exclude(qemus)

assert len(filtered) == 2
discovery_fixture.config.config["exclude_tags"] = []


def test_validate_ip(discovery_fixture, addresses):
Expand Down

0 comments on commit b5d3624

Please sign in to comment.