Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions proxmox/changelog.d/21399.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle AttributeError when Qemu Agent is not available
5 changes: 3 additions & 2 deletions proxmox/datadog_checks/proxmox/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,16 @@ def _get_vm_hostname(self, vm_id, vm_name, node):
url = f"{self.config.proxmox_server}/nodes/{node}/qemu/{vm_id}/agent/get-host-name"
hostname_response = self.http.get(url)
hostname_json = hostname_response.json()
except (HTTPError, InvalidURL, ConnectionError, Timeout, JSONDecodeError) as e:
except (HTTPError, InvalidURL, ConnectionError, Timeout, JSONDecodeError, AttributeError) as e:
self.log.info(
"Failed to get hostname for vm %s on node %s; endpoint: %s; %s",
vm_id,
node,
self.config.proxmox_server,
e,
)
hostname_json = {}
hostname = vm_name
return hostname
hostname = hostname_json.get("data", {}).get("result", {}).get("host-name", vm_name)
return hostname

Expand Down
30 changes: 30 additions & 0 deletions proxmox/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ def test_get_hostname_error(dd_run_check, aggregator, instance, caplog):
)


class AttributeErrorResponse:
"""Mock response that raises AttributeError when json() is called."""

def json(self):
raise AttributeError("Qemu Agent not available")


@pytest.mark.parametrize(
('mock_http_get'),
[
pytest.param(
{'http_error': {'/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': AttributeErrorResponse()}},
id='attribute_error',
),
],
indirect=['mock_http_get'],
)
@pytest.mark.usefixtures('mock_http_get')
def test_get_hostname_attribute_error(dd_run_check, aggregator, instance, caplog):
"""Test that AttributeError is handled when Qemu Agent is not available."""
check = ProxmoxCheck('proxmox', {}, [instance])
check.check_id = 'test:123'
caplog.set_level(logging.INFO)

dd_run_check(check)

aggregator.assert_metric("proxmox.vm.up", 1, tags=[], hostname="VM 100")
assert "Failed to get hostname for vm 100 on node ip-122-82-3-112" in caplog.text


@pytest.mark.usefixtures('mock_http_get')
def test_external_tags(dd_run_check, aggregator, instance, datadog_agent):
check = ProxmoxCheck('proxmox', {}, [instance])
Expand Down
Loading