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

Netdev 1189/underlying interface #236

Closed
wants to merge 17 commits into from
70 changes: 70 additions & 0 deletions .github/workflows/anx-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
on:
push:
branches:
- anx-prod
pull_request:
branches:
- "**"

name: anx-ci
jobs:
# test:
# # if: github.repository == ‘anexia/junos_exporter’
# strategy:
# matrix:
# go-version: [1.18.x]
# platform: [ubuntu-latest, macos-latest, windows-latest]
# runs-on: ${{ matrix.platform }}
# steps:
# - name: Install Go
# if: success()
# uses: actions/setup-go@v3
# with:
# go-version: ${{ matrix.go-version }}
# - name: Checkout code
# uses: actions/checkout@v3
# - name: Build
# run: go build
# - name: Run tests
# run: go test ./... -v -covermode=count

publish:
# if: github.repository == ‘anexia/junos_exporter’
# needs: test
runs-on: ubuntu-latest
env:
S3_URL: ${{ vars.S3_URL }}
S3_BUCKET: ${{ vars.S3_BUCKET }}
S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }}
S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }}

steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18

- name: Build
run: go build .

- uses: actions/upload-artifact@v3
with:
name: junos_exporter
path: junos_exporter

- uses: actions/setup-python@v4

- name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter
run: |
pip install s4cmd
s4cmd --force put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter
if: github.ref_type == 'tag' || github.ref == 'refs/heads/anx-prod'

- name: "publish feature-branch to s3://$S3_BUCKET/public/junos_exporter/staging/junos_exporter"
run: |
pip install s4cmd
s4cmd --force put --endpoint-url https://$S3_URL -f junos_exporter s3://$S3_BUCKET/public/junos_exporter/staging/junos_exporter
# value only there if pull_request or pull_request_target
if: github.head_ref
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@
tracingEnabled = flag.Bool("tracing.enabled", false, "Enables tracing using OpenTelemetry")
tracingProvider = flag.String("tracing.provider", "", "Sets the tracing provider (stdout or collector)")
tracingCollectorEndpoint = flag.String("tracing.collector.grpc-endpoint", "", "Sets the tracing provider (stdout or collector)")
subscriberEnabled = flag.Bool("subscriber.enabled", false, "Scrape subscribers detail")

Check failure on line 85 in main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

other declaration of subscriberEnabled

Check failure on line 85 in main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

other declaration of subscriberEnabled
cfg *config.Config
devices []*connector.Device
connManager *connector.SSHConnectionManager
reloadCh chan chan error
configMu sync.RWMutex
subscriberEnabled = flag.Bool("subscriber.enabled", false, "Scrape subscribers detail")

Check failure on line 91 in main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

subscriberEnabled redeclared in this block

Check failure on line 91 in main.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

subscriberEnabled redeclared in this block
)

func init() {
Expand Down
66 changes: 57 additions & 9 deletions pkg/features/subscriber/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
package subscriber

import (
"errors"
"fmt"
"strings"
"github.com/czerwonk/junos_exporter/pkg/collector"
"github.com/prometheus/client_golang/prometheus"
)

const prefix string = "junos_subscriber_info"

var subscriberInfoDesc *prometheus.Desc
var subscriberInfo *prometheus.Desc

func init() {
l := []string{"target", "interface", "agent_circuit_id", "agent_remote_id"}
subscriberInfoDesc = prometheus.NewDesc(prefix+"", "Subscriber Detail", l, nil)
l := []string{"target", "interface", "agent_circuit_id", "agent_remote_id", "underlying_ifd"}
subscriberInfo = prometheus.NewDesc(prefix+"", "Subscriber Detail", l, nil)
}

// Name implements collector.RPCCollector.
Expand All @@ -28,21 +30,67 @@ func NewCollector() collector.RPCCollector {

// Describe describes the metrics
func (*subcsribers_information) Describe(ch chan<- *prometheus.Desc) {
ch <- subscriberInfoDesc
ch <- subscriberInfo
}

// Collect collects metrics from JunOS
func (c *subcsribers_information) Collect(client collector.Client, ch chan<- prometheus.Metric, labelValues []string) error {
var x = subcsribers_information{}
err := client.RunCommandAndParse("show subscribers client-type dhcp detail", &x)
err := client.RunCommandAndParse("show subscribers client-type dhcp detail", &x) //TODO: see if client-type dhcp can be left out
if err != nil {
return err
}

for _, subscriber := range x.SubscribersInformation.Subscriber {
labels := append(labelValues, subscriber.Interface, subscriber.AgentCircuitID, subscriber.AgentRemoteID)
ch <- prometheus.MustNewConstMetric(subscriberInfoDesc, prometheus.CounterValue, 1, labels...)
logicalInterfaceMap, err := getLogicalInterfaceInformation(client)
if err != nil {
return err
}

for _, subscriber := range x.SubscribersInformation.Subscriber {
underlying_interface, err := findUnderlyingInterface(client, subscriber.UnderlyingInterface, logicalInterfaceMap, 2)
if err != nil {
fmt.Println(err)
}

labels := append(labelValues, subscriber.Interface, subscriber.AgentCircuitId, subscriber.AgentRemoteId, underlying_interface)
ch <- prometheus.MustNewConstMetric(subscriberInfo, prometheus.CounterValue, 1, labels...)
}
return nil
}

func getLogicalInterfaceInformation(client collector.Client) (map[string]string, error) {

var interfaceInformation = &InterfaceInformation{}
var interfaceMap = make(map[string]string)

err := client.RunCommandAndParse("show interfaces demux0 brief", interfaceInformation)
if err != nil {
return nil, err
}

for _, logicalInterface := range interfaceInformation.LogicalInterfaces {
interfaceMap[logicalInterface.Name] = logicalInterface.DemuxUnderlyingIfName
}

return interfaceMap, nil
}

func findUnderlyingInterface(client collector.Client, ifName string, logicalIfMap map[string]string, maxDepth int) (string, error) {

if !(strings.HasPrefix(ifName, "demux")) {
return ifName, nil
}

if maxDepth < 0 {
return "", errors.New("no underlying interface found, max treshold reached")
}

logicalIfName, exists := logicalIfMap[ifName]
if !exists {
return "", errors.New("no underlying interface found")
}

return findUnderlyingInterface(client, logicalIfName, logicalIfMap, maxDepth-1)


}
22 changes: 16 additions & 6 deletions pkg/features/subscriber/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ package subscriber

type subcsribers_information struct {
SubscribersInformation struct {
Subscriber []subscriber `xml:"subscriber"`
Subscriber []Subscriber `xml:"subscriber"`
} `xml:"subscribers-information"`
}

type subscriber struct {
AccessType string `xml:"access-type"`
Interface string `xml:"interface"`
AgentCircuitID string `xml:"agent-circuit-id"`
AgentRemoteID string `xml:"agent-remote-id"`
type Subscriber struct {
AccessType string `xml:"access-type"`
Interface string `xml:"interface"`
AgentCircuitId string `xml:"agent-circuit-id"`
AgentRemoteId string `xml:"agent-remote-id"`
UnderlyingInterface string `xml:"underlying-interface"`
}

type InterfaceInformation struct {
LogicalInterfaces []LogicalInterface `xml:"interface-information>physical-interface>logical-interface"`
}

type LogicalInterface struct {
Name string `xml:"name"`
DemuxUnderlyingIfName string `xml:"demux-information>demux-interface>demux-underlying-interface-name"`
}
Loading