Skip to content

Commit 5356005

Browse files
Merge pull request #150 from timvaillancourt/notify_no_fail
Send CRITICAL state to NSCA on backup failure
2 parents 93af5d6 + efdb0c5 commit 5356005

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

mongodb_consistent_backup/Main.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"):
4949
self.logger = None
5050
self.current_log_file = None
5151
self.backup_log_file = None
52+
self.last_error_msg = ''
5253

5354
try:
5455
self.setup_config()
@@ -57,6 +58,7 @@ def __init__(self, prog_name="mongodb-consistent-backup"):
5758
self.get_lock()
5859
self.logger.update_symlink()
5960
self.init()
61+
self.setup_notifier()
6062
self.set_backup_dirs()
6163
self.get_db_conn()
6264
self.setup_state()
@@ -98,6 +100,18 @@ def setup_state(self):
98100
self.state = StateBackup(self.backup_directory, self.config, self.backup_time, self.uri, sys.argv)
99101
self.state.write()
100102

103+
def setup_notifier(self):
104+
try:
105+
self.notify = Notify(
106+
self.manager,
107+
self.config,
108+
self.timer,
109+
self.backup_root_subdirectory,
110+
self.backup_directory
111+
)
112+
except Exception, e:
113+
self.exception("Problem starting notifier! Error: %s" % e, e)
114+
101115
def get_db_conn(self):
102116
self.uri = MongoUri(self.config.host, self.config.port)
103117
try:
@@ -171,23 +185,23 @@ def cleanup_and_exit(self, code, frame):
171185

172186
if self.manager:
173187
self.manager.shutdown()
188+
if self.db:
189+
self.db.close()
174190

175191
if self.notify:
176192
try:
177-
self.notify.notify("%s: backup '%s' failed!" % (
178-
self.config,
179-
self.program_name
180-
), False)
193+
self.notify.notify("%s: backup '%s/%s' failed! Error: '%s'" % (
194+
self.program_name,
195+
self.config.backup.name,
196+
self.backup_time,
197+
self.last_error_msg
198+
))
181199
self.notify.run()
182200
self.notify.close()
183-
except:
184-
pass
185-
186-
if self.db:
187-
self.db.close()
201+
except Exception, e:
202+
logging.error("Error from notifier: %s" % e)
188203

189204
logging.info("Cleanup complete, exiting")
190-
191205
if self.logger:
192206
self.logger.rotate()
193207
self.logger.close()
@@ -196,6 +210,7 @@ def cleanup_and_exit(self, code, frame):
196210
sys.exit(1)
197211

198212
def exception(self, error_message, error):
213+
self.last_error_msg = error_message
199214
if isinstance(error, NotifyError):
200215
logging.error(error_message)
201216
else:
@@ -237,18 +252,6 @@ def run(self):
237252
except Exception, e:
238253
self.exception("Problem starting archiver! Error: %s" % e, e)
239254

240-
# Setup the notifier
241-
try:
242-
self.notify = Notify(
243-
self.manager,
244-
self.config,
245-
self.timer,
246-
self.backup_root_subdirectory,
247-
self.backup_directory
248-
)
249-
except Exception, e:
250-
self.exception("Problem starting notifier! Error: %s" % e, e)
251-
252255
# Setup the uploader
253256
try:
254257
self.upload = Upload(

mongodb_consistent_backup/Notify/Notify.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import logging
22

3+
from mongodb_consistent_backup.Errors import Error, NotifyError
34
from mongodb_consistent_backup.Notify.Nsca import Nsca
45
from mongodb_consistent_backup.Pipeline import Stage
56

@@ -9,7 +10,6 @@ def __init__(self, manager, config, timer, base_dir, backup_dir):
910
super(Notify, self).__init__(self.__class__.__name__, manager, config, timer, base_dir, backup_dir)
1011
self.task = self.config.notify.method
1112

12-
self.completed = False
1313
self.notifications = []
1414
self.init()
1515

@@ -19,19 +19,19 @@ def notify(self, message, success=False):
1919

2020
def run(self, *args):
2121
if self._task and len(self.notifications) > 0:
22-
logging.info("Sending %i notification(s) to: %s" % (len(self.notifications), self._task.server))
23-
self.timers.start(self.stage)
24-
while len(self.notifications) > 0:
25-
try:
26-
(success, message) = self.notifications.pop()
27-
state = self._task.failed
28-
if success:
29-
state = self._task.success
30-
self._task.run(state, message)
31-
except:
32-
continue
33-
self.timers.stop(self.stage)
34-
self.completed = True
22+
try:
23+
logging.info("Sending %i notification(s) to: %s" % (len(self.notifications), self._task.server))
24+
while len(self.notifications) > 0:
25+
try:
26+
(success, message) = self.notifications.pop()
27+
state = self._task.failed
28+
if success == True:
29+
state = self._task.success
30+
self._task.run(state, message)
31+
except NotifyError:
32+
continue
33+
except Exception, e:
34+
raise Error(e)
3535

3636
def close(self):
3737
if self._task:

mongodb_consistent_backup/Notify/Nsca/Nsca.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def close(self):
5454

5555
def run(self, ret_code, output):
5656
if self.notifier:
57-
self.timer.start(self.timer_name)
5857
logging.info("Sending %sNSCA report to check host/name '%s/%s' at NSCA host %s" % (
5958
self.mode_type,
6059
self.check_host,
@@ -66,7 +65,6 @@ def run(self, ret_code, output):
6665
try:
6766
self.notifier.svc_result(self.check_host, self.check_name, ret_code, str(output))
6867
logging.debug('Sent %sNSCA report to host %s' % (self.mode_type, self.server))
69-
self.timer.stop(self.timer_name)
7068
except Exception, e:
7169
logging.error('Failed to send %sNSCA report to host %s: %s' % (self.mode_type, self.server, sys.exc_info()[1]))
7270
raise NotifyError(e)

0 commit comments

Comments
 (0)