2016-04-07 09:03:22 -04:00
|
|
|
#!/usr/bin/env python
|
2016-03-10 11:15:34 -05:00
|
|
|
from __future__ import print_function
|
2016-08-09 21:30:05 -04:00
|
|
|
|
|
|
|
import errno
|
2013-12-16 16:37:42 -05:00
|
|
|
import os
|
2016-08-09 21:30:05 -04:00
|
|
|
import platform
|
|
|
|
import re
|
2013-12-16 16:37:42 -05:00
|
|
|
import sys
|
|
|
|
import shutil
|
2016-08-09 21:30:05 -04:00
|
|
|
import subprocess
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Use the Zulip virtualenv if available
|
2017-06-01 20:57:42 -04:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "../../.."))
|
2016-08-09 21:30:05 -04:00
|
|
|
import scripts.lib.setup_path_on_import
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2013-12-16 16:37:42 -05:00
|
|
|
import json
|
|
|
|
import ujson
|
|
|
|
|
2017-06-01 20:57:42 -04:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../"))
|
2013-12-16 16:37:42 -05:00
|
|
|
import zulip
|
2017-03-03 13:01:52 -05:00
|
|
|
from typing import List
|
2013-12-16 16:37:42 -05:00
|
|
|
|
|
|
|
lock_path = "/var/tmp/log2zulip.lock"
|
|
|
|
control_path = "/etc/log2zulip.conf"
|
|
|
|
|
|
|
|
def mkdir_p(path):
|
2016-10-16 01:34:14 -04:00
|
|
|
# type: (str) -> None
|
2013-12-16 16:37:42 -05:00
|
|
|
# Python doesn't have an analog to `mkdir -p` < Python 3.2.
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
2016-03-10 07:53:26 -05:00
|
|
|
except OSError as e:
|
2013-12-16 16:37:42 -05:00
|
|
|
if e.errno == errno.EEXIST and os.path.isdir(path):
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
|
|
|
def send_log_zulip(file_name, count, lines, extra=""):
|
2016-10-16 01:34:14 -04:00
|
|
|
# type: (str, int, List[str], str) -> None
|
2013-12-16 16:37:42 -05:00
|
|
|
content = "%s new errors%s:\n```\n%s\n```" % (count, extra, "\n".join(lines))
|
|
|
|
zulip_client.send_message({
|
|
|
|
"type": "stream",
|
|
|
|
"to": "logs",
|
|
|
|
"subject": "%s on %s" % (file_name, platform.node()),
|
|
|
|
"content": content,
|
2017-01-24 00:34:26 -05:00
|
|
|
})
|
2013-12-16 16:37:42 -05:00
|
|
|
|
|
|
|
def process_lines(raw_lines, file_name):
|
2016-10-16 01:34:14 -04:00
|
|
|
# type: (List[str], str) -> None
|
2013-12-16 16:37:42 -05:00
|
|
|
lines = []
|
|
|
|
for line in raw_lines:
|
|
|
|
# Add any filtering or modification code here
|
2014-01-16 16:51:16 -05:00
|
|
|
if re.match(".*upstream timed out.*while reading upstream.*", line):
|
|
|
|
continue
|
2013-12-16 16:37:42 -05:00
|
|
|
lines.append(line)
|
|
|
|
|
2014-01-16 18:36:19 -05:00
|
|
|
if len(lines) == 0:
|
|
|
|
return
|
|
|
|
elif len(lines) > 10:
|
2013-12-16 16:37:42 -05:00
|
|
|
send_log_zulip(file_name, len(lines), lines[0:3], extra=", examples include")
|
|
|
|
else:
|
|
|
|
send_log_zulip(file_name, len(lines), lines)
|
|
|
|
|
|
|
|
def process_logs():
|
2016-10-16 01:34:14 -04:00
|
|
|
# type: () -> None
|
2013-12-16 16:37:42 -05:00
|
|
|
for filename in log_files:
|
|
|
|
data_file_path = "/var/tmp/log2zulip.state"
|
|
|
|
mkdir_p(os.path.dirname(data_file_path))
|
|
|
|
if not os.path.exists(data_file_path):
|
2016-03-10 12:18:37 -05:00
|
|
|
open(data_file_path, "w").write("{}")
|
|
|
|
last_data = ujson.loads(open(data_file_path).read())
|
2013-12-16 16:37:42 -05:00
|
|
|
new_data = {}
|
|
|
|
for log_file in log_files:
|
|
|
|
file_data = last_data.get(log_file, {})
|
|
|
|
if not os.path.exists(log_file):
|
|
|
|
# If the file doesn't exist, log an error and then move on to the next file
|
2016-03-10 11:15:34 -05:00
|
|
|
print("Log file does not exist or could not stat log file: %s" % (log_file,))
|
2013-12-16 16:37:42 -05:00
|
|
|
continue
|
|
|
|
length = int(subprocess.check_output(["wc", "-l", log_file]).split()[0])
|
|
|
|
if file_data.get("last") is None:
|
|
|
|
file_data["last"] = 1
|
|
|
|
if length + 1 < file_data["last"]:
|
|
|
|
# The log file was rotated, restart from empty. Note that
|
|
|
|
# because we don't actually store the log file content, if
|
|
|
|
# a log file ends up at the same line length as before
|
|
|
|
# immediately after rotation, this tool won't notice.
|
|
|
|
file_data["last"] = 1
|
|
|
|
new_lines = subprocess.check_output(["tail", "-n+%s" % (file_data["last"],), log_file]).split('\n')[:-1]
|
|
|
|
if len(new_lines) > 0:
|
|
|
|
process_lines(new_lines, filename)
|
|
|
|
file_data["last"] += len(new_lines)
|
|
|
|
new_data[log_file] = file_data
|
2016-03-10 12:18:37 -05:00
|
|
|
open(data_file_path, "w").write(ujson.dumps(new_data))
|
2013-12-16 16:37:42 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if os.path.exists(lock_path):
|
2016-03-10 11:15:34 -05:00
|
|
|
print("Log2zulip lock held; not doing anything")
|
2013-12-16 16:37:42 -05:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
try:
|
2016-03-10 12:18:37 -05:00
|
|
|
open(lock_path, "w").write("1")
|
2013-12-16 16:37:42 -05:00
|
|
|
zulip_client = zulip.Client(config_file="/etc/log2zulip.zuliprc")
|
|
|
|
try:
|
2016-03-10 12:18:37 -05:00
|
|
|
log_files = ujson.loads(open(control_path, "r").read())
|
2013-12-16 16:37:42 -05:00
|
|
|
except Exception:
|
2016-03-10 11:15:34 -05:00
|
|
|
print("Could not load control data from %s" % (control_path,))
|
2013-12-16 16:37:42 -05:00
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(1)
|
|
|
|
process_logs()
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(lock_path)
|
2016-03-10 07:53:26 -05:00
|
|
|
except OSError as IOError:
|
2013-12-16 16:37:42 -05:00
|
|
|
pass
|