Module: Fluent::Plugin::TDMonitorAgentInput::Mac
- Defined in:
- lib/fluent/plugin/in_td_monitor_agent.rb
Overview
from macaddr gem
Constant Summary collapse
- RE =
%r/(?:[^:\-]|\A)(?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F](?:[^:\-]|\Z)/io
Class Attribute Summary collapse
-
.mac_address ⇒ Object
Accessor for the system’s first MAC address, requires a call to #address first.
Class Method Summary collapse
-
.address ⇒ Object
Discovers and returns the system’s MAC addresses.
- .parse(output) ⇒ Object
Class Attribute Details
.mac_address ⇒ Object
Accessor for the system’s first MAC address, requires a call to #address first
433 434 435 |
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 433 def mac_address @mac_address end |
Class Method Details
.address ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 442 def address return @mac_address if defined? @mac_address and @mac_address re = %r/[^:\-](?:[0-9A-F][0-9A-F][:\-]){5}[0-9A-F][0-9A-F][^:\-]/io cmds = '/sbin/ifconfig', '/bin/ifconfig', 'ifconfig', 'ipconfig /all', 'cat /sys/class/net/*/address' null = test(?e, '/dev/null') ? '/dev/null' : 'NUL' output = nil cmds.each do |cmd| begin r, w = IO.pipe ::Process.waitpid(spawn(cmd, :out => w)) w.close stdout = r.read next unless stdout and stdout.size > 0 output = stdout and break rescue # go to next command! end end raise "all of #{ cmds.join ' ' } failed" unless output @mac_address = parse(output) end |
.parse(output) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 |
# File 'lib/fluent/plugin/in_td_monitor_agent.rb', line 467 def parse(output) lines = output.split(/\n/) candidates = lines.select{|line| line =~ RE} raise 'no mac address candidates' unless candidates.first candidates.map!{|c| c[RE].strip} maddr = candidates.first raise 'no mac address found' unless maddr maddr.strip! maddr.instance_eval{ @list = candidates; def list() @list end } maddr end |