oxidized: Oxidized unable to parse/search multiple tags in http api call from PRTG

I’m not a seasoned programmer, nor am I a linux engineer. I’m a network engineer with a goal, and I’m motivated.

We are running PRTG with a myriad of devices. I’m running Oxidized and using the PRTG API to retrieve a list of devices containing certain tags like fortios, procurve, aoscx, etc. (Since I believe this is the only way Oxidized is able to figure out the type of device and what script to use for logging in and making a backup?)

I’ve configured it according to the documentation, and that works. As long as I only have 1 tag per device.

source:
  default: http
  http:
    url: [Redacted]
    hosts_location: devices
    scheme: https
    secure: false
    map:
      name: device
      ip: host
      model: tags
      group: group
model_map:
  juniper: junos
  cisco: ios
  fortinet: fortios
  aoscx: aoscx
  procuve: procurve
  aossw: procurve

When I have more than 1 tag per device in PRTG the tag field in the API call is shown as a single string separated by spaces.

For example:

    {
      "device": "LAB-XXX-XXX-SW01",
      "device_raw": "LAB-XXX-XXX-SW01",
      "host": "xxx.xxx.xxx.xxx",
      "host_raw": "xxx.xxx.xxx.xxx",
      "tags": "procurve switch",
      "tags_raw": "procurve switch",
      "group": "LAB-Probe",
      "group_raw": "LAB-Probe"
    },

Oxidized then doesn’t show the device in the webUI and is not contained in the backup. I’ve seen someone (although commented out) use a ruby regex in the config file after model: tags. (https://github.com/ytti/oxidized/issues/1999)

      model: tags !ruby/regexp /\bios|\bdnos|\bnxos|\bftos|\bfortios|\betherhaul|\bsonicos|\bairfiber/ig

I tried this, (added procurve to the regex ofcourse) and it doesn’t work. all devices are added as Junos (default in the config file)

We use multiple tags for other automation tasks and right now I’d have to break them in order to automatically back-up devices with the correct model in oxidized.

Is there a way to use a regex? and so, what would be the right one to use?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 38 (17 by maintainers)

Commits related to this issue

Most upvoted comments

The problem is in config.rb line 13

asetus = Asetus.new(name: 'oxidized', load: false, key_to_s: true, usrdir: usrdir, cfgfile: cfgfile)

The key_to_s: trueturns the Regexp into string, therefore we never run the Regexp match.

I’m not sure why it is like that, it’s been like that since day1, but I for life of me can’t recall the reason for it. Possibly I thought about integer keys.

Can you try to remove key_to_s: true, it should default to false. Then the Regexp should be preserved, unless we break something else that I cannot recall

I’m not ok with that, sorry. Either I can work with the actual issue or we just have to postpone this for now, maybe one of us gets bright idea what we are doing wrong.

I am out of ideas, if the source.rb file is correct as in https://ytti.fi/ I think it should work and I don’t think I can debug it further without seeing the actual issue myself.

So if you can provide access to the device API call even better ssh access to the lab device seeing this issue, I’m afraid I can’t help. I have some ideas, but it’s too much trouble to troubleshoot back and forth over mail/issue.

Not sure what other tell you, I wrote test for it, and test passes.

require_relative '../spec_helper'
require 'oxidized/source/source'

describe Oxidized::Source do
  describe '#map_model' do
    before(:each) do
      Asetus.any_instance.expects(:load)
      Asetus.any_instance.expects(:create).returns(false)

      # Set :home_dir to make sure the OXIDIZED_HOME environment variable is not used
      Oxidized::Config.load({ home_dir: '/cfg_path/' })
      yaml = %Q(
        juniper: junos
        !ruby/regexp /procurve/: procurve
      )
      Oxidized.config.model_map = YAML.unsafe_load(yaml)
      @source = Oxidized::Source::Source.new
    end

    it 'maps existing string key to value' do
      _(@source.map_model('juniper')).must_equal 'junos'
    end

    it 'returns same value on non-existing string key' do
      _(@source.map_model('ios')).must_equal 'ios'
    end

    it 'maps matching regexp key to avlue' do
      _(@source.map_model('foo procurve1234 bar')).must_equal 'procurve'
    end
  end
end
def map_model(model)
  @model_map.each do |key, value|
    mthd = key.class == Regexp ? :match : :eql?
    return value if model.send(mthd, key)
  end
  model
end
❯ ruby -Ilib:test spec/source/source_spec.rb --verbose
Run options: --verbose --seed 5178

# Running:

Oxidized::Source::#map_model#test_0002_returns same value on non-existing string key = 0.00 s = .
Oxidized::Source::#map_model#test_0003_maps matching regexp key to avlue = 0.00 s = .
Oxidized::Source::#map_model#test_0001_maps existing string key to value = 0.00 s = .

Finished in 0.001571s, 1909.6117 runs/s, 5728.8351 assertions/s.

3 runs, 9 assertions, 0 failures, 0 errors, 0 skips
Coverage report generated for Minitest, RSpec to /Users/ytti/RubymineProjects/oxidized/coverage/coverage.xml. 327 / 865 LOC (37.80%) covered
Coverage report generated for Minitest, RSpec to /Users/ytti/RubymineProjects/oxidized/coverage.
Line Coverage: 37.8% (327 / 865)

I think your config is right for the proposed code.

Unclear why you see the actual string returned, possibly the key.class is never Regexp for reasons that would need troubleshooting with real data.