File size: 11,775 Bytes
8dc315b |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
"""
this script takes the raw dataset, clean it up, fill in TTL, and geenrate a cleaned dataset
"""
import os, sys
import time
import csv
import requests
import pickle, json
from inspect import currentframe, getframeinfo
from collections import defaultdict, Counter
import hashlib
import struct
import socket
# import geoip2.database
# ip_database = geoip2.database.Reader("GeoLite2-Country.mmdb")
CONTENT_TYPE_SET = set(['jpeg', 'bin', 'json', 'js', 'png', 'webp', '', 'ts', 'css', 'html', 'm3u8', 'svg', 'gif', 'empty', 'dat', 'mp4', 'xml', 'txt', 'ors', 'woff', 'bundle', 'woff2', 'tgz', 'jpg', 'cer', 'plist', 'mpga' , 'ico', 'pbf', 'ccbi', 'qt', 'ttf', 'webm', 'tar', 'zip', 'ics', 'rss', 'aac', 'pdf', 'conda', 'eot', 'debounce', 'crl', 'm4s', 'enc', 'otf', 'wav', 'm4a', 'flat', 'atlas', 'der', 'wys', 'oga', 'gz', 'manifest', 'get', 'mp3', 'php', 'swf', 'skel', 'jar', 'template', 'odft', 'bmp', 'apk', 'rpm', 'xz', 'mpd', 'io', 'btx', 'fnt', 'xhtml', 'pub', 'exe', 'unityweb', 'csv', 'mat', 'events', 'ogx', 'geo', 'docx', 'sprite', 'micro', 'mid', 'jp2', 'doc', 'identity', 'vtt', 'terrain', 'xlsx', 'atom', 'epub', 'chunk', 'si d', 'm3u', '7z', 'invert', 'findkey', 'axd', 'xls', 'db', 'mpf', 'lua', 'webvtt', 'unity3d', 'appcache', 'tiff', 'rtf', 'wasm', 'jpgx', 'gzip', 'md', 'ppt', 'lb', 'ashx', 'm4v', 'py', 'srt', 'psd', 'settings', 'dmg', 'asp', 'sh', 'aspx', 'patch', 'dib', 'mov', 'rar', 'last', 'gcfg', 'yml', 'atf', 'java', 'stl', 'chm', 'crc', 'yaml', 'ai', 'c', 'unity', 'srx', 'scss', 'includes', 'flac', 'caf', 'extra', 'xslt', 'gpg', 'rdf', 'bam', ])
EXTENSION_SET = set(['jpg', '', 'tgz', 'js', 'png', 'json', 'm3u8', 'ts', 'css', 'webp', 'jpeg', 'svg', 'php', 'gif', 'mp4', 'dat', 'woff2', 'html', 'bundle', 'woff', 'rss', 'axd', 'crt', 'plist', 'mp3', 'txt', 'ico', 'pbf', 'gz', 'mov', 'ttf', 'm4s', 'ccbi', 'bin', 'lb', 'gzip', 'aspx', 'webm', 'jpgx', 'bz2', 'xml', 'zip ', 'ics', 'aac', 'pdf', 'conda', 'asp', 'ashx', 'eot', 'do', 'htm', 'atlas', 'crl', 'id', 'otf', 'map', 'bam', 'lua', 'xsd', 'erik', 'fca', 'enc', 'rpoly', 'properties', 'wav', 'h', 'flat', 'ogg', 'wys', 'manifest', 'm4a', 'jar', 'ece', 'gmp', 'dds', 'sig', 'pk2mz', 'swf', 'debounce', 'skel', 'template', 'acz', 'pack', 'cdiff', 'cson', 'jsonp', 'hash', 'dmg', 'sml', 'apk', 'lzma', 'jsp', 'rpm', 'cdf', 'io', 'csv', 'xz', 'tga', 'mpd', 'sha256', 'fnt', 'btx', 'depmap', 'distro', 'unity3d', 'svga', 'omit', 'xhtml', 'vipr', 'uniq', 'res', 'pub', 'b3dm', 'exe', 'fit', 'ln4', 'crop', 'pairs', 'prefab', 'pl', 'mkv', 'mat', 'cef', 'wmt', 'bif', 'dxt', 'vtt', 'deb', 'lm', 'geo', 'py', 'sprite', 'vghddata', 'docx', 'jfif', 'nfs', 'js', 'gifv', 'dlt', 'mid', 'data', 'unityweb', 'cms', 'jp2', 'identity', 'gcd', 'img', 'bmp', 'doc', 'cur', 'ect', 'page', 'pic', 'db', 'mjs', 'tif', 'meta', 'image', 'faces', 'prop', 'dll', 'xlsx', 'pfx', 'box', 'ani', 'chunk', 'terrain', 'epub', '7z', 'jnx', 'midi', 'tfl', 'asr', 'act', 'xrf', 'mpf', 'ln3', 'ejs', 'lani', 'avif', 'sh', 'inc', 'vue', 'xaf', 'webvtt', 'pptx', 'aff', 'wasm', 'flow', 'jmm', 'atom', 'ovpn', 'log', 'so', 'xpf', 'xls', 'anm', 'pngx', 'cmfv', 'gaf', 'aiu', 'srt', 'hvm', 'dwg', 'yml', 'mem', 'mobile', 'cvd', '3ds', 'java', 'lmat', 'md', 'sha1', 'm4v', 'tar', 'vbs', 'msi', 'rtf', 'svgz', 'appcache', 'psd', 'tmx', 'eps', 's3d', 'vpk', 'ini', 'stl', 'link', 'shtml', 'ppt', 'pkg', 'br', 'ttc', 'patch', 'dib', 'gcfg', 'yaml', 'atf', 'app', 'heic', 'lyric', 'simple', 'rss2', 'ebr', 'unity', 'rar', 'rgx', 'obj', 'md2', 'chm', 'crc', 'trf', 'gpi', 'lib', 'jpe', 'scss', 'xsl', 'pkmlz', 'cgi', 'srx', 'mdr' 'stat', 'sqlite', 'tiff', 'flac', 'sep', 'caf', 'mps', 'tdb', 'jpeg', 'cfm', 'gpg', 'geojson', 'sql', 'scml', 'gtt', 'bat', 'c', 'xmf', 'gsm', 'fsp', 'gcode', 'gifx', 'odt', 'opus', 'rbxm', 'gl', 'apkm', 'pak', 'util', 'cr2', 'conf', 'dylib', 'dict', 'm3u', 'cer', 'cpp', 'md5', 'xlsm', 'tsx', 'javascript'])
def processing(datafile, sample_ratio=1):
ifile = open(datafile, errors="ignore")
reader = csv.reader(ifile, delimiter="\t")
ofilename = datafile
if sample_ratio > 1:
ofilename = "{}.sample{}".format(datafile, sample_ratio)
ofile = open(ofilename + ".clean", "w")
ofilebin = open(ofilename + ".cf.bin", "wb")
# ts, obj, sz, ttl, age, hostname, content (h), extension (h), n_level, n_param, method, colo
# 41 bytes
s = struct.Struct("<IQQiiihhhbbb")
ttl_dict = {}
colo = 28
content_mapping = defaultdict(int)
extension_mapping = defaultdict(int)
hostname_mapping = defaultdict(int)
content_cnt = Counter()
extension_cnt = Counter()
hostname_cnt = Counter()
method_cnt = Counter()
n_level_cnt = Counter()
n_param_cnt = Counter()
with open("ttl_dict.pickle", "rb") as f:
# for allcolo data, we do not have content_type,
# and we use convertTTLDict.py to convert ttl_dict to a different format
# with open("ttl_dict_new.pickle", "rb") as f:
ttl_dict = pickle.load(f)
n = 0
n_no_ttl, n_use_ttl_dict = 0, 0
no_age = 0
n_time_change = 0
last_ts = 0
for row in reader:
# ts country obj sz content tieredHit ttl age cstatus method zoneId
# colo28
# ts obj sz content tier ttl age cstatus method
# 1628639938 14769378147408796115 8688 jpeg 0 2666327358 39582683 miss GET s3.amazonaws.com /media.pinterest.com/150x150/3a/36/13/3a3613c5086a070f43afce13bf856b79.jpg
# all colo
# ts client colo obj sz tier cStatus
# 626591102 1433663661 87 10824946938601195600 2944141 0 hit static-v6.starmakerstudios.com /production/uploading/recordings/10977524122478104/master.mp4
n += 1
try:
ts, obj, sz, content_type, tieed_hit, ttl, age, cstatus, method, hostname, path, query_string = row
# for allcolo data, some information is missing
# ts, client, colo, obj, sz, tiered_hit, cstatus, hostname, path, query_string = row
# ttl, age, content_type, method = 86400*365*100, 86400*365*100, "", "GET"
# colo = int(colo)
# client = socket.inet_ntop(socket.AF_INET, struct.pack("!L", int(client)))
# client_country = ip_database.country(client).country.iso_code
except Exception as e:
print(e, row)
continue
if sample_ratio > 1 and int(obj) % sample_ratio != 1:
continue
ts, ttl, age = int(ts), int(ttl), int(age)
if ts < last_ts:
# ts = last_ts
n_time_change += 1
last_ts = ts
# hostname = str(int(hashlib.md5(hostname.encode()).hexdigest(), 16) % (2**30))
if ttl > 86400*365 and (hostname, content_type) in ttl_dict:
ttl = int(ttl_dict[(hostname, content_type)])
n_use_ttl_dict += 1
has_query_string = len(query_string.strip()) > 0
if has_query_string:
has_query_string = 1
else:
has_query_string = 0
n_param = query_string.count("&")+1
n_level = path.count("/") - 1
content_type = content_type.lower()
extension = path.split(".")[-1].lower()
if '=' in extension:
extension.split("=")[0]
if '%' in extension:
extension.split("%")[0]
if '@' in extension:
extension.split("@")[0]
if "-" in extension or "/" in extension or "_" in extension or len(extension) > 10 or extension == "unknown" or extension == "empty":
extension = ""
if extension.isdigit():
extension = ""
extension = extension.strip()
if content_type not in CONTENT_TYPE_SET:
content_type = ""
if extension not in EXTENSION_SET:
extension = "unknown"
# for allcolo data, we do not have content_type
# if ttl > 86400 * 365 * 30:
# if hostname in ttl_dict:
# if extension in ttl_dict[hostname]:
# ttl = int(ttl_dict[hostname][extension])
# else:
# ttl = int(list(ttl_dict[hostname].values())[0])
# n_use_ttl_dict += 1
# else:
# ttl = -1
if ttl > 86400*365*30:
n_no_ttl += 1
ttl = -1
if age > 86400*365*30:
no_age += 1
age = -1
ofile.write(",".join([str(ts), obj, str(sz), content_type, extension, str(n_level), str(ttl), str(age), cstatus, method, hostname, str(has_query_string), str(n_param)])+"\n")
if n % 20000000 == 0:
# print(sorted(content_cnt.items(), key=lambda x:x[1], reverse=True))
# print(sorted(extension_cnt.items(), key=lambda x:x[1], reverse=True))
print(sorted(method_cnt.items(), key=lambda x:x[1], reverse=True))
# print(sorted(n_level_cnt.items(), key=lambda x:x[1], reverse=True))
# print(sorted(n_param_cnt.items(), key=lambda x:x[1], reverse=True))
print("{:.0f} {} req, {} missingTTL, {} replaceTTL, {} noAge, {} timeDiff".format(time.time(), n//1e6, n_no_ttl//1e6, n_use_ttl_dict//1e6, no_age//1e6, n_time_change))
content_int = content_mapping.get(content_type, len(content_mapping)+1)
extension_int = extension_mapping.get(extension, len(extension_mapping)+1)
hostname_int = hostname_mapping.get(hostname, len(content_mapping)+1)
content_mapping[content_type] = content_int
extension_mapping[extension] = extension_int
hostname_mapping[hostname] = hostname_int
method_int = 0
if method == "GET":
method_int = 1
elif method == "PURGE":
method_int = 2
else:
print(f"unknown method {method}")
if has_query_string == 0:
n_param = 0
if n_level > 127:
n_level = 127
if n_param > 127:
n_param = 127
# ts, obj, sz, ttl, age, hostname, content (h), extension (h), n_level (b), n_param (b), method (b), colo (b)
try:
ofilebin.write(s.pack(int(ts), int(obj), int(sz), ttl, age, hostname_int, content_int, extension_int, colo, n_level, n_param, method_int))
except Exception as e:
print(e)
print(row)
print(int(ts), int(obj), int(sz), ttl, age, hostname_int, content_int, extension_int, n_level, n_param, method_int, colo)
print(",".join([str(ts), obj, str(sz), content_type, extension, str(n_level), str(ttl), str(age), cstatus, method, hostname, str(has_query_string), str(n_param)])+"\n")
content_cnt[content_type] += 1
extension_cnt[extension] += 1
hostname_cnt[hostname] += 1
method_cnt[method] += 1
n_level_cnt[n_level] += 1
n_param_cnt[n_param] += 1
ifile.close()
ofile.close()
ofilebin.close()
with open("{}_stat.json".format(ofilename), "w") as ofile:
json.dump((content_cnt, extension_cnt, hostname_cnt, method_cnt, n_level_cnt, n_param_cnt), ofile)
with open("{}_content_mapping.json".format(ofilename), "w") as ofile:
json.dump(content_mapping, ofile)
with open("{}_extension_mapping.json".format(ofilename), "w") as ofile:
json.dump(extension_mapping, ofile)
with open("{}_hostname_mapping.json".format(ofilename), "w") as ofile:
json.dump(hostname_mapping, ofile)
if __name__ == "__main__":
processing(sys.argv[1])
|