[icinga-checkins] icinga.org: dashing-icinga2/master: Implement problem severity listing ( replaced uptime)

git at icinga.org git at icinga.org
Sun Nov 27 19:17:00 CET 2016


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

Author: Michael Friedrich <michael.friedrich at icinga.com>
Date:   Sun Nov 27 18:39:06 2016 +0100

Implement problem severity listing (replaced uptime)

---

 dashboards/icinga2.erb   |    3 +-
 jobs/icinga2.rb          |   12 ++++
 lib/icinga2.rb           |  147 +++++++++++++++++++++++++++++++++++++++++++++-
 widgets/list/list.scss   |    8 ++-
 widgets/table/table.html |    2 +-
 widgets/table/table.scss |    2 +-
 6 files changed, 165 insertions(+), 9 deletions(-)

diff --git a/dashboards/icinga2.erb b/dashboards/icinga2.erb
index fe83793..9d8ed67 100644
--- a/dashboards/icinga2.erb
+++ b/dashboards/icinga2.erb
@@ -12,9 +12,8 @@ $(function() {
   <ul>
     <!-- Stats -->
     <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>
+      <div data-id="icinga-severity" data-view="List" data-unordered="true" data-title="Problems" style="background-color: #0095bf;"></div>
     </li>
-
     <li data-row="1" data-col="2" data-sizex="1" data-sizey="1">
       <div data-id="icinga-checks" data-view="List" data-unordered="true" data-title="Checks (1 min)" style="background-color: #0095bf;"></div>
     </li>
diff --git a/jobs/icinga2.rb b/jobs/icinga2.rb
index efd66cc..af14bf6 100644
--- a/jobs/icinga2.rb
+++ b/jobs/icinga2.rb
@@ -51,6 +51,14 @@ SCHEDULER.every '5s', :first_in => 0 do |job|
   ]
   puts "Checks: " + check_stats.to_s
 
+  # severity list
+  severity_stats = []
+  icinga.service_problems_severity.each do |name, state|
+    #severity_stats.push({ "label" => icinga.formatService(name), "value" => state})
+    severity_stats.push({ "label" => icinga.formatService(name)})
+  end
+
+  puts "Severity: " + severity_stats.to_s
 
   ### Events
   send_event('icinga-version', {
@@ -80,6 +88,10 @@ SCHEDULER.every '5s', :first_in => 0 do |job|
    moreinfo: "Avg latency: " + icinga.avg_latency.to_s + "s",
    color: 'blue' })
 
+  send_event('icinga-severity', {
+   items: severity_stats,
+   color: 'blue' })
+
   # down, critical, warning, unknown
   send_event('icinga-host-down', {
    value: icinga.host_count_down.to_s,
diff --git a/lib/icinga2.rb b/lib/icinga2.rb
index 75be467..6a4f341 100644
--- a/lib/icinga2.rb
+++ b/lib/icinga2.rb
@@ -39,6 +39,8 @@ class Icinga2
   attr_reader :service_active_checks_1min
   attr_reader :service_passive_checks_1min
 
+  attr_reader :service_problems_severity
+
   # host stats
   attr_reader :host_count_all
   attr_reader :host_count_problems
@@ -78,7 +80,6 @@ class Icinga2
   @@headers
   @@options
 
-
   def initialize(configFile)
     # add logger
     file = File.open('/tmp/dashing-icinga2.log', File::WRONLY | File::APPEND | File::CREAT)
@@ -199,7 +200,7 @@ class Icinga2
   end
 
   def getServiceObjects(attrs = nil, filter = nil, joins = nil)
-    apiUrl = sprintf('%s/objects/services', @apiUrlBase)
+    apiUrl = sprintf('%s/objects/services?joins=host', @apiUrlBase)
     # TODO change to X-HTTP-Method-Override: GET in @headers, use POST and send filters, joins, attrs in request bondy
     restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
     data = JSON.parse(restClient.get(@headers).body)
@@ -208,6 +209,29 @@ class Icinga2
     return result
   end
 
+  def formatService(name)
+    service_map = name.split('!', 2)
+    return service_map[0].to_s + " - " + service_map[1].to_s
+  end
+
+  def stateToString(state, is_host = false)
+    if (is_host && state >= 1)
+      return "Down"
+    elsif (is_host && state == 0)
+      return "Up"
+    elsif (state == 0)
+      return "OK"
+    elsif (state == 1)
+      return "Warning"
+    elsif (state == 2)
+      return "Critical"
+    elsif (state == 3)
+      return "Unknown"
+    end
+
+    return "Undefined state. Programming error."
+  end
+
   def countProblems(objects)
     problems = 0
 
@@ -226,6 +250,120 @@ class Icinga2
     return problems
   end
 
+  def getObjectHasBeenChecked(object)
+    return object["attrs"]["last_check_result"] != nil
+  end
+
+  # stolen from Icinga Web 2, ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
+  def getHostSeverity(host)
+    attrs = host["attrs"]
+
+    severity = 0
+
+    if (attrs["state"] == 0)
+      if (getObjectHasBeenChecked(host))
+        severity += 16
+      end
+
+      if (attrs["acknowledgement"] != 0)
+        severity += 2
+      elsif (attrs["downtime_depth"] > 0)
+        severity += 1
+      else
+        severity += 4
+      end
+    else
+      if (getObjectHasBeenChecked(host))
+        severity += 16
+      elsif (attrs["state"] == 1)
+        severity += 32
+      elsif (attrs["state"] == 2)
+        severity += 64
+      else
+        severity += 256
+      end
+    end
+
+    return severity
+  end
+
+  # stolen from Icinga Web 2, ./modules/monitoring/library/Monitoring/Backend/Ido/Query/ServicestatusQuery.php
+  def getServiceSeverity(service)
+    attrs = service["attrs"]
+
+    severity = 0
+
+    if (attrs["state"] == 0)
+      if (getObjectHasBeenChecked(service))
+        severity += 16
+      end
+
+      if (attrs["acknowledgement"] != 0)
+        severity += 2
+      elsif (attrs["downtime_depth"] > 0)
+        severity += 1
+      else
+        severity += 4
+      end
+    else
+      if (getObjectHasBeenChecked(service))
+        severity += 16
+      elsif (attrs["state"] == 1)
+        severity += 32
+      elsif (attrs["state"] == 2)
+        severity += 128
+      elsif (attrs["state"] == 3)
+        severity += 64
+      else
+        severity += 256
+      end
+
+      # requires joins
+      host_attrs = service["joins"]["host"]
+
+      if (host_attrs["state"] > 0)
+        severity += 1024
+      elsif (attrs["acknowledgement"])
+        severity += 512
+      elsif (attrs["downtime_depth"] > 0)
+        severity += 256
+      else
+        severity += 2048
+      end
+    end
+
+    return severity
+  end
+
+  def getProblemServices(max_items = 5)
+    @service_problems = {}
+
+    @all_services_data.each do |service|
+      #puts "Severity for " + service["name"] + ": " + getServiceSeverity(service).to_s
+      @service_problems[service] = getServiceSeverity(service)
+    end
+
+    count = 0
+    @service_problems_severity = {}
+
+    # debug
+    #@service_problems.sort_by {|k, v| v}.reverse.each do |obj, severity|
+    #  puts obj["name"] + ": " + severity.to_s
+    #end
+
+    @service_problems.sort_by {|k, v| v}.reverse.each do |obj, severity|
+      if (count >= max_items)
+        break
+      end
+
+      name = obj["name"]
+      state = stateToString(obj["attrs"]["state"].to_int, false)
+      @service_problems_severity[name] = state
+
+      count += 1
+    end
+  end
+
   def fetchVersion(version)
     #version = "v2.4.10-504-gab4ba18"
     #version = "v2.4.10"
@@ -280,5 +418,10 @@ class Icinga2
     @host_passive_checks_1min = cib_data["passive_host_checks_1min"]
     @service_active_checks_1min = cib_data["active_service_checks_1min"]
     @service_passive_checks_1min = cib_data["passive_service_checks_1min"]
+
+
+    # severity
+    getProblemServices()
+
   end
 end
diff --git a/widgets/list/list.scss b/widgets/list/list.scss
index bce7010..483fd13 100644
--- a/widgets/list/list.scss
+++ b/widgets/list/list.scss
@@ -21,7 +21,7 @@ $moreinfo-color:    rgba(255, 255, 255, 0.7);
   }
 
   ol, ul {
-    margin: 0 15px;
+    margin: 0 5px;
     text-align: left;
     color: $label-color;
   }
@@ -40,12 +40,14 @@ $moreinfo-color:    rgba(255, 255, 255, 0.7);
 
   .label {
     color: $label-color;
+    font-size: 15px;
   }
 
   .value {
     float: right;
-    margin-left: 12px;
-    font-weight: 600;
+    margin-left: 10px;
+    font-weight: 500;
+    font-size: 15px;
     color: $value-color;
   }
 
diff --git a/widgets/table/table.html b/widgets/table/table.html
index 789f2e3..14f5668 100644
--- a/widgets/table/table.html
+++ b/widgets/table/table.html
@@ -1 +1 @@
-<div data-bind="value"></div>
\ No newline at end of file
+<div data-bind="value"></div>
diff --git a/widgets/table/table.scss b/widgets/table/table.scss
index 8ef9f84..0801b82 100644
--- a/widgets/table/table.scss
+++ b/widgets/table/table.scss
@@ -1,3 +1,3 @@
 .widget-table {
 
-}
\ No newline at end of file
+}



More information about the icinga-checkins mailing list