Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# -*- coding: utf-8 -*- 

 

from requests.exceptions import ConnectionError 

from tornado import gen, httpclient 

import json 

import datetime 

 

class AccessLogger(object): 

pass 

 

class AsyncAccessLogger(AccessLogger): 

pass 

 

class NullAccessLogger(AccessLogger): 

def logAccess(self, *args, **kwargs): 

pass 

 

class AsyncNullAccessLogger(AccessLogger): 

@gen.coroutine 

def logAccess(self, *args, **kwargs): 

pass 

 

class AsyncInfluxDBAccessLogger(AsyncAccessLogger): 

def __init__(self, host, port=8086, username="root", password="root", database=None, timeout=None): 

assert database is not None 

self.host = host 

self.port = port 

self.username = username 

self.password = password 

self.database = database 

self.timeout = timeout 

self.url = 'http://%s:%s@%s:%d/write?db=%s'%(self.username, 

self.password, 

self.host, 

self.port, 

self.database) 

self.client = httpclient.AsyncHTTPClient() 

@gen.coroutine 

def logAccess(self, ip, url, authok, user=None, authmode=None, oid=None, method='GET'): 

tags = {"ip": ip, "url": url, "method": method} 

if user is not None: 

tags["username"] = user 

if oid is not None: 

tags["oid"] = oid 

if authmode is not None: 

tags["authmode"] = authmode 

tagdata = ','.join(['%s=%s'%(k,v.replace(' ', r'\ ').replace(',', r'\,')) for k,v in sorted(tags.items())]) 

valuedata = 'value=%d'%(1 if authok else 0) 

body = 'url_access,%s %s\n'%(tagdata, valuedata) 

request = httpclient.HTTPRequest(self.url, 

body=body, 

method="POST") 

try: 

yield self.client.fetch(request) 

except Exception as e: 

print "WARNING: logging error: %s"%e 

 

class AsyncElasticSearchAccessLogger(AsyncAccessLogger): 

def __init__(self, host, port=9200, username="root", password="root", index=None, timeout=None): 

assert index is not None 

self.host = host 

self.port = port 

self.username = username 

self.password = password 

self.index = index 

self.timeout = timeout 

self.url = 'http://%s:%d/%s/url_access/'%(self.host, 

self.port, 

self.index) 

self.client = httpclient.AsyncHTTPClient() 

@gen.coroutine 

def logAccess(self, ip, url, authok, user=None, authmode=None, oid=None, method='GET', useragent=None): 

tags = {"ip": ip, "url": url, "method": method, "authok": authok} 

if user is not None: 

tags["username"] = user 

if oid is not None: 

tags["oid"] = oid 

if authmode is not None: 

tags["authmode"] = authmode 

if useragent is not None: 

tags["useragent"] = useragent 

now = datetime.datetime.utcnow() 

ms = int(now.microsecond / 1000) 

tags["timestamp"] = now.strftime('%Y-%m-%dT%H:%M:%S') + '.%03d'%ms 

body = json.dumps(tags) 

print body 

request = httpclient.HTTPRequest(self.url, 

body=body, 

method="POST") 

try: 

yield self.client.fetch(request) 

except Exception as e: 

print "WARNING: logging error: %s"%e 

 

class InfluxDBAccessLogger(AccessLogger): 

def __init__(self, *args, **kwargs): 

""" 

Creates an influxdb access logger, all arguments are passed to the influxdb client. 

""" 

import influxdb 

self.client = influxdb.client.InfluxDBClient(*args, **kwargs) 

def logAccess(self, ip, url, authok, user=None, authmode=None, oid=None, method='GET'): 

tags = {"ip": ip, "url": url, "method": method} 

 

if user is not None: 

tags["username"] = user 

if oid is not None: 

tags["oid"] = oid 

if authmode is not None: 

tags["authmode"] = authmode 

data = [ 

{"measurement": "url_access", 

"tags": tags, 

"fields": {"value": 1 if authok else 0}, 

} 

] 

try: 

self.client.write_points(data) 

except ConnectionError: 

pass