|
#!/usr/bin/env ruby |
|
require 'net/http' |
|
require 'json' |
|
require 'time' |
|
|
|
FOREMAN_API_ENDPOINT = "https://url_to_foreman/api/" |
|
|
|
# Set user and password if you want to enable authentication. |
|
# Otherwise, leave them blank. |
|
FOREMAN_API_USER = '' |
|
FOREMAN_API_PASSWORD = '' |
|
|
|
queries = { |
|
'disabled' => 'status.enabled = false', |
|
'error' => 'last_report > "35 minutes ago" and (status.failed > 0 or status.failed_restarts > 0 or status.skipped > 0)', |
|
'out of sync' => 'last_report < "30 minutes ago" and status.enabled = true', |
|
} |
|
|
|
logger = Logger.new "#{Dir.pwd}/log/foreman.log" |
|
logger.formatter = Logger::Formatter.new |
|
logger.formatter.datetime_format = "%Y-%m-%d %H:%M:%S.%3N" |
|
logger.level = Logger::ERROR |
|
|
|
SCHEDULER.every '30s', :first_in => 0 do |job| |
|
|
|
data = [] |
|
|
|
queries.each { |name, query| |
|
auth = (FOREMAN_API_USER.empty? || FOREMAN_API_PASSWORD.empty?) ? false : true |
|
uri = URI.parse(URI.escape(FOREMAN_API_ENDPOINT+"hosts/?format=json&search=#{query}")) |
|
req = Net::HTTP::Get.new(uri) |
|
req.basic_auth FOREMAN_API_USER, FOREMAN_API_PASSWORD if auth |
|
logger.info "Started querying foreman" |
|
response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https', :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http| |
|
http.request(req) |
|
} |
|
logger.debug("Response: #{response.code} - #{response.message}") |
|
|
|
hosts = JSON.parse(response.body) |
|
logger.info("#{hosts['subtotal']} #{name} hosts found, out of a total of #{hosts['total']}") |
|
# hosts['results'].each do |host| |
|
# // do some more magic here |
|
# end |
|
data.push({ label: name, value: hosts['subtotal']}) |
|
} |
|
|
|
send_event('foreman', {items: data}) |
|
|
|
end |