mocdaniel / dashing-icinga2

Dashing dashboard for Icinga 2 using the REST API
MIT License
204 stars 47 forks source link

Guidance modifying dashlet #127

Open bensig opened 2 years ago

bensig commented 2 years ago

I could use a little guidance here. I want to create a public dashboard to show the status of various APIs that we have.

I am looking to modify the "Service Problems" to just show a list of hosts instead. I have a service group defined, but I cannot get the filter to work in the test/icinga.rb - and I'm not sure how I could adapt your "Service Problems" to be a list of "Main Services".

This works using the API to get the list that I want to use:

curl -k -s -S -i -u 'root:icinga' -H 'Accept: application/json' \
 -H 'X-HTTP-Method-Override: GET' -X POST \
 'https://localhost:5665/v1/objects/services' \
 -d '{ "filter": "group in service.groups", "filter_vars": { "group": "main" }, "pretty": true }'

For example, I tried to define:

serviceFilter = "groups.service == mainnet"

but it results in an error:

Traceback (most recent call last):
    11: from ./test/icinga2.rb:44:in `<main>'
    10: from /home/ubuntu/src/dashing-icinga2/lib/icinga2.rb:362:in `getServiceObjects'
     9: from /home/ubuntu/src/dashing-icinga2/lib/icinga2.rb:256:in `getApiData'
     8: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/resource.rb:69:in `post'
     7: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/request.rb:63:in `execute'
     6: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/request.rb:163:in `execute'
     5: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/request.rb:727:in `transmit'
     4: from /usr/lib/ruby/2.7.0/net/http.rb:933:in `start'
     3: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/request.rb:743:in `block in transmit'
     2: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/request.rb:836:in `process_result'
     1: from /var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/abstract_response.rb:129:in `return!'
/var/lib/gems/2.7.0/gems/rest-client-2.1.0/lib/restclient/abstract_response.rb:249:in `exception_with_response': 404 Not Found (RestClient::NotFound)
bensig commented 2 years ago

I have also tried to use a host group, but it returns no result even though it works on the curl

puts "Host listing: " + host_stats.to_s

hostFilter = "host.groups==\"API\""

bensig commented 2 years ago

I have also tried this

serviceFilter = [ "service.state==state && match(pattern,service.name)", "filter_vars": { "state": 2, "pattern": "*API*" } ]

mocdaniel commented 2 years ago

Hey Ben,

from what I gathered, you want a widget showing a list of all host problems, is that right? You could do something like this:

./dashboards/icinga2.erb

[...]
<li data-row="3" data-col="1" data-sizex="2" data-sizey="2">
      <div data-id="icinga-host-severity" data-view="List" data-unordered="true" data-title="Problems"></div>
    </li>

This goes within the huge SCHEDULER loop, basically wherever you want:

./jobs/icinga2.rb

[...]
# host problems
  host_severity_stats = []
  icinga.getHostObjects().each do |host|
    if host['attrs']['groups'].include? 'dummy-hosts'
      name = host['attrs']['__name']
      state = host['attrs']['state']
      host_severity_stats.push({
        "label" => name,
        "color" => icinga.stateToColor(state.to_int, false),
        "state" => state.to_int
      })
    end
  end

  order = [ 2,1,3 ]
  host_result = host_severity_stats.sort do |a, b|
    order.index(a['state'].to_int) <=> order.index(b['state'].to_int)
  end

  send_event('icinga-host-severity', {
    items: host_result,
    color: 'blue'
  })
[...]

This example will display all non-OK host states of hosts within the group dummy-hosts.

Does this help?

bensig commented 2 years ago

Thank you for the response. Very helpful to see how you did this... However, I have tried using this to substitute my own HostGroup instead of 'dummy-hosts' and it does not seem to limit to just that group.

It's still displaying service issues from all hosts. The actual hostgroup is defined in groups.conf and it loads correctly in Icinga2web with this filter:

/monitoring/list/hosts?hostgroup_name=mainnet-hosts&sort=host_severity&limit=50

mocdaniel commented 2 years ago

Sorry for the long delay, would you mind sharing your edited jobs/icinga2.rb and dashboards/icinga2.erb?

bensig commented 2 years ago

Not a problem, thanks for the reply. Here are the files: jobs/icinga2.rb dashboards/icinga2.erb

mocdaniel commented 2 years ago

Seems like you never actually use the data you prepare in your jobs/icinga2.rb - for this you'd need to rename the data-id property of one of the widgets of type List to point to icinga-host-severity (as you named the data being pushed by the backend job like this) in your dashboards/icinga2.erb.

You might have to rearrange/uncomment some of the widgets, too. Let me know if I can be of further help.