Module: check_mk
Branch: master
Commit: 142b2054eb5a43d7de296368f7271a189738e7bf
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=142b2054eb5a43…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Tue May 31 16:47:52 2016 +0200
3585 Implemented API for exporting the full host inventory
The HW/SW inventory data can now be exported using a webservice. This webservice
outputs the raw structured inventory data of a host.
The URL to this webservice is
<tt>http://[MONITORING-SERVER]/[SITE]/check_mk/host_inv_api.py?host=[HOST]&output_format=json</tt>.
You choose one of these output formats: <tt>json</tt>,
<tt>xml</tt>, <tt>python</tt>.
The data provided by this webservice looks as follows:
C+:
{
"result": {
"hardware": {
"memory": {
"total_ram_usable": 16697331712,
"total_swap": 17049841664,
"total_vmalloc": 35184372087808
}
},
"networking": {
"hostname": "Klappspaten"
}
},
"result_code": 0
}
C-:
The data below the key <tt>result</tt> is the HW/SW inventory data.
In case an error occurs during processing of the request, for example a host can not be
found,
the <tt>result_code</tt> is set to 1 and the result contains the error
message:
C+:
{"result": "Found no inventory data for this host.",
"result_code": 1}
C+:
---
.werks/3585 | 44 ++++++++++++++++++++++++++
ChangeLog | 1 +
web/htdocs/htmllib.py | 3 ++
web/htdocs/inventory.py | 70 +++++++++++++++++++++++++++++++++++++++++-
web/htdocs/webapi.py | 2 --
web/plugins/pages/shipped.py | 2 ++
6 files changed, 119 insertions(+), 3 deletions(-)
diff --git a/.werks/3585 b/.werks/3585
new file mode 100644
index 0000000..ccdacac
--- /dev/null
+++ b/.werks/3585
@@ -0,0 +1,44 @@
+Title: Implemented API for exporting the full host inventory
+Level: 2
+Component: inv
+Class: feature
+Compatible: compat
+State: unknown
+Version: 1.2.9i1
+Date: 1464704941
+
+The HW/SW inventory data can now be exported using a webservice. This webservice
+outputs the raw structured inventory data of a host.
+
+The URL to this webservice is
<tt>http://[MONITORING-SERVER]/[SITE]/check_mk/host_inv_api.py?host=[HOST]&output_format=json</tt>.
+
+You choose one of these output formats: <tt>json</tt>,
<tt>xml</tt>, <tt>python</tt>.
+
+The data provided by this webservice looks as follows:
+
+C+:
+{
+ "result": {
+ "hardware": {
+ "memory": {
+ "total_ram_usable": 16697331712,
+ "total_swap": 17049841664,
+ "total_vmalloc": 35184372087808
+ }
+ },
+ "networking": {
+ "hostname": "Klappspaten"
+ }
+ },
+ "result_code": 0
+}
+C-:
+
+The data below the key <tt>result</tt> is the HW/SW inventory data.
+
+In case an error occurs during processing of the request, for example a host can not be
found,
+the <tt>result_code</tt> is set to 1 and the result contains the error
message:
+
+C+:
+{"result": "Found no inventory data for this host.",
"result_code": 1}
+C+:
diff --git a/ChangeLog b/ChangeLog
index e13d1f1..6bf595a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -473,6 +473,7 @@
* 3028 statgrab_mem, solaris_mem: updated inventory plugins for solaris systems...
* 3447 mssql_versions: Added inventory plugin for MSSQL instances
* 3581 mssql_versions: Added product name to inventory information
+ * 3585 Implemented API for exporting the full host inventory...
* 3219 FIX: lnx_distro: Now detecting Oracle VM Server correctly
* 3229 FIX: lnx_distro: SLES based systems have now an OS name inventorized\
* 3265 FIX: mk_inventory.solaris: fix problem on Solaris 10, do prtdiag and prtpicl
only in global zone
diff --git a/web/htdocs/htmllib.py b/web/htdocs/htmllib.py
index a42ffbb..bea4704 100644
--- a/web/htdocs/htmllib.py
+++ b/web/htdocs/htmllib.py
@@ -192,6 +192,9 @@ class html(GUITester):
elif f == "html":
content_type = "text/html; charset=UTF-8"
+ elif f == "xml":
+ content_type = "text/xml; charset=UTF-8"
+
elif f == "pdf":
content_type = "application/pdf"
diff --git a/web/htdocs/inventory.py b/web/htdocs/inventory.py
index 595ff5a..3758dfc 100644
--- a/web/htdocs/inventory.py
+++ b/web/htdocs/inventory.py
@@ -25,7 +25,16 @@
# Boston, MA 02110-1301 USA.
import defaults, re, os
-from lib import MKGeneralException
+
+try:
+ import simplejson as json
+except ImportError:
+ import json
+
+
+import config
+import sites
+from lib import MKException, MKGeneralException, lqencode
# Load data of a host, cache it in the current HTTP request
def host(hostname):
@@ -286,3 +295,62 @@ def count_items(tree):
return sum(map(count_items, tree))
else:
return 1
+
+
+# The response is always a top level dict with two elements:
+# a) result_code - This is 0 for expected processing and 1 for an error
+# b) result - In case of an error this is the error message, a UTF-8 encoded
string.
+# In case of success this is a dictionary containing the host
inventory.
+def page_host_inv_api():
+ try:
+ host_name = html.var("host")
+ if not may_see(host_name):
+ raise MKAuthException(_("Sorry, you are not allowed to access this
host."))
+
+ host_inv = host(host_name)
+
+ if not host_inv and not has_inventory(host_name):
+ raise MKGeneralException(_("Found no inventory data for this
host."))
+
+ response = { "result_code": 0, "result": host_inv }
+
+ except MKException, e:
+ response = { "result_code": 1, "result": "%s" % e
}
+
+ except Exception, e:
+ if config.debug:
+ raise
+ response = { "result_code": 1, "result": "%s" % e
}
+
+ if html.output_format == "json":
+ write_json(response)
+ elif html.output_format == "xml":
+ write_xml(response)
+ else:
+ write_python(response)
+
+
+def may_see(host_name):
+ if config.may("general.see_all"):
+ return True
+
+ return sites.live().query_value("GET hosts\nStats: state >= 0\nFilter: name =
%s\n" % lqencode(host_name)) > 0
+
+
+def write_xml(response):
+ try:
+ import dicttoxml
+ except ImportError:
+ raise MKGeneralException(_("You need to have the \"dicttoxml\"
python module installed to "
+ "be able to use the XML format."))
+
+ html.write(dicttoxml.dicttoxml(response))
+
+
+def write_json(response):
+ html.write(json.dumps(response,
+ sort_keys=True, indent=4, separators=(',', ':
')))
+
+
+def write_python(response):
+ html.write(repr(response))
diff --git a/web/htdocs/webapi.py b/web/htdocs/webapi.py
index 5b09b20..200c110 100644
--- a/web/htdocs/webapi.py
+++ b/web/htdocs/webapi.py
@@ -101,5 +101,3 @@ def page_api():
else:
html.set_output_format("python")
html.write(repr(response))
-
-
diff --git a/web/plugins/pages/shipped.py b/web/plugins/pages/shipped.py
index 80f5fd5..1f26d67 100644
--- a/web/plugins/pages/shipped.py
+++ b/web/plugins/pages/shipped.py
@@ -44,6 +44,7 @@ import visuals
import crash_reporting
import metrics
import werks
+import inventory
# map URLs to page rendering functions
@@ -61,6 +62,7 @@ pagehandlers.update({
"ajax_set_viewoption" : views.ajax_set_viewoption,
"view" : views.page_view,
"ajax_inv_render_tree" : views.ajax_inv_render_tree,
+ "host_inv_api" : inventory.page_host_inv_api,
"host_service_graph_popup" : metrics.page_host_service_graph_popup,
"graph_dashlet" : metrics.page_graph_dashlet,
"ajax_set_rowselection" : weblib.ajax_set_rowselection,