-
Notifications
You must be signed in to change notification settings - Fork 75
/
collector.go
129 lines (103 loc) · 5.35 KB
/
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
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
package accounting
import (
"errors"
"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/rpc"
"github.com/prometheus/client_golang/prometheus"
)
const prefix string = "junos_accounting_inline_"
var (
inlineActiveFlowsDesc *prometheus.Desc
inlineIpv4ActiveFlowsDesc *prometheus.Desc
inlineIpv6ActiveFlowsDesc *prometheus.Desc
inlineFlowsDesc *prometheus.Desc
inlineIpv4TotalFlowsDesc *prometheus.Desc
inlineIpv6TotalFlowsDesc *prometheus.Desc
inlineFlowCreationFailuresDesc *prometheus.Desc
inlineIpv4FlowCreationFailuresDesc *prometheus.Desc
inlineIpv6FlowCreationFailuresDesc *prometheus.Desc
)
func init() {
l := []string{"target", "fpc"}
inlineActiveFlowsDesc = prometheus.NewDesc(prefix+"active_flow_count", "Number of active flows", l, nil)
inlineIpv4ActiveFlowsDesc = prometheus.NewDesc(prefix+"ipv4_active_flow_count", "Number of active ipv4 flows", l, nil)
inlineIpv6ActiveFlowsDesc = prometheus.NewDesc(prefix+"ipv6_active_flow_count", "Number of active ipv6 flows", l, nil)
inlineFlowsDesc = prometheus.NewDesc(prefix+"flow_count", "Number of flows", l, nil)
inlineIpv4TotalFlowsDesc = prometheus.NewDesc(prefix+"ipv4_flow_count", "Number of ipv4 flows", l, nil)
inlineIpv6TotalFlowsDesc = prometheus.NewDesc(prefix+"ipv6_flow_count", "Number of ipv6 flows", l, nil)
inlineFlowCreationFailuresDesc = prometheus.NewDesc(prefix+"creation_failure_count", "Number of flow creation failures", l, nil)
inlineIpv4FlowCreationFailuresDesc = prometheus.NewDesc(prefix+"ipv4_creation_failure_count", "Number of ipv4 flow creation failures", l, nil)
inlineIpv6FlowCreationFailuresDesc = prometheus.NewDesc(prefix+"ipv6_creation_failure_count", "Number of ipv6 flow creation failures", l, nil)
}
type accountingCollector struct {
}
// NewCollector creates a new collector
func NewCollector() collector.RPCCollector {
return &accountingCollector{}
}
// Describe describes the metrics
func (*accountingCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- inlineActiveFlowsDesc
ch <- inlineIpv4ActiveFlowsDesc
ch <- inlineIpv6ActiveFlowsDesc
ch <- inlineFlowsDesc
ch <- inlineIpv4TotalFlowsDesc
ch <- inlineIpv6TotalFlowsDesc
ch <- inlineFlowCreationFailuresDesc
ch <- inlineIpv4FlowCreationFailuresDesc
ch <- inlineIpv6FlowCreationFailuresDesc
}
// Collect collects metrics from JunOS
func (c *accountingCollector) Collect(client *rpc.Client, ch chan<- prometheus.Metric, labelValues []string) error {
flow, err := c.accountingFlows(client)
if err != nil {
return err
}
failure, err := c.accountingFailures(client)
if err != nil {
return err
}
l := append(labelValues, []string{flow.FpcSlot}...)
ch <- prometheus.MustNewConstMetric(inlineActiveFlowsDesc, prometheus.GaugeValue, float64(flow.InlineActiveFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv4ActiveFlowsDesc, prometheus.GaugeValue, float64(flow.InlineIpv4ActiveFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv6ActiveFlowsDesc, prometheus.GaugeValue, float64(flow.InlineIpv6ActiveFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineFlowsDesc, prometheus.GaugeValue, float64(flow.InlineFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv4TotalFlowsDesc, prometheus.GaugeValue, float64(flow.InlineIpv4TotalFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv6TotalFlowsDesc, prometheus.GaugeValue, float64(flow.InlineIpv6TotalFlows), l...)
ch <- prometheus.MustNewConstMetric(inlineFlowCreationFailuresDesc, prometheus.GaugeValue, float64(failure.InlineFlowCreationFailures), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv4FlowCreationFailuresDesc, prometheus.GaugeValue, float64(failure.InlineIpv4FlowCreationFailures), l...)
ch <- prometheus.MustNewConstMetric(inlineIpv6FlowCreationFailuresDesc, prometheus.GaugeValue, float64(failure.InlineIpv6FlowCreationFailures), l...)
return nil
}
func (c *accountingCollector) accountingFlows(client *rpc.Client) (*AccountingFlow, error) {
var x = AccountingFlowRpc{}
err := client.RunCommandAndParse("show services accounting flow inline-jflow fpc-slot 0", &x)
if err != nil {
return nil, err
}
if x.Error.Message != "" {
return nil, errors.New("Accounting command not supported")
}
return &AccountingFlow{
FpcSlot: x.Information.InlineFlow.FpcSlot,
InlineActiveFlows: float64(x.Information.InlineFlow.InlineActiveFlows),
InlineIpv4ActiveFlows: float64(x.Information.InlineFlow.InlineIpv4ActiveFlows),
InlineIpv6ActiveFlows: float64(x.Information.InlineFlow.InlineIpv6ActiveFlows),
InlineFlows: float64(x.Information.InlineFlow.InlineFlows),
InlineIpv4TotalFlows: float64(x.Information.InlineFlow.InlineIpv4TotalFlows),
InlineIpv6TotalFlows: float64(x.Information.InlineFlow.InlineIpv6TotalFlows),
}, nil
}
func (c *accountingCollector) accountingFailures(client *rpc.Client) (*AccountingError, error) {
var x = AccountingFlowErrorRpc{}
err := client.RunCommandAndParse("show services accounting errors inline-jflow fpc-slot 0", &x)
if err != nil {
return nil, err
}
return &AccountingError{
FpcSlot: x.Information.InlineFlow.FpcSlot,
InlineFlowCreationFailures: float64(x.Information.InlineFlow.InlineFlowCreationFailures),
InlineIpv4FlowCreationFailures: float64(x.Information.InlineFlow.InlineIpv4FlowCreationFailures),
InlineIpv6FlowCreationFailures: float64(x.Information.InlineFlow.InlineIpv6FlowCreationFailures),
}, nil
}