Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(proxmox): handle AttributeError when Qemu Agent is not available
- Add AttributeError to existing exception handling in _get_vm_hostname()
- Fall back to using vm_name when Qemu Agent is unavailable
- Add test case for AttributeError scenario
- Maintain existing logging behavior for debugging

Fixes #21300

Signed-off-by: puretension <[email protected]>
  • Loading branch information
puretension committed Sep 30, 2025
commit e4715ebe4e9d93b6f898fae6c933614c5f0261fc
3 changes: 2 additions & 1 deletion proxmox/datadog_checks/proxmox/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ def _get_vm_hostname(self, vm_id, vm_name, 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