Skip to content

Instantly share code, notes, and snippets.

@AlexeyDemidov
Created July 5, 2011 07:41
Show Gist options
  • Save AlexeyDemidov/1064420 to your computer and use it in GitHub Desktop.
Save AlexeyDemidov/1064420 to your computer and use it in GitHub Desktop.
python sample
#!/usr/local/bin/python
import radix
import re
import time
import sys
def printf(format, *args):
sys.stdout.write(format % args)
def get_account( ip ):
node = atree.search_best( ip )
if node :
return node.data['site']
def load_snetnum( today ):
atree = radix.Radix()
site = {}
input = open('Snetnum', 'r') # FIXME hardcoded, no error handling
for line in input :
if re.compile('^\s*(#.*)?$').match( line ) :
site = {}
continue
match = re.compile('^\s*(\w+):\s*([\S]+)\s*$').match( line )
if match:
site[match.group(1)] = match.group(2)
if site.has_key('site') and site.has_key('inetnum') :
if site.has_key('closedate') and int(site['closedate']) < int(today) :
site = {}
continue
if site.has_key('opendate') and site['opendate'] > today :
site = {}
continue
rnode = atree.add( site['inetnum'])
rnode.data['site'] = site['site']
return atree
def load_ipservices( today ) :
stree = radix.Radix()
input = open('ipservices', 'r') # FIXME hardcoded filename and no error handling
try:
for line in input :
if re.compile('^\s*(#.*)?$').match( line ) :
service = {}
continue
match = re.compile('^\s*(\w+):\s*([\S]+)\s*$').match( line )
if match:
service[match.group(1)] = match.group(2)
if service.has_key('name') and service.has_key('dst') and service.has_key('src') :
if service.has_key('closedate') and int(service['closedate']) < int(today) :
service = {}
continue
if service.has_key('opendate') and service['opendate'] > today :
service = {}
continue
rnode = stree.search_exact( service['src'] )
if rnode :
snode = rnode.data['tree'].add(service['dst'])
snode.data['service'] = service['name']
else :
rnode = stree.add(service['src'])
subtree = radix.Radix()
rnode.data['tree'] = subtree
snode = subtree.add(service['dst'])
snode.data['service'] = service['name']
finally:
input.close()
return stree
# main
today = time.strftime('%Y%m%d')
atree = load_snetnum( today )
stree = load_ipservices( today )
if 0 :
nodes = stree.nodes()
for rnode in nodes:
stree = rnode.data["tree"]
snodes = stree.nodes()
for snode in snodes :
print snode.data["service"], rnode.prefix, snode.prefix
input = open('ipacct.txt', 'r'); # FIXME hardcoded file name and no error handling
pattern = re.compile('^([\d\.]+)\s+([\d\.]+)\s+(\d+)\s+(\d+)\s*$')
db = {}
usage = {}
while 1:
line = input.readline()
if not line:
break
m = pattern.match(line)
if not m:
continue
chunk = m.group(1) + ':' + m.group(2)
amount = long(m.group(4))
if db.has_key(chunk) :
db[chunk] += amount
else :
db[chunk] = amount
for chunk in db :
amount = db[chunk]
# if amount < 1000 :
# continue
[src, dst] = re.split( ':', chunk )
service = stree.search_best( src ).data['tree'].search_best( dst ).data['service']
account = get_account( dst )
if not account :
service = 'samara-out' # FIXME hardcoded
account = get_account( src )
if not account :
account = 'unknown'
if not account in usage :
usage[account] = { }
usage[account][service] = usage[account].get( service, 0 ) + amount
if 0 :
print '%(service)s %(account)s %(src)s -> %(dst)s %(amount)d' % \
{ 'service' : service , \
'account' : account, \
'src' : src, \
'dst' : dst, \
'amount' : db[chunk] }
for account in usage.keys() :
for service in usage[account].keys() :
print '%(account)s %(service)s %(amount)d' % \
{ 'account': account, 'service' : service, 'amount' : usage[account][service] }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment