mirror of
https://github.com/prometheus/node_exporter.git
synced 2024-12-28 06:59:44 -08:00
Convert netstat collector to use ConstMetrics
Fixes https://github.com/prometheus/node_exporter/issues/280
This commit is contained in:
parent
b55019f10b
commit
8c5c880705
|
@ -30,20 +30,16 @@ const (
|
||||||
netStatsSubsystem = "netstat"
|
netStatsSubsystem = "netstat"
|
||||||
)
|
)
|
||||||
|
|
||||||
type netStatCollector struct {
|
type netStatCollector struct{}
|
||||||
metrics map[string]prometheus.Gauge
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Factories["netstat"] = NewNetStatCollector
|
Factories["netstat"] = NewNetStatCollector
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNetStatCollector takes a returns
|
// NewNetStatCollector takes and returns
|
||||||
// a new Collector exposing network stats.
|
// a new Collector exposing network stats.
|
||||||
func NewNetStatCollector() (Collector, error) {
|
func NewNetStatCollector() (Collector, error) {
|
||||||
return &netStatCollector{
|
return &netStatCollector{}, nil
|
||||||
metrics: map[string]prometheus.Gauge{},
|
|
||||||
}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
|
@ -56,34 +52,28 @@ func (c *netStatCollector) Update(ch chan<- prometheus.Metric) (err error) {
|
||||||
return fmt.Errorf("couldn't get SNMP stats: %s", err)
|
return fmt.Errorf("couldn't get SNMP stats: %s", err)
|
||||||
}
|
}
|
||||||
// Merge the results of snmpStats into netStats (collisions are possible, but
|
// Merge the results of snmpStats into netStats (collisions are possible, but
|
||||||
// we know that the keys are always unique for the given use case)
|
// we know that the keys are always unique for the given use case).
|
||||||
for k, v := range snmpStats {
|
for k, v := range snmpStats {
|
||||||
netStats[k] = v
|
netStats[k] = v
|
||||||
}
|
}
|
||||||
for protocol, protocolStats := range netStats {
|
for protocol, protocolStats := range netStats {
|
||||||
for name, value := range protocolStats {
|
for name, value := range protocolStats {
|
||||||
key := protocol + "_" + name
|
key := protocol + "_" + name
|
||||||
if _, ok := c.metrics[key]; !ok {
|
|
||||||
c.metrics[key] = prometheus.NewGauge(
|
|
||||||
prometheus.GaugeOpts{
|
|
||||||
Namespace: Namespace,
|
|
||||||
Subsystem: netStatsSubsystem,
|
|
||||||
Name: key,
|
|
||||||
Help: fmt.Sprintf("Protocol %s statistic %s.", protocol, name),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
v, err := strconv.ParseFloat(value, 64)
|
v, err := strconv.ParseFloat(value, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid value %s in netstats: %s", value, err)
|
return fmt.Errorf("invalid value %s in netstats: %s", value, err)
|
||||||
}
|
}
|
||||||
c.metrics[key].Set(v)
|
ch <- prometheus.MustNewConstMetric(
|
||||||
|
prometheus.NewDesc(
|
||||||
|
prometheus.BuildFQName(Namespace, netStatsSubsystem, key),
|
||||||
|
fmt.Sprintf("Protocol %s statistic %s.", protocol, name),
|
||||||
|
nil, nil,
|
||||||
|
),
|
||||||
|
prometheus.UntypedValue, v,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, m := range c.metrics {
|
return nil
|
||||||
m.Collect(ch)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getNetStats(fileName string) (map[string]map[string]string, error) {
|
func getNetStats(fileName string) (map[string]map[string]string, error) {
|
||||||
|
|
Loading…
Reference in a new issue