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

Dont resolve name if IP given #78

Closed
MrRJ45 opened this issue Feb 21, 2015 · 4 comments
Closed

Dont resolve name if IP given #78

MrRJ45 opened this issue Feb 21, 2015 · 4 comments

Comments

@MrRJ45
Copy link
Contributor

MrRJ45 commented Feb 21, 2015

We have alot of switches backed up using oxidized and they all have internal names, so no DNS for these.

I've modified "lib/oxidized/node.rb" with the following, and it seems to work well.

if opt[:ip].length > 0
@ip = opt[:ip]
else
@ip = Resolv.getaddress @name
end

Would it be possible to get this changed officially if people think this is a good idea?

@ytti
Copy link
Owner

ytti commented Feb 21, 2015

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?

[ytti@ytti.fi ~]% 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)>

@MrRJ45
Copy link
Contributor Author

MrRJ45 commented Feb 21, 2015

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 :)

@ytti
Copy link
Owner

ytti commented Feb 21, 2015

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.

@MrRJ45
Copy link
Contributor Author

MrRJ45 commented Feb 21, 2015

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants