proxmox: ignore QEMU templates and iron out a few bugs (#8326)

This commit is contained in:
Krzysztof Dąbrowski 2020-11-13 00:12:29 +01:00 committed by GitHub
parent 97fb465c2d
commit fb463bcc17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 24 deletions

View File

@ -11,8 +11,8 @@ Telegraf minimum version: Telegraf 1.16.0
## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /. ## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /.
base_url = "https://localhost:8006/api2/json" base_url = "https://localhost:8006/api2/json"
api_token = "USER@REALM!TOKENID=UUID" api_token = "USER@REALM!TOKENID=UUID"
## Optional node name config ## Node name, defaults to OS hostname
# node_name = "localhost" # node_name = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"

View File

@ -3,19 +3,22 @@ package proxmox
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"strings" "strings"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
) )
var sampleConfig = ` var sampleConfig = `
## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /. ## API connection configuration. The API token was introduced in Proxmox v6.2. Required permissions for user and token: PVEAuditor role on /.
base_url = "https://localhost:8006/api2/json" base_url = "https://localhost:8006/api2/json"
api_token = "USER@REALM!TOKENID=UUID" api_token = "USER@REALM!TOKENID=UUID"
## Node name, defaults to OS hostname
# node_name = ""
## Optional TLS Config ## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem" # tls_ca = "/etc/telegraf/ca.pem"
@ -49,9 +52,10 @@ func (px *Proxmox) Gather(acc telegraf.Accumulator) error {
} }
func (px *Proxmox) Init() error { func (px *Proxmox) Init() error {
// Set hostname as default node name for backwards compatibility
if px.NodeName == "" { if px.NodeName == "" {
return errors.New("node_name must be configured") hostname, _ := os.Hostname()
px.NodeName = hostname
} }
tlsCfg, err := px.ClientConfig.TLSConfig() tlsCfg, err := px.ClientConfig.TLSConfig()
@ -69,15 +73,11 @@ func (px *Proxmox) Init() error {
} }
func init() { func init() {
px := Proxmox{ inputs.Add("proxmox", func() telegraf.Input {
requestFunction: performRequest, return &Proxmox{
} requestFunction: performRequest,
}
// Set hostname as default node name for backwards compatibility })
hostname, _ := os.Hostname()
px.NodeName = hostname
inputs.Add("proxmox", func() telegraf.Input { return &px })
} }
func getNodeSearchDomain(px *Proxmox) error { func getNodeSearchDomain(px *Proxmox) error {
@ -94,7 +94,7 @@ func getNodeSearchDomain(px *Proxmox) error {
} }
if nodeDns.Data.Searchdomain == "" { if nodeDns.Data.Searchdomain == "" {
return errors.New("node_name not found") return errors.New("search domain is not set")
} }
px.nodeSearchDomain = nodeDns.Data.Searchdomain px.nodeSearchDomain = nodeDns.Data.Searchdomain
@ -141,20 +141,28 @@ func gatherVmData(px *Proxmox, acc telegraf.Accumulator, rt ResourceType) {
for _, vmStat := range vmStats.Data { for _, vmStat := range vmStats.Data {
vmConfig, err := getVmConfig(px, vmStat.ID, rt) vmConfig, err := getVmConfig(px, vmStat.ID, rt)
if err != nil { if err != nil {
px.Log.Error("Error getting VM config: %v", err) px.Log.Errorf("Error getting VM config: %v", err)
return return
} }
if vmConfig.Data.Template == 1 {
px.Log.Debugf("Ignoring template VM %s (%s)", vmStat.ID, vmStat.Name)
continue
}
tags := getTags(px, vmStat.Name, vmConfig, rt) tags := getTags(px, vmStat.Name, vmConfig, rt)
currentVMStatus, err := getCurrentVMStatus(px, rt, vmStat.ID) currentVMStatus, err := getCurrentVMStatus(px, rt, vmStat.ID)
if err != nil { if err != nil {
px.Log.Error("Error getting VM curent VM status: %v", err) px.Log.Errorf("Error getting VM curent VM status: %v", err)
return return
} }
fields, err := getFields(currentVMStatus) fields, err := getFields(currentVMStatus)
if err != nil { if err != nil {
px.Log.Error("Error getting VM measurements: %v", err) px.Log.Errorf("Error getting VM measurements: %v", err)
return return
} }
acc.AddFields("proxmox", fields, tags) acc.AddFields("proxmox", fields, tags)
} }
} }

View File

@ -1,12 +1,13 @@
package proxmox package proxmox
import ( import (
"github.com/bmizerany/assert"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"net/url" "net/url"
"strings" "strings"
"testing" "testing"
"github.com/bmizerany/assert"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
) )
var nodeSearchDomainTestData = `{"data":{"search":"test.example.com","dns1":"1.0.0.1"}}` var nodeSearchDomainTestData = `{"data":{"search":"test.example.com","dns1":"1.0.0.1"}}`

View File

@ -2,11 +2,12 @@ package proxmox
import ( import (
"encoding/json" "encoding/json"
"net/http"
"net/url"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal" "github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls" "github.com/influxdata/telegraf/plugins/common/tls"
"net/http"
"net/url"
) )
type Proxmox struct { type Proxmox struct {
@ -57,6 +58,7 @@ type VmConfig struct {
Data struct { Data struct {
Searchdomain string `json:"searchdomain"` Searchdomain string `json:"searchdomain"`
Hostname string `json:"hostname"` Hostname string `json:"hostname"`
Template int `json:"template"`
} `json:"data"` } `json:"data"`
} }