News features and improvements (#8)
* added config file implementation, refactoring * add gitignore * improved test * preperations for more metrics * added resource metrics * added first bgp metrics * added asn as label for bgp metrics * added prefix and message counts to bgp metrics * simplified * Update README.md * added yaml dependency * fixed go routine call * added timeout * clean up * added TLS support * set default api port for TLS * added routes metric * added missing log information
This commit is contained in:
committed by
Steve Brunton
parent
c37abb638f
commit
f2866a3a2f
82
collector/resource_collector.go
Normal file
82
collector/resource_collector.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/nshttpd/mikrotik-exporter/config"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gopkg.in/routeros.v2"
|
||||
"gopkg.in/routeros.v2/proto"
|
||||
)
|
||||
|
||||
var (
|
||||
resourceLabelNames = []string{"name", "address"}
|
||||
resourceProps = []string{"free-memory", "total-memory", "cpu-load", "free-hdd-space", "total-hdd-space"}
|
||||
resourceDescriptions map[string]*prometheus.Desc
|
||||
)
|
||||
|
||||
func init() {
|
||||
resourceDescriptions = make(map[string]*prometheus.Desc)
|
||||
for _, p := range resourceProps {
|
||||
resourceDescriptions[p] = descriptionForPropertyName("system", p, resourceLabelNames)
|
||||
}
|
||||
}
|
||||
|
||||
type resourceCollector struct {
|
||||
}
|
||||
|
||||
func (c *resourceCollector) describe(ch chan<- *prometheus.Desc) {
|
||||
for _, d := range resourceDescriptions {
|
||||
ch <- d
|
||||
}
|
||||
}
|
||||
|
||||
func (c *resourceCollector) collect(ch chan<- prometheus.Metric, device *config.Device, client *routeros.Client) error {
|
||||
stats, err := c.fetch(client, device)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, re := range stats {
|
||||
c.collectForStat(re, device, ch)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *resourceCollector) fetch(client *routeros.Client, device *config.Device) ([]*proto.Sentence, error) {
|
||||
reply, err := client.Run("/system/resource/print", "=.proplist="+strings.Join(resourceProps, ","))
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"device": device.Name,
|
||||
"error": err,
|
||||
}).Error("error fetching system resource metrics")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reply.Re, nil
|
||||
}
|
||||
|
||||
func (c *resourceCollector) collectForStat(re *proto.Sentence, device *config.Device, ch chan<- prometheus.Metric) {
|
||||
for _, p := range resourceProps {
|
||||
c.collectMetricForProperty(p, device, re, ch)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *resourceCollector) collectMetricForProperty(property string, device *config.Device, re *proto.Sentence, ch chan<- prometheus.Metric) {
|
||||
v, err := strconv.ParseFloat(re.Map[property], 64)
|
||||
if err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"device": device.Name,
|
||||
"property": property,
|
||||
"value": re.Map[property],
|
||||
"error": err,
|
||||
}).Error("error parsing system resource metric value")
|
||||
return
|
||||
}
|
||||
|
||||
desc := resourceDescriptions[property]
|
||||
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, v, device.Name, device.Address)
|
||||
}
|
||||
Reference in New Issue
Block a user