Class: Zabbix::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix/monitor.rb

Overview

Base class for processing and delivering the results to an endpoint

Instance Method Summary collapse

Instance Method Details

#collect_datavoid

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
  message = "#{current_zabbix_rule[:zabbix_key]} command: #{current_zabbix_rule[:command]} with message: #{e.message}"
  Zabbix.logger.error "[Monitor] failed collecting data for rule: #{message}"
end

#configZabbix::Config (private)

Returns the zabbix-monitor config.

Returns:



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

Parameters:

  • key (String)

    zabbix key

  • value (String)

    the result from the rule’s command



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

#schedulevoid

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_dataObject (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

Parameters:

  • key (String)

    zabbix key

  • value (String)

    the result from the rule’s command



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

Parameters:

  • key (String)

    zabbix key

  • value (String)

    the result from the rule’s command



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

Parameters:

  • key (String)

    zabbix key

  • value (String)

    the result from the rule’s command



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