-
Notifications
You must be signed in to change notification settings - Fork 36
/
check_pve.py
executable file
·1168 lines (983 loc) · 42.5 KB
/
check_pve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# check_pve.py - A check plugin for Proxmox Virtual Environment (PVE).
# Copyright (C) 2018-2024 Nicolai Buchwitz <[email protected]>
#
# Version: 1.4.0
#
# ------------------------------------------------------------------------------
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# ------------------------------------------------------------------------------
"""Proxmox VE monitoring check command for various monitoring systems like Icinga and others."""
import re
import sys
from typing import Callable, Dict, Optional, Union, List
try:
import argparse
from datetime import datetime, timezone
from enum import Enum
import requests
from packaging import version
from requests.packages.urllib3.exceptions import InsecureRequestWarning
except ImportError as e:
print(f"Missing python module: {str(e)}")
sys.exit(255)
# Timeout for API requests in seconds
CHECK_API_TIMEOUT = 30
def compare_thresholds(
threshold_warning: Dict, threshold_critical: Dict, comparator: Callable
) -> bool:
"""Perform sanity checks on thresholds parameters (used for argparse validation)."""
ok = True
keys = set(list(threshold_warning.keys()) + list(threshold_critical.keys()))
for key in keys:
if (key in threshold_warning and key in threshold_critical) or (
None in threshold_warning and None in threshold_critical
):
ok = ok and comparator(threshold_warning[key], threshold_critical[key])
elif key in threshold_warning and None in threshold_critical:
ok = ok and comparator(threshold_warning[key], threshold_critical[None])
elif key in threshold_critical and None in threshold_warning:
ok = ok and comparator(threshold_warning[None], threshold_critical[key])
return ok
class CheckState(Enum):
"""Check return values."""
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
class CheckThreshold:
"""Threshold representation used by the check command."""
def __init__(self, value: float) -> None:
self.value = value
def __eq__(self, other: "CheckThreshold") -> bool:
"""Threshold is equal to given one."""
return self.value == other.value
def __lt__(self, other: "CheckThreshold") -> bool:
"""Threshold is lower to given one."""
return self.value < other.value
def __le__(self, other: "CheckThreshold") -> bool:
"""Threshold is lower or equal to given one."""
return self.value <= other.value
def __gt__(self, other: "CheckThreshold") -> bool:
"""Threshold is greater than given one."""
return self.value > other.value
def __ge__(self, other: "CheckThreshold") -> bool:
"""Threshold is greater or equal than given one."""
return self.value >= other.value
def check(self, value: float, lower: bool = False) -> bool:
"""Check threshold value as upper or lower boundary for given value."""
if lower:
return value < self.value
return value > self.value
@staticmethod
def threshold_type(arg: str) -> Dict[str, "CheckThreshold"]:
"""Convert string argument(s) to threshold dict."""
thresholds = {}
try:
thresholds[None] = CheckThreshold(float(arg))
except ValueError:
for t in arg.split(","):
m = re.match("([a-z_0-9]+):([0-9.]+)", t)
if m:
thresholds[m.group(1)] = CheckThreshold(float(m.group(2)))
else:
raise argparse.ArgumentTypeError(f"Invalid threshold format: {t}") # noqa: B904
return thresholds
class RequestError(Exception):
"""Exception for request related errors."""
def __init__(self, message: str, rc: int) -> None:
self.message = message
self.rc = rc
super().__init__(self.message)
class CheckPVE:
"""Check command for Proxmox VE."""
VERSION = "1.3.0"
API_URL = "https://{hostname}:{port}/api2/json/{command}"
UNIT_SCALE = {
"GB": 10**9,
"MB": 10**6,
"KB": 10**3,
"GiB": 2**30,
"MiB": 2**20,
"KiB": 2**10,
"B": 1,
}
def check_output(self) -> None:
"""Print check command output with perfdata and return code."""
message = self.check_message
if self.perfdata:
message += self.get_perfdata()
self.output(self.check_result, message)
@staticmethod
def output(rc: CheckState, message: str) -> None:
"""Print message to stdout and exit with given return code."""
prefix = rc.name
print(f"{prefix} - {message}")
sys.exit(rc.value)
def get_url(self, command: str) -> str:
"""Get API url for specific command."""
return self.API_URL.format(
hostname=self.options.api_endpoint, command=command, port=self.options.api_port
)
def request(self, url: str, method: str = "get", **kwargs: Dict) -> Union[Dict, None]:
"""Execute request against Proxmox VE API and return json data."""
response = None
try:
if method == "post":
response = requests.post(
url,
verify=not self.options.api_insecure,
data=kwargs.get("data", None),
timeout=5,
)
elif method == "get":
response = requests.get(
url,
verify=not self.options.api_insecure,
cookies=self.__cookies,
headers=self.__headers,
params=kwargs.get("params", None),
timeout=CHECK_API_TIMEOUT,
)
else:
self.output(CheckState.CRITICAL, f"Unsupport request method: {method}")
except requests.exceptions.ConnectTimeout:
self.output(CheckState.UNKNOWN, "Could not connect to PVE API: Connection timeout")
except requests.exceptions.SSLError:
self.output(
CheckState.UNKNOWN, "Could not connect to PVE API: Certificate validation failed"
)
except requests.exceptions.ConnectionError:
self.output(
CheckState.UNKNOWN, "Could not connect to PVE API: Failed to resolve hostname"
)
if response.ok:
return response.json()["data"]
message = "Could not fetch data from API: "
if response.status_code == 401:
message += "Could not connection to PVE API: invalid username or password"
elif response.status_code == 403:
message += (
"Access denied. Please check if API user has sufficient permissions / "
"the correct role has been assigned."
)
else:
message += f"HTTP error code was {response.status_code}"
if kwargs.get("raise_error", False):
raise RequestError(message, response.status_code)
self.output(CheckState.UNKNOWN, message)
def get_ticket(self) -> str:
"""Perform login and fetch ticket for further API calls."""
url = self.get_url("access/ticket")
data = {"username": self.options.api_user, "password": self.options.api_password}
result = self.request(url, "post", data=data)
return result["ticket"]
def check_api_value(self, url: StopIteration, message: str, **kwargs: Dict) -> None:
"""Perform simple threshold based check command."""
result = self.request(url)
used = None
if "key" in kwargs:
result = result[kwargs.get("key")]
if isinstance(result, (dict,)):
used_percent = self.get_value(result["used"], result["total"])
used = self.get_value(result["used"])
total = self.get_value(result["total"])
self.add_perfdata(kwargs.get("perfkey", "usage"), used_percent)
self.add_perfdata(
kwargs.get("perfkey", "used"), used, max=total, unit=self.options.unit
)
else:
used_percent = round(float(result) * 100, 2)
self.add_perfdata(kwargs.get("perfkey", "usage"), used_percent)
if self.options.values_mb:
message += f" {used} {self.options.unit}"
value = used
else:
message += f" {used_percent} %"
value = used_percent
self.check_thresholds(value, message)
def check_vm_status(self, idx: Union[str, int], **kwargs: str) -> None:
"""Check status of virtual machine by vmid or name."""
url = self.get_url(
"cluster/resources",
)
data = self.request(url, params={"type": "vm"})
expected_state = kwargs.get("expected_state", "running")
only_status = kwargs.get("only_status", False)
found = False
for vm in data:
if idx in (vm.get("name", None), vm.get("vmid", None)):
# Check if VM (default) or LXC
vm_type = "VM"
if vm["type"] == "lxc":
vm_type = "LXC"
if vm["status"] != expected_state:
self.check_message = (
f"{vm_type} '{vm['name']}' is {vm['status']} (expected: {expected_state})"
)
if not self.options.ignore_vm_status:
self.check_result = CheckState.CRITICAL
else:
if self.options.node and self.options.node != vm["node"]:
self.check_message = (
f"{vm_type} '{vm['name']}' is {expected_state}, "
f"but located on node '{vm['node']}' instead of '{self.options.node}'"
)
self.check_result = CheckState.WARNING
else:
self.check_message = (
f"{vm_type} '{vm['name']}' is {expected_state} on node '{vm['node']}'"
)
if vm["status"] == "running" and not only_status:
cpu = round(vm["cpu"] * 100, 2)
self.add_perfdata("cpu", cpu)
if self.options.values_mb:
memory = self.scale_value(vm["mem"])
self.add_perfdata(
"memory",
memory,
unit=self.options.unit,
max=self.scale_value(vm["maxmem"]),
)
disk = self.scale_value(vm["disk"])
self.add_perfdata(
"disk",
disk,
unit=self.options.unit,
max=self.scale_value(vm["maxdisk"]),
)
else:
memory = self.get_value(vm["mem"], vm["maxmem"])
self.add_perfdata("memory", memory)
disk = self.get_value(vm["disk"], vm["maxdisk"])
self.add_perfdata("disk", disk)
self.check_thresholds(
{"cpu": cpu, "memory": memory, "disk": disk}, message=self.check_message
)
found = True
break
if not found:
self.check_message = f"VM or LXC '{idx}' not found"
self.check_result = CheckState.WARNING
def check_disks(self) -> None:
"""Check disk health on specific Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/disks")
failed = []
unknown = []
disks = self.request(url + "/list")
for disk in disks:
name = disk["devpath"].replace("/dev/", "")
if name in self.options.ignore_disks:
continue
if disk["health"] == "UNKNOWN":
self.check_result = CheckState.WARNING
unknown.append({"serial": disk["serial"], "device": disk["devpath"]})
elif disk["health"] not in ("PASSED", "OK"):
self.check_result = CheckState.WARNING
failed.append({"serial": disk["serial"], "device": disk["devpath"]})
if disk["wearout"] != "N/A":
self.add_perfdata(f"wearout_{name}", disk["wearout"])
if failed:
self.check_message = f"{len(failed)} of {len(disks)} disks failed the health test:\n"
for disk in failed:
self.check_message += f"- {disk['device']} with serial '{disk['serial']}'\n"
if unknown:
self.check_message += (
f"{len(unknown)} of {len(disks)} disks have unknown health status:\n"
)
for disk in unknown:
self.check_message += f"- {disk['device']} with serial '{disk['serial']}'\n"
if not failed and not unknown:
self.check_message = "All disks are healthy"
def check_replication(self) -> None:
"""Check replication status for either all or one specific vm / container."""
url = self.get_url(f"nodes/{self.options.node}/replication")
if self.options.vmid:
data = self.request(url, params={"guest": self.options.vmid})
else:
data = self.request(url)
failed_jobs = [] # format: [{guest: str, fail_count: int, error: str}]
performance_data = []
for job in data:
if job["fail_count"] > 0:
failed_jobs.append(
{"guest": job["guest"], "fail_count": job["fail_count"], "error": job["error"]}
)
else:
performance_data.append({"id": job["id"], "duration": job["duration"]})
if len(failed_jobs) > 0:
message = f"Failed replication jobs on {self.options.node}: "
for job in failed_jobs:
message = (
message
+ "GUEST: {j[guest]}, FAIL_COUNT: {j[fail_count]}, ERROR: {j[error]} ; ".format(
j=job
)
)
self.check_message = message
self.check_result = CheckState.WARNING
else:
self.check_message = f"No failed replication jobs on {self.options.node}"
self.check_result = CheckState.OK
if len(performance_data) > 0:
for metric in performance_data:
self.add_perfdata("duration_" + metric["id"], metric["duration"], unit="s")
def check_services(self) -> None:
"""Check state of core services on Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/services")
data = self.request(url)
failed = {}
for service in data:
if (
service["state"] != "running"
and service.get("active-state", "active") == "active"
and service["name"] not in self.options.ignore_services
):
failed[service["name"]] = service["desc"]
if failed:
self.check_result = CheckState.CRITICAL
message = f"{len(failed)} services are not running:\n\n"
for name, description in failed.items():
message += f"- {description} ({name}) is not running\n"
self.check_message = message
else:
self.check_message = "All services are running"
def check_subscription(self) -> None:
"""Check subscription status on Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/subscription")
data = self.request(url)
if data["status"].lower() == "notfound":
self.check_result = CheckState.WARNING
self.check_message = "No valid subscription found"
if data["status"].lower() == "inactive":
self.check_result = CheckState.CRITICAL
self.check_message = "Subscription expired"
elif data["status"].lower() == "active":
subscription_due_date = data["nextduedate"]
subscription_product_name = data["productname"]
date_expire = datetime.strptime(subscription_due_date, "%Y-%m-%d")
date_today = datetime.today()
delta = (date_expire - date_today).days
message = f"{subscription_product_name} is valid until {subscription_due_date}"
message_warning_critical = (
"{subscription_product_name} will expire in {delta} days ({subscription_due_date})"
)
self.check_thresholds(
delta,
message,
messageWarning=message_warning_critical,
messageCritical=message_warning_critical,
lowerValue=True,
)
def check_updates(self) -> None:
"""Check for package updates on Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/apt/update")
count = len(self.request(url))
if count:
self.check_result = CheckState.WARNING
msg = "{} pending update"
if count > 1:
msg += "s"
self.check_message = msg.format(count)
else:
self.check_message = "System up to date"
def check_cluster_status(self) -> None:
"""Check if cluster is operational."""
url = self.get_url("cluster/status")
data = self.request(url)
nodes = {}
quorate = None
cluster = ""
for elem in data:
if elem["type"] == "cluster":
quorate = elem["quorate"]
cluster = elem["name"]
elif elem["type"] == "node":
nodes[elem["name"]] = elem["online"]
if quorate is None:
self.check_message = "No cluster configuration found"
elif quorate:
node_count = len(nodes)
nodes_online_count = len({k: v for k, v in nodes.items() if v})
if node_count > nodes_online_count:
diff = node_count - nodes_online_count
self.check_result = CheckState.WARNING
self.check_message = f"Cluster '{cluster}' is healthy, but {diff} node(s) offline'"
else:
self.check_message = f"Cluster '{cluster}' is healthy'"
self.add_perfdata("nodes_total", node_count, unit="")
self.add_perfdata("nodes_online", nodes_online_count, unit="")
else:
self.check_result = CheckState.CRITICAL
self.check_message = "Cluster is unhealthy - no quorum"
def check_zfs_fragmentation(self, name: Optional[str] = None) -> None:
"""Check all or one specific ZFS pool for fragmentation."""
url = self.get_url(f"nodes/{self.options.node}/disks/zfs")
data = self.request(url)
warnings = []
critical = []
found = name is None
for pool in data:
found = found or name == pool["name"]
if (name is not None and name == pool["name"]) or name is None:
key = "fragmentation"
if name is None:
key += f"_{pool['name']}"
self.add_perfdata(key, pool["frag"])
threshold_name = f"fragmentation_{name}"
threshold_warning = self.threshold_warning(threshold_name)
threshold_critical = self.threshold_critical(threshold_name)
if threshold_critical is not None and pool["frag"] > float(
threshold_critical.value
):
critical.append(pool)
elif threshold_warning is not None and pool["frag"] > float(
threshold_warning.value
):
warnings.append(pool)
if not found:
self.check_result = CheckState.UNKNOWN
self.check_message = f"Could not fetch fragmentation of ZFS pool '{name}'"
else:
if warnings or critical:
value = None
if critical:
self.check_result = CheckState.CRITICAL
if name is not None:
value = critical[0]["frag"]
else:
self.check_result = CheckState.WARNING
if name is not None:
value = warnings[0]["frag"]
if name is not None:
self.check_message = (
f"Fragmentation of ZFS pool '{name}' is above thresholds: {value} %"
)
else:
pool_above = len(warnings) + len(critical)
message = (
f"{pool_above} of {len(data)} ZFS pools are above fragmentation "
"thresholds:\n\n"
)
message += "\n".join(
[f"- {pool['name']} ({pool['frag']} %) is CRITICAL\n" for pool in critical]
)
message += "\n".join(
[f"- {pool['name']} ({pool['frag']} %) is WARNING\n" for pool in warnings]
)
self.check_message = message
else:
self.check_result = CheckState.OK
if name is not None:
self.check_message = f"Fragmentation of ZFS pool '{name}' is OK"
else:
self.check_message = "Fragmentation of all ZFS pools is OK"
def check_zfs_health(self, name: Optional[str] = None) -> None:
"""Check all or one specific ZFS pool for health."""
url = self.get_url(f"nodes/{self.options.node}/disks/zfs")
data = self.request(url)
unhealthy = []
found = name is None
healthy_conditions = ["online"]
for pool in data:
found = found or name == pool["name"]
if (name is not None and name == pool["name"]) or name is None:
if pool["health"].lower() not in healthy_conditions:
unhealthy.append(pool)
if not found:
self.check_result = CheckState.UNKNOWN
self.check_message = f"Could not fetch health of ZFS pool '{name}'"
else:
if unhealthy:
self.check_result = CheckState.CRITICAL
message = f"{len(unhealthy)} ZFS pools are not healthy:\n\n"
message += "\n".join(
[f"- {pool['name']} ({pool['health']}) is not healthy" for pool in unhealthy]
)
self.check_message = message
else:
self.check_result = CheckState.OK
if name is not None:
self.check_message = f"ZFS pool '{name}' is healthy"
else:
self.check_message = "All ZFS pools are healthy"
def check_ceph_health(self) -> None:
"""Check health of CEPH cluster."""
url = self.get_url("cluster/ceph/status")
data = self.request(url)
ceph_health = data.get("health", {})
if "status" not in ceph_health:
self.check_result = CheckState.UNKNOWN
self.check_message = (
"Could not fetch Ceph status from API. "
"Check the output of 'pvesh get cluster/ceph' on your node"
)
return
if ceph_health["status"] == "HEALTH_OK":
self.check_result = CheckState.OK
self.check_message = "Ceph Cluster is healthy"
elif ceph_health["status"] == "HEALTH_WARN":
self.check_result = CheckState.WARNING
self.check_message = "Ceph Cluster is in warning state"
elif ceph_health["status"] == "HEALTH_CRIT":
self.check_result = CheckState.CRITICAL
self.check_message = "Ceph Cluster is in critical state"
else:
self.check_result = CheckState.UNKNOWN
self.check_message = "Ceph Cluster is in unknown state"
def check_storage(self, name: str) -> None:
"""Check if storage exists and return usage."""
url = self.get_url(f"nodes/{self.options.node}/storage")
data = self.request(url)
if not any(s["storage"] == name for s in data):
self.check_result = CheckState.CRITICAL
self.check_message = f"Storage '{name}' doesn't exist on node '{self.options.node}'"
return
url = self.get_url(f"nodes/{self.options.node}/storage/{name}/status")
self.check_api_value(url, f"Usage of storage '{name}' is")
def check_version(self) -> None:
"""Check PVE version."""
url = self.get_url("version")
data = self.request(url)
if not data["version"]:
self.check_result = CheckState.UNKNOWN
self.check_message = "Unable to determine pve version"
elif self.options.min_version and version.parse(self.options.min_version) > version.parse(
data["version"]
):
self.check_result = CheckState.CRITICAL
self.check_message = (
f"Current PVE version '{data['version']}' "
f"({data['repoid']}) is lower than the min. "
f"required version '{self.options.min_version}'"
)
else:
self.check_message = (
f"Your PVE instance version '{data['version']}' ({data['repoid']}) is up to date"
)
def _get_pool_members(self, pool: str) -> List[int]:
"""Get a list of vmids, which are members of a given resource pool.
NOTE: The request needs the Pool.Audit permission!
"""
members = []
try:
url = self.get_url(f"pools/{pool}")
pools = self.request(url, raise_error=True)
for pool in pools.get("members", []):
members.append(pool["vmid"])
except RequestError:
print(
f"Unable to fetch members of pool '{pool}'. "
"Check if the name is correct and the role has the 'Pool.Audit' permission"
)
return members
def check_vzdump_backup(self, name: Optional[str] = None) -> None:
"""Check for failed vzdump backup jobs."""
tasks_url = self.get_url("cluster/tasks")
tasks = self.request(tasks_url)
tasks = [t for t in tasks if t["type"] == "vzdump"]
# Filter by node id, if one is provided
if self.options.node is not None:
tasks = [t for t in tasks if t["node"] == self.options.node]
# Filter by timestamp, if provided
delta = self.threshold_critical("delta")
if delta is not None:
now = datetime.now(timezone.utc).timestamp()
tasks = [t for t in tasks if not delta.check(now - t["starttime"])]
# absent status = job still running
tasks = [t for t in tasks if "status" in t]
failed = len([t for t in tasks if t["status"] != "OK"])
success = len(tasks) - failed
self.check_message = f"{success} backup tasks successful, {failed} backup tasks failed"
if failed > 0:
self.check_result = CheckState.CRITICAL
else:
self.check_result = CheckState.OK
if delta is not None:
self.check_message += f" within the last {delta.value}s"
nbu_url = self.get_url("cluster/backup-info/not-backed-up")
not_backed_up = self.request(nbu_url)
if len(not_backed_up) > 0:
guest_ids = []
for guest in not_backed_up:
guest_ids.append(str(guest["vmid"]))
ignored_vmids = []
for pool in self.options.ignore_pools:
ignored_vmids += map(str, self._get_pool_members(pool))
remaining_not_backed_up = sorted(list(set(guest_ids) - set(ignored_vmids)))
if len(remaining_not_backed_up) > 0:
if self.check_result not in [CheckState.CRITICAL, CheckState.UNKNOWN]:
self.check_result = CheckState.WARNING
self.check_message += (
"\nThere are unignored guests not covered by any backup schedule: "
+ ", ".join(remaining_not_backed_up)
)
def check_memory(self) -> None:
"""Check memory usage of Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/status")
self.check_api_value(url, "Memory usage is", key="memory")
def check_swap(self) -> None:
"""Check swap usage of Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/status")
self.check_api_value(url, "Swap usage is", key="swap")
def check_cpu(self) -> None:
"""Check cpu usage of Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/status")
self.check_api_value(url, "CPU usage is", key="cpu")
def check_io_wait(self) -> None:
"""Check io wait of Proxmox VE node."""
url = self.get_url(f"nodes/{self.options.node}/status")
self.check_api_value(url, "IO wait is", key="wait", perfkey="wait")
def check_thresholds(
self,
values: Union[Dict[str, Union[int, float]], Union[int, float]],
message: str,
**kwargs: Dict,
) -> None:
"""Check numeric value against threshold for given metric name."""
is_warning = False
is_critical = False
if not isinstance(values, dict):
values = {None: values}
for metric, value in values.items():
value_warning = self.threshold_warning(metric)
if value_warning is not None:
is_warning = is_warning or value_warning.check(
value, kwargs.get("lowerValue", False)
)
value_critical = self.threshold_critical(metric)
if value_critical is not None:
is_critical = is_critical or value_critical.check(
value, kwargs.get("lowerValue", False)
)
if is_critical:
self.check_result = CheckState.CRITICAL
self.check_message = kwargs.get("messageCritical", message)
elif is_warning:
self.check_result = CheckState.WARNING
self.check_message = kwargs.get("messageWarning", message)
else:
self.check_message = message
def scale_value(self, value: Union[int, float]) -> float:
"""Scale value according to unit."""
if self.options.unit in self.UNIT_SCALE:
return value / self.UNIT_SCALE[self.options.unit]
raise ValueError("wrong unit")
def threshold_warning(self, name: str) -> CheckThreshold:
"""Get warning threshold for metric name (empty if none)."""
return self.options.threshold_warning.get(
name, self.options.threshold_warning.get(None, None)
)
def threshold_critical(self, name: str) -> CheckThreshold:
"""Get critical threshold for metric name (empty if none)."""
return self.options.threshold_critical.get(
name, self.options.threshold_critical.get(None, None)
)
def get_value(
self, value: Union[int, float], total: Optional[Union[int, float]] = None
) -> float:
"""Get value scaled or as percentage."""
value = float(value)
if total:
value /= float(total) / 100
else:
value = self.scale_value(value)
return round(value, 2)
def add_perfdata(self, name: str, value: Union[int, float], **kwargs: Dict) -> None:
"""Add metric to perfdata output."""
unit = kwargs.get("unit", "%")
perfdata = f"{name}={value}{unit}"
threshold_warning = self.threshold_warning(name)
threshold_critical = self.threshold_critical(name)
perfdata += ";"
if threshold_warning:
perfdata += str(threshold_warning.value)
perfdata += ";"
if threshold_critical:
perfdata += str(threshold_critical.value)
perfdata += ";" + str(kwargs.get("min", 0))
perfdata += ";" + str(kwargs.get("max", ""))
self.perfdata.append(perfdata)
def get_perfdata(self) -> str:
"""Get perfdata string."""
perfdata = ""
if self.perfdata:
perfdata = "|"
perfdata += " ".join(self.perfdata)
return perfdata
def check(self) -> None:
"""Execute the real check command."""
self.check_result = CheckState.OK
if self.options.mode == "cluster":
self.check_cluster_status()
elif self.options.mode == "version":
self.check_version()
elif self.options.mode == "memory":
self.check_memory()
elif self.options.mode == "swap":
self.check_swap()
elif self.options.mode in ("io_wait", "io-wait"):
self.check_io_wait()
elif self.options.mode == "disk-health":
self.check_disks()
elif self.options.mode == "cpu":
self.check_cpu()
elif self.options.mode == "services":
self.check_services()
elif self.options.mode == "updates":
self.check_updates()
elif self.options.mode == "subscription":
self.check_subscription()
elif self.options.mode == "storage":
self.check_storage(self.options.name)
elif self.options.mode in ["vm", "vm_status", "vm-status"]:
only_status = self.options.mode in ["vm_status", "vm-status"]
if self.options.name:
idx = self.options.name
else:
idx = self.options.vmid
if self.options.expected_vm_status:
self.check_vm_status(
idx, expected_state=self.options.expected_vm_status, only_status=only_status
)
else:
self.check_vm_status(idx, only_status=only_status)
elif self.options.mode == "replication":
self.check_replication()
elif self.options.mode == "ceph-health":
self.check_ceph_health()
elif self.options.mode == "zfs-health":
self.check_zfs_health(self.options.name)
elif self.options.mode == "zfs-fragmentation":
self.check_zfs_fragmentation(self.options.name)
elif self.options.mode == "backup":
self.check_vzdump_backup(self.options.name)
else:
message = f"Check mode '{self.options.mode}' not known"
self.output(CheckState.UNKNOWN, message)
self.check_output()
def parse_args(self) -> None:
"""Parse CLI arguments."""
p = argparse.ArgumentParser(description="Check command for PVE hosts via API")
api_opts = p.add_argument_group("API Options")
api_opts.add_argument(
"-e",
"-H",
"--api-endpoint",
required=True,
help="PVE api endpoint hostname or ip address (no additional data like paths)",
)
api_opts.add_argument("--api-port", required=False, help="PVE api endpoint port")
api_opts.add_argument(
"-u",
"--username",
dest="api_user",
required=True,
help="PVE api user (e.g. icinga2@pve or icinga2@pam, depending on which backend you "
"have chosen in proxmox)",
)
group = api_opts.add_mutually_exclusive_group(required=True)
group.add_argument("-p", "--password", dest="api_password", help="PVE API user password")
group.add_argument(
"-t",
"--api-token",
dest="api_token",
help="PVE API token (format: TOKEN_ID=TOKEN_SECRET",
)
api_opts.add_argument(
"-k",
"--insecure",
dest="api_insecure",
action="store_true",
default=False,
help="Don't verify HTTPS certificate",
)
api_opts.set_defaults(api_port=8006)
check_opts = p.add_argument_group("Check Options")
check_opts.add_argument(
"-m",
"--mode",
choices=(
"cluster",
"version",
"cpu",
"memory",
"swap",
"storage",
"io_wait",
"io-wait",
"updates",
"services",
"subscription",
"vm",
"vm_status",
"vm-status",
"replication",
"disk-health",
"ceph-health",
"zfs-health",
"zfs-fragmentation",
"backup",
),
required=True,
help="Mode to use.",
)