Class: Fluent::OpenLDAPMonitorInput

Inherits:
Input
  • Object
show all
Defined in:
lib/fluent/plugin/in_openldap_monitor.rb

Defined Under Namespace

Classes: TimerWatcher

Constant Summary collapse

SUPPRESS_ATTRIBUTES =
%w{ dn structuralobjectclass creatorsname modifiersname createtimestamp modifytimestamp monitortimestamp entrydn subschemasubentry hassubordinates }

Instance Method Summary collapse

Constructor Details

#initializeOpenLDAPMonitorInput

Returns a new instance of OpenLDAPMonitorInput.



11
12
13
14
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 11

def initialize
  super
  require 'net/ldap'
end

Instance Method Details

#configure(conf) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 16

def configure(conf)
  super

  auth = {
    method: :simple,
    username: @bind_dn,
    password: @bind_password,
  }
  @ldap = Net::LDAP.new(host: @host, port: @port, auth: auth)
end

#convert_entries(entries) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 50

def convert_entries(entries)
  result = {}
  entries.each do |entry|
    values_of = {}
    entry.each do |attribute, values|
      next if SUPPRESS_ATTRIBUTES.include?(attribute.to_s)
      values_of[attribute] = convert_values_type(values)
    end

    dn = entry[:dn].first # e.g.) cn=Current,cn=Connections,cn=Monitor
    dn = dn.split(',').map { |d| d.sub(/cn\=/,'').gsub(/\s+/,'_').downcase.to_sym }.reverse # e.g.) [:monitor, :connections, :current]

    case dn.size
    when 1
      result[dn[0]] ||= values_of
    when 2
      result[dn[0]][dn[1]] ||= values_of
    when 3
      result[dn[0]][dn[1]][dn[2]] ||= values_of
    when 4
      result[dn[0]][dn[1]][dn[2]][dn[3]] ||= values_of
    else raise dn.size.to_s
    end
  end
  result
end

#convert_values_type(values) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 77

def convert_values_type(values)
  values = [values].flatten.map { |v|
    if v == 'TRUE'
      true
    elsif v == 'FALSE'
      false
    elsif v.match(/^\d+$/)
      v.to_i
    else
      v
    end
  }

  values.size == 1 ? values.first : values
end

#ldapsearchObject



41
42
43
44
45
46
47
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 41

def ldapsearch
  entries = @ldap.search(attributes: ['+'], base: 'cn=Monitor')
  result = convert_entries(entries.flatten)
  Fluent::Engine.emit(@tag, Fluent::Engine.now, result)
rescue => e
  log.warn("#{e.message} (#{e.class.to_s}) #{e.backtrace.to_s}")
end

#shutdownObject



35
36
37
38
39
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 35

def shutdown
  @watcher.detach
  @loop.stop
  @thread.join
end

#startObject



27
28
29
30
31
32
33
# File 'lib/fluent/plugin/in_openldap_monitor.rb', line 27

def start
  super
  @watcher = TimerWatcher.new(@check_interval, true, &method(:ldapsearch))
  @loop = Coolio::Loop.new
  @watcher.attach(@loop)
  @thread = Thread.new(@loop.run)
end