Giter Site home page Giter Site logo

Dont resolve name if IP given about oxidized HOT 4 CLOSED

ytti avatar ytti commented on May 27, 2024
Dont resolve name if IP given

from oxidized.

Comments (4)

ytti avatar ytti commented on May 27, 2024

And you're specifically providing in your source (sql or 'router.db') 'ip' field, instead of 'name' field, or you're providing both? And what you want is node.rb to also accept that source can provide 'ip'?

If I understood correctly, I think it's useful. And I'm wondering why #length > 0? Wouldn't just 'if opt[:ip]' work? Or is there case when it's defined but it's empty?

@ip = opt[:ip]
@ip ||= Resolve.getaddress @name

So if opt[:ip] is defined, we use it, if it's not defined, we try to resolve?

Should we try to validate it being actually IP adddress?

Something like this maybe?

[[email protected] ~]% pry -r ipaddr
[1] pry(main)> ip_src = '192.0.2.42'
=> "192.0.2.42"
[2] pry(main)> ip = IPAddr.new(ip_src).to_s rescue nil
=> "192.0.2.42"
[3] pry(main)> ip ||= 'name'
=> "192.0.2.42"
[4] pry(main)> ip_src = '2001:db8::42'
=> "2001:db8::42"
[5] pry(main)> ip = IPAddr.new(ip_src).to_s rescue nil
=> "2001:db8::42"
[6] pry(main)> ip ||= 'name'
=> "2001:db8::42"
[7] pry(main)> ip_src = ''
=> ""
[8] pry(main)> ip = IPAddr.new(ip_src).to_s rescue nil
=> nil
[9] pry(main)> ip ||= 'name'
=> "name"
[10] pry(main)> ip_src = nil
=> nil
[11] pry(main)> ip = IPAddr.new(ip_src).to_s rescue nil
=> nil
[12] pry(main)> ip ||= 'name'
=> "name"
[13] pry(main)>

from oxidized.

MrRJ45 avatar MrRJ45 commented on May 27, 2024

I should have included my config :) I am providing both, like this:

source:
default: sql
sql:
adapter: sqlite
database: "/home/oxidized/devices.db"
table: devices
map:
name: name
ip: ipv4
model: model

In the database the "name" column is our friendly name of the device.

I think checking the length is good as if somebody inserts into the database a hostname with an empty "ipv4" column then it will attempt to resolve the host. Of course, it should be null in the database, but that depends on the db schema.

Ruby is something I'm new to, so please be gentle :)

from oxidized.

ytti avatar ytti commented on May 27, 2024

I think it makes sense and is a good idea. But maybe we should be more strict about getting real IP address.
Can you test this:

ytti@ytti ~/u/g/o/l/oxidized> git diff
diff --git a/lib/oxidized/node.rb b/lib/oxidized/node.rb
index 6bc2b0f..12ed5cc 100644
--- a/lib/oxidized/node.rb
+++ b/lib/oxidized/node.rb
@@ -1,5 +1,6 @@
 module Oxidized
   require 'resolv'
+  require 'ipaddr'
   require 'ostruct'
   require_relative 'node/stats'
   class MethodNotFound < OxidizedError; end
@@ -10,7 +11,8 @@ module Oxidized
     alias :running? :running
     def initialize opt
       @name           = opt[:name]
-      @ip             = Resolv.getaddress @name
+      @ip             = IPAddr.new(opt[:ip]).to_s rescue nil
+      @ip           ||= Resolv.getaddress @name
       @group          = opt[:group]
       @input          = resolve_input opt
       @output         = resolve_output opt
ytti@ytti ~/u/g/o/l/oxidized> 

If possible with bogus data in SQL too and monitor syslog.

from oxidized.

MrRJ45 avatar MrRJ45 commented on May 27, 2024

That looks to work good to me. Tests I have done:

  1. Name and IP - Works as expected
  2. Name and invalid IP specified - Ignores IP and looks up host.
  3. Name and blank IP (Length 0) - Same as 2
  4. Name and null IP - Same as both above

A elegant solution compared to mine :)

from oxidized.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.