def send_message(self, message): for msg in message['messages']: payload = { 'channel': message['channel'], 'text': msg, 'username': '******', 'icon_emoji': ':sos:', 'link_names': 1, 'attachments': [{"text": message['attachment'], "color": "danger"}] } if message['webhook']: webhook = IncomingWebhook(url=message['webhook']) webhook.post(data=payload) if message['token']: slack = Slacker(token=message['token']) slack.chat.post_message(**payload)
def send_batch_notification(self): for msg in self.batchmsgs: hostname, processname, from_state, eventname = msg.rsplit(';') payload = { 'channel': self.channel, 'username': self.username, 'icon_emoji': self.icon, 'attachments': [{ 'fallback': msg, "color": self.EVENTS_SLACK_COLORS[eventname], "pretext": "Supervisor event", 'fields': [ { "title": "Hostname", "value": hostname, "short": True, }, { "title": "Process", "value": processname, "short": True, }, { "title": "From state", "value": from_state, "short": True, }, { "title": "Event", "value": eventname, "short": True, }, ] }] } if self.webhook: webhook = IncomingWebhook(url=self.webhook) webhook.post(data=payload) elif self.token: slack = Slacker(token=self.token) slack.chat.post_message(**payload)
def send_message(self, message): for msg in message['messages']: payload = { 'channel': message['channel'], 'text': msg, 'username': '******', 'icon_emoji': ':sos:', 'link_names': 1, 'attachments': [{ "text": message['attachment'], "color": "danger" }] } if message['webhook']: webhook = IncomingWebhook(url=message['webhook']) webhook.post(data=payload) if message['token']: slack = Slacker(token=message['token']) slack.chat.post_message(**payload)
class Slack(object): __slacker = None __data = {} __propeties = ('username', 'icon_emoji', 'channel', 'text', 'attachments') def __init__(self, url, timeout=DEFAULT_TIMEOUT): self.__slacker = IncomingWebhook(url, timeout) self.__initial_data() def post(self): self.__generate_data() self.__slacker.post(self.__data) def __generate_data(self): for prop in self.__propeties: if getattr(self, prop) is not None: self.__data[prop] = getattr(self, prop) def __initial_data(self): for prop in self.__propeties: setattr(self, prop, None)
class Slack(object): __slacker = None __data = {} __propeties = ( 'username', 'icon_emoji', 'channel', 'text', 'attachments' ) def __init__(self, url, timeout=DEFAULT_TIMEOUT): self.__slacker = IncomingWebhook(url, timeout) self.__initial_data() def post(self): self.__generate_data() self.__slacker.post(self.__data) def __generate_data(self): for prop in self.__propeties: if getattr(self, prop) is not None: self.__data[prop] = getattr(self, prop) def __initial_data(self): for prop in self.__propeties: setattr(self, prop, None)
def __init__(self, url, timeout=DEFAULT_TIMEOUT): self.__slacker = IncomingWebhook(url, timeout) self.__initial_data()
def send_slack_msg(tattle_props): slack_cfg = cfg["slack"] slack_webhook_url = slack_cfg["webhook_url"] slack_channel = slack_cfg["channel"] slack_username = slack_cfg["username"] slack_icon_url = slack_cfg["icon_url"] slack_icon_emoji = slack_cfg["icon_emoji"] pipedream_cfg = cfg["pipedream"] pipedream_webhook_url = pipedream_cfg["webhook_url"] tattle_id = tattle_props["id"] tattle_timestamp = tattle_props["timestamp"] tattle_recording_url = tattle_props["rec_url"] tattle_recording_public_url = tattle_props["rec_public_url"] tattle_recording_duration = tattle_props["rec_duration"] tattle_transcript = tattle_props["transcript"] tattle_sentiment = tattle_props["sentiment"] tattle_key_phrases = tattle_props["key_phrases"] pipedream_msg = {} #good, warning, danger color = "good" if tattle_sentiment: if tattle_sentiment == "MIXED": tattle_sentiment = "Mixed" color = "warning" elif tattle_sentiment == "POSITIVE": tattle_sentiment = "Positive" color = "good" elif tattle_sentiment == "NEUTRAL": tattle_sentiment = "Neutral" color = "good" elif tattle_sentiment == "NEGATIVE": tattle_sentiment = "Negative" color = "danger" attachments = [{}] attachment = attachments[0] attachment["mrkdwn_in"] = ["text", "pretext"] attachment["pretext"] = ":heavy_minus_sign: New " + str( int(round( tattle_recording_duration))) + "s tattle @ " + tattle_timestamp attachment["fallback"] = attachment["pretext"] attachment["color"] = color pipedream_msg["timestamp"] = tattle_timestamp pipedream_msg["duration"] = round(tattle_recording_duration, 2) pipedream_msg["tattle_url_private"] = tattle_recording_url pipedream_msg["tattle_url_public"] = tattle_recording_public_url text_arr = [] url_link = "<" + tattle_recording_url + "|Private link (No expiration)>" if tattle_recording_public_url and len(tattle_recording_public_url) > 0: url_link = url_link + " - " + "<" + tattle_recording_public_url + "|Public link (7 day expiration)>" text_arr.append(url_link) if tattle_sentiment and len(tattle_sentiment) > 0: text_arr.append("*Sentiment* " + tattle_sentiment) pipedream_msg["sentiment"] = tattle_sentiment if tattle_key_phrases and len(tattle_key_phrases) > 0: text_arr.append("*Key Phrases* " + ", ".join(tattle_key_phrases)) pipedream_msg["key_phrases"] = tattle_key_phrases if tattle_transcript and len(tattle_transcript) > 0: if len(tattle_transcript) > 2000: tattle_transcript = tattle_transcript[0:2000] text_arr.append("*Transcript* " + tattle_transcript) pipedream_msg["transcript"] = tattle_transcript if not text_arr == None and len(text_arr) > 0: text = "\n".join(text_arr) attachment["text"] = text #attachment["footer"] = "My footer" attachments_json = json.dumps(attachments, sort_keys=True, indent=4) slack_msg = {} if not slack_channel == None and len(slack_channel) > 0: slack_msg["channel"] = slack_channel if not slack_icon_url == None and len(slack_icon_url) > 0: slack_msg["icon_url"] = slack_icon_url if not slack_icon_emoji == None and len(slack_icon_emoji) > 0: slack_msg["icon_emoji"] = slack_icon_emoji if not slack_username == None and len(slack_username) > 0: slack_msg["username"] = slack_username slack_msg["attachments"] = attachments try: logger.info("Slack WebHook postMessage json:\n" + json.dumps(slack_msg, sort_keys=True, indent=4)) webHook = IncomingWebhook(slack_webhook_url) webHookRsp = webHook.post(slack_msg) logger.info("Slack WebHook postMessage response: " + webHookRsp.text) if not webHookRsp.ok: logger.error("Slack WebHook message send failed: " + webHookRsp.text) except Exception as e: logger.exception("Slack WebHook message send error: " + str(e)) try: logger.info("Pipedream WebHook post json:\n" + json.dumps(pipedream_msg, sort_keys=True, indent=4)) webHookRsp = requests.post( pipedream_webhook_url, data=json.dumps(pipedream_msg), headers={'Content-Type': 'application/json'}) logger.info("Pipedream WebHook post response: " + webHookRsp.text) if not webHookRsp.ok: logger.error("Pipedream WebHook message send failed: " + webHookRsp.text) except Exception as e: logger.exception("Pipedream WebHook message send error: " + str(e))
try: s.connect((ip, int(port))) s.shutdown(socket.SHUT_RDWR) return True except: return False finally: s.close() conn = sqlite3.connect('hosts.sqlite3') cursor = conn.cursor() cursor.execute('''CREATE TABLE IF NOT EXISTS dead_hosts (host TEXT PRIMARY KEY)''') conn.commit() hosts = get_hosts() slack = IncomingWebhook(url=os.environ['webhook_url']) for host in hosts: cursor.execute("SELECT host FROM dead_hosts WHERE host='{}'".format(host)) if tcp_open(host): if cursor.fetchone() is not None: cursor.execute("DELETE FROM dead_hosts WHERE host='{}'".format(host)) conn.commit() slack.post({ "attachments": [{ "author_name": host, "title": ":relieved: 서비스 정상화", }] }) elif cursor.fetchone() is None : cursor.execute("INSERT INTO dead_hosts (host) VALUES ('{}')".format(host))
from elasticsearch import Elasticsearch from slacker import IncomingWebhook import datetime # Settings link = "http://your-kibana.com/kibana/app/kibana#/doc/logstash-*/%s/%s?id=%s" # Init clients client = Elasticsearch(hosts=['http://127.0.0.1:9200']) slack = IncomingWebhook(url='https://hooks.slack.com/services/XXX/YYY/ZZZ') # Read the black list black_list = [line.strip() for line in open("black.list", 'r')] black_list = [line for line in black_list if line] #print(black_list) query = {"match_all": {}} if black_list: exclude_data = {"must_not": []} for error in black_list: exclude_data.get('must_not').append({ "query_string": { "default_field": "message", "minimum_should_match": "100%", "query": error } }) query = {"bool": exclude_data} #print(query)
from elasticsearch import Elasticsearch from slacker import IncomingWebhook import datetime # Settings link = "http://your-kibana.com/kibana/app/kibana#/doc/logstash-*/%s/%s?id=%s" # Init clients client = Elasticsearch( hosts=['http://127.0.0.1:9200'] ) slack = IncomingWebhook(url='https://hooks.slack.com/services/XXX/YYY/ZZZ') # Read the black list black_list = [line.strip() for line in open("black.list", 'r')] black_list = [line for line in black_list if line] #print(black_list) query = {"match_all": {}} if black_list: exclude_data = {"must_not": []} for error in black_list: exclude_data.get('must_not').append({ "query_string": { "default_field": "message", "minimum_should_match": "100%",