Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
[unfold.git] / activity / __init__.py
1 #
2 # Activity monitor
3 #
4
5 import urllib, urllib2
6 import threading
7 from datetime import datetime
8
9
10 def logWrite(request, action, message):
11     url = "http://localhost:5000/log"
12     log = {
13         "date" : datetime.today(),
14         "client_ip" : getClientIp(request),
15         "host" : request.get_host(),
16         "referrer" : request.META.get('HTTP_REFERER'),
17         "user" : request.user
18     }
19     
20     try :
21         result = urllib2.urlopen(url, urllib.urlencode(log))
22         content = result.read()
23     except urllib2.URLError as e:
24         print "Error: connection to " + url + " impossible, logging disabled"
25
26 def spawnThread(request, action, message):
27     print "aaaaaaaaa"
28     # Create a new thread in Daemon mode to send the log entry
29     t = threading.Thread(target=logWrite, args=(request, action, message))
30     t.setDaemon(True)
31     t.start()
32
33 def userLogin(request):
34     spawnThread(request, 'userlogin', 'User logged in')
35
36 def userLogout(request):
37     spawnThread(request, 'userlogout', 'User logged out')
38
39 def userRegistration(request):
40     spawnThread(request, 'userregistration', 'User registered')
41
42 def userSliceRequest(request):
43     spawnThread(request, 'userslicerequest', 'User requested a slice')
44
45 def userContactSupport(request):
46     spawnThread(request, 'usercontactsupport', 'User contacted suppport')
47
48 def userAddResource(request):
49     spawnThread(request, 'useraddresource', 'User added resource to slice')
50
51 def userDelResource(request):
52     spawnThread(request, 'userdelresource', 'User removed resource from slice')
53
54
55 def getClientIp(request):
56     x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
57     if x_forwarded_for:
58         ip = x_forwarded_for.split(',')[0]
59     else:
60         ip = request.META.get('REMOTE_ADDR')
61     return ip