-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathGetipinfo.py
98 lines (81 loc) · 2.21 KB
/
Getipinfo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python3
import sys
#import geoip2.webservice
print ('*************************************')
print (sys.argv[1])
#print str(sys.argv[1])
import geoip2.database
import socket
print(socket.gethostname())
reader = geoip2.database.Reader('/GeoLite2-City.mmdb')
response = reader.city(str(sys.argv[1]))
Lat = response.location.latitude
ISO = response.country.iso_code
Long = response.location.longitude
State = response.subdivisions.most_specific.name
City = response.city.name
Country = response.country.name
Zip = response.postal.code
IP = str(sys.argv[1])
Domain = str(sys.argv[2])
duration = int(sys.argv[3])
print (Country)
print (State)
print (City)
print (Zip)
print (Long)
print (Lat)
print (ISO)
print (IP)
reader.close()
import datetime
from influxdb import InfluxDBClient
## get env vars and use
import os
# influx configuration - edit these
npmhome = "/root/.config/NPMGRAF"
npmhome = os.getenv('NPMGRAF_HOME')
ifuser = os.getenv('INFLUX_USER')
ifpass = os.getenv('INFLUX_PW')
ifdb = os.getenv('INFLUX_DB')
ifhost = os.getenv('INFLUX_HOST')
ifport = os.getenv('INFLUX_PORT')
hostname = socket.gethostname()
measurement_name = ("ReverseProxyConnections")
print (measurement_name)
print ('*************************************')
# take a timestamp for this measurement
time = datetime.datetime.utcnow()
# format the data as a single measurement for influx
body = [
{
"measurement": measurement_name,
"time": time,
"tags": {
"key": ISO,
"latitude": Lat,
"longitude": Long,
"Domain": Domain,
"City": City,
"State": State,
"name": Country,
"IP": IP
},
"fields": {
"Domain": Domain,
"latitude": Lat,
"longitude": Long,
"State": State,
"City": City,
"key": ISO,
"IP": IP,
"name": Country,
"duration": duration,
"metric": 1
}
}
]
# connect to influx
ifclient = InfluxDBClient(ifhost,ifport,ifuser,ifpass,ifdb)
# write the measurement
ifclient.write_points(body)