AMP Nginx OK

pull/863/head
nicolargo 2016-04-16 16:15:36 +02:00
parent 3894233375
commit 5481771263
3 changed files with 30 additions and 11 deletions

View File

@ -29,11 +29,18 @@ from glances.logger import logger
class GlancesAmp(object):
"""Main class for Glances AMP."""
NAME = '?'
VERSION = '?'
DESCRIPTION = '?'
AUTHOR = '?'
EMAIL = '?'
def __init__(self, args=None):
"""Init AMP classe."""
logger.debug("Init {0} version {1}".format(self.NAME, self.VERSION))
# AMP name (= module name without glances_)
self.amp_name = self.__class__.__module__[len('glances_'):]
@ -66,7 +73,7 @@ class GlancesAmp(object):
#
if (hasattr(config, 'has_section') and
config.has_section(self.amp_name)):
logger.debug("AMP: Load {0} configuration".format(self.amp_name))
logger.debug("{0}: Load configuration".format(self.NAME))
for param, _ in config.items(self.amp_name):
try:
self.configs[param] = config.get_float_value(self.amp_name, param)
@ -74,19 +81,19 @@ class GlancesAmp(object):
self.configs[param] = config.get_value(self.amp_name, param).split(',')
if len(self.configs[param]) == 1:
self.configs[param] = self.configs[param][0]
logger.debug("AMP: Load {0} parameter: {1} = {2}".format(self.amp_name, param, self.configs[param]))
logger.debug("{0}: Load parameter: {1} = {2}".format(self.NAME, param, self.configs[param]))
else:
logger.warning("AMP: Can not find section {0} in the configuration file".format(self.amp_name))
logger.warning("{0}: Can not find section {1} in the configuration file".format(self.NAME, self.amp_name))
# enable, regex and refresh are mandatories
# if not configured then AMP is disabled
for k in ['enable', 'regex', 'refresh']:
if k not in self.configs:
logger.warning("AMP: Can not find configuration key {0} in section {1}".format(k, self.amp_name))
logger.warning("{0}: Can not find configuration key {1} in section {2}".format(self.NAME, k, self.amp_name))
self.configs['enable'] = 'false'
if not self.enable():
logger.warning("AMP: {0} is disabled".format(self.amp_name))
logger.warning("{0} is disabled".format(self.NAME))
def get(self, key):
"""Generic method to get the item in the AMP configuration"""

View File

@ -19,6 +19,13 @@
"""Nginx AMP."""
"""
A Glances AMP is a Python script called if a process is running.
The script should define a Amp (GlancesAmp) class with an update method.
The update method should call the set_result method to fill the AMP return string.
The return string is a string with one or more line (\n between lines).
"""
import requests
from glances.logger import logger
@ -26,9 +33,14 @@ from glances.amps.glances_amp import GlancesAmp
class Amp(GlancesAmp):
"""Glances' Nginx AMP."""
NAME = 'Nginx Glances AMP'
VERSION = '1.0'
DESCRIPTION = 'Get Nginx stats from status-page'
AUTHOR = 'Nicolargo'
EMAIL = 'contact@nicolargo.com'
# def __init__(self, args=None):
# """Init the AMP."""
# super(Amp, self).__init__(args=args)
@ -37,7 +49,7 @@ class Amp(GlancesAmp):
"""Update the AMP"""
if self.should_update():
logger.debug('AMPS: Update {0} using status URL {1}'.format(self.amp_name, self.get('status_url')))
logger.debug('{0}: Update stats using status URL {1}'.format(self.NAME, self.get('status_url')))
# Get the Nginx status
req = requests.get(self.get('status_url'))
if req.ok:
@ -47,6 +59,6 @@ class Amp(GlancesAmp):
else:
self.set_result(req.text)
else:
logger.debug('AMPS: Can not grab status URL {0} ({1})'.format(self.get('status_url'), req.reason))
logger.debug('{0}: Can not grab status URL {1} ({2})'.format(self.NAME, self.get('status_url'), req.reason))
return self.result()

View File

@ -78,12 +78,12 @@ class Plugin(GlancesPlugin):
# Build the string message
for m in self.stats:
# Only display AMP if a result exist
if m['result'] is None:
# Only display AMP if a result exist
continue
# Display AMP
# first_column = '{0} {1}/{2}'.format(m['key'], int(m['timer']), int(m['refresh']))
first_column = '{0}'.format(m['key'])
# first_column = '{0} {1}/{2}'.format(m['key'], int(m['timer']), int(m['refresh']))
for l in m['result'].split('\n'):
# Display first column with the process name...
msg = '{0:<20} '.format(first_column)