|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from .agent_based_api.v1 import ( |
| 5 | + get_value_store, |
| 6 | + register, |
| 7 | + Service, |
| 8 | + ServiceLabel, |
| 9 | + State, |
| 10 | + Result, |
| 11 | +) |
| 12 | +from .utils import df |
| 13 | +import re |
| 14 | +import json |
| 15 | + |
| 16 | +proxmox_bs_subsection_start = re.compile("^===") |
| 17 | +proxmox_bs_subsection_int = re.compile("===.*$") |
| 18 | +proxmox_bs_subsection_end = re.compile("^=") |
| 19 | + |
| 20 | + |
| 21 | +def proxmox_bs_subsections_discovery(section): |
| 22 | + active = False |
| 23 | + |
| 24 | + name = None |
| 25 | + key = None |
| 26 | + content = "" |
| 27 | + key_i = 0 |
| 28 | + |
| 29 | + for line in section: |
| 30 | + line = ' '.join(''.join(_) for _ in line) |
| 31 | + if active and proxmox_bs_subsection_end.match(line): |
| 32 | + active = False |
| 33 | + yield name, key, content |
| 34 | + key = None |
| 35 | + name = None |
| 36 | + content = "" |
| 37 | + if (not active) and proxmox_bs_subsection_start.match(line): |
| 38 | + active = True |
| 39 | + rest = str(line)[3:].strip() |
| 40 | + s_rest = rest.split('===') |
| 41 | + name = s_rest[0] |
| 42 | + if len(s_rest) == 2: |
| 43 | + key = s_rest[1] |
| 44 | + key_i += 1 |
| 45 | + content = "" |
| 46 | + continue |
| 47 | + if active: |
| 48 | + content += str(line).rstrip() |
| 49 | + |
| 50 | + |
| 51 | +def proxmox_bs_subsections_checks(section): |
| 52 | + active = False |
| 53 | + |
| 54 | + name = None |
| 55 | + key = None |
| 56 | + content = "" |
| 57 | + key_i = 0 |
| 58 | + |
| 59 | + for line in section: |
| 60 | + line = ' '.join(line) |
| 61 | + if active and proxmox_bs_subsection_end.match(line): |
| 62 | + active = False |
| 63 | + yield name, key, content |
| 64 | + key = None |
| 65 | + name = None |
| 66 | + content = "" |
| 67 | + if (not active) and proxmox_bs_subsection_start.match(line): |
| 68 | + active = True |
| 69 | + rest = line[3:].strip() |
| 70 | + s_rest = rest.split('===') |
| 71 | + name = s_rest[0].rstrip() |
| 72 | + if len(s_rest) == 2: |
| 73 | + key = s_rest[1] |
| 74 | + key_i += 1 |
| 75 | + content = "" |
| 76 | + continue |
| 77 | + if active: |
| 78 | + content += line.rstrip() |
| 79 | + |
| 80 | + |
| 81 | +def proxmox_bs_discovery(section): |
| 82 | + for n, k, c in proxmox_bs_subsections_discovery(section): |
| 83 | + if n == "proxmox-backup-manager datastore list": |
| 84 | + for ds in json.loads(c): |
| 85 | + if "name" in ds: |
| 86 | + yield Service( |
| 87 | + item=ds["name"], |
| 88 | + labels=[ServiceLabel('pbs/datastore', 'yes')] |
| 89 | + ) |
| 90 | + return |
| 91 | + |
| 92 | +def proxmox_bs_checks(item, params, section): |
| 93 | + req_ok = True |
| 94 | + expected = [] |
| 95 | + missing = [] |
| 96 | + |
| 97 | + upid = None |
| 98 | + upid_status = {} |
| 99 | + |
| 100 | + gc_ok = False |
| 101 | + gc_running = False |
| 102 | + |
| 103 | + running_tasks = [] |
| 104 | + value_store = get_value_store() |
| 105 | + |
| 106 | + for n, k, c in proxmox_bs_subsections_checks(section): |
| 107 | + if n == "proxmox-backup-manager task list": |
| 108 | + task_list = json.loads(c) |
| 109 | + for task in task_list: |
| 110 | + if ("starttime" in task) and ("endtime" not in task): |
| 111 | + running_tasks.append(task["upid"]) |
| 112 | + if task.get("worker_id", None) is not None: |
| 113 | + if task.get("worker_id", None) == item: |
| 114 | + gc_running = True |
| 115 | + if n == "proxmox-backup-manager garbage-collection status": |
| 116 | + if k == item: |
| 117 | + gc = json.loads(c) |
| 118 | + if "upid" in gc: |
| 119 | + upid = gc["upid"] |
| 120 | + if (n == "proxmox-backup-client status") and (k == item): |
| 121 | + try: |
| 122 | + ds_status = json.loads(c) |
| 123 | + size_mb = int(ds_status["total"])/(1024**2) |
| 124 | + avail_mb = int(ds_status["avail"])/(1024**2) |
| 125 | + |
| 126 | + yield from df.df_check_filesystem_single( |
| 127 | + value_store, item, size_mb, avail_mb, |
| 128 | + None, None, None, params |
| 129 | + ) |
| 130 | + except: |
| 131 | + yield Result( |
| 132 | + state=State.UNKNOWN, |
| 133 | + summary="error checking datastore status" |
| 134 | + ) |
| 135 | + if (n == "proxmox-backup-manager task log") and (k == upid): |
| 136 | + if "TASK OK" in c.strip()[-7:]: |
| 137 | + gc_ok = True |
| 138 | + |
| 139 | + if n == "EOD": |
| 140 | + if gc_running: |
| 141 | + yield Result( state=State.OK, summary="GC running" ) |
| 142 | + elif gc_ok: |
| 143 | + yield Result( state=State.OK, summary="GC ok" ) |
| 144 | + elif upid == None: |
| 145 | + yield Result( state=State.UNKNOWN, summary="GC not run yet" ) |
| 146 | + else: |
| 147 | + yield Result( state=State.WARN, summary="GC Task failed" ) |
| 148 | + |
| 149 | + |
| 150 | +register.agent_section( |
| 151 | + name="proxmox_bs", |
| 152 | + #host_label_function = proxmox_bs_hostlabels, |
| 153 | +) |
| 154 | + |
| 155 | +register.check_plugin( |
| 156 | + name="proxmox_bs", |
| 157 | + service_name="PBS Datastore %s", |
| 158 | + sections=["proxmox_bs"], |
| 159 | + discovery_function=proxmox_bs_discovery, |
| 160 | + check_function=proxmox_bs_checks, |
| 161 | + check_default_parameters=df.FILESYSTEM_DEFAULT_LEVELS, |
| 162 | + check_ruleset_name="filesystem", |
| 163 | +) |
| 164 | + |
0 commit comments