/
collect.py
105 lines (84 loc) · 2.71 KB
/
collect.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
# -*- coding: utf-8 -*-
import sys
import json
import time
import uuid
import socket
import getpass
import argparse
import datetime
import platform
import socklusion
DEFAULT_HOST = '127.0.0.1'
DEFAULT_PORT = 8888
DEFAULT_PATH = '/v1/on_import'
TIMEOUT = 5.0
INSTANCE_ID = uuid.uuid4()
IS_64BIT = sys.maxsize > 2 ** 32
HAVE_READLINE = True
try:
import readline
except:
HAVE_READLINE = False
HAVE_UCS4 = getattr(sys, 'maxunicode', 0) > 65536
TIME_INFO = {'utc_epoch': str(datetime.datetime.utcnow()),
'std_utc_offset': -time.timezone / 3600.0}
def get_python_info():
ret = {}
ret['argv'] = ' \t '.join(sys.argv)
ret['bin'] = sys.executable
ret['is_64bit'] = IS_64BIT
try:
ret['version'] = sys.version.split()[0]
except:
ret['version'] = ''
ret['version_full'] = sys.version
ret['compiler'] = platform.python_compiler()
ret['build_date'] = platform.python_build()[1]
ret['version_info'] = list(sys.version_info)
ret['have_ucs4'] = HAVE_UCS4
ret['have_readline'] = HAVE_READLINE
return ret
def get_all_info():
ret = {}
ret['username'] = getpass.getuser()
ret['uuid'] = str(INSTANCE_ID)
ret['hostname'] = socket.gethostname()
ret['hostfqdn'] = socket.getfqdn()
ret['uname'] = platform.uname()
ret['python'] = get_python_info()
ret['time'] = TIME_INFO
return ret
def build_post_message(data, host=DEFAULT_HOST, path=DEFAULT_PATH):
msg_lines = ['POST %s HTTP/1.0' % path,
'Host: %s' % host,
'Content-Length: ' + str(len(data)),
'',
data]
msg = '\r\n'.join(msg_lines)
return msg
def send_import_analytics(host=DEFAULT_HOST, port=DEFAULT_PORT, data_dict=None,
timeout=TIMEOUT, path=DEFAULT_PATH):
if data_dict is None:
data_dict = get_all_info()
msg = build_post_message(json.dumps(data_dict), host=host, path=path)
return socklusion.send_data(msg,
host=host,
port=port,
wrap_ssl=False,
timeout=timeout,
want_response=True)
def main():
prs = argparse.ArgumentParser()
prs.add_argument('--host', default=DEFAULT_HOST)
prs.add_argument('--port', default=DEFAULT_PORT, type=int)
prs.add_argument('--path', default=DEFAULT_PATH)
prs.add_argument('--verbose', action='store_true')
args = prs.parse_args()
output = send_import_analytics(host=args.host,
port=args.port,
path=args.path)
if args.verbose:
print output
if __name__ == '__main__':
main()