-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotlog_alarm.py
132 lines (100 loc) · 5.13 KB
/
botlog_alarm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import time
import json
from logwatcher import *
from slacker import Slacker
import ConfigParser
import io
Config = ConfigParser.ConfigParser()
Config.read('botlog.ini')
LogDir = Config.get('General', 'LogDir')
BotLogFile = Config.get('AlarmBot', 'BotLogFile')
SlackToken = Config.get('Slack', 'APIToken')
SlackSecurityChannel = Config.get('Slack', 'Channel')
SlackUser = Config.get('AlarmBot', 'SlackUser')
SlackAlarmChannel = Config.get('AlarmBot', 'SlackChannel')
DEBUG = Config.getboolean('General', 'Debug')
BotList = Config.items('Bots')
BotDict = {}
for tup in BotList:
key = tup[0]
BotDict[key] = tup[1]
# -----------------------------------------------------------------
tail_text = '%s monitor online. Most recent messages below:\n' % (SlackUser)
def sendMsg(channel, timestamp, color, fallback, text):
if timestamp == 'now':
timestamp = time.strftime('%B %d %H:%M:%S %Z', time.localtime())
attachments = []
attachment_data = { 'fallback': fallback, 'color': color, 'text': text, 'mrkdwn_in':['text', 'pretext'] }
attachments.append(attachment_data)
slack.chat.post_message(channel, '_' + timestamp + '_', username=SlackUser, attachments=json.dumps(attachments))
if DEBUG:
print fallback
print text
def callback(filename, lines, tailing):
global tail_text
if DEBUG:
print 'callback ' + filename + ' is tailing ' + str(tailing)
if filename == BotLogFile:
if tailing and (lines == None):
# finished tailing
# send a startup message
sendMsg(SlackAlarmChannel, 'now', '#439FE0', '%s monitor online' % (SlackUser), tail_text)
sendMsg(SlackSecurityChannel, 'now', '#439FE0', '%s monitor online' % SlackUser, '%s monitor online' % SlackUser)
return
for line in lines:
# Mar 16 12:33:54 alarmdecoder gunicorn[517]: --------------------------------------------------------------------------------
# Mar 16 12:33:54 alarmdecoder gunicorn[517]: DEBUG in types [/opt/alarmdecoder-webapp/ad2web/notifications/types.py:260]:
# Mar 16 12:33:54 alarmdecoder gunicorn[517]: Event: Zone <unnamed> (8) has been faulted.
# Mar 16 12:33:54 alarmdecoder gunicorn[517]: --------------------------------------------------------------------------------
# Mar 16 12:33:57 alarmdecoder gunicorn[517]: --------------------------------------------------------------------------------
# Mar 16 12:33:57 alarmdecoder gunicorn[517]: DEBUG in types [/opt/alarmdecoder-webapp/ad2web/notifications/types.py:260]:
# Mar 16 12:33:57 alarmdecoder gunicorn[517]: Event: Zone <unnamed> (8) has been restored.
# Mar 16 12:33:57 alarmdecoder gunicorn[517]: --------------------------------------------------------------------------------
# 0 1 2 3 4 5 ...
try:
fields = line.split()
if DEBUG:
print len(fields)
print fields
month = fields[0]
day = fields[1]
timehms = fields[2]
timeparsed = time.strptime(month + ' ' + day + ' ' + timehms, '%b %d %H:%M:%S')
timestamp = time.strftime('%B %d %H:%M:%S %Z', timeparsed)
host = fields[3]
if host in BotDict:
location = BotDict[host]
if (len(fields) >= 5) and (fields[5] == "Event:"):
desc = ' '.join(fields[6:])
fallback_msg = timestamp + ' ' + desc
full_msg = '*' + desc + '*'
color = '#000000'
if 'faulted' in desc:
color = 'warning'
elif 'restored' in desc:
color = 'good'
if tailing:
tail_text += '> _' + timestamp + '_ ' + full_msg + '\n'
else:
sendMsg(SlackAlarmChannel, timestamp, color, fallback_msg, full_msg)
if 'has been armed' in desc or 'has been disarmed' in desc:
sendMsg(SlackSecurityChannel, timestamp, color, fallback_msg, full_msg)
else:
# random messages that are not parseable
pass
else:
fallback_msg = 'Unknown bot logging to syslog.'
full_msg = '*Unknown bot* logging to syslog.'
if tailing:
tail_text += '> _' + timestamp + '_ ' + full_msg + '\n'
else:
sendMsg(SlackAlarmChannel, timestamp, 'warning', fallback_msg, full_msg)
except:
if DEBUG:
print "parse error:"
print line
# init slack
slack = Slacker(SlackToken)
# start the log watcher
watcher = LogWatcher('/var/log/', callback, tail_lines=20)
watcher.loop()