Skip to content

Commit fb463bc

Browse files
authored
proxmox: ignore QEMU templates and iron out a few bugs (#8326)
1 parent 97fb465 commit fb463bc

4 files changed

Lines changed: 35 additions & 24 deletions

File tree

plugins/inputs/proxmox/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ Telegraf minimum version: Telegraf 1.16.0
1111
## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /.
1212
base_url = "https://localhost:8006/api2/json"
1313
api_token = "USER@REALM!TOKENID=UUID"
14-
## Optional node name config
15-
# node_name = "localhost"
14+
## Node name, defaults to OS hostname
15+
# node_name = ""
1616

1717
## Optional TLS Config
1818
# tls_ca = "/etc/telegraf/ca.pem"

plugins/inputs/proxmox/proxmox.go

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@ package proxmox
33
import (
44
"encoding/json"
55
"errors"
6-
"github.com/influxdata/telegraf"
7-
"github.com/influxdata/telegraf/plugins/inputs"
86
"io/ioutil"
97
"net/http"
108
"net/url"
119
"os"
1210
"strings"
11+
12+
"github.com/influxdata/telegraf"
13+
"github.com/influxdata/telegraf/plugins/inputs"
1314
)
1415

1516
var sampleConfig = `
1617
## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /.
1718
base_url = "https://localhost:8006/api2/json"
1819
api_token = "USER@REALM!TOKENID=UUID"
20+
## Node name, defaults to OS hostname
21+
# node_name = ""
1922
2023
## Optional TLS Config
2124
# tls_ca = "/etc/telegraf/ca.pem"
@@ -49,9 +52,10 @@ func (px *Proxmox) Gather(acc telegraf.Accumulator) error {
4952
}
5053

5154
func (px *Proxmox) Init() error {
52-
55+
// Set hostname as default node name for backwards compatibility
5356
if px.NodeName == "" {
54-
return errors.New("node_name must be configured")
57+
hostname, _ := os.Hostname()
58+
px.NodeName = hostname
5559
}
5660

5761
tlsCfg, err := px.ClientConfig.TLSConfig()
@@ -69,15 +73,11 @@ func (px *Proxmox) Init() error {
6973
}
7074

7175
func init() {
72-
px := Proxmox{
73-
requestFunction: performRequest,
74-
}
75-
76-
// Set hostname as default node name for backwards compatibility
77-
hostname, _ := os.Hostname()
78-
px.NodeName = hostname
79-
80-
inputs.Add("proxmox", func() telegraf.Input { return &px })
76+
inputs.Add("proxmox", func() telegraf.Input {
77+
return &Proxmox{
78+
requestFunction: performRequest,
79+
}
80+
})
8181
}
8282

8383
func getNodeSearchDomain(px *Proxmox) error {
@@ -94,7 +94,7 @@ func getNodeSearchDomain(px *Proxmox) error {
9494
}
9595

9696
if nodeDns.Data.Searchdomain == "" {
97-
return errors.New("node_name not found")
97+
return errors.New("search domain is not set")
9898
}
9999
px.nodeSearchDomain = nodeDns.Data.Searchdomain
100100

@@ -141,20 +141,28 @@ func gatherVmData(px *Proxmox, acc telegraf.Accumulator, rt ResourceType) {
141141
for _, vmStat := range vmStats.Data {
142142
vmConfig, err := getVmConfig(px, vmStat.ID, rt)
143143
if err != nil {
144-
px.Log.Error("Error getting VM config: %v", err)
144+
px.Log.Errorf("Error getting VM config: %v", err)
145145
return
146146
}
147+
148+
if vmConfig.Data.Template == 1 {
149+
px.Log.Debugf("Ignoring template VM %s (%s)", vmStat.ID, vmStat.Name)
150+
continue
151+
}
152+
147153
tags := getTags(px, vmStat.Name, vmConfig, rt)
148154
currentVMStatus, err := getCurrentVMStatus(px, rt, vmStat.ID)
149155
if err != nil {
150-
px.Log.Error("Error getting VM curent VM status: %v", err)
156+
px.Log.Errorf("Error getting VM curent VM status: %v", err)
151157
return
152158
}
159+
153160
fields, err := getFields(currentVMStatus)
154161
if err != nil {
155-
px.Log.Error("Error getting VM measurements: %v", err)
162+
px.Log.Errorf("Error getting VM measurements: %v", err)
156163
return
157164
}
165+
158166
acc.AddFields("proxmox", fields, tags)
159167
}
160168
}

plugins/inputs/proxmox/proxmox_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package proxmox
22

33
import (
4-
"github.com/bmizerany/assert"
5-
"github.com/influxdata/telegraf/testutil"
6-
"github.com/stretchr/testify/require"
74
"net/url"
85
"strings"
96
"testing"
7+
8+
"github.com/bmizerany/assert"
9+
"github.com/influxdata/telegraf/testutil"
10+
"github.com/stretchr/testify/require"
1011
)
1112

1213
var nodeSearchDomainTestData = `{"data":{"search":"test.example.com","dns1":"1.0.0.1"}}`

plugins/inputs/proxmox/structs.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package proxmox
22

33
import (
44
"encoding/json"
5+
"net/http"
6+
"net/url"
7+
58
"github.com/influxdata/telegraf"
69
"github.com/influxdata/telegraf/internal"
710
"github.com/influxdata/telegraf/plugins/common/tls"
8-
"net/http"
9-
"net/url"
1011
)
1112

1213
type Proxmox struct {
@@ -57,6 +58,7 @@ type VmConfig struct {
5758
Data struct {
5859
Searchdomain string `json:"searchdomain"`
5960
Hostname string `json:"hostname"`
61+
Template int `json:"template"`
6062
} `json:"data"`
6163
}
6264

0 commit comments

Comments
 (0)