Class: Riemann::Babbler::Plugin
- Inherits:
-
Object
- Object
- Riemann::Babbler::Plugin
- Includes:
- Errors, Logging, Options, Riemann::Babbler::Plugins::Helpers
- Defined in:
- lib/riemann/babbler/plugin.rb
Direct Known Subclasses
Cpu, CpuFan, CpuTemp, Disk, DiskStat, ErrorsReporter, Exim4, FindFiles, Haproxy, Http, Iptables, La, Mdadm, MegaCli, Memory, Net, NetStat, Nginx, Ntp, Pgsql, ResponderHttp, ResponderUdp, Runit, StatusFile, TwCli
Defined Under Namespace
Classes: Cpu, CpuFan, CpuTemp, Disk, DiskStat, ErrorsReporter, Exim4, FindFiles, Haproxy, Http, Iptables, La, Mdadm, MegaCli, Memory, Net, NetStat, Nginx, Ntp, Pgsql, ResponderHttp, ResponderUdp, Runit, StatusFile, TwCli
Constant Summary
Constants included from Riemann::Babbler::Plugins::Helpers
Riemann::Babbler::Plugins::Helpers::RIEMANN_RESERVED_FIELDS
Constants included from Errors
Errors::CONNECTION_PROBLEM, Errors::INIT_CONNECT, Errors::RESOLV_RIEMANN_SERVER, Errors::USER_CALL_SHUTDOWN
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#plugin ⇒ Object
readonly
Returns the value of attribute plugin.
-
#plugin_name ⇒ Object
readonly
Returns the value of attribute plugin_name.
-
#riemann ⇒ Object
readonly
Returns the value of attribute riemann.
Class Method Summary collapse
Instance Method Summary collapse
-
#get_state(event) ⇒ Object
State.
- #init ⇒ Object
-
#initialize(riemann) ⇒ Plugin
constructor
A new instance of Plugin.
-
#not_minimize_sent_event(event) ⇒ Object
True if event may be sended.
-
#plugin_error!(msg) ⇒ Object
Errors ###.
- #plugin_no_error! ⇒ Object
- #report(event) ⇒ Object
-
#report_with_diff(event) ⇒ Object
Helper for reports ###.
-
#run! ⇒ Object
Main method.
- #run_plugin ⇒ Object
- #set_default ⇒ Object
- #set_last_event(event) ⇒ Object
- #tick ⇒ Object
Methods included from Riemann::Babbler::Plugins::Helpers
#event_from_hash, #event_from_json, #rest_get, #shell, #unixnow
Methods included from Options
included, #merge_config, #name_to_underscore, #opts, #opts_reset!, #underscore_to_name
Methods included from Logging
#get_logger_speaker, included, #log, #set_logger_speaker
Constructor Details
#initialize(riemann) ⇒ Plugin
Returns a new instance of Plugin.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/riemann/babbler/plugin.rb', line 31 def initialize(riemann) @riemann = riemann @storage = Hash.new @storage['last_state'] = Hash.new @storage['last_metric'] = Hash.new @plugin_name = name_to_underscore(self.class.name) @plugin = opts.plugins.send(plugin_name) @errors = opts.errors.send(plugin_name) set_default init end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
29 30 31 |
# File 'lib/riemann/babbler/plugin.rb', line 29 def errors @errors end |
#plugin ⇒ Object (readonly)
Returns the value of attribute plugin.
29 30 31 |
# File 'lib/riemann/babbler/plugin.rb', line 29 def plugin @plugin end |
#plugin_name ⇒ Object (readonly)
Returns the value of attribute plugin_name.
29 30 31 |
# File 'lib/riemann/babbler/plugin.rb', line 29 def plugin_name @plugin_name end |
#riemann ⇒ Object (readonly)
Returns the value of attribute riemann.
29 30 31 |
# File 'lib/riemann/babbler/plugin.rb', line 29 def riemann @riemann end |
Class Method Details
.inherited(klass) ⇒ Object
25 26 27 |
# File 'lib/riemann/babbler/plugin.rb', line 25 def self.inherited(klass) registered_plugins << klass end |
.registered_plugins ⇒ Object
21 22 23 |
# File 'lib/riemann/babbler/plugin.rb', line 21 def self.registered_plugins @plugins ||= [] end |
Instance Method Details
#get_state(event) ⇒ Object
Returns state.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/riemann/babbler/plugin.rb', line 95 def get_state(event) return event[:state] if event[:state] return event[:state] if event[:metric].nil? metric = event[:metric].to_f warning = plugin.states.warning.nil? ? nil : plugin.states.warning critical = plugin.states.critical.nil? ? nil : plugin.states.critical return State::OK if (warning || critical).nil? if warning && critical return case when metric.between?(warning, critical) State::WARNING when metric > warning State::CRITICAL else State::OK end end if warning return (metric >= warning) ? State::WARNING : State::OK end if critical return (metric >= critical) ? State::CRITICAL : State::OK end end |
#init ⇒ Object
54 55 56 |
# File 'lib/riemann/babbler/plugin.rb', line 54 def init # nothing to do end |
#not_minimize_sent_event(event) ⇒ Object
Returns true if event may be sended.
121 122 123 124 125 126 127 128 |
# File 'lib/riemann/babbler/plugin.rb', line 121 def not_minimize_sent_event(event) return true if !opts.riemann.minimize_event_count # нет задачи минизировать return true if event[:metric] # если есть метрика - надо отослать graphite return true if event[:state] != State::OK # все предупреждения отсылаем return true if @storage['last_state'][event[:service]] != State::OK log :debug, "Skip send event #{event}" false end |
#plugin_error!(msg) ⇒ Object
Errors ###
136 137 138 139 140 141 142 143 |
# File 'lib/riemann/babbler/plugin.rb', line 136 def plugin_error!(msg) errors.count += 1 log :error, "#{plugin.service} error num #{errors.count}:\n #{msg}" errors.has_last_error = true errors.reported = false errors.last_error_at = Time.now errors.last_error = "#{msg}" end |
#plugin_no_error! ⇒ Object
145 146 147 148 149 150 |
# File 'lib/riemann/babbler/plugin.rb', line 145 def plugin_no_error! if errors.has_last_error log :error, "#{plugin.service} error num #{errors.count} fixed" errors.has_last_error = false end end |
#report(event) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/riemann/babbler/plugin.rb', line 72 def report(event) report_with_diff(event) and return if event[:as_diff] event[:metric] = event[:metric].round(2) if event[:metric].kind_of? Float event[:state] = get_state(event) riemann << event if not_minimize_sent_event(event) set_last_event(event) end |
#report_with_diff(event) ⇒ Object
Helper for reports ###
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/riemann/babbler/plugin.rb', line 83 def report_with_diff(event) current_metric = event[:metric] old_metric = @storage['last_metric'][event[:service]] if old_metric event[:metric] = current_metric - old_metric event.delete(:as_diff) report(event) end @storage['last_metric'][event[:service]] = current_metric end |
#run! ⇒ Object
Main method
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/riemann/babbler/plugin.rb', line 153 def run! return 0 unless run_plugin sleep Random.new.rand(10) loop do t_start = Time.now begin Timeout::timeout(plugin.interval.to_f * 2/3 ) { tick } rescue TimeoutError plugin_error!('Timeout plugin execution') rescue => e plugin_error!("Plugin '#{plugin_name}' has a error.\n #{e.class}: #{e}\n #{e.backtrace.join("\n")}") else plugin_no_error! end sleep(plugin.interval - (Time.now - t_start).to_i) end end |
#run_plugin ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/riemann/babbler/plugin.rb', line 58 def run_plugin if plugin.run.nil? true else plugin.run ? true : false end end |
#set_default ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/riemann/babbler/plugin.rb', line 43 def set_default plugin.set_default(:interval, opts.riemann.interval) plugin.timeout = (plugin.interval * 2).to_f/3 errors.last_error = nil errors.last_error_at = nil errors.has_last_error = false errors.reported = true plugin_no_error! errors.count = 0 end |
#set_last_event(event) ⇒ Object
130 131 132 133 |
# File 'lib/riemann/babbler/plugin.rb', line 130 def set_last_event(event) # тут пока только last_state @storage['last_state'][event[:service]] = event[:state] if opts.riemann.minimize_event_count end |
#tick ⇒ Object
66 67 68 69 70 |
# File 'lib/riemann/babbler/plugin.rb', line 66 def tick posted_array = collect posted_array = posted_array.class == Array ? posted_array : [posted_array] posted_array.uniq.each { |event| report event } end |