Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPSec single RE works #191

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 10 additions & 50 deletions ipsec/collector.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package ipsec

import (
"encoding/xml"
"fmt"
"strings"

"github.com/czerwonk/junos_exporter/collector"
"github.com/czerwonk/junos_exporter/rpc"
Expand All @@ -13,17 +11,15 @@ import (
const prefix string = "junos_ipsec_security_associations_"

var (
blockState *prometheus.Desc
activeTunnels *prometheus.Desc
configuredTunnels *prometheus.Desc
blockState *prometheus.Desc
activeTunnels *prometheus.Desc
)

func init() {
l := []string{"target", "re_name", "description", "name"}
l := []string{"target", "description", "name"}

blockState = prometheus.NewDesc(prefix+"state", "State of the Security Association", l, nil)
activeTunnels = prometheus.NewDesc(prefix+"active_tunnels", "Total active tunnels", l, nil)
configuredTunnels = prometheus.NewDesc("junos_ipsec_configured_tunnels", "Total configured tunnels", l, nil)
}

type ipsecCollector struct {
Expand All @@ -34,44 +30,27 @@ func NewCollector() collector.RPCCollector {
return &ipsecCollector{}
}

// Name returns the name of the collector
func (*ipsecCollector) Name() string {
return "IPSec"
}

// Describe describes the metrics
func (*ipsecCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- blockState
ch <- activeTunnels
ch <- configuredTunnels
}

// Collect collects metrics from JunOS
func (c *ipsecCollector) Collect(client *rpc.Client, ch chan<- prometheus.Metric, labelValues []string) error {
var x = RpcReply{}
var x = IpsecRpc{}
err := client.RunCommandAndParse("show security ipsec security-associations", &x)
if err != nil {
return err
}

for _, re := range x.MultiRoutingEngineResults.RoutingEngine {
ls := append(labelValues, re.Name, "active tunnels", "")
ch <- prometheus.MustNewConstMetric(activeTunnels, prometheus.GaugeValue, float64(re.IpSec.ActiveTunnels), ls...)
ls := append(labelValues, "active tunnels", "")
ch <- prometheus.MustNewConstMetric(activeTunnels, prometheus.GaugeValue, float64(x.Information.ActiveTunnels), ls...)

for _, block := range re.IpSec.SecurityAssociations {
c.collectForSecurityAssociation(block, ch, append(labelValues, re.Name))
}
}

var conf = ConfigurationSecurityIpsec{}
err = client.RunCommandAndParse("show configuration security ipsec", &conf)
if err != nil {
return err
for _, block := range x.Information.SecurityAssociations {
c.collectForSecurityAssociation(block, ch, labelValues)
}

cls := append(labelValues, "N/A", "configured tunnels", "")
ch <- prometheus.MustNewConstMetric(configuredTunnels, prometheus.GaugeValue, float64(len(conf.Configuration.Security.Ipsec.Vpn)), cls...)

return nil
}

Expand All @@ -95,26 +74,7 @@ func stateToInt(state *string) int {
retval = 1
}

return retval
}
fmt.Printf("state: %s -> %d\n", *state, retval)

func parseXML(b []byte, res *RpcReply) error {
if strings.Contains(string(b), "multi-routing-engine-results") {
return xml.Unmarshal(b, res)
}

fi := RpcReplyNoRE{}

err := xml.Unmarshal(b, &fi)
if err != nil {
return err
}

res.MultiRoutingEngineResults.RoutingEngine = []RoutingEngine{
{
Name: "N/A",
IpSec: fi.IpSec,
},
}
return nil
return retval
}
62 changes: 6 additions & 56 deletions ipsec/rpc.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
package ipsec

import "encoding/xml"

type RpcReply struct {
XMLName xml.Name `xml:"rpc-reply"`
MultiRoutingEngineResults MultiRoutingEngineResults `xml:"multi-routing-engine-results"`
}

type MultiRoutingEngineResults struct {
RoutingEngine []RoutingEngine `xml:"multi-routing-engine-item"`
}

type RoutingEngine struct {
Name string `xml:"re-name"`
IpSec IpSecRpc `xml:"ipsec-security-associations-information"`
}

type IpSecRpc struct {
ActiveTunnels int `xml:"total-active-tunnels"`
SecurityAssociations []IpsecSecurityAssociationBlock `xml:"ipsec-security-associations-block"`
// IpsecRpc is the root element for xml unmarshalling
type IpsecRpc struct {
Information struct {
ActiveTunnels int `xml:"total-active-tunnels"`
SecurityAssociations []IpsecSecurityAssociationBlock `xml:"ipsec-security-associations-block"`
} `xml:"ipsec-security-associations-information"`
}

// IpsecSecurityAssociationBlock is used for xml unmarshalling
Expand All @@ -43,40 +30,3 @@ type IpsecSecurityAssociation struct {
LifesizeRemaining string `xml:"sa-lifesize-remaining"`
VirtualSystem string `xml:"sa-virtual-system"`
}

type RpcReplyNoRE struct {
XMLName xml.Name `xml:"rpc-reply"`
IpSec IpSecRpc `xml:"ipsec-security-associations-information"`
}

// ConfigurationSecurityIpsec is used for xml unmarshalling
// In order to get the number of configured VPNs
type ConfigurationSecurityIpsec struct {
Configuration struct {
Security struct {
Ipsec struct {
Proposal struct {
Text string `xml:",chardata"`
Name string `xml:"name"`
Protocol string `xml:"protocol"`
AuthenticationAlgorithm string `xml:"authentication-algorithm"`
EncryptionAlgorithm string `xml:"encryption-algorithm"`
LifetimeSeconds string `xml:"lifetime-seconds"`
} `xml:"proposal"`
Policy struct {
Name string `xml:"name"`
Proposals string `xml:"proposals"`
} `xml:"policy"`
Vpn []struct {
Name string `xml:"name"`
BindInterface string `xml:"bind-interface"`
Ike struct {
Gateway string `xml:"gateway"`
IpsecPolicy string `xml:"ipsec-policy"`
} `xml:"ike"`
EstablishTunnels string `xml:"establish-tunnels"`
} `xml:"vpn"`
} `xml:"ipsec"`
} `xml:"security"`
} `xml:"configuration"`
}