-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmountmon.py
More file actions
executable file
·223 lines (192 loc) · 7.32 KB
/
mountmon.py
File metadata and controls
executable file
·223 lines (192 loc) · 7.32 KB
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
#!/usr/bin/env python
"""
mountmon.py -- daemon to monitor mountpoints
usage: mountmon.py [ -c <cfgfile> | --cfgfile=<cfgfile> ] [ -z | --zabbix_clear ]
exit codes, also sent to Zabbix if configured:
0 = good
1 = not mounted
2 = mounted, can't list
3 = mounted, no checkdir and can't create
4 = mounted, can't write file
5 = no mountmon config file at startup
default config file location: /etc/mountmon/mountmon.yaml
"""
import logging
import daemon
import lockfile
import os
import time
import subprocess
import sys
import yaml
import argparse
from pyzabbix import ZabbixSender, ZabbixMetric
def RunCommand(cmd):
try:
proc = subprocess.Popen(cmd)
proc.wait()
return proc.returncode == 0
except:
return False
class mountmon (object):
def __init__(self):
self.cfg = {
'daemonize' : False,
'interval' : 60.0,
'zabbix' : False,
'zabbix_trigger' : 'mountmon.error',
'logfile' : 'test_mountmon.log',
'loglevel' : 'DEBUG',
'hostname' : os.uname()[1],
'remount' : False,
'pidfile' : '/var/run/mountmon.pid',
'working_dir' : '/etc/mountmon',
'mountpoints' : {
'/mymount' : {
'checkdir' : 'check',
'checkfile' : 'foo',
'write_check' : True
}
}
}
def GetConfig(self, cfgfile):
try:
with open (cfgfile, 'r') as yamlfile:
cfg_from_file = yaml.load(yamlfile)
self.cfg.update(cfg_from_file)
except:
self.Error("Error loading config from file {}.".format(cfgfile), 5)
def SetLogging(self):
self.logger = logging.getLogger()
logging.basicConfig (
format = '%(asctime)s %(levelname)s %(message)s',
filename = self.cfg['logfile'],
level = self.cfg['loglevel']
)
if self.cfg['daemonize'] == False:
term = logging.StreamHandler(sys.stdout)
term.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
term.setFormatter(formatter)
self.logger.addHandler(term)
def ZabbixSend(self, value, key=''):
packet = []
if key == '':
key = self.cfg['zabbix_trigger']
metric = ZabbixMetric(self.cfg['hostname'], "{}".format(key), value)
packet.append(metric)
zbx = ZabbixSender(self.cfg['zabbix_address'])
try:
zbx.send(packet)
self.logger.debug("Sent key '{}' value '{}' to zabbix".format(key, value))
except:
self.logger.error("Error sending key '{}' value '{}' to zabbix.".format(key,value))
def Error (self, output, to_zabbix=1):
if self.cfg['zabbix']:
self.ZabbixSend( to_zabbix )
self.logger.error(output)
def Mount (self, mp):
return RunCommand(['/usr/bin/mount', mp])
def Umount(self, mp):
result = RunCommand(['/usr/bin/umount', mp])
if result:
return result
else:
return RunCommand(['/usr/bin/umount', '-l', mp])
def MountMon(self, mp):
checkdir = "{}/{}".format(mp, self.cfg['mountpoints'][mp]['checkdir'])
checkfile = "{}/{}".format(checkdir, self.cfg['mountpoints'][mp]['checkfile'])
## mountpoint check
if not os.path.ismount(mp):
if self.cfg['remount']:
self.Error("{} found unmounted, attempting to mount".format(mp), 1)
if self.Mount(mp):
## insert delay before attempting to access
time.sleep(2)
else:
self.Error("{} could not be mounted".format(mp), 1)
return 1
else:
self.Error("{} is not mounted".format(mp), 1)
return 1
## list mountpoint directory
## if we can't, the mount is possibly stale, try to remount
try:
os.listdir(mp)
except OSError:
if self.cfg['remount']:
self.Error("mountpoint {} appears stale, attmepting remount".format(mp), 2)
if self.Umount(mp):
time.sleep(2)
if self.Mount(mp):
time.sleep(2)
else:
self.Error("Unmounted stale mountpoint {}, but cannot remount".format(mp), 2)
return 2
else:
self.Error("{} could not be unmounted for remount".format(mp), 2)
return 2
else:
self.Error("{} not readable, appears stale".format(mp), 2)
return 2
## file creation/writing check
if self.cfg['mountpoints'][mp]['write_check']:
## checkdir
try:
os.listdir(checkdir)
except OSError:
try:
os.mkdir(checkdir, 0700)
self.logger.warning("Created monitor directory {}".format(checkdir))
except:
self.Error("Dir {} does not exist and could not create it.".format(mp), 3)
return 3
## write file
## TODO: add timer
try:
with open(checkfile, 'w+') as f:
looptime = time.ctime()
f.write( "{}\n".format(looptime))
f.close()
except:
self.Error("Could not write file: {}".format(checkfile), 4)
return 4
return 0
def MainLoop(self):
starttime = time.time()
while True:
try:
for mountpoint in self.cfg['mountpoints']:
err = self.MountMon(mountpoint)
self.logger.debug ( "Looped at {}".format(time.time()))
self.logger.debug ( "return = {}".format(err) )
time.sleep (self.cfg['interval'] - ((time.time() - starttime) % self.cfg['interval']))
except KeyboardInterrupt:
return
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--cfgfile', dest='cfgfile', help='Config file path',
default='/etc/mountmon/mountmon.yaml')
parser.add_argument('-z', '--zabbix_clear', help='Clear Zabbix alert', action='store_true')
args = parser.parse_args()
monitor = mountmon()
monitor.GetConfig(args.cfgfile)
## For -z to clear zabbix: don't daemonize, send a 0, and exit
if args.zabbix_clear:
print("Clearing zabbix errors...")
monitor.cfg['daemonize'] = False
monitor.SetLogging()
monitor.ZabbixSend(0)
sys.exit()
if monitor.cfg['daemonize']:
context = daemon.DaemonContext (
working_directory = monitor.cfg['working_dir'],
pidfile = lockfile.FileLock(monitor.cfg['pidfile'])
)
with context:
monitor.SetLogging()
monitor.logger.info('Started mountmon')
monitor.MainLoop()
else:
monitor.SetLogging()
monitor.MainLoop()