Class: Zabbix::Monitor
- Inherits:
-
Object
- Object
- Zabbix::Monitor
- Defined in:
- lib/zabbix/monitor.rb
Overview
Base class for processing and delivering the results to an endpoint
Instance Method Summary collapse
-
#collect_data ⇒ void
Loops through all rules and process the result.
-
#config ⇒ Zabbix::Config
private
The zabbix-monitor config.
-
#process_data(key, value) ⇒ void
private
Process the result for a rule based on the mode specified in the config.
-
#schedule ⇒ void
Schedule the data collector.
- #scheduled_collect_data ⇒ Object private
-
#to_file(key, value) ⇒ void
private
Writes the result to a file.
-
#to_stdout(key, value) ⇒ void
private
Prints the result of the monitored rule to stdout.
-
#to_zabbix(key, value) ⇒ void
private
Use
zabbix_sender
command to send each result to the Zabbix server.
Instance Method Details
#collect_data ⇒ void
This method returns an undefined value.
Loops through all rules and process the result
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/zabbix/monitor.rb', line 24 def collect_data Zabbix.logger.info '[Monitor] collecting data' current_zabbix_rule = nil config.rules.each do |rule| current_zabbix_rule = rule value = eval rule[:command] process_data rule[:zabbix_key], value end rescue Exception => e = "#{current_zabbix_rule[:zabbix_key]} command: #{current_zabbix_rule[:command]} with message: #{e.}" Zabbix.logger.error "[Monitor] failed collecting data for rule: #{}" end |
#config ⇒ Zabbix::Config (private)
Returns the zabbix-monitor config.
40 41 42 |
# File 'lib/zabbix/monitor.rb', line 40 def config Zabbix.config end |
#process_data(key, value) ⇒ void (private)
This method returns an undefined value.
Process the result for a rule based on the mode specified in the config
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/zabbix/monitor.rb', line 50 def process_data key, value case config.mode when :push to_zabbix key, value when :file to_file key, value when :stdout to_stdout key, value end end |
#schedule ⇒ void
This method returns an undefined value.
Schedule the data collector
14 15 16 17 18 19 20 |
# File 'lib/zabbix/monitor.rb', line 14 def schedule Rufus::Scheduler.new.tap do |scheduler| scheduler.every config.rate do scheduled_collect_data end end.join end |
#scheduled_collect_data ⇒ Object (private)
106 107 108 109 110 |
# File 'lib/zabbix/monitor.rb', line 106 def scheduled_collect_data ActiveRecord::Base.connection_pool.with_connection do collect_data end end |
#to_file(key, value) ⇒ void (private)
This method returns an undefined value.
Writes the result to a file
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/zabbix/monitor.rb', line 83 def to_file key, value filename = 'tmp/zabbix-stats.yml' Dir.mkdir 'tmp' unless Dir.exists? 'tmp' if File.exists? filename yml = YAML::load_file(filename) else yml = {'statistics' => {'created_at' => Time.now.to_i}} end yml['statistics'][key] = value File.open(filename, 'w') { |file| file.write yml.to_yaml } end |
#to_stdout(key, value) ⇒ void (private)
This method returns an undefined value.
Prints the result of the monitored rule to stdout
102 103 104 |
# File 'lib/zabbix/monitor.rb', line 102 def to_stdout key, value $stdout.puts "#{key}: #{value}" end |
#to_zabbix(key, value) ⇒ void (private)
This method returns an undefined value.
Use zabbix_sender
command to send each result to the Zabbix server
67 68 69 70 71 72 73 74 75 |
# File 'lib/zabbix/monitor.rb', line 67 def to_zabbix key, value result = `zabbix_sender -c #{config.config_file_path} -k #{key} -o #{value}` case $?.to_i when 0 # SUCCESS Zabbix.logger.info "[Monitor] successfully sent rule: '#{key}' with value: '#{value}' to zabbix server" else Zabbix.logger.error "[Monitor] failed sending rule: '#{key}' with value: '#{value}' to zabbix server" end end |