Files
links/scripts/analyze_ips.py
T
2026-04-01 21:33:07 +11:00

28 lines
1.1 KiB
Python

import django, os, sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'core.settings'
sys.path.insert(0, '/Users/junv/code/links')
django.setup()
from nginxmon.models import NginxAccessLog
from django.db.models import Count
from django.utils import timezone
from datetime import timedelta
since = timezone.now() - timedelta(hours=24)
ips = [
"210.231.178.225","139.162.37.203","20.116.48.67","4.204.200.32",
"139.162.37.41","115.171.57.239","20.151.201.236","74.248.155.72",
"130.12.98.27","112.1.105.92","20.220.232.240","172.190.142.176",
]
for ip in ips:
paths = list(NginxAccessLog.objects.filter(remote_addr=ip, timestamp__gte=since)
.values("request_uri","status").annotate(cnt=Count("id")).order_by("-cnt")[:5])
statuses = list(NginxAccessLog.objects.filter(remote_addr=ip, timestamp__gte=since)
.values("status").annotate(cnt=Count("id")).order_by("-cnt"))
ss = ", ".join(str(r["status"])+"("+str(r["cnt"])+")" for r in statuses)
print("-- "+ip+" "+ss)
for p in paths:
print(" ["+str(p["status"])+"] "+str(p["cnt"])+"x "+p["request_uri"][:80])
print("")