diff options
Diffstat (limited to 'glances/outputs/glances_bottle.py')
-rw-r--r-- | glances/outputs/glances_bottle.py | 61 |
1 files changed, 51 insertions, 10 deletions
diff --git a/glances/outputs/glances_bottle.py b/glances/outputs/glances_bottle.py index 94de21b3..053a1175 100644 --- a/glances/outputs/glances_bottle.py +++ b/glances/outputs/glances_bottle.py @@ -23,8 +23,9 @@ import json import os import sys import tempfile +from io import open -from glances.core.glances_logging import logger +from glances.logger import logger try: from bottle import Bottle, static_file, abort, response, request, auth_basic @@ -61,7 +62,7 @@ class GlancesBottle(object): def check_auth(self, username, password): """Check if a username/password combination is valid.""" if username == self.args.username: - from glances.core.glances_password import GlancesPassword + from glances.password import GlancesPassword pwd = GlancesPassword() return pwd.check_password(self.args.password, pwd.sha256_hash(password)) else: @@ -70,7 +71,7 @@ class GlancesBottle(object): def _route(self): """Define route.""" self._app.route('/', method="GET", callback=self._index) - self._app.route('/<refresh_time:int>', method=["GET", "POST"], callback=self._index) + self._app.route('/<refresh_time:int>', method=["GET"], callback=self._index) self._app.route('/<filename:re:.*\.css>', method="GET", callback=self._css) self._app.route('/<filename:re:.*\.js>', method="GET", callback=self._js) @@ -81,6 +82,8 @@ class GlancesBottle(object): self._app.route('/favicon.ico', method="GET", callback=self._favicon) # REST API + self._app.route('/api/2/args', method="GET", callback=self._api_args) + self._app.route('/api/2/args/:item', method="GET", callback=self._api_args_item) self._app.route('/api/2/help', method="GET", callback=self._api_help) self._app.route('/api/2/pluginslist', method="GET", callback=self._api_plugins) self._app.route('/api/2/all', method="GET", callback=self._api_all) @@ -112,10 +115,6 @@ class GlancesBottle(object): def _index(self, refresh_time=None): """Bottle callback for index.html (/) file.""" - # Manage parameter - if refresh_time is None: - refresh_time = self.args.time - # Update the stat self.stats.update() @@ -215,8 +214,11 @@ class GlancesBottle(object): if self.args.debug: fname = os.path.join(tempfile.gettempdir(), 'glances-debug.json') - with open(fname) as f: - return f.read() + try: + with open(fname) as f: + return f.read() + except IOError: + logger.debug("Debug file (%s) not found" % fname) # Update the stat self.stats.update() @@ -228,7 +230,6 @@ class GlancesBottle(object): abort(404, "Cannot get stats (%s)" % str(e)) return statval - def _api_all_limits(self): """Glances API RESTFul implementation. @@ -379,6 +380,46 @@ class GlancesBottle(object): else: return pdict + def _api_args(self): + """Glances API RESTFul implementation. + + Return the JSON representation of the Glances command line arguments + HTTP/200 if OK + HTTP/404 if others error + """ + response.content_type = 'application/json' + + try: + # Get the JSON value of the args' dict + # Use vars to convert namespace to dict + # Source: https://docs.python.org/2/library/functions.html#vars + args_json = json.dumps(vars(self.args)) + except Exception as e: + abort(404, "Cannot get args (%s)" % str(e)) + return args_json + + def _api_args_item(self, item): + """Glances API RESTFul implementation. + + Return the JSON representation of the Glances command line arguments item + HTTP/200 if OK + HTTP/400 if item is not found + HTTP/404 if others error + """ + response.content_type = 'application/json' + + if item not in self.args: + abort(400, "Unknown item %s" % item) + + try: + # Get the JSON value of the args' dict + # Use vars to convert namespace to dict + # Source: https://docs.python.org/2/library/functions.html#vars + args_json = json.dumps(vars(self.args)[item]) + except Exception as e: + abort(404, "Cannot get args item (%s)" % str(e)) + return args_json + class EnableCors(object): name = 'enable_cors' |