Permalink
Browse files

First pass at an agent that shows all hosts that have seen a given MA…

…C address
  • Loading branch information...
1 parent d2dc926 commit 90adce4086b3219920cc2ac42e407af8aa2297d5 @deanwilson committed Nov 15, 2010
Showing with 66 additions and 0 deletions.
  1. +32 −0 findmacaddress/findmacaddress.rb
  2. +34 −0 findmacaddress/mc-findmacaddress
@@ -0,0 +1,32 @@
+module MCollective
+ module Agent
+ class Findmacaddress<RPC::Agent
+
+ metadata :name => "SimpleRPC MAC Addess Finder",
+ :description => "Agent that finds which machines have seen a MAC address",
+ :author => "Dean Wilson",
+ :license => "GPLv2",
+ :version => "0.1",
+ :url => "http://www.unixdaemon.net/mcollective-plugins/",
+ :timeout => 20
+
+
+ action "findmacaddress" do
+ validate :macaddress, String
+
+ reply[:sightings] = []
+ macaddress = request[:macaddress].upcase
+
+ arpcmd = '/sbin/arp -n'
+ %x{#{arpcmd}}.each do |line| # TODO make the arp bin location a config option.
+ fields = line.split(/\s+/)
+
+ if fields[2].upcase == macaddress
+ reply[:sightings] << { :ip => fields[0], :interface => fields[4] }
+ end
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,34 @@
+#!/usr/bin/ruby
+require 'mcollective'
+
+include MCollective::RPC
+
+if ARGV.length > 0
+ macaddress = ARGV.shift
+else
+ puts "findmacaddress needs a MAC address"
+ exit 1
+end
+
+findmacaddress = rpcclient("findmacaddress")
+
+seen = Hash.new()
+
+findmacaddress.findmacaddress(:macaddress => macaddress).each do |result|
+ if result[:statuscode] == 0
+ result[:data][:sightings].each do |sighting|
+ seen[sighting[:ip]] ||= []
+ seen[sighting[:ip]] << { :by => result[:sender], :on => sighting[:interface] }
+ end
+ end
+end
+
+printf "Found MAC Address for #{macaddress}\n\n"
+seen.keys.sort.each do |ip|
+ puts " #{ip} seen #{seen[ip].size} times"
+ seen[ip].sort { |a,b| a[:by] <=> b[:by] }.each do |sighting|
+ printf(" %-40s %-40s\n", sighting[:by], sighting[:on])
+ end
+end
+
+findmacaddress.printrpcstats

0 comments on commit 90adce4

Please sign in to comment.