| 1 |
#! /usr/bin/python3 |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
import json |
| 5 |
import os |
| 6 |
import sys |
| 7 |
import time |
| 8 |
|
| 9 |
from vdo.statistics import NotAvailable, Samples, VDOStatistics |
| 10 |
from vdo.vdomgmnt import Configuration, SizeString |
| 11 |
|
| 12 |
|
| 13 |
class Watcher: |
| 14 |
def __init__(self, path): |
| 15 |
self.inotify = Inotify() |
| 16 |
self.path = path |
| 17 |
self.wd = -1 |
| 18 |
self.setup() |
| 19 |
|
| 20 |
def setup(self): |
| 21 |
self.cur_path = self.path |
| 22 |
self.cur_wait = None |
| 23 |
while not os.path.exists(self.cur_path): |
| 24 |
self.cur_wait = os.path.basename(self.cur_path) |
| 25 |
self.cur_path = os.path.dirname(self.cur_path) |
| 26 |
|
| 27 |
events = (IN_CREATE | |
| 28 |
IN_MOVED_TO | |
| 29 |
IN_MOVED_FROM | |
| 30 |
IN_CLOSE_WRITE | |
| 31 |
IN_MOVE_SELF) |
| 32 |
self.wd = self.inotify.add_watch(self.cur_path, events) |
| 33 |
|
| 34 |
def process(self, callback=None): |
| 35 |
def event(wd, mask, name): |
| 36 |
want_callback = self.cur_path == self.path |
| 37 |
if self.cur_wait and name == self.cur_wait: |
| 38 |
self.inotify.rem_watch(self.wd) |
| 39 |
self.setup() |
| 40 |
elif mask & IN_IGNORED: |
| 41 |
self.setup() |
| 42 |
want_callback = want_callback or self.cur_path == self.path |
| 43 |
if want_callback and callback: |
| 44 |
callback() |
| 45 |
self.inotify.process(event) |
| 46 |
|
| 47 |
|
| 48 |
# Converts NotAvailable to None, recursively, and other things. The goal is to make OBJ serializable. |
| 49 |
def wash(obj): |
| 50 |
if isinstance(obj, NotAvailable): |
| 51 |
return None |
| 52 |
elif isinstance(obj, SizeString): |
| 53 |
return int(obj) |
| 54 |
elif isinstance(obj, dict): |
| 55 |
return {key: wash(obj[key]) for key in obj.keys()} |
| 56 |
elif isinstance(obj, list): |
| 57 |
return list(map(wash, obj)) |
| 58 |
else: |
| 59 |
return obj |
| 60 |
|
| 61 |
|
| 62 |
def dump_washed(obj): |
| 63 |
sys.stdout.write(json.dumps(wash(obj)) + "\n") |
| 64 |
sys.stdout.flush() |
| 65 |
|
| 66 |
|
| 67 |
def monitor_config(): |
| 68 |
def query(): |
| 69 |
try: |
| 70 |
conf = Configuration("/etc/vdoconf.yml") |
| 71 |
return [{"name": vdo.getName(), |
| 72 |
"broken": vdo.unrecoverablePreviousOperationFailure, |
| 73 |
"device": vdo.device, |
| 74 |
"logical_size": vdo.logicalSize, |
| 75 |
"physical_size": vdo.physicalSize, |
| 76 |
"index_mem": vdo.indexMemory, |
| 77 |
"activated": vdo.activated, |
| 78 |
"compression": vdo.enableCompression, |
| 79 |
"deduplication": vdo.enableDeduplication} |
| 80 |
for vdo in conf.getAllVdos().values()] |
| 81 |
except Exception as e: |
| 82 |
sys.stderr.write(str(e) + "\n") |
| 83 |
return [] |
| 84 |
|
| 85 |
def event(): |
| 86 |
dump_washed(query()) |
| 87 |
|
| 88 |
watcher = Watcher("/etc/vdoconf.yml") |
| 89 |
event() |
| 90 |
while True: |
| 91 |
watcher.process(event) |
| 92 |
|
| 93 |
|
| 94 |
def monitor_volume(dev): |
| 95 |
|
| 96 |
monitored_fields = ['blockSize', |
| 97 |
'dataBlocksUsed', 'overheadBlocksUsed', |
| 98 |
'logicalBlocksUsed', |
| 99 |
'usedPercent', 'savingPercent' |
| 100 |
] |
| 101 |
|
| 102 |
# Older versions let us use a string directly, newer versions want |
| 103 |
# it to be pre-processed. |
| 104 |
try: |
| 105 |
dev = Samples.samplingDevice(dev, dev) |
| 106 |
except AttributeError: |
| 107 |
pass |
| 108 |
|
| 109 |
def sample(): |
| 110 |
try: |
| 111 |
stats = Samples.assay([VDOStatistics()], dev, mustBeVDO=False).samples[0].sample |
| 112 |
return {key: stats.get(key) for key in monitored_fields} |
| 113 |
except Exception as e: |
| 114 |
# Ignore errors from non-existing devices. These happen |
| 115 |
# briefly when a VDO volume is being stopped or deleted |
| 116 |
# and the monitor hasn't been killed yet. |
| 117 |
if "[Errno 2]" not in str(e): |
| 118 |
raise |
| 119 |
return {} |
| 120 |
|
| 121 |
prev = None |
| 122 |
while True: |
| 123 |
data = sample() |
| 124 |
if data != prev: |
| 125 |
dump_washed(data) |
| 126 |
prev = data |
| 127 |
time.sleep(2) |
| 128 |
|
| 129 |
|
| 130 |
if len(sys.argv) == 1: |
| 131 |
monitor_config() |
| 132 |
else: |
| 133 |
monitor_volume(sys.argv[1]) |