Skip to content

Instantly share code, notes, and snippets.

@gdbassett
Created August 24, 2024 17:40
Show Gist options
  • Save gdbassett/4e45cabf25e9a4e6c7950892fc0afbe4 to your computer and use it in GitHub Desktop.
Save gdbassett/4e45cabf25e9a4e6c7950892fc0afbe4 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
## logs in /userfs/WEB/logging/python.log
## start with freshwater_logs.py /userfs/WEB/logging/python.log in a separate line on the python configuration page int he DIGI web UI
# imports
import sys
try:
import os.path
except ImportError:
# os comes from python.zip on our gateways.
print """\
Unable to import 'os' module. Check that your system has a 'python.zip' file,\r
and that it is the correct one for your device.\r
"""
sys.exit(1)
### Imports from dia script. we know at least these exist
import os
import gc
import time
#import httplib
#import json
#import re
def spin_forever(python_log_file_location):
""" This routine prevents the main thread from exiting when the
framework is run directly from __main__.
"""
last_time = time.strptime("1970/01/01T00:00:00", "%Y/%m/%dT%H:%M:%S")
garbage_collect_time = 0
try:
while True:
current_year = time.strftime("%Y")
### give us a starting place for new log lines
max_log_time = last_time
if garbage_collect_time >= 60:
garbage_collect_time = 0
collected_items = gc.collect()
if collected_items:
# only print if something accomplished
print ("GarbageCollector: collected %d objects."
% collected_items)
with open(python_log_file_location, 'r') as filehandle_in:
with open("./freshwater.log", 'a') as filehandle_out:
for line in filehandle_in:
line = line.strip()
line_time = time.strptime(line[:15] + " " + current_year, "%b %d %H:%M:%S %Y")
# only parse things since the last time we parsed things
if line_time > last_time:
# keep track of the newest log we parsed
if line_time > max_log_time:
max_log_time = line_time
# if the line contains the log
if line[63:71] == "1C 29 01" or line[63:71] == "1C 01 29":
# write the full buffer and the time out
filehandle_out.write(time.strftime("%Y/%m/%dT%H:%M:%S", line_time) + ",\"" + line[63:125] + "\"\n")
#print(time.strftime("%Y/%m/%dT%H:%M:%S", line_time) + ",\"" + line[63:125] + "\"")
if len(line) > 125:
filehandle_out.write(time.strftime("%Y/%m/%dT%H:%M:%S", line_time) + ",\"" + line[126:] + "\"\n")
#print(time.strftime("%Y/%m/%dT%H:%M:%S", line_time) + ",\"" + line[126:] + "\"")
### Update the last_time to the max_log_time so that we don't repeat log lines
last_time = max_log_time
garbage_collect_time = garbage_collect_time + 1
time.sleep(60 * 5) # sleep 5 minutes
finally:
print "freshwater log parser is exiting...\n"
def main():
if sys.argv and len(sys.argv) > 1:
#Use the one supplied by the args
python_log_file_location = sys.argv[1]
else:
### testing
python_log_file_location = "./python.log"
### testing
if __name__ == "__main__":
# Don't exit. If not __main__ then the caller needs to guarantee this.
spin_forever(python_log_file_location)
return core
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment