def executeBatch(self, notification, signal, targets): log.debug("Executing %s action for targets: %s", self.name, targets) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) if signal.clear: log.debug('This is a clearing signal.') subject = processTalSource(notification.content['clear_subject_format'], **data) body = processTalSource(notification.content['clear_body_format'], **data) else: subject = processTalSource(notification.content['subject_format'], **data) body = processTalSource(notification.content['body_format'], **data) log.debug('Sending this subject: %s' % subject) log.debug('Sending this body: %s' % body) plain_body = MIMEText(self._stripTags(body)) email_message = plain_body if notification.content['body_content_type'] == 'html': email_message = MIMEMultipart('related') email_message_alternative = MIMEMultipart('alternative') email_message_alternative.attach(plain_body) html_body = MIMEText(body.replace('\n', '<br />\n')) html_body.set_type('text/html') email_message_alternative.attach(html_body) email_message.attach(email_message_alternative) host = notification.content['host'] port = notification.content['port'] user = notification.content['user'] password = notification.content['password'] useTls = notification.content['useTls'] email_from = notification.content['email_from'] email_message['Subject'] = subject email_message['From'] = email_from email_message['To'] = ','.join(targets) email_message['Date'] = formatdate(None, True) result, errorMsg = sendEmail( email_message, host, port, useTls, user, password ) if result: log.debug("Notification '%s' sent emails to: %s", notification.id, targets) else: raise ActionExecutionException( "Notification '%s' FAILED to send emails to %s: %s" % (notification.id, targets, errorMsg) )
def execute(self, notification, signal): """ Set up the execution environment and run a CMD command. """ self.setupAction(notification.dmd) log.debug('Executing action: {0}'.format(self.name)) if signal.clear: command = notification.content['clear_wincmd_command'] else: command = notification.content['wincmd_command'] environ = self._get_environ(notification, signal) if not command: log.debug("The CMD command was not set") return if not hasattr(environ['dev'], 'windows_servername'): log.debug("The target device is non-Windows device") return try: command = processTalSource(command, **environ) except Exception: log.error('Unable to perform TALES evaluation on "{0}" ' '-- is there an unescaped $?'.format(command)) log.debug('Executing this compiled command "{0}"'.format(command)) self._execute_command(environ['dev'], command)
def executeOnTarget(self, notification, signal, targets): log.debug("Executing %s action for targets: %s", self.name, targets) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) if signal.clear: log.debug('This is a clearing signal.') subject = processTalSource(notification.content['clear_subject_format'], **data) body = processTalSource(notification.content['clear_body_format'], **data) else: subject = processTalSource(notification.content['subject_format'], **data) body = processTalSource(notification.content['body_format'], **data) log.debug('Sending this subject: %s' % subject) log.debug('Sending this body: %s' % body) email_message = body if notification.content['body_content_type'] == 'html': email_message = body.replace('\n', '<br />\n') aws_access_key = notification.content['aws_access_key'] aws_secret_key = notification.content['aws_secret_key'] aws_region = notification.content['aws_region'] email_from = notification.content['email_from'] email_format = notification.content['body_content_type'] email_subject = subject email_to = targets conn = boto.ses.connect_to_region( aws_region, aws_access_key_id=aws_access_key, aws_secret_access_key=aws_secret_key ) conn.send_email( email_from, email_subject, email_message, email_to, format=email_format ) log.debug("Notification '%s' sent emails to: %s", notification.id, targets)
def processTALES(expression, skipfails, data): if not expression: return '' if skipfails: m = re.search(TAL_RE, expression) if m: expression = "${%s | string:}" % m.groups()[0] return processTalSource(expression, skipfails, **data)
def executeOnTarget(self, notification, signal, target): self.setupAction(notification.dmd) log.debug('Executing action: %s on %s', self.name, target) if signal.clear: command = notification.content['clear_body_format'] else: command = notification.content['body_format'] log.debug('Executing this command: %s', command) actor = signal.event.occurrence[0].actor device = None if actor.element_uuid: device = self.guidManager.getObject(actor.element_uuid) component = None if actor.element_sub_uuid: component = self.guidManager.getObject(actor.element_sub_uuid) user_env_format = notification.content['user_env_format'] env = dict( envvar.split('=') for envvar in user_env_format.split(';') if '=' in envvar) environ = {'dev': device, 'component': component, 'dmd': notification.dmd, 'env': env} data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) environ.update(data) if environ.get('evt', None): environ['evt'] = self._escapeEvent(environ['evt']) if environ.get('clearEvt', None): environ['clearEvt'] = self._escapeEvent(environ['clearEvt']) environ['user'] = getattr(self.dmd.ZenUsers, target, None) try: command = processTalSource(command, **environ) except Exception: raise ActionExecutionException('Unable to perform TALES evaluation on "%s" -- is there an unescaped $?' % command) log.debug('Executing this compiled command: "%s"' % command) _protocol = EventCommandProtocol(command) log.debug('Queueing up command action process.') self.processQueue.queueProcess( '/bin/sh', ('/bin/sh', '-c', command), env=environ['env'], processProtocol=_protocol, timeout=int(notification.content['action_timeout']), timeout_callback=_protocol.timedOut )
def _processTalExpression(self, value, environ): if type(value) is str or type(value) is unicode: if '${' not in value: return value try: return processTalSource(value, **environ) except Exception: raise ActionExecutionException( 'Unable to perform TALES evaluation on "%s" -- is there an unescaped $?' % value) else: return value
def _processTalExpressions(self, data, environ): if type(data) is str or type(data) is unicode: try: return processTalSource(data, **environ) except Exception: raise ActionExecutionException( 'Unable to perform TALES evaluation on "%s" -- is there an unescaped $?' % data) elif type(data) is list: return [self._processTalExpressions(e, environ) for e in data] elif type(data) is dict: return dict([(k, self._processTalExpressions(v, environ)) for (k, v) in data.iteritems()]) else: return data
def execute(self, notification, signal): log.debug("Executing %s action", self.name) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) bot = TelegramBot(notification.content['token']) bot.update_bot_info().wait() if signal.clear: text = processTalSource(notification.content['message_clear'], **data) else: text = processTalSource(notification.content['message_body'], **data) bot.send_message(notification.content['chat_id'], text, parse_mode="Markdown").wait() print
def execute(self, notification, signal): log.debug("Executing %s action", self.name) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) message_body = processTalSource(notification.content['message_body'], **data) sendHipChat(message_body, data['evt'].severity, notification.content['hipChatUrl'], notification.content['proxyUrl'], notification.content['proxyUsername'], notification.content['proxyPassword'])
def processEventFields(self, data, content, name): log.debug('[research] process TAL expressions') try: content = processTalSource(content, **data) log.debug('[research] %s : %s' % (name, content)) except Exception: log.debug('[research] unable to process : %s' % (name)) raise ActionExecutionException( '[research] failed to process TAL in %s' % (name)) if (content == 'None'): content = '' return content
def executeBatch(self, notification, signal, targets): log.debug("Executing %s action for targets: %s", self.name, targets) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) if signal.clear: log.debug('This is a clearing signal.') subject = processTalSource( notification.content['clear_subject_format'], **data) body = processTalSource(notification.content['clear_body_format'], **data) else: subject = processTalSource(notification.content['subject_format'], **data) body = processTalSource(notification.content['body_format'], **data) log.debug('Sending this subject: %s' % subject) log.debug('Sending this body: %s' % body) #plain_body = MIMEText(self._stripTags(body)) plain_body = MIMEText(self._stripTags(body), _subtype='plain', _charset='utf-8') email_message = plain_body if notification.content['body_content_type'] == 'html': email_message = MIMEMultipart('related') email_message_alternative = MIMEMultipart('alternative') email_message_alternative.attach(plain_body) html_body = MIMEText(body.replace('\n', '<br />\n'), _subtype='html', _charset='utf-8') #html_body = MIMEText(body.replace('\n', '<br />\n')) html_body.set_type('text/html') email_message_alternative.attach(html_body) email_message.attach(email_message_alternative) # Attach image (zpdir, tail) = os.path.split(__file__) # Get path to this directory imFile = zpdir + '/imageFile0.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image0>') email_message.attach(msgImage) # Add the trailer images - see and of body_format and clear_body_format if signal.clear: imFile = zpdir + '/imageFile2.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image2>') email_message.attach(msgImage) else: imFile = zpdir + '/imageFile1.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image1>') email_message.attach(msgImage) host = notification.content['host'] port = notification.content['port'] user = notification.content['user'] password = notification.content['password'] useTls = notification.content['useTls'] email_from = notification.content['email_from'] email_message['Subject'] = subject email_message['From'] = email_from email_message['To'] = ','.join(targets) email_message['Date'] = formatdate(None, True) result, errorMsg = sendEmail(email_message, host, port, useTls, user, password) if result: log.debug("Notification '%s' sent emails to: %s", notification.id, targets) else: raise ActionExecutionException( "Notification '%s' FAILED to send emails to %s: %s" % (notification.id, targets, errorMsg))
def executeBatch(self, notification, signal, targets): log.debug("Executing %s action for targets: %s", self.name, targets) self.setupAction(notification.dmd) data = _signalToContextDict(signal, self.options.get('zopeurl'), notification, self.guidManager) if signal.clear: log.debug('This is a clearing signal.') subject = processTalSource(notification.content['clear_subject_format'], **data) body = processTalSource(notification.content['clear_body_format'], **data) else: subject = processTalSource(notification.content['subject_format'], **data) body = processTalSource(notification.content['body_format'], **data) log.debug('Sending this subject: %s' % subject) log.debug('Sending this body: %s' % body) #plain_body = MIMEText(self._stripTags(body)) plain_body = MIMEText(self._stripTags(body), _subtype='plain', _charset='utf-8') email_message = plain_body if notification.content['body_content_type'] == 'html': email_message = MIMEMultipart('related') email_message_alternative = MIMEMultipart('alternative') email_message_alternative.attach(plain_body) html_body = MIMEText(body.replace('\n', '<br />\n'), _subtype='html', _charset='utf-8') #html_body = MIMEText(body.replace('\n', '<br />\n')) html_body.set_type('text/html') email_message_alternative.attach(html_body) email_message.attach(email_message_alternative) # Attach image (zpdir, tail) = os.path.split(__file__) # Get path to this directory imFile = zpdir + '/imageFile0.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image0>') email_message.attach(msgImage) # Add the trailer images - see and of body_format and clear_body_format if signal.clear: imFile = zpdir + '/imageFile2.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image2>') email_message.attach(msgImage) else: imFile = zpdir + '/imageFile1.jpg' imageFile = open(imFile, 'rb') msgImage = MIMEImage(imageFile.read()) imageFile.close() msgImage.add_header('Content-ID', '<image1>') email_message.attach(msgImage) host = notification.content['host'] port = notification.content['port'] user = notification.content['user'] password = notification.content['password'] useTls = notification.content['useTls'] email_from = notification.content['email_from'] email_message['Subject'] = subject email_message['From'] = email_from email_message['To'] = ','.join(targets) email_message['Date'] = formatdate(None, True) result, errorMsg = sendEmail( email_message, host, port, useTls, user, password ) if result: log.debug("Notification '%s' sent emails to: %s", notification.id, targets) else: raise ActionExecutionException( "Notification '%s' FAILED to send emails to %s: %s" % (notification.id, targets, errorMsg) )