def handle_task(self, job): user = job.get('user', 'root') group = job.get('group', 'root') mail = job.get('sender', None) account = Account(user=user, group=group, mail=mail) recipients = job.get('recipients', None) subject = ensure_unicode(job.get('subject', '')) body = ensure_unicode(job.get('body', '')) attachments = job.get('attachments', None) smtp_host = job.get('smtp_host', 'localhost') smtp_port = job.get('smtp_port', 25) html = job.get('html', False) template_data = job.get('jobctx', {}) body = Template(body)(template_data) subject = Template(subject)(template_data) if not html: h = HTMLParser() body = h.unescape(body) subject = h.unescape(subject) # Execute the task return self.sendmail(account, recipients, subject, body, attachments, smtp_host, smtp_port, html)
def test_forceunicode(self): """Test the forceunicode function. """ if PYVER < '3': self.assertTrue(isinstance(ensure_unicode(str()), unicode)) self.assertTrue(isinstance(ensure_unicode(unicode()), unicode)) self.assertRaises(TypeError, ensure_unicode)
def handle_task(self, job): user = job.get('user', 'root') group = job.get('group', 'root') mail = job.get('sender', None) account = Account(user=user, group=group, mail=mail) recipients = job.get('recipients', None) subject = ensure_unicode(job.get('subject', '')) body = ensure_unicode(job.get('body', '')) attachments = job.get('attachments', None) smtp_host = job.get('smtp_host', 'localhost') smtp_port = job.get('smtp_port', 25) html = job.get('html', False) template_data = job.get('jobctx', {}) body = Template(body)(template_data) subject = Template(subject)(template_data) if not html: h = HTMLParser() body = h.unescape(body) subject = h.unescape(subject) # Execute the task return self.sendmail( account, recipients, subject, body, attachments, smtp_host, smtp_port, html)
def handle_task(self, job): user = job.get("user", "root") group = job.get("group", "root") mail = job.get("sender", None) account = Account(user=user, group=group, mail=mail) recipients = job.get("recipients", None) subject = ensure_unicode(job.get("subject", "")) body = ensure_unicode(job.get("body", "")) attachments = job.get("attachments", None) smtp_host = job.get("smtp_host", "localhost") smtp_port = job.get("smtp_port", 25) html = job.get("html", False) template_data = job.get("jobctx", {}) body = Template(body)(template_data) subject = Template(subject)(template_data) if not html: h = HTMLParser() body = h.unescape(body) subject = h.unescape(subject) # Execute the task return self.sendmail(account, recipients, subject, body, attachments, smtp_host, smtp_port, html)
def __init__(self, source, *args, **kwargs): super(Template, self).__init__(*args, **kwargs) self.source = ensure_unicode(source) self.vars = {} self.register_helper(u'foreach', self._helper_foreach) self.register_helper(u'ifnot', self._helper_ifnot) self.register_helper(u'ifeq', self._helper_ifeq) self.register_helper(u'set', self._helper_set) self.register_helper(u'get', self._helper_get) self.register_helper(u'increment', self._helper_increment) self.register_helper(u'compact', self._helper_compact) self.register_helper(u'formattedDate', self._helper_formatdate) self.register_helper(u'strip-newlines', self._help_strip_nl) self.register_helper(u'today', self._helper_today) self.register_helper(u'state2text', self._helper_state2text)
def work(self, body, msg, *args, **kargs): ## Sanity Checks rk = msg.delivery_info['routing_key'] if not rk: raise Exception("Invalid routing-key '%s' (%s)" % (rk, body)) #self.logger.info( body ) ## Try to decode event if isinstance(body, dict): event = body # force utf8 only if python version is 2 if PYVER < '3': event = forceUTF8(event) else: self.logger.debug(" + Decode JSON") try: if isinstance(body, basestring): try: event = loads(body) self.logger.debug(" + Ok") except Exception as err: try: self.logger.debug(" + Try hack for windows string") # Hack for windows FS -_- event = loads(body.replace('\\', '\\\\')) self.logger.debug(" + Ok") except Exception as err: try: self.logger.debug(" + Decode BSON") bson = BSON(body) event = bson.decode() self.logger.debug(" + Ok") except Exception as err: raise Exception(err) except Exception as err: self.logger.error(" + Failed (%s)" % err) self.logger.debug("RK: '%s', Body:" % rk) self.logger.debug(body) raise Exception("Impossible to parse event '%s'" % rk) event['rk'] = ensure_unicode(rk) if "resource" in event: if not isinstance(event['resource'], basestring): event['resource'] = '' else: event['resource'] = ensure_unicode(event['resource']) if not event['resource']: del event['resource'] # Clean tags field event['tags'] = event.get('tags', []) tags = event['tags'] if isinstance(tags, basestring) and tags != "": event['tags'] = [event['tags']] elif not isinstance(tags, list): event['tags'] = [] event["timestamp"] = int(event.get("timestamp", time())) event["state"] = event.get("state", 0) event["state_type"] = event.get("state_type", 1) event["event_type"] = event.get("event_type", "check") default_status = 0 if not event["state"] else 1 event["status"] = event.get("status", default_status) event['output'] = event.get('output', '') # Get perfdata perf_data = event.get('perf_data') perf_data_array = event.get('perf_data_array') if perf_data_array is None: perf_data_array = [] # Parse perfdata if perf_data: self.logger.debug(u' + perf_data: {0}'.format(perf_data)) try: perf_data_array += PerfDataParser(perf_data).perf_data_array except Exception as err: self.logger.error( "Impossible to parse perfdata from: {0} ({1})".format( event, err ) ) event['perf_data_array'] = perf_data_array return event
def work(self, body, msg, *args, **kargs): ## Sanity Checks rk = msg.delivery_info['routing_key'] if not rk: raise Exception("Invalid routing-key '%s' (%s)" % (rk, body)) #self.logger.info( body ) ## Try to decode event if isinstance(body, dict): event = body # force utf8 only if python version is 2 if PYVER < '3': event = forceUTF8(event) else: self.logger.debug(" + Decode JSON") try: if isinstance(body, basestring): try: event = loads(body) self.logger.debug(" + Ok") except Exception as err: try: self.logger.debug(" + Try hack for windows string") # Hack for windows FS -_- event = loads(body.replace('\\', '\\\\')) self.logger.debug(" + Ok") except Exception as err: try: self.logger.debug(" + Decode BSON") bson = BSON(body) event = bson.decode() self.logger.debug(" + Ok") except Exception as err: raise Exception(err) except Exception as err: self.logger.error(" + Failed (%s)" % err) self.logger.debug("RK: '%s', Body:" % rk) self.logger.debug(body) raise Exception("Impossible to parse event '%s'" % rk) event['rk'] = ensure_unicode(rk) if event.get("resource", ''): if not isinstance(event['resource'], basestring): event['resource'] = '' else: event['resource'] = ensure_unicode(event['resource']) if not event['resource']: del event['resource'] # Clean tags field event['tags'] = event.get('tags', []) tags = event['tags'] if isinstance(tags, basestring) and tags != "": event['tags'] = [event['tags']] elif not isinstance(tags, list): event['tags'] = [] event['timestamp'] = int(event.get('timestamp', time())) try: event['state'] = int(event.get('state', 0)) except (ValueError, TypeError, AttributeError): self.logger.error( 'state convertion failed: must be an int ({})'.format(event)) event['state'] = 0 event['state_type'] = event.get('state_type', 1) event['event_type'] = event.get('event_type', 'check') default_status = 0 if not event["state"] else 1 event['status'] = event.get("status", default_status) event['output'] = event.get('output', '') # Get perfdata perf_data = event.get('perf_data') perf_data_array = event.get('perf_data_array') if perf_data_array is None: perf_data_array = [] # Parse perfdata if perf_data: self.logger.debug(u' + perf_data: {0}'.format(perf_data)) try: perf_data_array += PerfDataParser(perf_data).perf_data_array except Exception as err: self.logger.error( "Impossible to parse perfdata from: {0} ({1})".format( event, err)) event['perf_data_array'] = perf_data_array return event
def register_helper(self, name, handler): name = ensure_unicode(name) _compiler._pybars_['helpers'][name] = handler