From 5e51e4e051c6d4af801a8077eccc02aa931a88f1 Mon Sep 17 00:00:00 2001 From: Sven Rebhan <36194019+srebhan@users.noreply.github.com> Date: Tue, 18 Feb 2025 16:27:25 +0100 Subject: [PATCH] fix(inputs.proxmox): Allow search domain to be empty (#16511) --- plugins/inputs/proxmox/README.md | 4 +--- plugins/inputs/proxmox/proxmox.go | 34 +++++++++++++++--------------- plugins/inputs/proxmox/sample.conf | 4 +--- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/plugins/inputs/proxmox/README.md b/plugins/inputs/proxmox/README.md index f22d8ac23..0ecc237bb 100644 --- a/plugins/inputs/proxmox/README.md +++ b/plugins/inputs/proxmox/README.md @@ -25,9 +25,7 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details. api_token = "USER@REALM!TOKENID=UUID" ## Node name, defaults to OS hostname - ## Unless Telegraf is on the same host as Proxmox, setting this is required - ## for Telegraf to successfully connect to Proxmox. If not on the same host, - ## leaving this empty will often lead to a "search domain is not set" error. + ## Unless Telegraf is on the same host as Proxmox, setting this is required. # node_name = "" ## Additional tags of the VM stats data to add as a tag diff --git a/plugins/inputs/proxmox/proxmox.go b/plugins/inputs/proxmox/proxmox.go index 40dcdcc5b..a0534276f 100644 --- a/plugins/inputs/proxmox/proxmox.go +++ b/plugins/inputs/proxmox/proxmox.go @@ -4,7 +4,6 @@ package proxmox import ( _ "embed" "encoding/json" - "errors" "fmt" "io" "net/http" @@ -78,7 +77,7 @@ func (px *Proxmox) Init() error { func (px *Proxmox) Gather(acc telegraf.Accumulator) error { if err := px.getNodeSearchDomain(); err != nil { - return err + return fmt.Errorf("getting search domain failed: %w", err) } px.gatherVMData(acc, lxc) @@ -91,17 +90,12 @@ func (px *Proxmox) getNodeSearchDomain() error { apiURL := "/nodes/" + px.NodeName + "/dns" jsonData, err := px.requestFunction(apiURL, http.MethodGet, nil) if err != nil { - return err + return fmt.Errorf("requesting data failed: %w", err) } var nodeDNS nodeDNS - err = json.Unmarshal(jsonData, &nodeDNS) - if err != nil { - return err - } - - if nodeDNS.Data.Searchdomain == "" { - return errors.New("search domain is not set") + if err := json.Unmarshal(jsonData, &nodeDNS); err != nil { + return fmt.Errorf("decoding message failed: %w", err) } px.nodeSearchDomain = nodeDNS.Data.Searchdomain @@ -154,20 +148,27 @@ func (px *Proxmox) gatherVMData(acc telegraf.Accumulator, rt resourceType) { return } - hostname := vmConfig.Data.Hostname - if hostname == "" { - hostname = vmStat.Name + vmFQDN := vmConfig.Data.Hostname + if vmFQDN == "" { + vmFQDN = vmStat.Name } domain := vmConfig.Data.Searchdomain if domain == "" { domain = px.nodeSearchDomain } - fqdn := hostname + "." + domain + if domain != "" { + vmFQDN += "." + domain + } + + nodeFQDN := px.NodeName + if px.nodeSearchDomain != "" { + nodeFQDN += "." + domain + } tags := map[string]string{ - "node_fqdn": px.NodeName + "." + px.nodeSearchDomain, + "node_fqdn": nodeFQDN, "vm_name": vmStat.Name, - "vm_fqdn": fqdn, + "vm_fqdn": vmFQDN, "vm_type": string(rt), } if slices.Contains(px.AdditionalVmstatsTags, "vmid") { @@ -204,7 +205,6 @@ func (px *Proxmox) gatherVMData(acc telegraf.Accumulator, rt resourceType) { func (px *Proxmox) getCurrentVMStatus(rt resourceType, id json.Number) (vmStat, error) { apiURL := "/nodes/" + px.NodeName + "/" + string(rt) + "/" + string(id) + "/status/current" - jsonData, err := px.requestFunction(apiURL, http.MethodGet, nil) if err != nil { return vmStat{}, err diff --git a/plugins/inputs/proxmox/sample.conf b/plugins/inputs/proxmox/sample.conf index 2eac6dcc9..0a961fefa 100644 --- a/plugins/inputs/proxmox/sample.conf +++ b/plugins/inputs/proxmox/sample.conf @@ -6,9 +6,7 @@ api_token = "USER@REALM!TOKENID=UUID" ## Node name, defaults to OS hostname - ## Unless Telegraf is on the same host as Proxmox, setting this is required - ## for Telegraf to successfully connect to Proxmox. If not on the same host, - ## leaving this empty will often lead to a "search domain is not set" error. + ## Unless Telegraf is on the same host as Proxmox, setting this is required. # node_name = "" ## Additional tags of the VM stats data to add as a tag