forked from ma-karai/nginxproxymanagerGraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetipinfo.py
132 lines (111 loc) · 3.23 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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]).strip())
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]).strip()
Domain = str(sys.argv[2]).strip()
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
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
## 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')
ifbucket = os.getenv('INFLUX_BUCKET')
iforg = os.getenv('INFLUX_ORG')
iftoken = os.getenv('INFLUX_TOKEN')
print(ifbucket, iforg, ifuser, ifpass)
hostname = socket.gethostname()
measurement_name = ("ReverseProxyConnections")
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)
ifclient = influxdb_client.InfluxDBClient(
url=ifhost,
org=iforg,
username=ifuser,
password=ifpass
)
# write the measurement
write_api = ifclient.write_api(write_options=SYNCHRONOUS)
point = influxdb_client.Point(measurement_name)
point.tag("key", ISO)
point.tag("Latitude", Lat)
point.tag("Longitude", Long)
point.tag("Domain", Domain)
point.tag("City", City)
point.tag("State", State)
point.tag("Name", Country)
point.tag("IP", IP)
point.field("Domain", Domain)
point.field("Latitude", Lat)
point.field("Longitude", Long)
point.field("State", State)
point.field("City", City)
point.field("key", ISO)
point.field("IP", IP)
point.field("Name", Country)
point.field("duration", duration)
point.field("metric", 1)
write_api.write(bucket=ifbucket, org=iforg, record=point)