Class which connects to a Icinga-Web instance to get information about service and hosts via a REST API
See also : IcingaWeb REST API
Constructor
Path to the IcingaWeb root URL
(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
Return the services (or hosts) number matching a given pattern
Query type. The available values are:
host
service
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
Queries Icinga about different services or hosts and return a Hash
Query type. The available values are:
host
service
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'
Array specifying the columns returned by the query
(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