diff --git a/stats.py b/stats.py new file mode 100644 index 0000000..1a0de90 --- /dev/null +++ b/stats.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# vim: ai ts=4 sts=4 et sw=4 + + +""" + Exctract usefull infos from web server logs +""" + +import re + + +# define your web server logs path +LOGS_PATH = "/var/log/nginx/access_0bin.log" + + +rexp = re.compile('(\d+\.\d+\.\d+\.\d+) - - \[([^\[\]:]+):(\d+:\d+:\d+) -(\d\d\d\d\)] ("[^"]*")(\d+) (-|\d+) ("[^"]*") (".*")\s*\Z') + + +f = open(LOGS_PATH, 'r') + +for line in f: + a = rexp.match(line) + + if not a is None: + # a.group(1) #IP address + # a.group(2) #day/month/year + # a.group(3) #time of day + # a.group(4) #timezone + # a.group(5) #request + # a.group(6) #code 200 for success, 404 for not found, etc. + # a.group(7) #bytes transferred + # a.group(8) #referrer + # a.group(9) #browser + print a.group(8) #referrer + +f.close() \ No newline at end of file diff --git a/zerobin/default_settings.py b/zerobin/default_settings.py index efc48d2..4e2b6ff 100644 --- a/zerobin/default_settings.py +++ b/zerobin/default_settings.py @@ -57,7 +57,7 @@ GROUP = None # Be carreful if your site have to many pastes this can hurt your hard drive performances. # Refresh counter interval. Default to every minute after a paste. DISPLAY_COUNTER = True -REFRESH_COUNTER = 60 * 1 +REFRESH_COUNTER = 1 * 1 # Names/links to insert in the menu bar. # Any link with "mailto:" will be escaped to prevent spam diff --git a/zerobin/paste.py b/zerobin/paste.py index 4662d77..53d26b4 100644 --- a/zerobin/paste.py +++ b/zerobin/paste.py @@ -1,11 +1,8 @@ # -*- coding: utf-8 -*- import os -import fcntl -import sys import hashlib - from datetime import datetime, timedelta from utils import settings @@ -127,22 +124,44 @@ class Paste(object): path = settings.PASTE_FILES_ROOT - counter_file = os.path.join(path, 'counter') + counter_file = os.path.join(path, 'counter') + lock_file = os.path.join(path, 'counter.lock') + + if not os.path.isfile(lock_file): + try: + #make lock file + flock = open(lock_file, "w") + flock.write('lock') + flock.close() + + # init counter (first time) + if not os.path.isfile(counter_file): + fcounter = open(counter_file, "w") + fcounter.write('1') + fcounter.close() + + # get counter value + fcounter = open(counter_file, "r") + counter_value = fcounter.read(50) + fcounter.close() + + try: + counter_value = long(counter_value) + 1 + except ValueError: + counter_value = 1 + + # write new value to counter + fcounter = open(counter_file, "w") + fcounter.write(str(counter_value)) + fcounter.close() + + #remove lock file + os.remove(lock_file) + except (IOError, OSError): + if os.path.isfile(lock_file): + #remove lock file + os.remove(lock_file) - fd = os.open(counter_file, os.O_RDWR | os.O_CREAT) - fcntl.lockf(fd, fcntl.LOCK_EX) - s = os.read(fd, 4096) - try: - n = long(float(s)) - except ValueError: - raise ValueError(u"Couldn't read value from counter file " + counter_file + ", assuming 0") - n = 0 - fnn = counter_file + ".new" - f = open(fnn, "w") - f.write(str(n + 1)) - f.close() - os.rename(fnn, counter_file) - os.close(fd) def save(self): diff --git a/zerobin/utils.py b/zerobin/utils.py index 6c2c2c5..40b59de 100644 --- a/zerobin/utils.py +++ b/zerobin/utils.py @@ -59,7 +59,7 @@ def get_pastes_count(): Return the number of pastes created (must have option DISPLAY_COUNTER enabled) """ locale.setlocale(locale.LC_ALL, 'en_US') - counter_path = default_settings.PASTE_FILES_ROOT + counter_path = settings.PASTE_FILES_ROOT counter_file = os.path.join(counter_path, 'counter') try: with open(counter_file, "r") as f: