openconfig / gnmic

gNMIc is a gNMI CLI client and collector
https://gnmic.openconfig.net
Apache License 2.0
166 stars 52 forks source link

event-strings transforms not working correctly (string to int, juniper router) #466

Closed lucasalvatore closed 4 weeks ago

lucasalvatore commented 4 weeks ago

Having an issue trying to have event-strings replace a string with an int. I'm using gnmic to talk to a juniper MX router, and trying to change the PSU states from "Online" and "Offline" to "1" and "0" so i can send this to prometheus.

This is my config.yml

targets:
  bbr2:
    address: "10.10.10.10:32767"
    username: admin
    password: xxx
    insecure: true
    subscriptions:
      - sub2

subscriptions:
  sub2:
    paths:
       - "/components/component[name=PEM2]/properties/"
       - "/components/component[name=PEM1]/properties/"
    encoding: json
    mode: stream
    format: event
    stream-mode: sample
    sample-interval: 15s

processors:
 - pem-state:
    event-strings:
      value-name:
        - "/components/component/properties/property/state/value"
      transforms:
        - replace:
            apply-on: "value"
            old: "Online"
            new: "1"
        - replace:
            apply-on: "value"
            old: "Offline"
            new: "0"
outputs:
  out1:
    type: file
    format: event
    event-processors:
      - pem-state
    msg-template: |
      {{range .}}
      {{if index .values "/components/component/properties/property/state/value"}}
      Name: {{.name}}
      Component Name: {{(index .tags "component_name")}}
      Subscription Name: {{(index .tags "subscription-name")}}
      State: {{ index .values "/components/component/properties/property/state/value" }}
      {{- end}}
      {{- end}}

#  prom-output:
#    type: prometheus
#    listen: localhost:9091
#    path: /metrics

(trying to send this to prometheus but for testing i'm just outputting it to a the screen)

This is the response from the Juniper device i'm querying:

    "name": "default-1718636139",
    "timestamp": 1718636144994525462,
    "tags": {
      "component_name": "PEM2",
      "property_name": "state",
      "source": "145.40.84.1:32767",
      "subscription-name": "default-1718636139"
    },
    "values": {
      "/components/component/properties/property/state/value": "Offline"
    }
  },

When i run gnmic --config config.yml sub --debug i'm not seeing anything get replace... this is my output on the screen:

Name: sub2
Component Name: PEM2
Subscription Name: sub2
State: Offline

the debug output shows this:

2024/06/17 15:33:48.612352 /home/runner/work/gnmic/gnmic/pkg/app/collector.go:71: [gnmic] target "bbr2": gNMI Subscribe Response: &{SubscriptionName:sub2 SubscriptionConfig:{"name":"sub2","paths":["/components/component[name=PEM2]/properties/","/components/component[name=PEM1]/properties/"],"mode":"stream","stream-mode":"sample","encoding":"json","sample-interval":15000000000} Response:update:{timestamp:1718638428606405141  prefix:{elem:{name:"components"}  elem:{name:"component"  key:{key:"name"  value:"PEM2"}}  elem:{name:"properties"}  elem:{name:"property"  key:{key:"name"  value:"state"}}}  update:{path:{elem:{name:"state"}  elem:{name:"value"}}  val:{json_val:"\"Offline\""}}  update:{path:{elem:{name:"state"} 

Any thoughts as to what i'm missing here? gnmic version is 0.37.0

lucasalvatore commented 4 weeks ago

ended up getting this working:

processors:
 - pem-state:
    event-strings:
            #      debug: true
      value-names:
        - "/components/component/properties/property/state/value"
      transforms:
        - replace:
            apply-on: value
            old: "Online"
            new: "1"
        - replace:
            apply-on: value
            old: "Offline"
            new: "0"

diff is i had "value" quoted