[icinga-checkins] icinga.org: dashing-icinga2/master: Add attrs/filter/ joins to object get methods; add simple test runner; add development docs

git at icinga.org git at icinga.org
Fri Dec 2 16:28:45 CET 2016


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

Author: Michael Friedrich <michael.friedrich at icinga.com>
Date:   Fri Dec  2 15:55:53 2016 +0100

Add attrs/filter/joins to object get methods; add simple test runner; add development docs

---

 README.md       |   21 ++++++++++++++++++++
 lib/icinga2.rb  |   52 ++++++++++++++++++++++++++++++++++++++++++++------
 test/icinga2.rb |   57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 124 insertions(+), 6 deletions(-)

diff --git a/README.md b/README.md
index a9c893f..703ac64 100644
--- a/README.md
+++ b/README.md
@@ -144,6 +144,27 @@ The Icinga 2 dashboard mainly depends on the following files:
 Additional changes are inside the widgets. `simplemon` was added. `meter` was modified to update the
 maximum value at runtime. `list` was updated to highlight colors and change font sizes.
 
+## Icinga 2 Library
+
+`lib/icinga2.rb` provides a class `icinga` which is responsible
+for reading the configuration file, initializing the api connection
+and fetching data.
+
+Several public attributes are exposed by this class already. You'll
+need to instantiate a new object and then call the `run` method.
+
+Then you are able to access these attributes and public functions
+such as `getHostobjects` and `getServiceObjects`. These two functions
+can be called by passing
+
+* attrs as an array of attribute strings
+* filter as Icinga 2 API filter string
+* joins as an array of joined objects and/or attributes
+
+A simple test runner for testing own modifications has been added
+into `test/icinga2.rb`. You can find additional examples over there as
+well.
+
 ## TODO
 
 * Add ticket system demo (e.g. dev.icinga.org)
diff --git a/lib/icinga2.rb b/lib/icinga2.rb
index 53972d8..2997690 100644
--- a/lib/icinga2.rb
+++ b/lib/icinga2.rb
@@ -171,17 +171,57 @@ class Icinga2
 
   def getHostObjects(attrs = nil, filter = nil, joins = nil)
     apiUrl = sprintf('%s/objects/hosts', @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)
+
+    @headers["X-HTTP-Method-Override"] = "GET"
+    requestBody = {}
+
+    if (attrs)
+      requestBody["attrs"] = attrs
+    end
+
+    if (filter)
+      requestBody["filter"] = filter
+    end
+
+    if (joins)
+      requestBody["joins"] = joins
+    end
+
+    payload = JSON.generate(requestBody)
+    res = restClient.post(payload, @headers)
+    body = res.body
+    data = JSON.parse(body)
     return data['results']
   end
 
   def getServiceObjects(attrs = nil, filter = nil, joins = nil)
-    apiUrl = sprintf('%s/objects/services?joins=host', @apiUrlBase) # joins is required!
-    # TODO change to X-HTTP-Method-Override: GET in @headers, use POST and send filters, joins, attrs in request bondy
+    apiUrl = sprintf('%s/objects/services', @apiUrlBase)
     restClient = RestClient::Resource.new(URI.encode(apiUrl), @options)
-    data = JSON.parse(restClient.get(@headers).body)
+
+    @headers["X-HTTP-Method-Override"] = "GET"
+    requestBody = {}
+
+    if (attrs)
+      requestBody["attrs"] = attrs
+    end
+
+    if (filter)
+      requestBody["filter"] = filter
+    end
+
+    tmpJoin = [ "host" ]
+
+    if (joins)
+      requestBody["joins"] = joins
+    end
+
+    #puts "request body: " + requestBody.to_s
+
+    payload = JSON.generate(requestBody)
+    res = restClient.post(payload, @headers)
+    body = res.body
+    data = JSON.parse(body)
     return data['results']
   end
 
@@ -393,7 +433,7 @@ class Icinga2
     @app_starttime = Time.at(@app_data['icingaapplication']['app']['program_start'].to_f)
 
     @all_hosts_data = getHostObjects() #exported
-    @all_services_data = getServiceObjects() #exported
+    @all_services_data = getServiceObjects(nil, nil, [ "host" ]) #exported, requires "host" join
     @cib_data = getCIBData() #exported
 
     uptimeTmp = cib_data["uptime"].round(2)
diff --git a/test/icinga2.rb b/test/icinga2.rb
new file mode 100755
index 0000000..9909930
--- /dev/null
+++ b/test/icinga2.rb
@@ -0,0 +1,57 @@
+#/******************************************************************************
+# * Icinga 2 Dashing Library Test                                                       *
+# * Copyright (C) 2015-2016 Icinga Development Team (https://www.icinga.org)   *
+# *                                                                            *
+# * This program is free software; you can redistribute it and/or              *
+# * modify it under the terms of the GNU General Public License                *
+# * as published by the Free Software Foundation; either version 2             *
+# * of the License, or (at your option) any later version.                     *
+# *                                                                            *
+# * This program is distributed in the hope that it will be useful,            *
+# * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
+# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
+# * GNU General Public License for more details.                               *
+# *                                                                            *
+# * You should have received a copy of the GNU General Public License          *
+# * along with this program; if not, write to the Free Software Foundation     *
+# * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+# ******************************************************************************/
+
+require './lib/icinga2'
+
+# initialize data provider
+icinga = Icinga2.new('config/icinga2.json') # fixed path
+
+# run data provider
+icinga.run
+
+########## tests begin
+hostAttrs = [ "__name", "state" ]
+hostFilter = "host.name == NodeName"
+hostJoins = nil
+hostObjs = icinga.getHostObjects(hostAttrs, hostFilter, hostJoins)
+
+host_stats = []
+hostObjs.each do |host|
+  host_stats.push({ "label" => host["attrs"]["__name"], "value" => host["attrs"]["state"] })
+end
+
+puts "Host listing: " + host_stats.to_s
+
+serviceAttrs = [ "__name", "state" ]
+serviceFilter = "host.name == NodeName"
+serviceJoins = [ "host.name", "host.state" ]
+serviceObjs = icinga.getServiceObjects(serviceAttrs, serviceFilter, serviceJoins)
+
+#puts "Host Objects: " + hostObjs.to_s
+#puts "Service Object: " + serviceObjs.to_s
+
+service_stats = []
+serviceObjs.each do |service|
+  service_stats.push({ "label" => icinga.formatService(service["attrs"]["__name"]), "value" => service["attrs"]["state"] })
+end
+
+puts "Service listing: " + service_stats.to_s
+
+########## tests end
+



More information about the icinga-checkins mailing list