class IcingaWebParser

Class which connects to a Icinga-Web instance to get information about service and hosts via a REST API

Public Class Methods

new(url, authkey = nil) click to toggle source

Constructor

url

Path to the IcingaWeb root URL

authkey

(Optional) Authentication key used to connect to the REST API

# File icingawebparser.rb, line 21
def initialize(url, authkey = nil)
    @url = url 
    @authkey = authkey
end

Public Instance Methods

count(type, filters) click to toggle source

Return the services (or hosts) number matching a given pattern

type

Query type. The available values are:

  • host

  • service

filters

String indicating the search scope. It corresponds to the filter field indicated in IcingaWeb REST API. For instance:

  • AND(SERVICE_CURRENT_STATE|=|2;SERVICE_NOTIFICATIONS_ENABLED|=|1) return all the CRITICAL services whose notifications are enabled

  • AND(HOST_CURRENT_STATE|=|0;HOST_NAME|like|*.company) return all the UP hosts whose name ends with '.company'


The filters field must contain at least one OR or AND group, so in case of only one condition is needed, the filters field must be like:

  • AND(condition1;0)

# File icingawebparser.rb, line 77
def count(type, filters)
    filters = CGI.escape "filter[#{filters}]"
    columns = CGI.escape "columns[SERVICE_NAME]"
    url = "#{@url}/web/api/#{type}/#{filters}/#{columns}/authkey=#{@authkey}/json"
    begin
        response = RestClient.get url, {:accept => :json}
    rescue
        raise Exception, "Cannot get: #{url}"
    end
    result = JSON.parse(response.body, :symbolize_names => true)
    if result[:success] == "true"
        return result[:result].count
    else
        return -1
    end
end
query(type, filters, columns, order = nil) click to toggle source

Queries Icinga about different services or hosts and return a Hash

type

Query type. The available values are:

  • host

  • service

filters

String indicating the search scope. It corresponds to the filter field indicated in IcingaWeb REST API. For instance:

  • AND(SERVICE_CURRENT_STATE|=|2;SERVICE_NOTIFICATIONS_ENABLED|=|1) return all the CRITICAL services whose notifications are enabled

  • AND(HOST_CURRENT_STATE|=|0;HOST_NAME|like|*.company) return all the UP hosts whose name ends with '.company'

columns

Array specifying the columns returned by the query

order

(Optional) String to order the result following a given pattern. The fiels used to order the returns must be one of the formerly specified columns. For instance:

  • HOST_NAME;DESC

  • SERVICE_DISPLAY_NAME;ASC


The filters field must contain at least one OR or AND group, so in case of only one condition is needed, the filters field must be like:

  • AND(condition1;0)

# File icingawebparser.rb, line 45
def query(type, filters, columns, order = nil)
    filters = CGI.escape "filter[#{filters}]"
    columns = CGI.escape "columns[#{columns*"|"}]"
    order = (CGI.escape "order[#{order}]") + "/" if order != nil
    url = "#{@url}/web/api/#{type}/#{filters}/#{columns}/#{order}authkey=#{@authkey}/json"
    begin
        response = RestClient.get url, {:accept => :json}
    rescue
        raise Exception, "Cannot get: #{url}"
    end
    response = RestClient.get url
    result = JSON.parse(response.body, :symbolize_names => true)
    if result[:success] == "true"
        return result[:result]
    else
        return nil
    end        
end