Skip to content

Commit 6bab59a

Browse files
committed
Add a suppression window option
Add the ability to specify the data directory
1 parent 4075aaf commit 6bab59a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

example.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[settings]
22
pushover_api_token=abcdefghijklmnopqrstuvwxyz
33
pushover_user_key=abcdefghijklmnopqrstuvwxyz
4+
data_directory=data
5+
suppression_window=3600
46

57
[btrfs_mount_points]
68
mount1=/mnt/data

src/raid_status_notifier/main.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,48 @@
44
import re
55
import os
66
import pickle
7+
import time
78

89
from argparse import ArgumentParser
910
from subprocess import check_output
1011
from pushover import init, Client
1112
from configparser import ConfigParser
13+
from functools import wraps
14+
15+
16+
def suppression_window(function):
17+
@wraps(function)
18+
def wrapper(inst, *args, **kwargs):
19+
last_checked_filename = "%s/last-checked_%s.p" % (inst.data_dir, function.__name__)
20+
21+
last_checked_time = 0
22+
if os.path.exists(last_checked_filename):
23+
last_checked_time = pickle.load(open(last_checked_filename, "rb"))
24+
25+
current_time = int(time.time())
26+
if current_time >= last_checked_time + inst.suppression_window:
27+
function(inst, *args, **kwargs)
28+
29+
if not os.path.exists(inst.data_dir):
30+
os.mkdir(inst.data_dir)
31+
32+
pickle.dump(current_time, open(last_checked_filename, "wb"))
33+
34+
return wrapper
1235

1336

1437
class RaidStatusChecker(object):
1538
def __init__(self, config):
1639
init(config.get("settings", "pushover_api_token"))
1740
self.client = Client(config.get("settings", "pushover_user_key"))
1841
self.btrfs_mount_points = [path for key, path in config.items("btrfs_mount_points")]
42+
self.data_dir = config.get("settings", "data_directory")
43+
self.suppression_window = int(config.get("settings", "suppression_window"))
1944

45+
@suppression_window
2046
def check_btrfs_stats(self):
2147
for mount_point in self.btrfs_mount_points:
22-
stats_filename = "data/btrfs-stats_%s.p" % mount_point[1:].replace("/", "-")
48+
stats_filename = "%s/btrfs-stats_%s.p" % (self.data_dir, mount_point[1:].replace("/", "-"))
2349

2450
device_stats = {}
2551
if os.path.exists(stats_filename):
@@ -39,21 +65,23 @@ def check_btrfs_stats(self):
3965
device_stats[match.group(1)][match.group(2)] = int(match.group(3))
4066
new_errors = True
4167

42-
if not os.path.exists("data"):
43-
os.mkdir("data")
68+
if not os.path.exists(self.data_dir):
69+
os.mkdir(self.data_dir)
4470

4571
pickle.dump(device_stats, open(stats_filename, "wb"))
4672

4773
if new_errors is not False:
4874
self.client.send_message(status, title="BTRFS Errors: %s" % mount_point)
4975

76+
@suppression_window
5077
def check_btrfs_drives(self):
5178
status = check_output(["sudo", "btrfs", "fi", "show", "-d"]).decode("utf-8").strip()
5279

5380
regex = re.compile('(missing|warning)')
5481
if regex.match(status) is not None:
5582
self.client.send_message(status, title="BTRFS Array Error")
5683

84+
@suppression_window
5785
def check_zfs_drives(self):
5886
status = check_output(["sudo", "zpool", "status", "-x"])
5987
if status != "all pools are healthy":
@@ -70,7 +98,7 @@ def main(argv=None):
7098
argv = sys.argv[1:]
7199

72100
parser = ArgumentParser()
73-
parser.add_argument("-c", "--config", action="store", help="Configuration File", metavar="CONFIGFILE")
101+
parser.add_argument("-c", "--config", action="store", help="Configuration File", metavar="CONFIG_FILE")
74102
parser.set_defaults(config="settings.cfg")
75103

76104
options = parser.parse_args(argv)

0 commit comments

Comments
 (0)