def email(dataset, threshold, avail): send_mail(subject="Volume threshold", text="""Hi, Your volume %s has reached the threshold of %s. Currently there is %s of available space. """ % (dataset, threshold, avail))
def email(self): """ Use alert.last to hold a sha256 hash of the last sent alerts If the hash is the same do not resend the email """ if len(self.__logs[self.LOG_CRIT]) == 0: if os.path.exists(LAST_ALERT_FILE): os.unlink(LAST_ALERT_FILE) return try: with open(LAST_ALERT_FILE) as f: sha256 = f.read() except: sha256 = '' newsha = hashlib.sha256(repr(self.__logs[self.LOG_CRIT])).hexdigest() if newsha != sha256: send_mail(subject=_("Critical Alerts"), text='\n'.join(self.__logs[self.LOG_CRIT])) with open(LAST_ALERT_FILE, 'w') as f: f.write(newsha)
def do_sendmail(msg, to_addrs=None, parse_recipients=False): if to_addrs is None: if not parse_recipients: raise ValueError('Do not know who to send the message to.') to_addrs = [] # XXX: this should probably be a FeedParser because reading from sys.stdin # is blocking. em_parser = email.parser.Parser() em = em_parser.parsestr(msg) if parse_recipients: # Strip away the comma based delimiters and whitespace. to_addrs = map(str.strip, em.get('To').split(',')) if to_addrs: aliases = get_aliases() to_addrs_repl = [] for to_addr in to_addrs: if to_addr.find('@') == -1 and to_addr in aliases: to_addr = aliases[to_addr] to_addrs_repl.append(to_addr) margs = {} margs['extra_headers'] = dict(em) margs['subject'] = em.get('Subject') if em.is_multipart(): margs['attachments'] = filter( lambda part: part.get_content_maintype() != 'multipart', em.walk()) margs['text'] = u"%s" % _( 'This is a MIME formatted message. If you see ' 'this text it means that your email software ' 'does not support MIME formatted messages.') else: margs['text'] = ''.join(email.iterators.body_line_iterator(em)) if to_addrs_repl: margs['to'] = to_addrs_repl send_mail(**margs)
def do_sendmail(msg, to_addrs=None, parse_recipients=False): if to_addrs is None: if not parse_recipients: raise ValueError('Do not know who to send the message to.') to_addrs = [] # XXX: this should probably be a FeedParser because reading from sys.stdin # is blocking. em_parser = email.parser.Parser() em = em_parser.parsestr(msg) if parse_recipients: # Strip away the comma based delimiters and whitespace. to_addrs = map(str.strip, em.get('To').split(',')) if to_addrs: aliases = get_aliases() to_addrs_repl = [] for to_addr in to_addrs: if to_addr.find('@') == -1 and to_addr in aliases: to_addr = aliases[to_addr] to_addrs_repl.append(to_addr) margs = {} margs['extra_headers'] = dict(em) margs['subject'] = em.get('Subject') if em.is_multipart(): margs['attachments'] = filter(lambda part: part.get_content_maintype() != 'multipart', em.walk()) margs['text'] = u"%s" % _('This is a MIME formatted message. If you see ' 'this text it means that your email software ' 'does not support MIME formatted messages.') else: margs['text'] = ''.join(email.iterators.body_line_iterator(em)) if to_addrs_repl: margs['to'] = to_addrs_repl send_mail(**margs)
def testmail(request): form = forms.EmailForm(request.POST) if not form.is_valid(): return JsonResp(request, form=form) sid = transaction.savepoint() form.save() error = False if request.is_ajax(): sw_name = get_sw_name() error, errmsg = send_mail( subject=_("Test message from %s" % (sw_name)), text=_("This is a message test from %s" % (sw_name,)) ) if error: errmsg = _("Your test email could not be sent: %s") % errmsg else: errmsg = _("Your test email has been sent!") transaction.savepoint_rollback(sid) return JsonResp(request, error=error, message=errmsg)
def testmail(request): form = forms.EmailForm(request.POST) if not form.is_valid(): return JsonResp(request, form=form) sid = transaction.savepoint() form.save() error = False if request.is_ajax(): sw_name = get_sw_name() error, errmsg = send_mail(subject=_('Test message from %s' % (sw_name)), text=_('This is a message test from %s' % (sw_name, ))) if error: errmsg = _("Your test email could not be sent: %s") % errmsg else: errmsg = _('Your test email has been sent!') transaction.savepoint_rollback(sid) return JsonResp(request, error=error, message=errmsg)
MNTLOCK.lock() zfsproc = pipeopen('/sbin/zfs list -Ht snapshot -o name,metanas:state %s' % (expected_local_snapshot), debug) output = zfsproc.communicate()[0] if output != '': last_snapshot, state = output.split('\n')[0].split('\t') syslog.syslog(syslog.LOG_INFO, "Marking %s as latest snapshot" % (last_snapshot)) if state == '-': system('/sbin/zfs inherit metanas:state %s' % (known_latest_snapshot)) system('/sbin/zfs set metanas:state=LATEST %s' % (last_snapshot)) known_latest_snapshot = last_snapshot else: syslog.syslog(syslog.LOG_ALERT, "Can not locate a proper local snapshot for %s" % (localfs)) # Can NOT proceed any further. Report this situation. error, errmsg = send_mail(subject="Replication failed!", text=\ """ Hello, The replication failed for the local ZFS %s because the remote system have diverged snapshot with us. """ % (localfs), interval=datetime.timedelta(hours=2), channel='autorepl') MNTLOCK.unlock() continue MNTLOCK.unlock() else: syslog.syslog(syslog.LOG_NOTICE, "Can not locate %s on remote system, starting from there" % (known_latest_snapshot)) # Reset the "latest" snapshot to a new one. system('/sbin/zfs set metanas:state=NEW %s' % (known_latest_snapshot)) wanted_list.insert(0, known_latest_snapshot) last_snapshot = '' known_latest_snapshot = '' if resetonce: syslog.syslog(syslog.LOG_NOTICE, "Destroying remote %s" % (remotefs_final)) destroycmd = '%s -p %d %s /sbin/zfs destroy -rRf %s' % (sshcmd, remote_port, remote, remotefs_final)
"Marking %s as latest snapshot" % (last_snapshot)) if state == '-': system('/sbin/zfs inherit metanas:state %s' % (known_latest_snapshot)) system('/sbin/zfs set metanas:state=LATEST %s' % (last_snapshot)) known_latest_snapshot = last_snapshot else: syslog.syslog( syslog.LOG_ALERT, "Can not locate a proper local snapshot for %s" % (localfs)) # Can NOT proceed any further. Report this situation. error, errmsg = send_mail(subject="Replication failed!", text=\ """ Hello, The replication failed for the local ZFS %s because the remote system have diverged snapshot with us. """ % (localfs), interval=datetime.timedelta(hours=2), channel='autorepl') MNTLOCK.unlock() continue MNTLOCK.unlock() else: syslog.syslog( syslog.LOG_NOTICE, "Can not locate %s on remote system, starting from there" % (known_latest_snapshot)) # Reset the "latest" snapshot to a new one. system('/sbin/zfs set metanas:state=NEW %s' % (known_latest_snapshot)) wanted_list.insert(0, known_latest_snapshot) last_snapshot = ''