forked from czerwonk/junos_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request czerwonk#2 from czerwonk/rip_snmp
R.I.P. SNMP
- Loading branch information
Showing
46 changed files
with
565 additions
and
6,854 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package alarm | ||
|
||
import "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 { | ||
} | ||
|
||
func (*AlarmCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- alarmsYellowCount | ||
ch <- alarmsRedCount | ||
} | ||
|
||
func (c *AlarmCollector) Collect(datasource AlarmDatasource, ch chan<- prometheus.Metric, labelValues []string) error { | ||
counter, err := datasource.AlarmCounter() | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package alarm | ||
|
||
type AlarmCounter struct { | ||
YellowCount float64 | ||
RedCount float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package alarm | ||
|
||
type AlarmDatasource interface { | ||
AlarmCounter() (*AlarmCounter, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package bgp | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
const prefix string = "junos_bgp_seesion_" | ||
|
||
var ( | ||
upDesc *prometheus.Desc | ||
receivedPrefixesDesc *prometheus.Desc | ||
acceptedPrefixesDesc *prometheus.Desc | ||
rejectedPrefixesDesc *prometheus.Desc | ||
activePrefixesDesc *prometheus.Desc | ||
inputMessagesDesc *prometheus.Desc | ||
outputMessagesDesc *prometheus.Desc | ||
flapsDesc *prometheus.Desc | ||
) | ||
|
||
func init() { | ||
l := []string{"target", "asn", "ip"} | ||
upDesc = prometheus.NewDesc(prefix+"up", "Session is up (1 = Established)", l, nil) | ||
receivedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_received_count", "Number of received prefixes", l, nil) | ||
acceptedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_accepted_count", "Number of accepted prefixes", l, nil) | ||
rejectedPrefixesDesc = prometheus.NewDesc(prefix+"prefixes_rejected_count", "Number of rejected prefixes", l, nil) | ||
activePrefixesDesc = prometheus.NewDesc(prefix+"prefixes_active_count", "Number of active prefixes (best route in RIB)", l, nil) | ||
inputMessagesDesc = prometheus.NewDesc(prefix+"messages_input_count", "Number of received messages", l, nil) | ||
outputMessagesDesc = prometheus.NewDesc(prefix+"messages_output_count", "Number of transmitted messages", l, nil) | ||
flapsDesc = prometheus.NewDesc(prefix+"flap_count", "Number of session flaps", l, nil) | ||
} | ||
|
||
type BgpCollector struct { | ||
} | ||
|
||
func (*BgpCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- upDesc | ||
ch <- receivedPrefixesDesc | ||
ch <- acceptedPrefixesDesc | ||
ch <- rejectedPrefixesDesc | ||
ch <- activePrefixesDesc | ||
ch <- inputMessagesDesc | ||
ch <- outputMessagesDesc | ||
ch <- flapsDesc | ||
} | ||
|
||
func (c *BgpCollector) Collect(datasource BgpDatasource, ch chan<- prometheus.Metric, labelValues []string) error { | ||
sessions, err := datasource.BgpSessions() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range sessions { | ||
c.collectForSession(s, ch, labelValues) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (*BgpCollector) collectForSession(s *BgpSession, ch chan<- prometheus.Metric, labelValues []string) { | ||
l := append(labelValues, []string{s.Asn, s.Ip}...) | ||
|
||
up := 0 | ||
if s.Up { | ||
up = 1 | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, float64(up), l...) | ||
ch <- prometheus.MustNewConstMetric(receivedPrefixesDesc, prometheus.GaugeValue, float64(s.ReceivedPrefixes), l...) | ||
ch <- prometheus.MustNewConstMetric(acceptedPrefixesDesc, prometheus.GaugeValue, float64(s.AcceptedPrefixes), l...) | ||
ch <- prometheus.MustNewConstMetric(rejectedPrefixesDesc, prometheus.GaugeValue, float64(s.RejectedPrefixes), l...) | ||
ch <- prometheus.MustNewConstMetric(activePrefixesDesc, prometheus.GaugeValue, float64(s.ActivePrefixes), l...) | ||
ch <- prometheus.MustNewConstMetric(inputMessagesDesc, prometheus.GaugeValue, float64(s.InputMessages), l...) | ||
ch <- prometheus.MustNewConstMetric(outputMessagesDesc, prometheus.GaugeValue, float64(s.OutputMessages), l...) | ||
ch <- prometheus.MustNewConstMetric(flapsDesc, prometheus.GaugeValue, float64(s.Flaps), l...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package bgp | ||
|
||
type BgpSession struct { | ||
Ip string | ||
Asn string | ||
Up bool | ||
ReceivedPrefixes float64 | ||
AcceptedPrefixes float64 | ||
RejectedPrefixes float64 | ||
ActivePrefixes float64 | ||
InputMessages float64 | ||
OutputMessages float64 | ||
Flaps float64 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package bgp | ||
|
||
type BgpDatasource interface { | ||
BgpSessions() ([]*BgpSession, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package connector | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
func NewSshConnection(host, user, keyFile string) (*SshConnection, error) { | ||
if !strings.Contains(host, ":") { | ||
host = host + ":22" | ||
} | ||
|
||
c := &SshConnection{Host: host} | ||
err := c.Connect(user, keyFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return c, nil | ||
} | ||
|
||
type SshConnection struct { | ||
conn *ssh.Client | ||
Host string | ||
} | ||
|
||
func (c *SshConnection) Connect(user, keyFile string) error { | ||
pk, err := loadPublicKeyFile(keyFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
config := &ssh.ClientConfig{ | ||
User: user, | ||
Auth: []ssh.AuthMethod{pk}, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
} | ||
|
||
c.conn, err = ssh.Dial("tcp", c.Host, config) | ||
return err | ||
} | ||
|
||
func (c *SshConnection) RunCommand(cmd string) ([]byte, error) { | ||
session, err := c.conn.NewSession() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer session.Close() | ||
|
||
var b = &bytes.Buffer{} | ||
session.Stdout = b | ||
|
||
err = session.Run(cmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return b.Bytes(), nil | ||
} | ||
|
||
func (c *SshConnection) Close() { | ||
if c.conn == nil { | ||
return | ||
} | ||
|
||
c.conn.Close() | ||
c.conn = nil | ||
} | ||
|
||
func loadPublicKeyFile(file string) (ssh.AuthMethod, error) { | ||
b, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
key, err := ssh.ParsePrivateKey(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ssh.PublicKeys(key), nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package interfaces | ||
|
||
type InterfaceStatsDatasource interface { | ||
InterfaceStats() ([]*InterfaceStats, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package interfaces | ||
|
||
import "github.com/prometheus/client_golang/prometheus" | ||
|
||
const prefix = "junos_interface_" | ||
|
||
var ( | ||
receiveBytesDesc *prometheus.Desc | ||
receiveErrorsDesc *prometheus.Desc | ||
receiveDropsDesc *prometheus.Desc | ||
transmitBytesDesc *prometheus.Desc | ||
transmitErrorsDesc *prometheus.Desc | ||
transmitDropsDesc *prometheus.Desc | ||
) | ||
|
||
func init() { | ||
l := []string{"target", "name", "description", "mac"} | ||
receiveBytesDesc = prometheus.NewDesc(prefix+"receive_bytes", "Received data in bytes", l, nil) | ||
receiveErrorsDesc = prometheus.NewDesc(prefix+"receive_errors", "Number of errors caused by incoming packets", l, nil) | ||
receiveDropsDesc = prometheus.NewDesc(prefix+"receive_drops", "Number of dropped incoming packets", l, nil) | ||
transmitBytesDesc = prometheus.NewDesc(prefix+"transmit_bytes", "Transmitted data in bytes", l, nil) | ||
transmitErrorsDesc = prometheus.NewDesc(prefix+"transmit_errors", "Number of errors caused by outgoing packets", l, nil) | ||
transmitDropsDesc = prometheus.NewDesc(prefix+"transmit_drops", "Number of dropped outgoing packets", l, nil) | ||
} | ||
|
||
type InterfaceCollector struct { | ||
} | ||
|
||
func (*InterfaceCollector) Describe(ch chan<- *prometheus.Desc) { | ||
ch <- receiveBytesDesc | ||
ch <- receiveErrorsDesc | ||
ch <- receiveDropsDesc | ||
ch <- transmitBytesDesc | ||
ch <- transmitDropsDesc | ||
ch <- transmitErrorsDesc | ||
} | ||
|
||
func (c *InterfaceCollector) Collect(datasource InterfaceStatsDatasource, ch chan<- prometheus.Metric, labelValues []string) error { | ||
stats, err := datasource.InterfaceStats() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range stats { | ||
c.collectForInterface(s, ch, labelValues) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (*InterfaceCollector) collectForInterface(s *InterfaceStats, ch chan<- prometheus.Metric, labelValues []string) { | ||
l := append(labelValues, []string{s.Name, s.Description, s.Mac}...) | ||
ch <- prometheus.MustNewConstMetric(receiveBytesDesc, prometheus.GaugeValue, s.ReceiveBytes, l...) | ||
ch <- prometheus.MustNewConstMetric(transmitBytesDesc, prometheus.GaugeValue, s.TransmitBytes, l...) | ||
|
||
if s.IsPhysical { | ||
ch <- prometheus.MustNewConstMetric(transmitErrorsDesc, prometheus.GaugeValue, s.TransmitErrors, l...) | ||
ch <- prometheus.MustNewConstMetric(transmitDropsDesc, prometheus.GaugeValue, s.TransmitDrops, l...) | ||
ch <- prometheus.MustNewConstMetric(receiveErrorsDesc, prometheus.GaugeValue, s.ReceiveErrors, l...) | ||
ch <- prometheus.MustNewConstMetric(receiveDropsDesc, prometheus.GaugeValue, s.ReceiveDrops, l...) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package interfaces | ||
|
||
type InterfaceStats struct { | ||
Name string | ||
Description string | ||
Mac string | ||
IsPhysical bool | ||
ReceiveBytes float64 | ||
ReceiveErrors float64 | ||
ReceiveDrops float64 | ||
TransmitBytes float64 | ||
TransmitErrors float64 | ||
TransmitDrops float64 | ||
} |
Oops, something went wrong.