Skip to content

Commit

Permalink
integration test, part 1
Browse files Browse the repository at this point in the history
pending prometheus/client_golang#58, just check for presence of expected
metrics
  • Loading branch information
Matthias Rampke committed May 7, 2015
1 parent f5441bc commit a37e2f6
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 100 deletions.
File renamed without changes.
File renamed without changes.
58 changes: 35 additions & 23 deletions collector/ipvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,9 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const (
procNetIPVS = "/proc/net/ip_vs"
procNetIPVSStats = "/proc/net/ip_vs_stats"
ipvsSubsystem = "ipvs"
)

type ipvsCollector struct {
config Config
Collector
procNetIPVS, procNetIPVSStats string
backendConnectionsActive, backendConnectionsInact, backendWeight *prometheus.GaugeVec
connections, incomingPackets, outgoingPackets, incomingBytes, outgoingBytes prometheus.Counter
}
Expand All @@ -48,6 +43,10 @@ func init() {
}

func NewIPVSCollector(config Config) (Collector, error) {
return newIPVSCollector(config)
}

func newIPVSCollector(config Config) (*ipvsCollector, error) {
var (
ipvsBackendLabelNames = []string{
"local_address",
Expand All @@ -56,47 +55,60 @@ func NewIPVSCollector(config Config) (Collector, error) {
"remote_port",
"proto",
}
c ipvsCollector
c ipvsCollector
procfs string
subsystem string
)

c.config = config
if p, ok := config.Config["procfs"]; ok {
procfs = p
} else {
procfs = "/proc"
}
c.procNetIPVS = procfs + "/net/ip_vs"
c.procNetIPVSStats = procfs + "/net/ip_vs_stats"
if s, ok := config.Config["ipvs_subsystem"]; ok {
subsystem = s
} else {
subsystem = "ipvs"
}

c.connections = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "connections_total",
Help: "The total number of connections made.",
},
)
c.incomingPackets = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "incoming_packets_total",
Help: "The total number of incoming packets.",
},
)
c.outgoingPackets = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "outgoing_packets_total",
Help: "The total number of outgoing packets.",
},
)
c.incomingBytes = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "incoming_bytes_total",
Help: "The total amount of incoming data.",
},
)
c.outgoingBytes = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "outgoing_bytes_total",
Help: "The total amount of outgoing data.",
},
Expand All @@ -105,7 +117,7 @@ func NewIPVSCollector(config Config) (Collector, error) {
c.backendConnectionsActive = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "backend_connections_active",
Help: "The current active connections by local and remote address.",
},
Expand All @@ -114,7 +126,7 @@ func NewIPVSCollector(config Config) (Collector, error) {
c.backendConnectionsInact = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "backend_connections_inactive",
Help: "The current inactive connections by local and remote address.",
},
Expand All @@ -123,7 +135,7 @@ func NewIPVSCollector(config Config) (Collector, error) {
c.backendWeight = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: Namespace,
Subsystem: ipvsSubsystem,
Subsystem: subsystem,
Name: "backend_weight",
Help: "The current backend weight by local and remote address.",
},
Expand All @@ -134,7 +146,7 @@ func NewIPVSCollector(config Config) (Collector, error) {
}

func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {
ipvsStats, err := getIPVSStats()
ipvsStats, err := getIPVSStats(c.procNetIPVSStats)
if err != nil {
return fmt.Errorf("could not get IPVS stats: %s", err)
}
Expand All @@ -151,7 +163,7 @@ func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {
c.incomingBytes.Collect(ch)
c.outgoingBytes.Collect(ch)

backendStats, err := getIPVSBackendStatus()
backendStats, err := getIPVSBackendStatus(c.procNetIPVS)
if err != nil {
return fmt.Errorf("could not get backend status: %s", err)
}
Expand All @@ -176,7 +188,7 @@ func (c *ipvsCollector) Update(ch chan<- prometheus.Metric) error {
return nil
}

func getIPVSBackendStatus() ([]ipvsBackendStatus, error) {
func getIPVSBackendStatus(procNetIPVS string) ([]ipvsBackendStatus, error) {
file, err := os.Open(procNetIPVS)
if err != nil {
return nil, err
Expand All @@ -188,7 +200,7 @@ func getIPVSBackendStatus() ([]ipvsBackendStatus, error) {

func parseIPVSBackendStatus(file io.Reader) ([]ipvsBackendStatus, error) {
var (
status = []ipvsBackendStatus{}
status []ipvsBackendStatus
scanner = bufio.NewScanner(file)
proto string
localAddress net.IP
Expand Down Expand Up @@ -248,7 +260,7 @@ func parseIPVSBackendStatus(file io.Reader) ([]ipvsBackendStatus, error) {
return status, nil
}

func getIPVSStats() (ipvsStats, error) {
func getIPVSStats(procNetIPVSStats string) (ipvsStats, error) {
file, err := os.Open(procNetIPVSStats)
if err != nil {
return ipvsStats{}, err
Expand All @@ -262,7 +274,7 @@ func parseIPVSStats(file io.Reader) (ipvsStats, error) {
var (
statContent []byte
statFields []string
stats = ipvsStats{}
stats ipvsStats
)

statContent, err := ioutil.ReadAll(file)
Expand Down
Loading

0 comments on commit a37e2f6

Please sign in to comment.