Skip to content

Commit

Permalink
Added GeoIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Beercow authored May 17, 2018
1 parent 9de7a06 commit 04e80e3
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
13 changes: 13 additions & 0 deletions GeoIP/Linux/GeoIP.pdp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Name = GeoIP
Author = Beercow
Description = Adds geoip data to details
Version = 0.1
Type = EventHandler
Architecture = LinuxShellScript
File = GeoIP.py
Priority = 9
RunHidden = 0
RunExclusively = 1
CanOverrideOtherPlugins = 0
CanOverrideProcdot = 0
Event = AfterRefresh
60 changes: 60 additions & 0 deletions GeoIP/Linux/GeoIP.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/usr/bin/env python

import os
import pygeoip
import sys
import urllib
import gzip

reload(sys)
sys.setdefaultencoding('utf8')
out = os.getenv('PROCDOTPLUGIN_ResultTXT')

def database(url,dbgz,db):
geo = urllib.URLopener()
try:
geo.retrieve(url, dbgz)
with gzip.open(dbgz, 'rb') as infile:
with open(db, 'wb') as outfile:
for line in infile:
outfile.write(line)
os.remove(dbgz)
except Exception as e:
open(out, 'wb').write(str(e))
sys.exit(0)

if os.path.isfile('GeoLiteCity.dat'):
pass
else:
database('http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', 'GeoLiteCity.dat.gz', 'GeoLiteCity.dat')

if os.path.isfile('GeoIPASNum.dat'):
pass
else:
database('http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz', 'GeoIPASNum.dat.gz', 'GeoIPASNum.dat')

tfolder = os.listdir(os.getenv('PROCDOTPLUGIN_TempFolder'))
details = open(os.getenv('PROCDOTPLUGIN_GraphFileDetails'),'rb').readlines()
n = open(os.getenv('PROCDOTPLUGIN_GraphFileDetails'),'w')

for num, line in enumerate(details,1):
if 'IP-Address' in line:
query = pygeoip.GeoIP('GeoLiteCity.dat')
asn = pygeoip.GeoIP('GeoIPASNum.dat')
x = [x.strip() for x in line.split(' ')][2]
try:
results = query.record_by_addr(x)
asn_info = asn.asn_by_addr(x)
for key, val in results.items():
details.insert(num,str(key) + ' = ' + str(val) + '\n')

try:
details.insert(num,'asn = '+asn_info+'\n')
except:
pass
except:
pass
details = "".join(details)
n.write(details)
n.close()
sys.exit(0)
10 changes: 10 additions & 0 deletions GeoIP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# GeoIP

## Project source can be downloaded from

## Author & Contributor List
Brian Maloney

## Overview
This plugin adds geographical information to server nodes. GeoIP is an event hadler plugin so you will not see anything added to the graph or plugin menus. It is designed to run after the refresh button is clicked. The first run of GeoIP, you might have a slight delay because teh plugin needs to download the Maxmind database.

61 changes: 61 additions & 0 deletions GeoIP/Windows/GeoIP.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
@setlocal enabledelayedexpansion && C:\Python27\python.exe -x "%~f0" %* & exit /b !ERRORLEVEL!
#!/usr/bin/env python

import os
import pygeoip
import sys
import urllib
import gzip

reload(sys)
sys.setdefaultencoding('utf8')
out = os.getenv('PROCDOTPLUGIN_ResultTXT')

def database(url,dbgz,db):
geo = urllib.URLopener()
try:
geo.retrieve(url, dbgz)
with gzip.open(dbgz, 'rb') as infile:
with open(db, 'wb') as outfile:
for line in infile:
outfile.write(line)
os.remove(dbgz)
except Exception as e:
open(out, 'wb').write(str(e))
sys.exit(0)

if os.path.isfile('GeoLiteCity.dat'):
pass
else:
database('http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz', 'GeoLiteCity.dat.gz', 'GeoLiteCity.dat')

if os.path.isfile('GeoIPASNum.dat'):
pass
else:
database('http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum.dat.gz', 'GeoIPASNum.dat.gz', 'GeoIPASNum.dat')

tfolder = os.listdir(os.getenv('PROCDOTPLUGIN_TempFolder'))
details = open(os.getenv('PROCDOTPLUGIN_GraphFileDetails'),'rb').readlines()
n = open(os.getenv('PROCDOTPLUGIN_GraphFileDetails'),'w')

for num, line in enumerate(details,1):
if 'IP-Address' in line:
query = pygeoip.GeoIP('GeoLiteCity.dat')
asn = pygeoip.GeoIP('GeoIPASNum.dat')
x = [x.strip() for x in line.split(' ')][2]
try:
results = query.record_by_addr(x)
asn_info = asn.asn_by_addr(x)
for key, val in results.items():
details.insert(num,str(key) + ' = ' + str(val) + '\n')

try:
details.insert(num,'asn = '+asn_info+'\n')
except:
pass
except:
pass
details = "".join(details)
n.write(details)
n.close()
sys.exit(0)
13 changes: 13 additions & 0 deletions GeoIP/Windows/GeoIP.pdp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Name = GeoIP
Author = Beercow
Description = Adds geoip data to details
Version = 0.1
Type = EventHandler
Architecture = WindowsBatchScript
File = GeoIP.bat
Priority = 9
RunHidden = 0
RunExclusively = 1
CanOverrideOtherPlugins = 0
CanOverrideProcdot = 0
Event = AfterRefresh
29 changes: 29 additions & 0 deletions GeoIP/pyinstaller/GeoIP.spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- mode: python -*-

block_cipher = None


a = Analysis(['GeoIP.py'],
pathex=['/home/remnux/Desktop/procdot/lin64/plugins'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='GeoIP',
debug=False,
strip=False,
upx=True,
runtime_tmpdir=None,
console=False )

0 comments on commit 04e80e3

Please sign in to comment.