Class: CollectdGearman::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/collectd_gearman/application.rb

Constant Summary collapse

CONF_FILE =
"/etc/collectd/nagios.yaml"
SEND_GEARMAN_BIN =
"/usr/bin/send_gearman"

Class Method Summary collapse

Class Method Details

.check_paramsObject



63
64
65
66
# File 'lib/collectd_gearman/application.rb', line 63

def self.check_params
  raise "Gearman server must be defined" unless options.gearman_server
  raise "Gearman key must be defined" unless options.gearman_key
end

.handle_optionsObject



24
25
26
27
28
29
# File 'lib/collectd_gearman/application.rb', line 24

def self.handle_options
  parse_args
  read_conf
  set_defaults
  check_params
end

.optionsObject



20
21
22
# File 'lib/collectd_gearman/application.rb', line 20

def self.options
  @options ||= OpenStruct.new
end

.parse_argsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/collectd_gearman/application.rb', line 31

def self.parse_args
  options.config_file = CONF_FILE

  OptionParser.new do |opts|
    opts.banner = "Usage: collectd_gearman [options]"

    opts.on('-f', '--config FILE', 'Config file') { |v| options.config_file = v }
    opts.on('-s', '--server SERVER', 'Gearman server') { |v| options.gearman_server = v }
    opts.on('-k', '--key KEY', 'Gearman key') { |v| options.gearman_key = v }
    opts.on('-g', '--send_gearman BIN', 'send_gearman binary') { |v| options.send_gearman = v }
    opts.on('-v', '--verbose', 'verbose') { |v| options.verbose = v }

    opts.on_tail("-h", "--help", "-H", "Display this help message.") do
      puts opts
      exit
    end

  end.parse!
end

.read_confObject



51
52
53
54
55
56
57
# File 'lib/collectd_gearman/application.rb', line 51

def self.read_conf
  return unless File.exist?(options.config_file)
  file_config = YAML.load_file(options.config_file)
  file_config.each do |k,v|
    options.send("#{k}=",v) unless options.send(k)
  end
end

.read_stdinObject

Read threshold data from Collectd



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/collectd_gearman/application.rb', line 69

def self.read_stdin
  data = {}
  ARGF.each do |line|
    if line =~ /^[a-zA-Z]+: .*/
      data.merge!({line.split(": ").first => line.split(": ").last.strip})
    else
      data.merge!(message: line.strip)
    end
  end

  # DataSource = value is the same as nothing
  data.delete("DataSource") if data["DataSource"] == "value"
  data
end

.runObject



12
13
14
15
16
17
18
# File 'lib/collectd_gearman/application.rb', line 12

def self.run
  standard_exception_handling do
    handle_options
    data = read_stdin
    send_commands data
  end
end

.send_commands(data) ⇒ Object

Send passive checks to all posible services



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/collectd_gearman/application.rb', line 85

def self.send_commands(data)
  # Plugin-Instance.Type-Instance.DataSource
  # Plugin-Instance.Type-Instance
  # Plugin-Instance.Type.DataSource
  # Plugin-Instance.Type
  # Plugin.Type-Instance.DataSource
  # Plugin.Type-Instance
  # Plugin.Type.DataSource
  # Plugin.Type
  
  if data["PluginInstance"] and data["TypeInstance"] and data["DataSource"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}-#{data["PluginInstance"]}.#{data["Type"]}-#{data["TypeInstance"]}.#{data["DataSource"]}"
  end
  
  if data["PluginInstance"] and data["TypeInstance"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}-#{data["PluginInstance"]}.#{data["Type"]}-#{data["TypeInstance"]}"
  end
  
  if data["PluginInstance"] and data["DataSource"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}-#{data["PluginInstance"]}.#{data["Type"]}.#{data["DataSource"]}"
  end
  
  if data["PluginInstance"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}-#{data["PluginInstance"]}.#{data["Type"]}"
  end
  
  if data["TypeInstance"] and data["DataSource"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}.#{data["Type"]}-#{data["TypeInstance"]}.#{data["DataSource"]}"
  end
  
  if data["TypeInstance"]
    send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}.#{data["Type"]}-#{data["TypeInstance"]}"
  end
  
  send_gearman data["Host"],data[:message],data["Severity"],"#{data["Plugin"]}.#{data["Type"]}"
end

.send_gearman(host, message, severity, service) ⇒ Object



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

def self.send_gearman(host,message,severity,service)
  case severity
  when "FAILURE"
    return_code = 2
  when "WARNING"
    return_code = 1
  when "OKAY"
    return_code = 0
  else
    return_code = 3
  end

  cmd = "#{options.send_gearman} --server=\"#{options.gearman_server}\" --encryption=yes --key=\"#{options.gearman_key}\" --host=\"#{host}\" --service=\"#{service}\" --message=\"#{message.gsub('"',"'")}\" -r=#{return_code}"

  raise "Command not found: #{options.send_gearman}" unless File.exist?(options.send_gearman)
  
  puts cmd if options.verbose

  system cmd
end

.set_defaultsObject



59
60
61
# File 'lib/collectd_gearman/application.rb', line 59

def self.set_defaults
  options.send_gearman ||= SEND_GEARMAN_BIN
end

.standard_exception_handlingObject



143
144
145
146
147
148
149
150
151
# File 'lib/collectd_gearman/application.rb', line 143

def self.standard_exception_handling
  yield
rescue SystemExit
  # Exit silently with current status
  raise
rescue Exception => ex
  $stderr.puts ex.message
  exit(false)
end