понедельник, 10 марта 2014 г.

Кол-во активных подключений к серверу с определением региона (GeoIP)

#!/usr/bin/python
# 'apt-get install python-geoip' if needed
import GeoIP,os

stream = os.popen("netstat -tna | awk -F' +|:' '/:80/ {print $6}' | sort | uniq -c | sort -n ")
gi = GeoIP.new(GeoIP.GEOIP_MEMORY_CACHE)
s=0
for line in stream.readlines():
    line = line.strip()
    l =line.split() # strip the last from the line
#    print l
    print l[0], "\t",l[1],"\t", gi.country_code_by_addr(l[1])
    s+=int(l[0])

print 'Total:',s