Class: CollectdGearman::Application
- Inherits:
-
Object
- Object
- CollectdGearman::Application
- 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
- .check_params ⇒ Object
- .handle_options ⇒ Object
- .options ⇒ Object
- .parse_args ⇒ Object
- .read_conf ⇒ Object
-
.read_stdin ⇒ Object
Read threshold data from Collectd.
- .run ⇒ Object
-
.send_commands(data) ⇒ Object
Send passive checks to all posible services.
- .send_gearman(host, message, severity, service) ⇒ Object
- .set_defaults ⇒ Object
- .standard_exception_handling ⇒ Object
Class Method Details
.check_params ⇒ Object
63 64 65 66 |
# File 'lib/collectd_gearman/application.rb', line 63 def self.check_params raise "Gearman server must be defined" unless .gearman_server raise "Gearman key must be defined" unless .gearman_key end |
.handle_options ⇒ Object
24 25 26 27 28 29 |
# File 'lib/collectd_gearman/application.rb', line 24 def self. parse_args read_conf set_defaults check_params end |
.options ⇒ Object
20 21 22 |
# File 'lib/collectd_gearman/application.rb', line 20 def self. @options ||= OpenStruct.new end |
.parse_args ⇒ Object
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 .config_file = CONF_FILE OptionParser.new do |opts| opts. = "Usage: collectd_gearman [options]" opts.on('-f', '--config FILE', 'Config file') { |v| .config_file = v } opts.on('-s', '--server SERVER', 'Gearman server') { |v| .gearman_server = v } opts.on('-k', '--key KEY', 'Gearman key') { |v| .gearman_key = v } opts.on('-g', '--send_gearman BIN', 'send_gearman binary') { |v| .send_gearman = v } opts.on('-v', '--verbose', 'verbose') { |v| .verbose = v } opts.on_tail("-h", "--help", "-H", "Display this help message.") do puts opts exit end end.parse! end |
.read_conf ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/collectd_gearman/application.rb', line 51 def self.read_conf return unless File.exist?(.config_file) file_config = YAML.load_file(.config_file) file_config.each do |k,v| .send("#{k}=",v) unless .send(k) end end |
.read_stdin ⇒ Object
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 |
.run ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/collectd_gearman/application.rb', line 12 def self.run standard_exception_handling do 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,,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 = "#{.send_gearman} --server=\"#{.gearman_server}\" --encryption=yes --key=\"#{.gearman_key}\" --host=\"#{host}\" --service=\"#{service}\" --message=\"#{.gsub('"',"'")}\" -r=#{return_code}" raise "Command not found: #{.send_gearman}" unless File.exist?(.send_gearman) puts cmd if .verbose system cmd end |
.set_defaults ⇒ Object
59 60 61 |
# File 'lib/collectd_gearman/application.rb', line 59 def self.set_defaults .send_gearman ||= SEND_GEARMAN_BIN end |
.standard_exception_handling ⇒ Object
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. exit(false) end |