Files
pve-exporter/configuration/configuration.go
Jan Lošťák a1ab163804 Initial commit
2024-05-27 21:27:07 +02:00

78 lines
2.6 KiB
Go

package configuration
import (
"errors"
"fmt"
"net"
"net/url"
)
// Application configuration which is parsed from config.yaml.
type Configuration struct {
LogLevel int `yaml:"logLevel"` // Application log level.
Host string `yaml:"host"` // Host address that application will bind to.
Port uint16 `yaml:"port"` // Host port which application will listen for HTTP requests.
PVE PveConfiguration `yaml:"proxmox"` // PVE configuration.
}
// PVE token configuration.
type PveTokenConfiguration struct {
TokenId string `yaml:"tokenId"` // Token ID from PVE API key management.
Secret string `yaml:"secret"` // Secret from PVE API key management.
}
// MetricsConfiguration represents the metrics configuration.
type PveMetricsConfiguration struct {
ClusterState bool `yaml:"clusterState"` // Enables cluster state metrics collection.
LXC bool `yaml:"lxc"` // Enables LXC container metrics collection.
QEMU bool `yaml:"qemu"` // Enable QEMU virtual machine metrics collection.
Disk bool `yaml:"disk"` // Enable physical disk metrics collection.
Storage bool `yaml:"storage"` // Enable node storage metrics collection.
NodeStatus bool `yaml:"nodeStatus"` // Enable node status metrics collection.
Subscription bool `yaml:"subscription"` // Enable node subscription detail collection.
SDN bool `yaml:"sdn"` // Enable node software defined network state collection.
}
// PVE configuration.
type PveConfiguration struct {
Token PveTokenConfiguration `yaml:"token"` // PVE token.
Hosts []string `yaml:"hosts"` // PVE hosts.
Interval int `yaml:"interval"` // PVE scrape interval.
Metrics PveMetricsConfiguration `yaml:"metrics"` // PVE metrics.
}
// Validate configuration and check for mandaotry field.
func (c *Configuration) Validate() error {
// Validate host IP
if net.ParseIP(c.Host) == nil {
return fmt.Errorf("Host is not a valid IP address: %s", c.Host)
}
// Validate PVE hosts
if len(c.PVE.Hosts) == 0 {
return errors.New("PVE hosts cannot be empty.")
}
for _, host := range c.PVE.Hosts {
u, err := url.Parse(host)
if err != nil {
return fmt.Errorf("PVE host '%s' is not valid URL. Error: %s", host, err)
}
if !(u.Scheme == "http" || u.Scheme == "https") {
return fmt.Errorf("PVE host '%s' must be protocol type of HTTP or HTTPS.", host)
}
}
// Validate PVE token
if c.PVE.Token.TokenId == "" {
return errors.New("PVE tokenId cannot be empty")
}
if c.PVE.Token.Secret == "" {
return errors.New("PVE secret cannot be empty")
}
return nil
}