-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.go
150 lines (141 loc) · 6.57 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"strconv"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
type ipvsCollector struct {
metrics map[string]*ipvsMetric
}
type ipvsMetric struct {
Desc *prometheus.Desc
ValType prometheus.ValueType
}
//NewIpvsCollector a new ipvsCollector instance
func NewIpvsCollector(namespace string) *ipvsCollector {
labels := []string{"vip", "vport", "rip", "rport", "protocol"}
return &ipvsCollector{
metrics: map[string]*ipvsMetric{
"ipvs_active_connections": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "active_connections"), "ipvs active connection counter", labels, nil),
ValType: prometheus.GaugeValue,
},
"ipvs_inactive_connections": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "inactive_connections"), "ipvs inactive connection counter", labels, nil),
ValType: prometheus.GaugeValue,
},
"ipvs_rate_cps": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "rate_cps"), "ipvs new connection counter per second", labels, nil),
ValType: prometheus.GaugeValue,
},
"ipvs_bytes_in": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "bytes_in"), "ipvs ingress bytes", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_bytes_out": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "bytes_out"), "ipvs egress bytes", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_packets_in": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "packets_in"), "ipvs ingress packets", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_packets_out": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "packets_out"), "ipvs egress packets", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_rate_inbps": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "rate_inbps"), "ipvs ingress rate bits per second", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_rate_outbps": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "rate_outbps"), "ipvs egress rate bits per second", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_rate_inpps": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "rate_inpps"), "ipvs ingress rate packets per second", labels, nil),
ValType: prometheus.CounterValue,
},
"ipvs_rate_outpps": {
Desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "rate_outpps"), "ipvs egress rate packets per second", labels, nil),
ValType: prometheus.CounterValue,
},
},
}
}
//Describe output ipvs metric descriptions
func (c *ipvsCollector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
ch <- metric.Desc
}
}
//Collect output ipvs metric values
func (c *ipvsCollector) Collect(ch chan<- prometheus.Metric) {
ipvs, err := NewIpvsWrapper()
if err != nil {
logrus.Errorf("fetch ipvs handler err:%s", err)
return
}
defer ipvs.Close()
svcs, err := ipvs.GetServices()
if err != nil {
logrus.Errorf("fetch ipvs services err:%s", err)
return
}
for _, svc := range svcs {
labels := []string{svc.Address.String(), strconv.Itoa(int(svc.Port)), "", "", ipvs.Protocol(svc.Protocol)}
stats := svc.Stats
metric := c.metrics["ipvs_active_connections"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.Connections), labels...)
metric = c.metrics["ipvs_rate_cps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.CPS), labels...)
metric = c.metrics["ipvs_bytes_in"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BytesIn), labels...)
metric = c.metrics["ipvs_bytes_out"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BytesOut), labels...)
metric = c.metrics["ipvs_packets_in"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PacketsIn), labels...)
metric = c.metrics["ipvs_packets_out"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PacketsOut), labels...)
metric = c.metrics["ipvs_rate_inbps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BPSIn), labels...)
metric = c.metrics["ipvs_rate_outbps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BPSOut), labels...)
metric = c.metrics["ipvs_rate_inpps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PPSIn), labels...)
metric = c.metrics["ipvs_rate_outpps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PPSOut), labels...)
dests, err := ipvs.GetDestinations(svc)
if err != nil {
logrus.Errorf("fetch destinations err:%s", err)
} else {
for _, dest := range dests {
labels[2] = dest.Address.String()
labels[3] = strconv.Itoa(int(dest.Port))
stats := dest.Stats
metric = c.metrics["ipvs_active_connections"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.Connections), labels...)
metric = c.metrics["ipvs_inactive_connections"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(dest.InactiveConnections), labels...)
metric = c.metrics["ipvs_rate_cps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.CPS), labels...)
metric = c.metrics["ipvs_bytes_in"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BytesIn), labels...)
metric = c.metrics["ipvs_bytes_out"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BytesOut), labels...)
metric = c.metrics["ipvs_packets_in"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PacketsIn), labels...)
metric = c.metrics["ipvs_packets_out"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PacketsOut), labels...)
metric = c.metrics["ipvs_rate_inbps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BPSIn), labels...)
metric = c.metrics["ipvs_rate_outbps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.BPSOut), labels...)
metric = c.metrics["ipvs_rate_inpps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PPSIn), labels...)
metric = c.metrics["ipvs_rate_outpps"]
ch <- prometheus.MustNewConstMetric(metric.Desc, metric.ValType, float64(stats.PPSOut), labels...)
}
}
}
}