/
alarm_collector.go
96 lines (75 loc) · 2.16 KB
/
alarm_collector.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
package alarm
import (
"regexp"
"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
)
const prefix = "junos_alarms_"
var (
alarmsYellowCount *prometheus.Desc
alarmsRedCount *prometheus.Desc
)
func init() {
l := []string{"target"}
alarmsYellowCount = prometheus.NewDesc(prefix+"yellow_count", "Number of yollow alarms (not silenced)", l, nil)
alarmsRedCount = prometheus.NewDesc(prefix+"red_count", "Number of red alarms (not silenced)", l, nil)
}
type alarmCollector struct {
filter *regexp.Regexp
}
// NewCollector creates a new collector
func NewCollector(alarmsFilter string) collector.RPCCollector {
c := &alarmCollector{}
if len(alarmsFilter) > 0 {
c.filter = regexp.MustCompile(alarmsFilter)
}
return c
}
// Describe describes the metrics
func (*alarmCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- alarmsYellowCount
ch <- alarmsRedCount
}
// Collect collects metrics from JunOS
func (c *alarmCollector) Collect(client *rpc.Client, ch chan<- prometheus.Metric, labelValues []string) error {
counter, err := c.alarmCounter(client)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(alarmsYellowCount, prometheus.GaugeValue, counter.YellowCount, labelValues...)
ch <- prometheus.MustNewConstMetric(alarmsRedCount, prometheus.GaugeValue, counter.RedCount, labelValues...)
return nil
}
func (c *alarmCollector) alarmCounter(client *rpc.Client) (*AlarmCounter, error) {
red := 0
yellow := 0
cmds := []string{
"show system alarms",
"show chassis alarms",
}
for _, cmd := range cmds {
var a = AlarmRpc{}
err := client.RunCommandAndParse(cmd, &a)
if err != nil {
return nil, err
}
for _, d := range a.Information.Details {
if c.shouldFilterAlarm(&d) {
continue
}
if d.Class == "Major" {
red++
} else if d.Class == "Minor" {
yellow++
}
}
}
return &AlarmCounter{RedCount: float64(red), YellowCount: float64(yellow)}, nil
}
func (c *alarmCollector) shouldFilterAlarm(a *AlarmDetails) bool {
if c.filter == nil {
return false
}
return c.filter.MatchString(a.Description) || c.filter.MatchString(a.Type)
}