NewsBlur-viq/utils/zgrep.py

124 lines
3.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2024-04-24 09:50:42 -04:00
import json
import os
2021-07-09 12:31:52 -04:00
import re
import select
import subprocess
import sys
2024-04-24 09:50:42 -04:00
import time
2021-07-09 12:31:52 -04:00
from pprint import pprint
2024-04-24 09:50:42 -04:00
2014-11-11 22:45:32 -08:00
from requests.exceptions import ConnectionError
2024-04-24 09:43:56 -04:00
sys.path.insert(0, "/srv/newsblur")
os.environ["DJANGO_SETTINGS_MODULE"] = "newsblur_web.settings"
2024-04-24 09:43:56 -04:00
NEWSBLUR_USERNAME = "nb"
IGNORE_HOSTS = [
2024-04-24 09:43:56 -04:00
"app-push",
]
2024-04-24 09:43:56 -04:00
2021-07-09 12:31:52 -04:00
def main(role, find):
2014-11-11 22:45:32 -08:00
delay = 1
2024-04-24 09:43:56 -04:00
hosts = subprocess.check_output(["ansible-inventory", "--list"])
2021-07-09 12:31:52 -04:00
if not hosts:
print(" ***> Could not load ansible-inventory!")
return
hosts = json.loads(hosts)
path = "/srv/newsblur/logs/newsblur.log*"
2024-04-24 09:43:56 -04:00
command = 'zgrep "%s" %s' % (find, path)
2021-07-09 12:31:52 -04:00
# if exclude:
# command += " | zgrep -v \"%s\"" % exclude
print(f" ---> {command}")
2014-11-11 22:45:32 -08:00
while True:
try:
2021-07-09 12:31:52 -04:00
streams = create_streams_for_role(hosts, role, command=command)
print(" --- Loading %s %s Log Tails ---" % (len(streams), role))
2014-11-11 22:45:32 -08:00
read_streams(streams)
2021-07-09 12:31:52 -04:00
# except UnicodeDecodeError: # unexpected end of data
# print " --- Lost connections - Retrying... ---"
# time.sleep(1)
# continue
except ConnectionError:
print(" --- Retrying in %s seconds... ---" % delay)
time.sleep(delay)
delay += 1
continue
except KeyboardInterrupt:
2020-06-19 02:27:48 -04:00
print(" --- End of Logging ---")
2014-11-11 22:45:32 -08:00
break
2024-04-24 09:43:56 -04:00
2021-07-09 12:31:52 -04:00
def create_streams_for_role(hosts, role, command):
streams = list()
2014-11-11 22:45:32 -08:00
found = set()
2021-07-09 12:31:52 -04:00
if role in hosts:
2024-04-24 09:43:56 -04:00
for hostname in hosts[role]["hosts"]:
if any(h in hostname for h in IGNORE_HOSTS) and role != "push":
continue
2021-07-09 12:31:52 -04:00
follow_host(hosts, streams, found, hostname, command)
else:
host = role
follow_host(hosts, streams, found, host, command)
2014-11-11 22:45:32 -08:00
return streams
2024-04-24 09:43:56 -04:00
2021-07-09 12:31:52 -04:00
def follow_host(hosts, streams, found, hostname, command=None):
if isinstance(hostname, dict):
2024-04-24 09:43:56 -04:00
address = hostname["address"]
hostname = hostname["name"]
elif ":" in hostname:
hostname, address = hostname.split(":", 1)
2021-07-09 12:31:52 -04:00
elif isinstance(hostname, tuple):
hostname, address = hostname[0], hostname[1]
else:
2024-04-24 09:43:56 -04:00
address = hosts["_meta"]["hostvars"][hostname]["ansible_host"]
2021-07-09 12:31:52 -04:00
print(" ---> Following %s \t[%s]" % (hostname, address))
2024-04-24 09:43:56 -04:00
if hostname in found:
return
s = subprocess.Popen(
[
"ssh",
"-l",
NEWSBLUR_USERNAME,
"-i",
os.path.expanduser("/srv/secrets-newsblur/keys/docker.key"),
address,
command,
],
stdout=subprocess.PIPE,
)
2021-07-09 12:31:52 -04:00
s.name = hostname
streams.append(s)
found.add(hostname)
2024-04-24 09:43:56 -04:00
2014-11-11 22:45:32 -08:00
def read_streams(streams):
while True:
2024-04-24 09:43:56 -04:00
r, _, _ = select.select([stream.stdout.fileno() for stream in streams], [], [])
2014-11-11 22:45:32 -08:00
for fileno in r:
for stream in streams:
if stream.stdout.fileno() != fileno:
continue
data = os.read(fileno, 4096)
if not data:
streams.remove(stream)
break
2021-07-09 12:31:52 -04:00
try:
combination_message = "[%-13s] %s" % (stream.name[:13], data.decode())
except UnicodeDecodeError:
continue
2014-11-11 22:45:32 -08:00
sys.stdout.write(combination_message)
2021-07-09 12:31:52 -04:00
sys.stdout.flush()
break
2024-04-24 09:43:56 -04:00
if __name__ == "__main__":
2021-07-09 12:31:52 -04:00
main(*sys.argv[1:])