Add logging system

This commit is contained in:
Mehdi Abaakouk 2012-08-21 11:12:58 +02:00
parent f30939f764
commit 17d11c0ec4
2 changed files with 16 additions and 18 deletions

View File

@ -20,6 +20,8 @@
### ###
import logging
from logging.handlers import TimedRotatingFileHandler
import subprocess import subprocess
from urllib import unquote from urllib import unquote
@ -28,8 +30,14 @@ from bird import BirdSocket
from flask import Flask, request, abort from flask import Flask, request, abort
app = Flask(__name__) app = Flask(__name__)
app.debug = app.config["DEBUG"]
app.config.from_pyfile('lg-proxy.cfg') app.config.from_pyfile('lg-proxy.cfg')
file_handler = TimedRotatingFileHandler(filename=app.config["LOG_FILE"], when="midnight")
file_handler.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper()))
app.logger.addHandler(file_handler)
def check_accesslist(): def check_accesslist():
if app.config["ACCESS_LIST"] and request.remote_addr not in app.config["ACCESS_LIST"]: if app.config["ACCESS_LIST"] and request.remote_addr not in app.config["ACCESS_LIST"]:
abort(401) abort(401)
@ -79,6 +87,5 @@ def bird():
if __name__ == "__main__": if __name__ == "__main__":
app.debug = True
app.run("0.0.0.0") app.run("0.0.0.0")

25
lg.py
View File

@ -21,6 +21,8 @@
### ###
import subprocess import subprocess
import logging
from logging.handlers import TimedRotatingFileHandler
import re import re
from urllib2 import urlopen from urllib2 import urlopen
from urllib import quote, unquote from urllib import quote, unquote
@ -34,6 +36,12 @@ from flask import Flask, render_template, jsonify, redirect, session, request, a
app = Flask(__name__) app = Flask(__name__)
app.config.from_pyfile('lg.cfg') app.config.from_pyfile('lg.cfg')
app.secret_key = app.config["SESSION_KEY"]
app.debug = app.config["DEBUG"]
file_handler = TimedRotatingFileHandler(filename=app.config["LOG_FILE"], when="midnight")
file_handler.setLevel(getattr(logging, app.config["LOG_LEVEL"].upper()))
app.logger.addHandler(file_handler)
def add_links(text): def add_links(text):
@ -306,9 +314,6 @@ def show_route_for_bgpmap(hosts, proto):
return show_route("prefix_bgpmap", hosts, proto) return show_route("prefix_bgpmap", hosts, proto)
ASNAME_CACHE_FILE = "/tmp/asname_cache.pickle"
ASNAME_CACHE = load_cache_pickle(ASNAME_CACHE_FILE, {})
def get_as_name(_as): def get_as_name(_as):
"""return a string that contain the as number following by the as name """return a string that contain the as number following by the as name
@ -324,18 +329,6 @@ def get_as_name(_as):
name = get_asn_from_as(_as)[-1].replace(" ","\r",1) name = get_asn_from_as(_as)[-1].replace(" ","\r",1)
return "AS%s | %s" % (_as, name) return "AS%s | %s" % (_as, name)
if _as not in ASNAME_CACHE:
whois_answer = whois_command("as%s" % _as)
as_name = re.search('(as-name|ASName): (.*)', whois_answer)
if as_name:
ASNAME_CACHE[_as] = as_name.group(2).strip()
else:
ASNAME_CACHE[_as] = _as
save_cache_pickle(ASNAME_CACHE_FILE, ASNAME_CACHE)
if ASNAME_CACHE[_as] == _as:
return "AS%s" % _as
else:
return "AS%s\r%s" % (_as, ASNAME_CACHE[_as])
def get_as_number_from_protocol_name(host, proto, protocol): def get_as_number_from_protocol_name(host, proto, protocol):
ret, res = bird_command(host, proto, "show protocols all %s" % protocol) ret, res = bird_command(host, proto, "show protocols all %s" % protocol)
@ -574,7 +567,5 @@ def show_route(request_type, hosts, proto):
return render_template((bgpmap and 'bgpmap.html' or 'route.html'), detail=detail, command=command, expression=expression, error="<br />".join(error)) return render_template((bgpmap and 'bgpmap.html' or 'route.html'), detail=detail, command=command, expression=expression, error="<br />".join(error))
app.secret_key = app.config["SESSION_KEY"]
app.debug = app.config["DEBUG"]
if __name__ == "__main__": if __name__ == "__main__":
app.run("0.0.0.0") app.run("0.0.0.0")