|
@@ -11,8 +11,8 @@ sock = socket.socket()
|
|
|
last_timestamp = 0
|
|
|
|
|
|
def logging(message, level = syslog.LOG_INFO):
|
|
|
- #GLOP#syslog.syslog(level, message)
|
|
|
- print message
|
|
|
+ syslog.syslog(level, message)
|
|
|
+ #print message
|
|
|
|
|
|
|
|
|
def read_vpn_acct_file(filename):
|
|
@@ -34,8 +34,8 @@ def read_vpn_acct_file(filename):
|
|
|
# format: username ip qos uptxbytes downrxbytes
|
|
|
if len(d) == 5:
|
|
|
metrics.extend([
|
|
|
- ( 'vpn1.%s.upxtbytes' % d[0], (tstamp, int(d[3])) ),
|
|
|
- ( 'vpn1.%s.downrxbytes' % d[0], (tstamp, int(d[4])) ),
|
|
|
+ ( '%s.%s.uptxbytes' % (options.prefix, d[0]), (tstamp, int(d[3])) ),
|
|
|
+ ( '%s.%s.downrxbytes' % (options.prefix, d[0]), (tstamp, int(d[4])) ),
|
|
|
])
|
|
|
return tstamp, metrics
|
|
|
|
|
@@ -47,7 +47,6 @@ def send_to_carbon(metrics):
|
|
|
header = struct.pack("!L", len(payload))
|
|
|
message = header + payload
|
|
|
sock.sendall(message)
|
|
|
- #GLOP#print metrics
|
|
|
|
|
|
|
|
|
def process_vpn_file(filename, old_tstamp = 0):
|
|
@@ -84,7 +83,11 @@ def handle_old_vpn_files(directory, old_tstamp):
|
|
|
|
|
|
class VPNAcctHandler(pyinotify.ProcessEvent):
|
|
|
|
|
|
+ def process_IN_MOVED_TO(self, event):
|
|
|
+ self.handle_file(event)
|
|
|
def process_IN_CLOSE_WRITE(self, event):
|
|
|
+ self.handle_file(event)
|
|
|
+ def handle_file(self, event):
|
|
|
logging("Event detected for file %s..." % (event.pathname, ), syslog.LOG_DEBUG)
|
|
|
|
|
|
if last_timestamp > 0:
|
|
@@ -104,6 +107,7 @@ if __name__ == '__main__':
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
|
|
|
|
parser.add_argument('directory', help='directory to watch')
|
|
|
+ parser.add_argument('prefix', help='prefix for Graphite metrics')
|
|
|
parser.add_argument('-t', '--timestamp', dest='tstamp', type=int, default=0, help='Timestamp to recover data from')
|
|
|
parser.add_argument('-s', '--server', dest='server', default='localhost', help='Carbon daemon address')
|
|
|
parser.add_argument('-p', '--port', dest='port', type=int, default=2004, help='Carbon daemon port')
|
|
@@ -140,14 +144,14 @@ if __name__ == '__main__':
|
|
|
last_timestamp = 0
|
|
|
|
|
|
# https://github.com/seb-m/pyinotify/blob/master/python2/examples/daemon.py
|
|
|
- logging("Starting to watch %s directory..." % (options.directory))
|
|
|
+ logging("Starting to watch %s directory..." % (options.directory, ))
|
|
|
wm = pyinotify.WatchManager()
|
|
|
handler = VPNAcctHandler()
|
|
|
notifier = pyinotify.Notifier(wm, handler)
|
|
|
- wm.add_watch(options.directory, pyinotify.IN_CLOSE_WRITE)
|
|
|
+ wm.add_watch(options.directory, pyinotify.IN_CLOSE_WRITE|pyinotify.IN_MOVED_TO)
|
|
|
|
|
|
try:
|
|
|
- notifier.loop(daemonize=False, callback=on_loop, pid_file='/tmp/pyinotify.pid', stdout='/tmp/pyinotify.log') #GLOP#
|
|
|
+ notifier.loop(daemonize=True, callback=on_loop, pid_file='/var/run/pyinotify.pid', stdout='/var/log/pyinotify.log')
|
|
|
except pyinotify.NotifierError, err:
|
|
|
print >> sys.stderr, err
|
|
|
|