Remedy all the complaints from pylint
This commit is contained in:
@ -1,8 +1,8 @@
|
||||
#!/usr/bin/env python3
|
||||
'''httpd component of routerstats'''
|
||||
|
||||
import logging
|
||||
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
||||
import io
|
||||
import sys
|
||||
import os
|
||||
from functools import partial
|
||||
@ -17,7 +17,9 @@ logging.basicConfig(
|
||||
datefmt='%Y-%m-%d %H:%M:%S')
|
||||
|
||||
class RequestHandler(SimpleHTTPRequestHandler):
|
||||
'''Extended SimpleHTTPRequestHandler'''
|
||||
def serve_png(self):
|
||||
'''Give a client a png file'''
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'image/png')
|
||||
self.end_headers()
|
||||
@ -51,17 +53,28 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||
else:
|
||||
title = 'monthly'
|
||||
startstr = '--start=end-1M'
|
||||
data = rrdtool.graphv("-", startstr, "--title=" + title, "DEF:in=" + self.rrdfile + ":net_dnat:AVERAGE", "DEF:out=" + self.rrdfile + ":loc-net:AVERAGE", "CDEF:result_in=in,UN,0,in,IF", "CDEF:tmp_out=out,UN,0,out,IF", "CDEF:result_out=tmp_out,-1,*", "AREA:result_in#00ff00:in", "AREA:result_out#0000ff:out")
|
||||
data = rrdtool.graphv(
|
||||
"-",
|
||||
startstr,
|
||||
"--title=" + title,
|
||||
"DEF:in=" + self.rrdfile + ":net_dnat:AVERAGE", #pylint: disable=no-member
|
||||
"DEF:out=" + self.rrdfile + ":loc-net:AVERAGE", #pylint: disable=no-member
|
||||
"CDEF:result_in=in,UN,0,in,IF",
|
||||
"CDEF:tmp_out=out,UN,0,out,IF",
|
||||
"CDEF:result_out=tmp_out,-1,*",
|
||||
"AREA:result_in#00ff00:in",
|
||||
"AREA:result_out#0000ff:out")
|
||||
#, "--width", "1024", "--height", "600"
|
||||
self.wfile.write(data['image'])
|
||||
break
|
||||
except rrdtool.OperationalError as error:
|
||||
retries += 1
|
||||
if retries > 10:
|
||||
logging.error('Could not graphv the data: ' + str(error))
|
||||
logging.error('Could not graphv the data: %s', error)
|
||||
break
|
||||
|
||||
def errorpage(self, errorcode, errormsg: str = ''):
|
||||
'''Give client an errorpage'''
|
||||
self.send_response(errorcode)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.end_headers()
|
||||
@ -69,6 +82,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||
self.wfile.write(errormsg.encode('utf-8'))
|
||||
|
||||
def do_GET(self):
|
||||
'''Reply to a GET request'''
|
||||
if self.path == '/':
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html')
|
||||
@ -82,6 +96,7 @@ class RequestHandler(SimpleHTTPRequestHandler):
|
||||
self.send_error(404, str(self.path))
|
||||
|
||||
def print_page(self):
|
||||
'''Print pretty html page'''
|
||||
self.wfile.write("<html><body>".encode('utf-8'))
|
||||
self.wfile.write('<img src="/graph/monthly">'.encode('utf-8'))
|
||||
self.wfile.write('<img src="/graph/weekly">'.encode('utf-8'))
|
||||
@ -96,6 +111,7 @@ class CustomHandler(RequestHandler):
|
||||
|
||||
|
||||
def server(rrdfile, port):
|
||||
'''Start server'''
|
||||
server_address = ('', port)
|
||||
server_class = ThreadingHTTPServer
|
||||
handler_class = partial(CustomHandler, rrdfile)
|
||||
@ -103,6 +119,7 @@ def server(rrdfile, port):
|
||||
httpd.serve_forever()
|
||||
|
||||
def main():
|
||||
'''This be main'''
|
||||
config_section = 'httpd'
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
@ -111,12 +128,12 @@ def main():
|
||||
parser.add_argument('-c', '--config', help='config file to load')
|
||||
parser.add_argument('-d', '--debug', action='store_true', help='enable debug')
|
||||
|
||||
args, remaining_args = parser.parse_known_args()
|
||||
args, _ = parser.parse_known_args()
|
||||
|
||||
if args.debug:
|
||||
logging.root.setLevel(logging.DEBUG)
|
||||
|
||||
logging.debug('Starting as PID ' + str(os.getpid()))
|
||||
logging.debug('Starting as PID %s', os.getpid())
|
||||
|
||||
found = False
|
||||
if args.config:
|
||||
@ -138,8 +155,17 @@ def main():
|
||||
logging.error('No config file found')
|
||||
sys.exit(0)
|
||||
|
||||
parser.add_argument('-v', '--vardir', help='directory storing rrd file', default=config[config_section]['var_dir'])
|
||||
parser.add_argument('-p', '--port', help='port to bind to', type=int, default=config[config_section]['port'])
|
||||
parser.add_argument(
|
||||
'-v',
|
||||
'--vardir',
|
||||
help='directory storing rrd file',
|
||||
default=config[config_section]['var_dir'])
|
||||
parser.add_argument(
|
||||
'-p',
|
||||
'--port',
|
||||
help='port to bind to',
|
||||
type=int,
|
||||
default=config[config_section]['port'])
|
||||
|
||||
args = parser.parse_args()
|
||||
logging.debug(args)
|
||||
|
||||
Reference in New Issue
Block a user