def main(smp): try: vap = varnishapi.VarnishLog(['-g', 'raw']) smp.execute(vap) except KeyboardInterrupt: vap.Fini() except Exception as e: syslog.openlog(sys.argv[0], syslog.LOG_PID | syslog.LOG_PERROR, syslog.LOG_LOCAL0) syslog.syslog(syslog.LOG_ERR, traceback.format_exc())
def __init__(self, args, callback): """Set up a callback handler. The callback function will be executed for all transactions matching the given args. args must be a list of varnishlog arguments such as: [ '-i', 'ReqURL', '-n', 'frontend'] callback needs to be a function with the following signature: callback(transaction_id, tag, value, remote_party) """ self.vap = varnishapi.VarnishLog(args) self.callback = callback if self.vap.error: raise Exception(self.vap.error) signal.signal(signal.SIGINT, self.fini) signal.signal(signal.SIGTERM, self.fini) self.keep_running = True
def main(): global storage global vap global task init_config() init_log() try: storage = LogDatabase(**__DB_PARAMS__) if cache_name: varnish_log_args.extend(['-n', cache_name]) snapshot.add_callback_func(snapshot_callback) vap = varnishapi.VarnishLog(varnish_log_args) task = PeriodicEvent(0.5, fetch_varnish_log) log.debug("Log Reader is about to start.") task.run() except OperationalError as op: print "Database error %s" % op.args[0] log.error(op) except Exception as ex: print "Unknown exception %s" % ex.args[0] log.error(ex)
def fetch_varnish_log(vap): out = vap.Dispatch(vap_callback) interval = 0 if not out: interval = callback_sleep_time callback_runner.set_interval(interval) if __name__ == '__main__': init_config() init_log() if cache_name: varnish_log_args.extend(['-n', cache_name]) vap = varnishapi.VarnishLog(varnish_log_args) callbacks = [fetch_varnish_log] callback_runner = CallbackRunner(callbacks, error_callback, False, vap) try: log_database = LogDatabase(**__DB_PARAMS__) log_storage = LogStorage(log_database) log_data_manager = LogDataManager(log_storage) log_cache_name = "default" if not cache_name else cache_name log.info("Starting log reader for cache: %s" % log_cache_name) callback_runner.run() except Exception as e: log.error(e)
sys.exit(1) PORT = 9999 HOST = '127.0.0.1' VARNISH_PORT = 6081 __DB_PARAMS__ = { 'host': '192.168.75.12', 'db': 'microservice', 'user': '******', 'passwd': 'WqPv5fBSLgnskM7', 'keep_alive': True, 'truncate_tables': False } _vap = varnishapi.VarnishLog(['-g', 'request']) _log_database = LogDatabase(**__DB_PARAMS__) _snapshot = Snapshot() _snapshot.add_callback_func(parse_log_row) def _vap_callback(vap, cbd, priv): lock = threading.Lock() try: lock.acquire() vxid = cbd['vxid'] request_type = cbd['type'] tag = cbd['tag'] t_tag = vap.VSL_tags[tag] data = cbd['data'] _snapshot.fill_snapshot(vxid, request_type, t_tag, data)