#!/usr/bin/env python3
"""
File: jsonFetcher.py
Author: Dennis Spera
Date: 2024-06-24
Description: Extract json from mongod.log on stdin based on date ranges.
"""
import sys
import json as j
from datetime import datetime
import re as regex
from commandlines import Command as cmd
b = str()
e = str()
start_d_t_fmt = None
end_d_t_fmt = None
max_input_length = 14
min_input_length = 8
def is_json(json):
try:
j.loads(json)
except ValueError:
return False
return True
try:
c = cmd()
try:
b = c.get_definition('b')
if len(b) > max_input_length:
sys.stderr.write('beginning timestamp exceeds beginning timesamp length of ' + str(max_input_length))
sys.exit(0)
if len(b) < min_input_length:
sys.stderr.write('beginning timestamp less than beginning timesamp length of ' + str(min_input_length))
sys.exit(0)
try:
test = int(b)
except:
sys.stderr.write('beginning timestamp is an invalid data format')
sys.exit(0)
padding = max_input_length - len(b)
if padding > 0:
for i in range(1, padding+1, 1):
b=b+'0'
e = c.get_definition('e')
if len(e) > max_input_length:
sys.stderr.write('ending timestamp exceeds ending timesamp length of ' + str(max_input_length))
sys.exit(0)
if len(b) < min_input_length:
sys.stderr.write('ending timestamp less than ending timesamp length of ' + str(min_input_length))
sys.exit(0)
try:
test = int(e)
except:
sys.stderr.write('end timestamp is an invalid data format')
sys.exit(0)
padding = max_input_length - len(e)
if padding > 0:
for i in range(1, padding+1, 1):
e=e+'0'
except:
sys.stderr.write('error parsing input parameters')
sys.exit(0)
for line in sys.stdin:
if is_json(line):
d = j.loads(line)
dateT = d["t"]["$date"]
match_object = regex.match( r'(\d{4})([-])(\d{2})([-])(\d{2})([T])(\d{2})([:])(\d{2})([:])(\d{2})(.*)', dateT)
d_t_fmt = datetime.strptime(match_object.group(1)+match_object.group(3)\
+match_object.group(5)\
+match_object.group(7)+match_object.group(9)
+match_object.group(11), '%Y%m%d%H%M%S')
match_object = regex.match( r'(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})', b)
try:
start_d_t_fmt = datetime.strptime(match_object.group(1)+match_object.group(2)\
+match_object.group(3)\
+match_object.group(4)+match_object.group(5)
+match_object.group(6), '%Y%m%d%H%M%S')
except:
sys.stderr.write('error converting start date-time to a datatime format')
match_object = regex.match( r'(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})', e)
try:
end_d_t_fmt = datetime.strptime(match_object.group(1)+match_object.group(2)\
+match_object.group(3)\
+match_object.group(4)+match_object.group(5)
+match_object.group(6), '%Y%m%d%H%M%S')
except:
sys.stderr.write('error converting end date-time to a datatime format')
if (start_d_t_fmt = d_t_fmt) and not regex.match(r'^$', line):
print(line,end="")
sys.stdin.close()
except OSError as err:
print('!!! ',err)
Похоже, что все мои исследования указывают на именование каталогов в Windows, хотя я не пишу ни в какие файлы
Я помещаю несколько файлов журналов в скрипт Python, который затем передаю в голову следующим образом: [code]cat *mongodb.log.* | python jsonFetcher.py -b 20240101000000 -e 20250101000000 | head [/code] вывод следующий: в конце я получаю ошибку. Я не получаю сообщение об ошибке при подключении к другому скрипту Python: [code]{"t":{"$date":"2024-06-09T02:47:32.053+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"192.168.240.198:57790","uuid":"8e485191-5c63-457e-b341-14515b593ef1","connectionId":207933,"connectionCount":773}} {"t":{"$date":"2024-06-09T02:47:32.084+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn207932","msg":"client metadata","attr":{"remote":"192.168.240.198:57776","client":"conn207932","negotiatedCompressors":[],"doc":{"application":{"name":"MongoDB Automation Agent v13.16.2.8826 (git: 36d72fa13b663f402e1285ab8458536766897530)"},"driver":{"name":"mongo-go-driver","version":"v1.12.0-cloud"},"os":{"type":"linux","architecture":"arm64"},"platform":"go1.21.10"}}} {"t":{"$date":"2024-06-09T02:47:32.085+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"192.168.240.198:57794","uuid":"3d76d2c2-2298-4fc8-80b6-e029d0d1f933","connectionId":207934,"connectionCount":774}} Exception ignored in: OSError: [Errno 22] Invalid argument [/code] Вот скрипт, отправляющий в голову свои входные данные: [code]#!/usr/bin/env python3
"""
File: jsonFetcher.py Author: Dennis Spera Date: 2024-06-24 Description: Extract json from mongod.log on stdin based on date ranges.
"""
import sys import json as j from datetime import datetime import re as regex from commandlines import Command as cmd
b = str() e = str() start_d_t_fmt = None end_d_t_fmt = None max_input_length = 14 min_input_length = 8
try: b = c.get_definition('b') if len(b) > max_input_length: sys.stderr.write('beginning timestamp exceeds beginning timesamp length of ' + str(max_input_length)) sys.exit(0) if len(b) < min_input_length: sys.stderr.write('beginning timestamp less than beginning timesamp length of ' + str(min_input_length)) sys.exit(0)
try: test = int(b) except: sys.stderr.write('beginning timestamp is an invalid data format') sys.exit(0)
padding = max_input_length - len(b)
if padding > 0: for i in range(1, padding+1, 1): b=b+'0'
e = c.get_definition('e') if len(e) > max_input_length: sys.stderr.write('ending timestamp exceeds ending timesamp length of ' + str(max_input_length)) sys.exit(0) if len(b) < min_input_length: sys.stderr.write('ending timestamp less than ending timesamp length of ' + str(min_input_length)) sys.exit(0)
try: test = int(e) except: sys.stderr.write('end timestamp is an invalid data format') sys.exit(0)
padding = max_input_length - len(e)
if padding > 0: for i in range(1, padding+1, 1): e=e+'0'
try: end_d_t_fmt = datetime.strptime(match_object.group(1)+match_object.group(2)\ +match_object.group(3)\ +match_object.group(4)+match_object.group(5) +match_object.group(6), '%Y%m%d%H%M%S') except: sys.stderr.write('error converting end date-time to a datatime format')
if (start_d_t_fmt = d_t_fmt) and not regex.match(r'^$', line): print(line,end="")
sys.stdin.close()
except OSError as err: print('!!! ',err) [/code] Похоже, что все мои исследования указывают на именование каталогов в Windows, хотя я не пишу ни в какие файлы
Я помещаю несколько файлов журналов в скрипт Python, который затем передаю в голову следующим образом:
cat *mongodb.log.* | python jsonFetcher.py -b 20240101000000 -e 20250101000000 | head
вывод следующий: в конце я получаю ошибку. Я не получаю...
Currently, I have a strange error in the command line Mode=TwoWay and Mode=OneWay , I have used these commands a lot and when I updated visual studio 2022, this error started appearing, I tried it but don't understand why. When this error occurs,...
Currently, I have a strange error in the command line Mode=TwoWay and Mode=OneWay , I
have used these commands a lot and when I updated visual studio 2022, this error started appearing, I tried it but don't understand why. When this error occurs,...
В контексте разработки мобильных приложений новостей выбор дизайна, такого как Dark Mode и Light Mode, может значительно повлиять на пользовательский опыт. Некоторые читатели предпочитают темный режим для уменьшения напряжения глаз и ночного чтения,...