4
4
import re
5
5
import os
6
6
import pickle
7
+ import time
7
8
8
9
from argparse import ArgumentParser
9
10
from subprocess import check_output
10
11
from pushover import init , Client
11
12
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
12
35
13
36
14
37
class RaidStatusChecker (object ):
15
38
def __init__ (self , config ):
16
39
init (config .get ("settings" , "pushover_api_token" ))
17
40
self .client = Client (config .get ("settings" , "pushover_user_key" ))
18
41
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" ))
19
44
45
+ @suppression_window
20
46
def check_btrfs_stats (self ):
21
47
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 ("/" , "-" ) )
23
49
24
50
device_stats = {}
25
51
if os .path .exists (stats_filename ):
@@ -39,21 +65,23 @@ def check_btrfs_stats(self):
39
65
device_stats [match .group (1 )][match .group (2 )] = int (match .group (3 ))
40
66
new_errors = True
41
67
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 )
44
70
45
71
pickle .dump (device_stats , open (stats_filename , "wb" ))
46
72
47
73
if new_errors is not False :
48
74
self .client .send_message (status , title = "BTRFS Errors: %s" % mount_point )
49
75
76
+ @suppression_window
50
77
def check_btrfs_drives (self ):
51
78
status = check_output (["sudo" , "btrfs" , "fi" , "show" , "-d" ]).decode ("utf-8" ).strip ()
52
79
53
80
regex = re .compile ('(missing|warning)' )
54
81
if regex .match (status ) is not None :
55
82
self .client .send_message (status , title = "BTRFS Array Error" )
56
83
84
+ @suppression_window
57
85
def check_zfs_drives (self ):
58
86
status = check_output (["sudo" , "zpool" , "status" , "-x" ])
59
87
if status != "all pools are healthy" :
@@ -70,7 +98,7 @@ def main(argv=None):
70
98
argv = sys .argv [1 :]
71
99
72
100
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 " )
74
102
parser .set_defaults (config = "settings.cfg" )
75
103
76
104
options = parser .parse_args (argv )
0 commit comments