Module: check_mk
Branch: master
Commit: 2265e5f98a771f37b5b0078560f191fd58c15648
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=2265e5f98a771f…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Thu Sep 27 08:55:22 2018 +0200
6577 agent_vsphere: Add "System Time" service
And a minor clean up along the way.
Change-Id: I0d7111313bb435a11d040fb88134d3b4001fe585
---
.werks/6577 | 9 ++++++++
agents/special/agent_vsphere | 52 ++++++++++++++++++++++++++++++++------------
2 files changed, 47 insertions(+), 14 deletions(-)
diff --git a/.werks/6577 b/.werks/6577
new file mode 100644
index 0000000..1d5b3d7
--- /dev/null
+++ b/.werks/6577
@@ -0,0 +1,9 @@
+Title: agent_vsphere: Add "System Time" service
+Level: 1
+Component: checks
+Compatible: compat
+Edition: cre
+Version: 1.6.0i1
+Date: 1538031189
+Class: feature
+
diff --git a/agents/special/agent_vsphere b/agents/special/agent_vsphere
index e0ef0b7..5cec8ee 100755
--- a/agents/special/agent_vsphere
+++ b/agents/special/agent_vsphere
@@ -39,6 +39,7 @@ import pprint
import socket
import httplib
import datetime
+from dateutil import tz
from xml.dom import minidom
# Great article how to get additional information
@@ -53,6 +54,10 @@ telegram_list = {
'<ns1:Login xsi:type="ns1:LoginRequestType"><ns1:_this
type="SessionManager">%(sessionManager)s</ns1:_this>'\
'<ns1:userName>%(username)s</ns1:userName><ns1:password>%(password)s</ns1:password></ns1:Login>',
+ "systemtime":
+ '<ns1:CurrentTime
xsi:type="ns1:CurrentTimeRequestType">'\
+ '<ns1:_this
type="ServiceInstance">ServiceInstance</ns1:_this></ns1:CurrentTime>',
+
"hostsystems":
'<ns1:RetrievePropertiesEx
xsi:type="ns1:RetrievePropertiesExRequestType">'\
'<ns1:_this
type="PropertyCollector">%(propertyCollector)s</ns1:_this><ns1:specSet>'\
@@ -952,6 +957,16 @@ def output(line):
vsphere_output.append(line)
+def write_output(vsphere_output, opt_agent, host):
+ if opt_agent:
+ win_agent_output = get_agent_info_tcp(host)
+ sys.stdout.write(win_agent_output)
+
+ for line in vsphere_output:
+ sys.stdout.write((line.encode("utf-8") if type(line) == unicode else
line) + "\n")
+ sys.stdout.flush()
+
+
socket.setdefaulttimeout(opt_timeout)
def get_agent_info_tcp(hostname):
response = ""
@@ -1050,10 +1065,12 @@ if "OMD_SITE" in os.environ:
host_cookie_path = "%s/cookie.%s" % (cookie_path, host_address)
+
def delete_server_cookie():
if host_cookie_path and os.path.exists(host_cookie_path):
os.unlink(host_cookie_path)
+
def query_server(payload, payload_params=None):
if payload_params is None:
payload_params = {}
@@ -1114,6 +1131,7 @@ def query_server(payload, payload_params=None):
return response.status, response.reason, response.msg,
"".join(response_data)
+
def encode_url(text):
for char, replacement in [ ( "&", "&"),
( ">", ">" ),
@@ -1151,6 +1169,7 @@ def connect_to_server():
error = "Cannot connect to vSphere Server. Please check the IP and SSL
certificate (if applicable) "\
"and try again. This error is not related to the login credentials.
Error message: %r" % e
+
def retrieve_systeminfo():
# Retrieve basic data, which requires no login
payload = telegram_list["systeminfo"]
@@ -1160,6 +1179,7 @@ def retrieve_systeminfo():
if element:
systeminfo[entry] = function(element[0]) if function else element[0]
+
def login():
global server_cookie
@@ -1200,6 +1220,21 @@ def output_aggregated_snapshots(vms, hostsystems=None):
output('<<<<>>>>')
+def section_systemtime():
+ try:
+ __, __, __, response = query_server(telegram_list["systemtime"])
+ elements = get_pattern('<returnval>(.*)</returnval>',
response)
+ if elements:
+ naive = datetime.datetime.strptime(elements[0],
"%Y-%m-%dT%H:%M:%S.%fZ")
+ utc = naive.replace(tzinfo=tz.tzutc())
+ localtime = utc.astimezone(tz.tzlocal())
+ output("<<<systemtime>>>")
+ output(localtime.strftime("%s"))
+ except:
+ if opt_debug:
+ raise
+
+
# .--Main----------------------------------------------------------------.
# | __ __ _ |
# | | \/ | __ _(_)_ __ |
@@ -1237,7 +1272,6 @@ if not error:
for entry in systeminfo.items():
output("%s %s" % entry)
-
#############################
# Determine available host systems
#############################
@@ -1629,11 +1663,8 @@ if not error:
elif opt_snapshot_display == 'vCenter':
output_aggregated_snapshots(vms)
-
output("<<<<>>>>")
-
-
if not opt_direct:
reply_code, reply_msg, reply_headers, response =
query_server(telegram_list["datacenters"])
datacenters = get_pattern('<objects><obj
type="Datacenter">(.*?)</obj>', response)
@@ -1709,6 +1740,8 @@ if not error:
output("\n".join(values))
output("<<<<>>>>")
+ section_systemtime()
+
# We only logout in non OMD-setups
if not host_cookie_path:
query_server(telegram_list["logout"])
@@ -1728,20 +1761,11 @@ if not error:
error = "Error while processing received data"
-def make_utf8(line):
- if type(line) == unicode:
- return line.encode("utf-8")
- else:
- return line
+write_output(vsphere_output, opt_agent, host_address)
if opt_agent:
- win_agent_output = get_agent_info_tcp(host_address)
- sys.stdout.write(win_agent_output)
- sys.stdout.flush()
error_exit = 0
-print "\n".join(map(make_utf8, vsphere_output))
-
if tracefile:
tracefile.close()