[icinga-checkins] icinga.org: dashing-icinga2/master: Implement new cool widgets and adopt Icinga style

git at icinga.org git at icinga.org
Sat Jun 25 18:57:27 CEST 2016


Module: dashing-icinga2
Branch: master
Commit: b0ea9caa1d014e13a5fc41e0c67fb36e5703e773
URL:    https://git.icinga.org/?p=dashing-icinga2.git;a=commit;h=b0ea9caa1d014e13a5fc41e0c67fb36e5703e773

Author: Michael Friedrich <michael.friedrich at netways.de>
Date:   Sat Jun 25 18:52:31 2016 +0200

Implement new cool widgets and adopt Icinga style

---

 dashboards/icinga2.erb |   26 +++++++++----
 jobs/icinga2.rb        |   99 ++++++++++++++++++++++++++++++++++++------------
 2 files changed, 94 insertions(+), 31 deletions(-)

diff --git a/dashboards/icinga2.erb b/dashboards/icinga2.erb
index 858904e..0cc1ba7 100644
--- a/dashboards/icinga2.erb
+++ b/dashboards/icinga2.erb
@@ -1,8 +1,17 @@
+<!--
+<script type='text/javascript'>
+$(function() {
+  // These settings override the defaults set in application.coffee. You can do this on a per dashboard basis.
+  Dashing.widget_base_dimensions ||= [300, 420]
+  Dashing.numColumns ||= 6
+});
+</script>
+-->
 <% content_for :title do %>Icinga 2 Dashboard<% end %>
 <div class="gridster">
   <ul>
     <!-- Stats -->
-    <li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
+    <li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
       <div data-id="icinga-uptime" data-view="Simplemon" data-title="Uptime" style="background-color: #0095bf;"></div>
     </li>
 
@@ -10,21 +19,24 @@
       <div data-id="icinga-version" data-view="Simplemon" data-title="Version" style="background-color: #0095bf;"></div>
     </li>
     <li data-row="1" data-col="3" data-sizex="1" data-sizey="1">
-      <div data-id="icinga-latency" data-view="Simplemon" data-title="Latency"></div>
+      <div data-id="icinga-host-meter" data-view="Meter" data-title="Host Problems" data-min="0" data-max="100" style="background-color: #0095bf;"></div>
+    </li>
+    <li data-row="1" data-col="4" data-sizex="1" data-sizey="1">
+      <div data-id="icinga-service-meter" data-view="Meter" data-title="Service Problems" data-min="0" data-max="100" style="background-color: #0095bf"></div>
     </li>
 
-    <!-- Down, Critical, Warning -->
+    <!-- Down, Critical, Warning, Unknown -->
     <li data-row="2" data-col="1" data-sizex="1" data-sizey="1">
-      <div data-id="icinga-overview" data-view="Simplemon" data-title="Overview"></div>
+      <div data-id="icinga-host-down" data-view="Simplemon" data-title="Hosts Down"></div>
     </li>
     <li data-row="2" data-col="2" data-sizex="1" data-sizey="1">
-      <div data-id="icinga-host-down" data-view="Simplemon" data-title="Hosts Down"></div>
+      <div data-id="icinga-service-critical" data-view="Simplemon" data-title="Services Critical"></div>
     </li>
     <li data-row="2" data-col="3" data-sizex="1" data-sizey="1">
-      <div data-id="icinga-service-critical" data-view="Simplemon" data-title="Services Critical"></div>
+      <div data-id="icinga-service-warning" data-view="Simplemon" data-title="Services Warning"></div>
     </li>
     <li data-row="2" data-col="4" data-sizex="1" data-sizey="1">
-      <div data-id="icinga-service-warning" data-view="Simplemon" data-title="Services Warning"></div>
+      <div data-id="icinga-service-unknown" data-view="Simplemon" data-title="Services Unknown"></div>
     </li>
 
     <!-- Acknowledged -->
diff --git a/jobs/icinga2.rb b/jobs/icinga2.rb
index afb2e60..b6c12a9 100644
--- a/jobs/icinga2.rb
+++ b/jobs/icinga2.rb
@@ -42,7 +42,7 @@ end
 def prepare_rest_client(api_url)
   # check whether pki files are there, otherwise use basic auth
   if File.file?("pki/" + $node_name + ".crt")
-    puts "PKI found, using client certificates for connection to Icinga 2 API"
+    #puts "PKI found, using client certificates for connection to Icinga 2 API"
     cert_file = File.read("pki/" + $node_name + ".crt")
     key_file = File.read("pki/" + $node_name + ".key")
     ca_file = File.read("pki/ca.crt")
@@ -52,7 +52,7 @@ def prepare_rest_client(api_url)
 
     options = {:ssl_client_cert => cert, :ssl_client_key => key, :ssl_ca_file => ca_file, :verify_ssl => OpenSSL::SSL::VERIFY_NONE}
   else
-    puts "PKI not found, using basic auth for connection to Icinga 2 API"
+    #puts "PKI not found, using basic auth for connection to Icinga 2 API"
 
     options = { :user => $api_username, :password => $api_password, :verify_ssl => OpenSSL::SSL::VERIFY_NONE }
   end
@@ -77,14 +77,41 @@ def get_app()
   return rest_client.get(headers)
 end
 
+#TODO: move to lib, add filter/join params
+def get_hosts()
+  api_url = $api_url_base + "/v1/objects/hosts"
+  rest_client = prepare_rest_client(api_url)
+  headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
 
-SCHEDULER.every '1s' do
+  return rest_client.get(headers)
+end
+
+def get_services()
+  api_url = $api_url_base + "/v1/objects/services"
+  rest_client = prepare_rest_client(api_url)
+  headers = {"Content-Type" => "application/json", "Accept" => "application/json"}
+
+  return rest_client.get(headers)
+end
 
-  total_critical = 0
-  total_warning = 0
-  total_ack = 0
-  total = 0
+def count_problems(object)
+  problems = 0
+
+  object.each do |item|
+    item.each do |key, dict|
+      #TODO 2.5: use ack and downtimes as handled states
+      if (key == "attrs" && dict["state"] != 0 && (dict["downtime_depth"] == 0 or dict["acknowledgement"] == 0))
+      #if (key == "attrs" && dict["state"] != 0)
+        problems = problems + 1
+        #puts "Key: " + key.to_s + " State: " + dict["state"].to_s
+      end
+    end
+  end
+
+  return problems
+end
 
+SCHEDULER.every '1s' do
   app = get_app()
   result = JSON.parse(app.body)
   icingaapplication = result["results"][0] # there's only one row
@@ -123,12 +150,25 @@ SCHEDULER.every '1s' do
   avg_latency = status["avg_latency"].round(2)
 
   ### Hosts/Services
+  host_objects_fetch = get_hosts()
+  result = JSON.parse(host_objects_fetch.body)
+
+  total_hosts = result["results"].size
+  all_hosts = result["results"]
+  total_problem_hosts = count_problems(all_hosts)
 
   hosts_up = status["num_hosts_up"].to_int
   hosts_down = status["num_hosts_down"].to_int
   hosts_ack = status["num_hosts_acknowledged"].to_int
   hosts_downtime = status["num_hosts_in_downtime"].to_int
 
+  service_objects_fetch = get_services()
+  result = JSON.parse(service_objects_fetch.body)
+
+  total_services = result["results"].size
+  all_services = result["results"]
+  total_problem_services = count_problems(all_services)
+
   services_ok = status["num_services_ok"].to_int
   services_warning = status["num_services_warning"].to_int
   services_critical = status["num_services_critical"].to_int
@@ -136,25 +176,20 @@ SCHEDULER.every '1s' do
   services_ack = status["num_services_acknowledged"].to_int
   services_downtime = status["num_services_in_downtime"].to_int
 
-  total_critical = services_critical + hosts_down
-  total_warning = services_warning
+  # meter widget
+  #host_meter = ((total_problem_hosts.to_f / total_hosts.to_f) * 100).round(2)
+  # we'll update the patched meter widget with absolute values (set max dynamically)
+  host_meter = total_problem_hosts.to_f
+  host_meter_max = total_hosts
 
-  if total_critical > 0 then
-    color = 'red'
-    value = total_critical.to_s
-  elsif total_warning > 0 then
-    color = 'yellow'
-    value = total_warning.to_s
-  else
-    color = 'green'
-    value = total.to_s
-  end
+  #service_meter = ((total_problem_services.to_f / total_services.to_f) * 100).round(2)
+  # we'll update the patched meter widget with absolute values (set max dynamically)
+  service_meter = total_problem_services.to_f
+  service_meter_max = total_services
 
-  ### Events
-  send_event('icinga-overview', {
-   value: value,
-   color: color })
+  puts "Meter widget: Hosts " + host_meter.to_s + "/" + host_meter_max.to_s + " Services " + service_meter.to_s + "/" + service_meter_max.to_s
 
+  ### Events
   send_event('icinga-version', {
    value: version_str,
    moreinfo: 'Revision: ' + version_revision
@@ -169,7 +204,19 @@ SCHEDULER.every '1s' do
    value: avg_latency.to_s + "s",
    color: 'blue' })
 
-  # down, critical, warning
+  send_event('icinga-host-meter', {
+   value: host_meter,
+   max:   host_meter_max,
+   moreinfo: "Total hosts: " + host_meter_max.to_s,
+   color: 'blue' })
+
+  send_event('icinga-service-meter', {
+   value: service_meter,
+   max:   service_meter_max,
+   moreinfo: "Total services: " + service_meter_max.to_s,
+   color: 'blue' })
+
+  # down, critical, warning, unknown
   send_event('icinga-host-down', {
    value: hosts_down.to_s,
    color: 'red' })
@@ -182,6 +229,10 @@ SCHEDULER.every '1s' do
    value: services_warning.to_s,
    color: 'yellow' })
 
+  send_event('icinga-service-unknown', {
+   value: services_unknown.to_s,
+   color: 'purple' })
+
   # ack, downtime
   send_event('icinga-service-ack', {
    value: services_ack.to_s,



More information about the icinga-checkins mailing list