rda-python-metrics 1.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rda-python-metrics might be problematic. Click here for more details.
- rda_python_metrics/PgIPInfo.py +188 -0
- rda_python_metrics/PgView.py +782 -0
- rda_python_metrics/__init__.py +1 -0
- rda_python_metrics/fillawsusage.py +282 -0
- rda_python_metrics/fillawsusage.usg +17 -0
- rda_python_metrics/fillcodusage.py +247 -0
- rda_python_metrics/fillcodusage.usg +21 -0
- rda_python_metrics/fillcountry.py +79 -0
- rda_python_metrics/fillendtime.py +93 -0
- rda_python_metrics/fillglobususage.py +287 -0
- rda_python_metrics/fillglobususage.usg +17 -0
- rda_python_metrics/fillipinfo.py +185 -0
- rda_python_metrics/fillipinfo.usg +18 -0
- rda_python_metrics/filloneorder.py +155 -0
- rda_python_metrics/filloneorder.usg +41 -0
- rda_python_metrics/fillrdadb.py +151 -0
- rda_python_metrics/fillrdadb.usg +32 -0
- rda_python_metrics/filltdsusage.py +289 -0
- rda_python_metrics/filltdsusage.usg +17 -0
- rda_python_metrics/filluser.py +216 -0
- rda_python_metrics/filluser.usg +16 -0
- rda_python_metrics/logarch.py +359 -0
- rda_python_metrics/logarch.usg +27 -0
- rda_python_metrics/pgperson.py +72 -0
- rda_python_metrics/pgusername.py +50 -0
- rda_python_metrics/viewallusage.py +350 -0
- rda_python_metrics/viewallusage.usg +198 -0
- rda_python_metrics/viewcheckusage.py +289 -0
- rda_python_metrics/viewcheckusage.usg +185 -0
- rda_python_metrics/viewcodusage.py +314 -0
- rda_python_metrics/viewcodusage.usg +184 -0
- rda_python_metrics/viewordusage.py +340 -0
- rda_python_metrics/viewordusage.usg +224 -0
- rda_python_metrics/viewrqstusage.py +362 -0
- rda_python_metrics/viewrqstusage.usg +217 -0
- rda_python_metrics/viewtdsusage.py +323 -0
- rda_python_metrics/viewtdsusage.usg +191 -0
- rda_python_metrics/viewwebfile.py +294 -0
- rda_python_metrics/viewwebfile.usg +212 -0
- rda_python_metrics/viewwebusage.py +371 -0
- rda_python_metrics/viewwebusage.usg +211 -0
- rda_python_metrics-1.0.4.dist-info/METADATA +18 -0
- rda_python_metrics-1.0.4.dist-info/RECORD +47 -0
- rda_python_metrics-1.0.4.dist-info/WHEEL +5 -0
- rda_python_metrics-1.0.4.dist-info/entry_points.txt +22 -0
- rda_python_metrics-1.0.4.dist-info/licenses/LICENSE +21 -0
- rda_python_metrics-1.0.4.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
#
|
|
2
|
+
###############################################################################
|
|
3
|
+
#
|
|
4
|
+
# Title : PgIPInfo
|
|
5
|
+
# Author : Zaihua Ji, zji@ucar.edu
|
|
6
|
+
# Date : 08/22/2023
|
|
7
|
+
# 2025-03-26 transferred to package rda_python_metrics from
|
|
8
|
+
# https://github.com/NCAR/rda-shared-library.git
|
|
9
|
+
# Purpose : python module to retrieve ip info from ipinfo
|
|
10
|
+
# or geoip2 modules
|
|
11
|
+
#
|
|
12
|
+
# Github : https://github.com/NCAR/rda-python-common.git
|
|
13
|
+
#
|
|
14
|
+
###############################################################################
|
|
15
|
+
#
|
|
16
|
+
import geoip2.database as geodb
|
|
17
|
+
import ipinfo
|
|
18
|
+
import socket
|
|
19
|
+
from rda_python_common import PgLOG
|
|
20
|
+
from rda_python_common import PgDBI
|
|
21
|
+
from rda_python_common import PgUtil
|
|
22
|
+
|
|
23
|
+
IPINFO = {
|
|
24
|
+
'TOKEN' : 'b2a67fdd1a9ba3',
|
|
25
|
+
'DBFILE' : PgLOG.PGLOG['DSSHOME'] + '/dssdb/GeoLite2-City.mmdb',
|
|
26
|
+
'CDATE' : PgUtil.curdate(),
|
|
27
|
+
'IPUPDT' : 0,
|
|
28
|
+
'IPADD' : 0
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
IPDB = None
|
|
32
|
+
G2DB = None
|
|
33
|
+
IPRECS = {}
|
|
34
|
+
COUNTRIES = {}
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# Get country token name for given two-character domain id
|
|
38
|
+
#
|
|
39
|
+
def get_country_name_code(dm):
|
|
40
|
+
|
|
41
|
+
if dm not in COUNTRIES:
|
|
42
|
+
pgrec = PgDBI.pgget('countries', 'token', "domain_id = '{}'".format(dm))
|
|
43
|
+
COUNTRIES[dm] = pgrec['token'] if pgrec else 'Unknown'
|
|
44
|
+
return COUNTRIES[dm]
|
|
45
|
+
|
|
46
|
+
def get_country_record_code(cname, kname = None):
|
|
47
|
+
|
|
48
|
+
name = cname[kname] if kname else cname
|
|
49
|
+
name = name.replace(' ', '.').upper() if name else 'UNITED.STATES'
|
|
50
|
+
if name == 'CHINA': name = 'P.R.CHINA'
|
|
51
|
+
|
|
52
|
+
return name
|
|
53
|
+
|
|
54
|
+
def set_ipinfo_database():
|
|
55
|
+
|
|
56
|
+
global IPDB
|
|
57
|
+
try:
|
|
58
|
+
IPDB = ipinfo.getHandler(IPINFO['TOKEN'])
|
|
59
|
+
except Exception as e:
|
|
60
|
+
PgLOG.pglog('ipinfo: ' + str(e), PgLOG.LGEREX)
|
|
61
|
+
|
|
62
|
+
#
|
|
63
|
+
# get a ipinfo record for given ip address
|
|
64
|
+
#
|
|
65
|
+
def get_ipinfo_record(ip):
|
|
66
|
+
|
|
67
|
+
if not IPDB: set_ipinfo_database()
|
|
68
|
+
try:
|
|
69
|
+
iprec = IPDB.getDetails(ip).all
|
|
70
|
+
if 'hostname' not in iprec:
|
|
71
|
+
PgLOG.pglog("ipinfo: {} - ip address is not in the database".format(ip), PgLOG.LOGERR)
|
|
72
|
+
return None
|
|
73
|
+
except Exception as e:
|
|
74
|
+
PgLOG.pglog("ipinfo: {} - {}".format(ip, str(e)), PgLOG.LOGWRN)
|
|
75
|
+
return None
|
|
76
|
+
|
|
77
|
+
record = {'ip' : ip, 'stat_flag' : 'A', 'hostname' : ip}
|
|
78
|
+
if 'hostname' in iprec:
|
|
79
|
+
record['hostname'] = iprec['hostname']
|
|
80
|
+
record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
|
|
81
|
+
record['lat'] = float(iprec['latitude']) if iprec['latitude'] else 0
|
|
82
|
+
record['lon'] = float(iprec['longitude']) if iprec['longitude'] else 0
|
|
83
|
+
if 'org' in iprec: record['org_name'] = iprec['org']
|
|
84
|
+
record['country'] = get_country_record_code(iprec, 'country_name')
|
|
85
|
+
if 'city' in iprec: record['city'] = PgLOG.convert_chars(iprec['city'])
|
|
86
|
+
if 'postal' in iprec: record['postal'] = iprec['postal']
|
|
87
|
+
record['timezone'] = iprec['timezone']
|
|
88
|
+
|
|
89
|
+
return record
|
|
90
|
+
|
|
91
|
+
def set_geoip2_database():
|
|
92
|
+
|
|
93
|
+
global G2DB
|
|
94
|
+
try:
|
|
95
|
+
G2DB = geodb.Reader(IPINFO['DBFILE'])
|
|
96
|
+
except Exception as e:
|
|
97
|
+
PgLOG.pglog("geoip2: " + str(e), PgLOG.LGEREX)
|
|
98
|
+
|
|
99
|
+
#
|
|
100
|
+
# get a geoip2 record for given ip address
|
|
101
|
+
#
|
|
102
|
+
def get_geoip2_record(ip):
|
|
103
|
+
|
|
104
|
+
if not G2DB: set_geoip2_database()
|
|
105
|
+
try:
|
|
106
|
+
city = G2DB.city(ip)
|
|
107
|
+
except Exception as e:
|
|
108
|
+
PgLOG.pglog("geoip2: {} - {}".format(ip, str(e)), PgLOG.LOGWRN)
|
|
109
|
+
return None
|
|
110
|
+
|
|
111
|
+
record = {'ip' : ip, 'stat_flag' : 'M'}
|
|
112
|
+
record['lat'] = float(city.location.latitude) if city.location.latitude else 0
|
|
113
|
+
record['lon'] = float(city.location.longitude) if city.location.longitude else 0
|
|
114
|
+
record['country'] = get_country_name_code(city.country.name)
|
|
115
|
+
record['city'] = PgLOG.convert_chars(city.city.name)
|
|
116
|
+
record['postal'] = city.postal.code
|
|
117
|
+
record['timezone'] = city.location.time_zone
|
|
118
|
+
record['hostname'] = ip
|
|
119
|
+
record['org_type'] = '-'
|
|
120
|
+
|
|
121
|
+
try:
|
|
122
|
+
hostrec = socket.gethostbyaddr(ip)
|
|
123
|
+
except Exception as e:
|
|
124
|
+
PgLOG.pglog("socket: {} - {}".format(ip, str(e)), PgLOG.LOGWRN)
|
|
125
|
+
return record
|
|
126
|
+
record['hostname'] = hostrec[1][0] if hostrec[1] else hostrec[0]
|
|
127
|
+
record['org_type'] = PgDBI.get_org_type(None, record['hostname'])
|
|
128
|
+
|
|
129
|
+
return record
|
|
130
|
+
|
|
131
|
+
#
|
|
132
|
+
# update wuser.email for hostname changed
|
|
133
|
+
#
|
|
134
|
+
def update_wuser_email(nhost, ohost):
|
|
135
|
+
|
|
136
|
+
pgrec = PgDBI.pgget('wuser', 'widx', "email = 'unknown@{}'".format(ohost))
|
|
137
|
+
if pgrec: PgDBI.pgexec("UPDATE wuser SET email = 'unknown@{}' WHERE widx = {}".format(nhost, pgrec['widx']))
|
|
138
|
+
|
|
139
|
+
#
|
|
140
|
+
# update a ipinfo record; add a new one if not exists yet
|
|
141
|
+
#
|
|
142
|
+
def update_ipinfo_record(record, pgrec = None):
|
|
143
|
+
|
|
144
|
+
tname = 'ipinfo'
|
|
145
|
+
cnd = "ip = '{}'".format(record['ip'])
|
|
146
|
+
if not pgrec: pgrec = PgDBI.pgget(tname, '*', cnd)
|
|
147
|
+
if pgrec:
|
|
148
|
+
nrec = get_update_record(record, pgrec)
|
|
149
|
+
if 'hostname' in nrec: update_wuser_email(nrec['hostname'], pgrec['hostname'])
|
|
150
|
+
ret = PgDBI.pgupdt(tname, nrec, cnd) if nrec else 0
|
|
151
|
+
IPINFO['IPUPDT'] += ret
|
|
152
|
+
else:
|
|
153
|
+
record['adddate'] = IPINFO['CDATE']
|
|
154
|
+
ret = PgDBI.pgadd(tname, record)
|
|
155
|
+
IPINFO['IPADD'] += ret
|
|
156
|
+
|
|
157
|
+
return ret
|
|
158
|
+
|
|
159
|
+
#
|
|
160
|
+
# set ip info into table ipinfo from python module ipinfo
|
|
161
|
+
# if ipopt is True; otherwise, use module geoip2
|
|
162
|
+
#
|
|
163
|
+
def set_ipinfo(ip, ipopt = False):
|
|
164
|
+
|
|
165
|
+
if ip in IPRECS:
|
|
166
|
+
pgrec = IPRECS[ip]
|
|
167
|
+
if not pgrec or not ipopt or pgrec['stat_flag'] == 'A': return pgrec
|
|
168
|
+
else:
|
|
169
|
+
pgrec = PgDBI.pgget('ipinfo', '*', "ip = '{}'".format(ip))
|
|
170
|
+
|
|
171
|
+
if not pgrec or ipopt and pgrec['stat_flag'] == 'M':
|
|
172
|
+
record = None if ipopt else get_geoip2_record(ip)
|
|
173
|
+
if not (record and 'hostname' in record): record = get_ipinfo_record(ip)
|
|
174
|
+
if record and update_ipinfo_record(record, pgrec): pgrec = record
|
|
175
|
+
|
|
176
|
+
IPRECS[ip] = pgrec
|
|
177
|
+
return pgrec
|
|
178
|
+
|
|
179
|
+
#
|
|
180
|
+
# compare and return a new record holding fields with different values only
|
|
181
|
+
#
|
|
182
|
+
def get_update_record(nrec, orec):
|
|
183
|
+
|
|
184
|
+
record = {}
|
|
185
|
+
for fld in nrec:
|
|
186
|
+
if nrec[fld] != orec[fld]:
|
|
187
|
+
record[fld] = nrec[fld]
|
|
188
|
+
return record
|