Skip to content

Instantly share code, notes, and snippets.

@mustakimali
Last active February 9, 2022 20:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mustakimali/2122997aff4e82a6991dec204376eb3d to your computer and use it in GitHub Desktop.
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/)
#!/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