Module: check_mk
Branch: master
Commit: 65e231766e19b036f7cef7bcd6c9e73d54e85539
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=65e231766e19b0…
Author: Moritz Kiemer <mo(a)mathias-kettner.de>
Date: Wed Sep 26 12:11:24 2018 +0200
esx_vsphere_vms: clean up snapshot check
Change-Id: I316669110c7ce1036ba5fdb70bd9a6507eb5c24e
---
checks/esx_vsphere_vm | 98 ++++++++++------------
.../datasets/esx_vsphere_vm_snapshots.py | 36 ++++++++
2 files changed, 81 insertions(+), 53 deletions(-)
diff --git a/checks/esx_vsphere_vm b/checks/esx_vsphere_vm
index 6d4a1ee..3b1547b 100644
--- a/checks/esx_vsphere_vm
+++ b/checks/esx_vsphere_vm
@@ -392,59 +392,51 @@ def check_esx_vsphere_vm_snapshots(_no_item, params, info):
if 'snapshot.rootSnapshotList' not in data:
yield 0, "No snapshots found"
- else:
- last_snapshot = None
- oldest_snapshot = None
- powered_on_snapshot = None
- snapshots = [x.split(" ", 3) for x in "
".join(data["snapshot.rootSnapshotList"]).split("|")]
- snapshots = [(int(x[0]), int(x[1]), x[2], x[3]) for x in snapshots]
- yield 0, "Number of Snapshots %d" % len(snapshots)
-
- if len(snapshots) > 0:
- perfdata = []
- last_time = max(zip(*snapshots)[1])
- oldest_time = min(zip(*snapshots)[1])
- last_snapshot = snapshots[0]
- oldest_snapshot = snapshots[0]
- for snapshot in snapshots:
- if last_time == snapshot[1]:
- last_snapshot = snapshot
- elif oldest_time == snapshot[1]:
- oldest_snapshot = snapshot
-
- if snapshot[2] == "poweredOn":
- powered_on_snapshot = snapshot
-
- yield 0, "Powered On: %s" % (powered_on_snapshot and
powered_on_snapshot[3] or "None")
-
- last_snapshot_age = time.time() - last_snapshot[1]
- if params.get("age"):
- warn, crit = params["age"]
- state, message = check_levels(last_snapshot_age, warn, crit)
- if state > 0:
- yield state, "Last %s" % message,
- perfdata = [ ("age", last_snapshot_age, warn, crit) ]
- else:
- perfdata = [ ("age", last_snapshot_age) ]
- yield 0, "Last Snapshot: %s %s" % (last_snapshot[3],
- time.strftime("%D
%H:%M",time.localtime(last_snapshot[1]))),\
- perfdata
-
- # Display oldest snapshot only, if it is not identical with the last
snapshot
- if not oldest_snapshot == last_snapshot:
- oldest_snapshot_age = time.time() - oldest_snapshot[1]
- if params.get("age_oldest"):
- warn, crit = params["age_oldest"]
- state, message = check_levels(oldest_snapshot_age, warn, crit)
- if state > 0:
- yield state, "Oldest %s" % message,
- perfdata = [ ("age_oldst", oldest_snapshot_age, warn,
crit) ]
- else:
- perfdata = [ ("age_oldest", oldest_snapshot_age) ]
-
- yield 0, "Oldest Snapshot: %s %s" % (oldest_snapshot[3],
- time.strftime("%D
%H:%M",time.localtime(oldest_snapshot[1]))),\
- perfdata
+ return
+
+ Snapshot = collections.namedtuple("Snapshot", ["time",
"state", "name"])
+
+ snapshots = (x.split(" ", 3) for x in "
".join(data["snapshot.rootSnapshotList"]).split("|"))
+ snapshots = [Snapshot(int(x[1]), x[2], x[3]) for x in snapshots]
+ yield 0, "Number of Snapshots %d" % len(snapshots)
+
+ if not snapshots:
+ return
+
+ powerd_on_list = [s for s in snapshots if s.state == "poweredOn"]
+ yield 0, "Powered On: %s" % powerd_on_list[0].name if powerd_on_list else
"None"
+
+ latest_snapshot = max(snapshots, key=lambda s: s.time)
+ latest_snapshot_age = time.time() - latest_snapshot.time
+ perfdata = []
+ if params.get("age"):
+ warn, crit = params["age"]
+ state, message = check_levels(latest_snapshot_age, warn, crit)
+ if state > 0:
+ yield state, "Latest %s" % message,
+ perfdata = [("age", latest_snapshot_age, warn, crit)]
+ else:
+ perfdata = [("age", latest_snapshot_age)]
+
+ timestamp = get_timestamp_human_readable(latest_snapshot.time)
+ yield 0, "Latest Snapshot: %s %s" % (latest_snapshot.name, timestamp),
perfdata
+
+ oldest_snapshot = min(snapshots, key=lambda s: s.time)
+ # Display oldest snapshot only, if it is not identical with the last snapshot
+ if oldest_snapshot != latest_snapshot:
+ perfdata = []
+ oldest_snapshot_age = time.time() - oldest_snapshot.time
+ if params.get("age_oldest"):
+ warn, crit = params["age_oldest"]
+ state, message = check_levels(oldest_snapshot_age, warn, crit)
+ if state > 0:
+ yield state, "Oldest %s" % message,
+ perfdata = [("age_oldest", oldest_snapshot_age, warn, crit)]
+ else:
+ perfdata = [("age_oldest", oldest_snapshot_age)]
+
+ timestamp = get_timestamp_human_readable(oldest_snapshot.time)
+ yield 0, "Oldest Snapshot: %s %s" % (oldest_snapshot.name, timestamp),
perfdata
check_info['esx_vsphere_vm.snapshots'] = {
diff --git a/tests/unit/checks/generictests/datasets/esx_vsphere_vm_snapshots.py
b/tests/unit/checks/generictests/datasets/esx_vsphere_vm_snapshots.py
new file mode 100644
index 0000000..ccd91d6
--- /dev/null
+++ b/tests/unit/checks/generictests/datasets/esx_vsphere_vm_snapshots.py
@@ -0,0 +1,36 @@
+
+
+checkname = 'esx_vsphere_vm'
+
+
+info = [['snapshot.rootSnapshotList',
+ '1',
+ '1363596734',
+ 'poweredOff',
+ '20130318_105600_snapshot_LinuxI|2',
+ '1413977827',
+ 'poweredOn',
+ 'LinuxI',
+ 'Testsnapshot']]
+
+
+discovery = {'cpu': [],
+ 'datastores': [],
+ 'guest_tools': [],
+ 'heartbeat': [],
+ 'mem_usage': [],
+ 'mounted_devices': [],
+ 'name': [],
+ 'running_on': [],
+ 'snapshots': [(None, {})]}
+
+
+checks = {
+ 'snapshots': [
+ (None, 'default', [(0, 'Number of Snapshots 2', []),
+ (0, 'Powered On: LinuxI Testsnapshot', []),
+ (0, 'Latest Snapshot: LinuxI Testsnapshot 2014-10-22
13:37:07', []),
+ (0, 'Oldest Snapshot: 20130318_105600_snapshot_LinuxI
2013-03-18 09:52:14', []),
+ ]),
+ ]
+}