def _getOldRepoInfo(self, repoName, repoPath, notifySettings, isAdminMonitoring): if isAdminMonitoring: maxAge = notifySettings['adminMaxAge'] else: maxAge = notifySettings['anyRepoMaxAge'] if not maxAge: maxAge = notifySettings['repos'][repoName] if maxAge == 0: return None try: lastBackup = librdiff.getLastBackupHistoryEntry(repoPath, False) except librdiff.FileError: return { "repo" : repoName, "lastBackupDate" : "never", "maxAge" : maxAge } except Exception: rdw_logging.log_exception() rdw_logging.log('(Previous exception occurred for repo %s.)' % repoPath) else: if lastBackup: oldestGoodBackupTime = rdw_helpers.rdwTime() oldestGoodBackupTime.initFromMidnightUTC(-maxAge) if lastBackup.date < oldestGoodBackupTime: return { "repo" : repoName, "lastBackupDate" : lastBackup.date.getDisplayString(), "maxAge" : maxAge } return None
def findReposForUser(user, userDBModule): try: userRoot = userDBModule.getUserRoot(user) repoPaths = [] _findRdiffRepos(userRoot, repoPaths) def stripRoot(path): if not path[len(userRoot):]: return "/" return path[len(userRoot):] repoPaths = map(stripRoot, repoPaths) userDBModule.setUserRepos(user, repoPaths) except Exception: rdw_logging.log_exception()
def run(self): self.notifier = emailNotifier() if not self.notifier.notificationsEnabled(): return emailTimeStr = rdw_config.getConfigSetting("emailNotificationTime") while True: try: emailTime = time.strptime(emailTimeStr, "%H:%M") now = datetime.datetime.now() nextEmailTime = now.replace(hour=emailTime.tm_hour, minute=emailTime.tm_min, second=0, microsecond=0) if nextEmailTime < now: nextEmailTime = nextEmailTime + datetime.timedelta(days=1) delta = (nextEmailTime - now).seconds self.killEvent.wait(delta) if self.killEvent.isSet(): return self.notifier.sendEmails() except Exception: rdw_logging.log_exception()
def sendEmails(self): # Send emails to each user, if requested for user in self.userDB.getUserList(): try: notifySettings = self.userDB.getNotificationSettings(user) userRepos = self.userDB.getUserRepoPaths(user) oldRepos = [] for repo in userRepos: # get the last backup date repoPath = rdw_helpers.joinPaths(self.userDB.getUserRoot(user), repo) oldRepoInfo = self._getOldRepoInfo(repo, repoPath, notifySettings, False) if not oldRepoInfo is None: oldRepos.append(oldRepoInfo) if oldRepos: userEmailAddress = notifySettings['email'] parms = { 'repos': oldRepos, 'sender': self.getEmailSender(), 'user': user, 'to': userEmailAddress } self._sendNotifyEmail('email_notification', parms, userEmailAddress, self.getEmailSender(), 'Recent backup failures') except Exception: rdw_logging.log_exception() # Send admin-level emails, if requested adminEmails = [] for user in self.userDB.getUserList(): if self.userDB.userIsAdmin(user): userEmail = self.userDB.getNotificationSettings(user)['email'] if userEmail: adminEmails.append(userEmail) if adminEmails: oldUserRepos = [] for user in self.userDB.getUserList(): notifySettings = self.userDB.getNotificationSettings(user) userRepos = self.userDB.getUserRepoPaths(user) oldRepos = [] for repo in userRepos: repoPath = rdw_helpers.joinPaths(self.userDB.getUserRoot(user), repo) oldRepoInfo = self._getOldRepoInfo(repo, repoPath, notifySettings, True) if not oldRepoInfo is None: oldRepos.append(oldRepoInfo) if oldRepos: oldUserRepos.append({ 'user': user, 'maxAge': notifySettings['adminMaxAge'], 'repos': oldRepos }) email = ', '.join(adminEmails) parms = { 'users': oldUserRepos, 'sender': self.getEmailSender(), 'date': datetime.date.today().strftime('%m/%d/%Y'), 'to': email } self._sendNotifyEmail('admin_email_notification', parms, email, self.getEmailSender(), 'Backup Failures for ' + parms['date'])