Class: Fluent::MongostatInput

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

Instance Method Summary collapse

Constructor Details

#initializeMongostatInput

Returns a new instance of MongostatInput.



10
11
12
# File 'lib/fluent/plugin/in_mongostat.rb', line 10

def initialize
  super
end

Instance Method Details

#configure(conf) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fluent/plugin/in_mongostat.rb', line 25

def configure(conf)
  super

  if !mongostat_exists?
    raise ConfigError, '"mongostat" command not found.'
  end

  @command = %(mongostat #{@option} --json #{@refresh_interval})
  @hostname = get_hostname
end

#get_hostnameObject



151
152
153
# File 'lib/fluent/plugin/in_mongostat.rb', line 151

def get_hostname
  return `hostname`.chomp!
end

#mongostat_exists?Boolean

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
# File 'lib/fluent/plugin/in_mongostat.rb', line 142

def mongostat_exists?
  begin
    `mongostat --version`
  rescue Errno::ENOENT
    return false
  end
  return true
end

#parse_line(line) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fluent/plugin/in_mongostat.rb', line 55

def parse_line(line)
  begin
    json_hash = JSON.parse(line.delete('*'))
  rescue JSON::ParserError
    raise ParserError, 'response json parse error'
  end

  status = json_hash.values[0]

  if status.has_key?('error')
    return status
  end

 if status.has_key?('command')
    status['command'] = status['command'].split('|')[0].to_i
  end

  if !status.has_key?('host')
    status['hostname'] = @hostname
  else
    status['hostname'] = status.delete('host')
  end

  if status.has_key?('arw')
    arw = status['arw'].split('|')
    status['arw'] = {'ar' => arw[0].to_i, 'aw' => arw[1].to_i}
  elsif status.has_key?('ar|aw')
    arw = status['ar|aw'].split('|')
    status.delete('ar|aw')
    status['arw'] = {'ar' => arw[0].to_i, 'aw' => arw[1].to_i}
  end

  if status.has_key?('qrw')
    qrw = status['qrw'].split('|')
    status['qrw'] = {'qr' => qrw[0].to_i, 'qw' => qrw[1].to_i}
  elsif status.has_key?('qr|qw')
    qrw = status['qr|qw'].split('|')
    status.delete('qr|qw')
    status['qrw'] = {'qr' => qrw[0].to_i, 'qw' => qrw[1].to_i}
  end

  if status.has_key?('netIn')
    status['net_in'] = status.delete('netIn')
  end

  if status.has_key?('netOut')
    status['net_out'] = status.delete('netOut')
  end

  status['conn'] = status['conn'].to_i if status.has_key?('conn')
  status['delete'] = status['delete'].to_i  if status.has_key?('delete')
  status['flushes'] = status['flushes'].to_i  if status.has_key?('flushes')
  status['getmore'] = status['getmore'].to_i  if status.has_key?('getmore')
  status['insert'] = status['insert'].to_i  if status.has_key?('insert')
  status['query'] = status['query'].to_i  if status.has_key?('query')
  status['update'] = status['update'].to_i  if status.has_key?('update')
  status['dirty'] = status['dirty'].to_f  if status.has_key?('dirty')
  status['used'] = status['used'].to_f  if status.has_key?('used')

  status['net_in'] = parse_unit(status['net_in']) if status.has_key?('net_in')
  status['net_out'] = parse_unit(status['net_out']) if status.has_key?('net_out')
  status['res'] = parse_unit(status['res']) if status.has_key?('res')
  status['vsize'] = parse_unit(status['vsize']) if status.has_key?('vsize')

  return status
end

#parse_unit(str) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/fluent/plugin/in_mongostat.rb', line 122

def parse_unit(str)
  si_prefix = {
    'T'   =>  1e12,
    't'   =>  1e12,
    'G'   =>  1e9,
    'g'   =>  1e9,
    'M'   =>  1e6,
    'm'   =>  1e6,
    'K'   =>  1e3,
    'k'   =>  1e3
  }
  value = str.to_f
  unit = str[/[a-zA-Z]+/]
  if si_prefix.has_key?(unit)
    return (value * si_prefix[unit]).to_i
  else
    return value.to_i
  end
end

#runObject



46
47
48
49
50
51
52
53
# File 'lib/fluent/plugin/in_mongostat.rb', line 46

def run
  Open3.popen3(@command) do |i, o, e, w|
    o.each do |line|
      status = parse_line(line)
      router.emit(@tag, Fluent::Engine.now, status)
    end
  end
end

#shutdownObject



41
42
43
44
# File 'lib/fluent/plugin/in_mongostat.rb', line 41

def shutdown
  super
  Thread.kill(@thread)
end

#startObject



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

def start
  super
  @thread = Thread.new(&method(:run))
end