Last active
February 9, 2022 20:40
-
-
Save mustakimali/2122997aff4e82a6991dec204376eb3d to your computer and use it in GitHub Desktop.
Gets logs from nginx-ingress container and generate GoAccess dashboard for each of the services + all combined (Blog: https://mustak.im/server-side-kubernetes-nginx-ingress-log-analysis-using-goaccess/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
import subprocess | |
def process_log_for_svc(svc,out): | |
print('Processing ' + svc) | |
os.system("rm -f /storage/goaccess/imported-logs/imported-log.log") | |
os.system('find /var/log/containers/ | grep nginx-ingress | xargs sudo cat | grep ' + svc + ' >> /storage/goaccess/imported-logs/imported-log.log') | |
print("Parsing...") | |
os.system('goaccess -f /storage/goaccess/imported-logs/imported-log.log --real-os --log-format="%^ %^ [%h] - - [%d:%t] %~ %~ %m %U %^ %s %b %R %u %^ %^ %^ %^ %^ %T %^" --date-format="%d/%b/%Y" --time-format="%H:%M:%S +0000" > /storage/goaccess/out/' + out + '.html') | |
print("Cleaning...") | |
os.system("rm /storage/goaccess/imported-logs/imported-log.log") | |
print('Getting all services') | |
all_svc=os.popen('kubectl get svc | tail -n +2 | awk \'{print $1}\'').read() | |
all_svc_arr = all_svc.split('\n') | |
print('Creating index.html') | |
index_html='' | |
for svc in all_svc_arr: | |
index_html += '<a href=\"' + svc + '.html\">' + svc + '</a><br/>' | |
index_html = '<a href=\"all.html\">-ALL-</a><br/>' + index_html | |
text_file = open("/storage/goaccess/out/index.html", "w") | |
text_file.write(index_html) | |
text_file.close() | |
print('Processing All Logs') | |
process_log_for_svc('Mozilla','all') | |
for svc in all_svc_arr: | |
process_log_for_svc(svc,svc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment