From 217da900833f41779dd6af78aeaa0955620ffe2b Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 13:34:51 +0200 Subject: [PATCH 01/14] add missing anx-ci --- .github/workflows/anx-ci.yml | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 .github/workflows/anx-ci.yml diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml new file mode 100644 index 00000000..dab3d31e --- /dev/null +++ b/.github/workflows/anx-ci.yml @@ -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: ${{secrets.S3_URL}} + S3_BUCKET: ${{secrets.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 put --endpoint-url https://$S3_URL -f 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 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 \ No newline at end of file From a5e612950c55d6998e46671946cadd6352367895 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 13:51:55 +0200 Subject: [PATCH 02/14] test --- .github/workflows/anx-ci.yml | 48 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index dab3d31e..ce642cd9 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -8,35 +8,35 @@ on: 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 + # 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 + # needs: test runs-on: ubuntu-latest env: - S3_URL: ${{secrets.S3_URL}} - S3_BUCKET: ${{secrets.S3_BUCKET}} - S3_ACCESS_KEY: ${{secrets.S3_ACCESS_KEY}} - S3_SECRET_KEY: ${{secrets.S3_SECRET_KEY}} + S3_URL: ${{ secrets.S3_URL }} + S3_BUCKET: ${{ secrets.S3_BUCKET }} + S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} + S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} steps: - uses: actions/checkout@v3 From 0e478ca10bf41fcd11a5f1f8fdb98b46b2489d3f Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 13:55:18 +0200 Subject: [PATCH 03/14] test s3 creds --- .github/workflows/anx-ci.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index ce642cd9..c986ddca 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -39,6 +39,12 @@ jobs: S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} steps: + - name: print_env + run: | + echo $S3_URL + echo $S3_BUCKET + echo $S3_ACCESS_KEY + - uses: actions/checkout@v3 - name: Set up Go From 54a5de576adbe4c6201b821938dee8af6da394b3 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 14:05:52 +0200 Subject: [PATCH 04/14] test ci --- .github/workflows/anx-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index c986ddca..d4be2625 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -33,8 +33,8 @@ jobs: # needs: test runs-on: ubuntu-latest env: - S3_URL: ${{ secrets.S3_URL }} - S3_BUCKET: ${{ secrets.S3_BUCKET }} + S3_URL: ${{ vars.S3_URL }} + S3_BUCKET: ${{ vars.S3_BUCKET }} S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} From a8ff09d139cc1821e6ec95d3951b72af58fea726 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 14:08:42 +0200 Subject: [PATCH 05/14] test --- .github/workflows/anx-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index d4be2625..e868029f 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -35,8 +35,8 @@ jobs: 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 }} + AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }} + AWS_SECRET_ACCESS_KEY : ${{ secrets.S3_SECRET_KEY }} steps: - name: print_env From aecbd86260c55125db73ff32b8bf24a35ed17553 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 14:20:18 +0200 Subject: [PATCH 06/14] test ci --- .github/workflows/anx-ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index e868029f..94081cd0 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -35,8 +35,8 @@ jobs: env: S3_URL: ${{ vars.S3_URL }} S3_BUCKET: ${{ vars.S3_BUCKET }} - AWS_ACCESS_KEY_ID: ${{ secrets.S3_ACCESS_KEY }} - AWS_SECRET_ACCESS_KEY : ${{ secrets.S3_SECRET_KEY }} + S3_ACCESS_KEY: ${{ secrets.S3_ACCESS_KEY }} + S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} steps: - name: print_env @@ -65,7 +65,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd put --endpoint-url https://$S3_URL -f junos_exporter s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter + s4cmd --debug put --endpoint-url https://$S3_URL -f 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" From 5241c51434da23e882ddd6f0ffd849ec53b490c8 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 14:25:00 +0200 Subject: [PATCH 07/14] test --- .github/workflows/anx-ci.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index 94081cd0..71534204 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -39,12 +39,6 @@ jobs: S3_SECRET_KEY: ${{ secrets.S3_SECRET_KEY }} steps: - - name: print_env - run: | - echo $S3_URL - echo $S3_BUCKET - echo $S3_ACCESS_KEY - - uses: actions/checkout@v3 - name: Set up Go @@ -65,7 +59,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd --debug put --endpoint-url https://$S3_URL -f junos_exporter s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter + s4cmd --debug --force --dry-run 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" From 39a4d68d57be0f716a16c69a999150a765523a72 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Thu, 3 Aug 2023 14:32:10 +0200 Subject: [PATCH 08/14] test --- .github/workflows/anx-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index 71534204..cc9877f4 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -59,7 +59,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd --debug --force --dry-run put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter + s4cmd --debug --force --dry-run put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/Test/$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" From 9b3f72fe00a1349512a1c089607bcbbe96b61f76 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 23 Aug 2023 11:03:35 +0200 Subject: [PATCH 09/14] remove dry-run --- .github/workflows/anx-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index cc9877f4..66802f4b 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -59,7 +59,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd --debug --force --dry-run put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/Test/$GITHUB_REF_NAME/junos_exporter + s4cmd put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/Test/$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" @@ -67,4 +67,4 @@ jobs: pip install s4cmd s4cmd 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 \ No newline at end of file + if: github.head_ref From c38681e515f9ec8ce929d706950c7d2d80f8b360 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 23 Aug 2023 11:05:35 +0200 Subject: [PATCH 10/14] remove Test Folder --- .github/workflows/anx-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index 66802f4b..12cc367a 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -59,7 +59,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/Test/$GITHUB_REF_NAME/junos_exporter + s4cmd put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/$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" From c56bac6a3f6ed895e5f8f07746fe04e35012a482 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 23 Aug 2023 13:15:24 +0200 Subject: [PATCH 11/14] fix upload url --- .github/workflows/anx-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index 12cc367a..7e83976d 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -59,7 +59,7 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/$GITHUB_REF_NAME/junos_exporter + s4cmd 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" From 43d52a631805cd3e144ffe7e0397a3036698d732 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 23 Aug 2023 13:21:21 +0200 Subject: [PATCH 12/14] force push so that it will also upload file if already there --- .github/workflows/anx-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/anx-ci.yml b/.github/workflows/anx-ci.yml index 7e83976d..04d40b90 100644 --- a/.github/workflows/anx-ci.yml +++ b/.github/workflows/anx-ci.yml @@ -59,12 +59,12 @@ jobs: - name: publish anx-prod to s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter run: | pip install s4cmd - s4cmd put --endpoint-url https://$S3_URL junos_exporter s3://$S3_BUCKET/public/junos_exporter/$GITHUB_REF_NAME/junos_exporter + 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 put --endpoint-url https://$S3_URL -f junos_exporter s3://$S3_BUCKET/public/junos_exporter/staging/junos_exporter + 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 From 52f9f1f209451389f3e8ce62fb97677f0009b378 Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 13 Sep 2023 11:53:38 +0000 Subject: [PATCH 13/14] netdev-786/collect-subscriber-information --- README.md | 2 +- collectors.go | 2 ++ internal/config/config.go | 1 + main.go | 3 +- pkg/features/subscriber/collector.go | 48 ++++++++++++++++++++++++++++ pkg/features/subscriber/rpc.go | 14 ++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 pkg/features/subscriber/collector.go create mode 100644 pkg/features/subscriber/rpc.go diff --git a/README.md b/README.md index 6da6f1d3..141eaf83 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ The following metrics are supported by now: * L2circuits (tunnel state, number of tunnels) * LDP (number of neighbors, sessions and session states) * VRRP (state per interface) - +* Subscribers Information (show subscribers client-type dhcp detail) ## Feature specific mappings Some collected time series behave like enums - Integer values represent a certain state/meaning. diff --git a/collectors.go b/collectors.go index 2b9d1e20..8ef5c2b5 100644 --- a/collectors.go +++ b/collectors.go @@ -34,6 +34,7 @@ import ( "github.com/czerwonk/junos_exporter/pkg/features/security" "github.com/czerwonk/junos_exporter/pkg/features/securitypolicies" "github.com/czerwonk/junos_exporter/pkg/features/storage" + "github.com/czerwonk/junos_exporter/pkg/features/subscriber" "github.com/czerwonk/junos_exporter/pkg/features/system" "github.com/czerwonk/junos_exporter/pkg/features/vpws" "github.com/czerwonk/junos_exporter/pkg/features/vrrp" @@ -112,6 +113,7 @@ func (c *collectors) initCollectorsForDevices(device *connector.Device) { c.addCollectorIfEnabledForDevice(device, "vrrp", f.VRRP, vrrp.NewCollector) c.addCollectorIfEnabledForDevice(device, "vpws", f.VPWS, vpws.NewCollector) c.addCollectorIfEnabledForDevice(device, "mpls_lsp", f.MPLSLSP, mplslsp.NewCollector) + c.addCollectorIfEnabledForDevice(device, "subscriber", f.Subscriber, subscriber.NewCollector) } func (c *collectors) addCollectorIfEnabledForDevice(device *connector.Device, key string, enabled bool, newCollector func() collector.RPCCollector) { diff --git a/internal/config/config.go b/internal/config/config.go index fdd5cd23..c7bf82f4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -66,6 +66,7 @@ type FeatureConfig struct { VPWS bool `yaml:"vpws,omitempty"` VRRP bool `yaml:"vrrp,omitempty"` License bool `yaml:"license,omitempty"` + Subscriber bool `yaml:"subscriber,omitempty"` } // New creates a new config diff --git a/main.go b/main.go index 4ef70a6f..b9d8be7d 100644 --- a/main.go +++ b/main.go @@ -86,6 +86,7 @@ var ( connManager *connector.SSHConnectionManager reloadCh chan chan error configMu sync.RWMutex + subscriberEnabled = flag.Bool("subscriber.enabled", false, "Scrape subscribers detail") ) func init() { @@ -244,7 +245,7 @@ func loadConfigFromFlags() *config.Config { f.VPWS = *vpwsEnabled f.MPLSLSP = *mplsLSPEnabled f.License = *licenseEnabled - + f.Subscriber = *subscriberEnabled return c } diff --git a/pkg/features/subscriber/collector.go b/pkg/features/subscriber/collector.go new file mode 100644 index 00000000..c8c104b2 --- /dev/null +++ b/pkg/features/subscriber/collector.go @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT + +package subscriber + +import ( + "github.com/czerwonk/junos_exporter/pkg/collector" + "github.com/prometheus/client_golang/prometheus" +) + +const prefix string = "junos_subscriber_info" + +var subscriberInfo *prometheus.Desc + +func init() { + l := []string{"target", "interface", "agent_circuit_id", "agent_remote_id"} + subscriberInfo = prometheus.NewDesc(prefix+"", "Subscriber Detail", l, nil) +} + +// Name implements collector.RPCCollector. +func (*subcsribers_information) Name() string { + return "Subscriber Detail" +} + +// NewCollector creates a new collector +func NewCollector() collector.RPCCollector { + return &subcsribers_information{} +} + +// Describe describes the metrics +func (*subcsribers_information) Describe(ch chan<- *prometheus.Desc) { + 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) + if err != nil { + return err + } + + for _, subscriber := range x.SubscribersInformation.Subscriber { + labels := append(labelValues, subscriber.Interface, subscriber.AgentCircuitId, subscriber.AgentRemoteId) + ch <- prometheus.MustNewConstMetric(subscriberInfo, prometheus.CounterValue, 1, labels...) + } + + return nil +} diff --git a/pkg/features/subscriber/rpc.go b/pkg/features/subscriber/rpc.go new file mode 100644 index 00000000..8fb028b1 --- /dev/null +++ b/pkg/features/subscriber/rpc.go @@ -0,0 +1,14 @@ +package subscriber + +type subcsribers_information struct { + SubscribersInformation struct { + 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"` +} From f65453d24fb7758f604c9752e95c0a3a50517c2a Mon Sep 17 00:00:00 2001 From: Jakob Gastinger Date: Wed, 20 Dec 2023 17:23:21 +0100 Subject: [PATCH 14/14] now scrapes interface information to include underlying interfaces for each subscriber --- pkg/features/subscriber/collector.go | 57 ++++++++++++++++++++++++++-- pkg/features/subscriber/rpc.go | 22 ++++++++--- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/pkg/features/subscriber/collector.go b/pkg/features/subscriber/collector.go index c8c104b2..10c5a868 100644 --- a/pkg/features/subscriber/collector.go +++ b/pkg/features/subscriber/collector.go @@ -3,6 +3,10 @@ package subscriber import ( + "errors" + "fmt" + "strings" + "github.com/czerwonk/junos_exporter/pkg/collector" "github.com/prometheus/client_golang/prometheus" ) @@ -12,7 +16,7 @@ const prefix string = "junos_subscriber_info" var subscriberInfo *prometheus.Desc func init() { - l := []string{"target", "interface", "agent_circuit_id", "agent_remote_id"} + l := []string{"target", "interface", "agent_circuit_id", "agent_remote_id", "underlying_ifd"} subscriberInfo = prometheus.NewDesc(prefix+"", "Subscriber Detail", l, nil) } @@ -33,16 +37,61 @@ func (*subcsribers_information) Describe(ch chan<- *prometheus.Desc) { // 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 + } + + logicalInterfaceMap, err := getLogicalInterfaceInformation(client) if err != nil { return err } for _, subscriber := range x.SubscribersInformation.Subscriber { - labels := append(labelValues, subscriber.Interface, subscriber.AgentCircuitId, subscriber.AgentRemoteId) + 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) +} diff --git a/pkg/features/subscriber/rpc.go b/pkg/features/subscriber/rpc.go index 8fb028b1..3b8f359e 100644 --- a/pkg/features/subscriber/rpc.go +++ b/pkg/features/subscriber/rpc.go @@ -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"` }