class MonitoringReporter::MysqlLibreNMS

This is librenms class to request the various datas.

Public Instance Methods

array_to_hash(array) click to toggle source

Convert an array to hash.

# File lib/monitoringreporter/mysqllibrenms.rb, line 118
def array_to_hash(array)
  myhash = {}
  array.each do |pair|
    myhash[pair[0]] = [] if myhash[pair[0]].nil?
    myhash[pair[0]].push(pair[1])
  end
  myhash
end
average(since_period) click to toggle source

Get the average polling duration.

# File lib/monitoringreporter/mysqllibrenms.rb, line 79
def average(since_period)
  q = 'SELECT AVG(duration) FROM perf_times '\
    "WHERE start > '#{since_period}' AND perf_times.type = 'poll';"
  query(q)[0][0].to_i
end
down_host_count() click to toggle source

Get the number of hosts down.

# File lib/monitoringreporter/mysqllibrenms.rb, line 73
def down_host_count
  q = 'SELECT COUNT(*) FROM devices WHERE status = 0;'
  query(q)[0][0].to_i
end
format_data(results) click to toggle source

Format output to selected datatype.

# File lib/monitoringreporter/mysqllibrenms.rb, line 128
def format_data(results)
  case @opts['format'].downcase
  when 'text' then format_text(results)
  when 'json' then format_json(results)
  end
end
format_json(res) click to toggle source

Format to json output

# File lib/monitoringreporter/mysqllibrenms.rb, line 136
def format_json(res)
  {
    'hosts' => {
      'up' => res['up'], 'down' => res['down'], 'total' => res['total']
    },
    'polling' => {
      'average' => res['average'], 'longest' => Hash[res['longests']],
      'missing' => res['missings'].delete_if { |_, v| v.zero? }
                                  .sort_by { |_, v| v }.reverse
    }
  }.to_json
end
format_text(res) click to toggle source

Format to text output.

# File lib/monitoringreporter/mysqllibrenms.rb, line 150
def format_text(res)
  out = format_text_hosts(res['up'], res['down'], res['total'])
  out += format_text_average(res['average'])
  out += format_text_longest(res['longests'])
  out += format_text_missing(res['missings'], res['hosts'])
  out
end
format_text_average(value) click to toggle source

Format the text output for average polling.

# File lib/monitoringreporter/mysqllibrenms.rb, line 167
def format_text_average(value)
  "Average polling duration: #{value}\n"
end
format_text_hosts(up, down, total) click to toggle source

Format the text output for host status.

# File lib/monitoringreporter/mysqllibrenms.rb, line 159
def format_text_hosts(up, down, total)
  "Number of hosts UP: #{up}/#{total} " \
    "(#{(up.to_f / total * 100).round(2)}%)\n" \
    "Number of hosts DOWN: #{down}/#{total} " \
    "(#{(down.to_f / total * 100).round(2)}%)\n" \
end
format_text_longest(values) click to toggle source

Format the text output for longest polling list.

# File lib/monitoringreporter/mysqllibrenms.rb, line 172
def format_text_longest(values)
  out = "Longest pollers:\n"
  values.each { |v| out += "- #{v[0]} (#{v[1]} seconds)\n" }
  out
end
format_text_missing(values, hosts) click to toggle source

Format the text output for missing polling list.

# File lib/monitoringreporter/mysqllibrenms.rb, line 179
def format_text_missing(values, hosts)
  out = "Missing poll count per host (estimated):\n"
  base = "#{@opts['baseurl'].sub(%r{/^(.*)\/$/}, '\1')}/device/device=" \
    unless @opts['baseurl'].nil?
  Hash[values.sort_by { |_, v| v }.reverse].each_pair do |key, value|
    next if value.zero?
    out += "- #{key}: #{value}"
    out += " (#{base}#{hosts[key]})" unless base.nil?
    out += "\n"
  end
  out
end
id_to_host() click to toggle source

Get device_id and hostname matching

# File lib/monitoringreporter/mysqllibrenms.rb, line 55
def id_to_host
  q = 'SELECT hostname,device_id FROM devices;'
  Hash[query(q)]
end
longest(since_period) click to toggle source

Get the 5 longest poller.

# File lib/monitoringreporter/mysqllibrenms.rb, line 86
def longest(since_period)
  q = 'SELECT hostname,MAX(duration) AS drt FROM devices ' \
    'LEFT JOIN perf_times ON devices.device_id=perf_times.doing ' \
    "WHERE start > '#{since_period}' AND perf_times.type = 'poll' " \
    'GROUP BY doing ORDER BY drt DESC LIMIT 5;'
  query(q)
end
missing(since_period) click to toggle source

Get the missing polling count.

# File lib/monitoringreporter/mysqllibrenms.rb, line 95
def missing(since_period)
  q = 'SELECT hostname,start FROM devices LEFT JOIN perf_times ' \
    'ON devices.device_id=perf_times.doing ' \
    "WHERE start > '#{since_period}' AND perf_times.type = 'poll' " \
    'ORDER BY hostname DESC, start ASC;'
  search_for_missing(query(q))
end
print_to_output(data) click to toggle source

Print the result to the selected output

report() click to toggle source

Print the report.

# File lib/monitoringreporter/mysqllibrenms.rb, line 25
def report
  period = Time.now.to_i - @opts['periodicity']
  results = send_query(period)
  data = format_data(results)
  print_to_output(data)
end
search_for_missing(data_array) click to toggle source

Process the data to find the missing polling.

# File lib/monitoringreporter/mysqllibrenms.rb, line 104
def search_for_missing(data_array)
  miss_hash = {}
  array_to_hash(data_array).each_pair do |key, array|
    ref = array[0].to_i
    miss_hash[key] = 0
    array.each do |val|
      miss_hash[key] += (val.to_i - ref) / 420 unless val.to_i < ref + 420
      ref = val.to_i
    end
  end
  miss_hash
end
send_query(since_period) click to toggle source

Send queries to the database

# File lib/monitoringreporter/mysqllibrenms.rb, line 42
def send_query(since_period)
  results = {}
  results['hosts'] = id_to_host
  results['total'] = total_host_count
  results['up'] = up_host_count
  results['down'] = down_host_count
  results['average'] = average(since_period)
  results['longests'] = longest(since_period)
  results['missings'] = missing(since_period)
  results
end
total_host_count() click to toggle source

Get the total number of hosts.

# File lib/monitoringreporter/mysqllibrenms.rb, line 61
def total_host_count
  q = 'SELECT COUNT(*) FROM devices;'
  query(q)[0][0].to_i
end
up_host_count() click to toggle source

Get the number of hosts up.

# File lib/monitoringreporter/mysqllibrenms.rb, line 67
def up_host_count
  q = 'SELECT COUNT(*) FROM devices WHERE status = 1;'
  query(q)[0][0].to_i
end