2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2016-08-09 21:30:05 -04:00
|
|
|
|
2017-09-01 05:05:53 -04:00
|
|
|
import argparse
|
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
|
|
|
|
import subprocess
|
2021-05-28 05:00:04 -04:00
|
|
|
import sys
|
2017-09-01 05:14:32 -04:00
|
|
|
import tempfile
|
2016-08-09 21:30:05 -04:00
|
|
|
import traceback
|
|
|
|
|
2020-04-18 18:23:39 -04:00
|
|
|
# Use the Zulip virtualenv if available
|
|
|
|
sys.path.append("/home/zulip/deployments/current")
|
2016-08-09 21:30:05 -04:00
|
|
|
try:
|
2020-04-18 18:23:39 -04:00
|
|
|
from scripts.lib.setup_path import setup_path
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:23:39 -04:00
|
|
|
setup_path()
|
2016-08-09 21:30:05 -04:00
|
|
|
except ImportError:
|
2020-04-18 18:23:39 -04:00
|
|
|
try:
|
|
|
|
import scripts.lib.setup_path_on_import
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:23:39 -04:00
|
|
|
scripts.lib.setup_path_on_import # Suppress unused import warning
|
|
|
|
except ImportError:
|
|
|
|
pass
|
2016-08-09 21:30:05 -04:00
|
|
|
|
2013-12-16 16:37:42 -05:00
|
|
|
import json
|
|
|
|
|
2017-06-01 20:57:42 -04:00
|
|
|
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../"))
|
2017-03-03 13:01:52 -05:00
|
|
|
from typing import List
|
2013-12-16 16:37:42 -05:00
|
|
|
|
2021-05-28 05:00:04 -04:00
|
|
|
import zulip
|
|
|
|
|
2017-09-01 05:14:32 -04:00
|
|
|
temp_dir = "/var/tmp/" if os.name == "posix" else tempfile.gettempdir()
|
2013-12-16 16:37:42 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def mkdir_p(path: 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
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def send_log_zulip(file_name: str, count: int, lines: List[str], extra: str = "") -> None:
|
2021-05-28 07:19:40 -04:00
|
|
|
content = "{} new errors{}:\n```\n{}\n```".format(count, extra, "\n".join(lines))
|
2021-05-28 05:03:46 -04:00
|
|
|
zulip_client.send_message(
|
|
|
|
{
|
|
|
|
"type": "stream",
|
|
|
|
"to": "logs",
|
2021-05-28 07:19:40 -04:00
|
|
|
"subject": f"{file_name} on {platform.node()}",
|
2021-05-28 05:03:46 -04:00
|
|
|
"content": content,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2013-12-16 16:37:42 -05:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def process_lines(raw_lines: List[str], file_name: str) -> None:
|
2013-12-16 16:37:42 -05:00
|
|
|
lines = []
|
|
|
|
for line in raw_lines:
|
|
|
|
# Add any filtering or modification code here
|
2020-04-18 06:37:30 -04:00
|
|
|
if re.match(r".*upstream timed out.*while reading upstream.*", line):
|
2014-01-16 16:51:16 -05:00
|
|
|
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)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def process_logs() -> None:
|
2019-06-12 01:10:05 -04:00
|
|
|
data_file_path = os.path.join(temp_dir, "log2zulip.state")
|
2013-12-16 16:37:42 -05:00
|
|
|
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("{}")
|
2017-09-01 04:40:26 -04:00
|
|
|
last_data = json.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
|
2021-05-28 07:19:40 -04:00
|
|
|
print(f"Log file does not exist or could not stat log file: {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
|
2021-05-28 07:19:40 -04:00
|
|
|
output = subprocess.check_output(["tail", "-n+{}".format(file_data["last"]), log_file])
|
2021-05-28 05:05:11 -04:00
|
|
|
new_lines = output.decode("utf-8", errors="replace").split("\n")[:-1]
|
2013-12-16 16:37:42 -05:00
|
|
|
if len(new_lines) > 0:
|
2019-06-12 01:10:05 -04:00
|
|
|
process_lines(new_lines, log_file)
|
2013-12-16 16:37:42 -05:00
|
|
|
file_data["last"] += len(new_lines)
|
|
|
|
new_data[log_file] = file_data
|
2017-09-01 04:40:26 -04:00
|
|
|
open(data_file_path, "w").write(json.dumps(new_data))
|
2013-12-16 16:37:42 -05:00
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2013-12-16 16:37:42 -05:00
|
|
|
if __name__ == "__main__":
|
2017-10-11 18:17:10 -04:00
|
|
|
parser = zulip.add_default_arguments(argparse.ArgumentParser()) # type: argparse.ArgumentParser
|
2017-09-01 05:05:53 -04:00
|
|
|
parser.add_argument("--control-path", default="/etc/log2zulip.conf")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2017-09-01 05:14:32 -04:00
|
|
|
lock_path = os.path.join(temp_dir, "log2zulip.lock")
|
2013-12-16 16:37:42 -05:00
|
|
|
if os.path.exists(lock_path):
|
2017-09-15 10:02:47 -04:00
|
|
|
# This locking code is here to protect against log2zulip,
|
|
|
|
# running in a cron job, ending up with multiple copies
|
|
|
|
# running at the same time.
|
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)
|
|
|
|
|
2017-09-15 10:02:47 -04:00
|
|
|
# TODO: Convert this locking code to use a standard context manager.
|
2013-12-16 16:37:42 -05:00
|
|
|
try:
|
2016-03-10 12:18:37 -05:00
|
|
|
open(lock_path, "w").write("1")
|
2017-09-01 05:05:53 -04:00
|
|
|
zulip_client = zulip.init_from_options(args)
|
2013-12-16 16:37:42 -05:00
|
|
|
try:
|
2020-04-09 20:14:01 -04:00
|
|
|
log_files = json.loads(open(args.control_path).read())
|
2021-03-04 18:09:27 -05:00
|
|
|
except (json.JSONDecodeError, OSError):
|
2021-05-28 07:19:40 -04:00
|
|
|
print(f"Could not load control data from {args.control_path}")
|
2013-12-16 16:37:42 -05:00
|
|
|
traceback.print_exc()
|
|
|
|
sys.exit(1)
|
|
|
|
process_logs()
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
os.remove(lock_path)
|
2020-04-18 19:36:24 -04:00
|
|
|
except OSError:
|
2013-12-16 16:37:42 -05:00
|
|
|
pass
|