summaryrefslogtreecommitdiffstats
path: root/glances/amps
diff options
context:
space:
mode:
authornicolargo <nicolashennion@gmail.com>2016-04-14 19:40:36 +0200
committernicolargo <nicolashennion@gmail.com>2016-04-14 19:40:36 +0200
commit8efb5e2ac8f9f257485d296609ac401c02bed8e4 (patch)
tree500583664cae59d6ca71b794ee15f56f38c388b7 /glances/amps
parentb5e59d3e3ab673a8c02616204eaab4f1869e1b9c (diff)
First run for the Nginx AMP process
Diffstat (limited to 'glances/amps')
-rw-r--r--glances/amps/__init__.py0
-rw-r--r--glances/amps/glances_amp.py116
-rw-r--r--glances/amps/glances_nginx.py48
3 files changed, 164 insertions, 0 deletions
diff --git a/glances/amps/__init__.py b/glances/amps/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/glances/amps/__init__.py
diff --git a/glances/amps/glances_amp.py b/glances/amps/glances_amp.py
new file mode 100644
index 00000000..1a95f68a
--- /dev/null
+++ b/glances/amps/glances_amp.py
@@ -0,0 +1,116 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of Glances.
+#
+# Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
+#
+# Glances is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Glances is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""
+I am your father...
+
+...for all Glances Application Monitoring Processes (AMP).
+"""
+
+from glances.compat import u
+from glances.logger import logger
+
+
+class GlancesAmp(object):
+
+ """Main class for Glances AMP."""
+
+ def __init__(self, args=None):
+ """Init AMP classe."""
+ # AMP name (= module name without glances_)
+ self.amp_name = self.__class__.__module__[len('glances_'):]
+
+ # Init the args
+ self.args = args
+
+ # Init the configs
+ self.configs = {}
+
+ def load_config(self, config):
+ """Load AMP parameters from the configuration file."""
+
+ # Read AMP confifuration.
+ # For ex, the AMP foo should have the following section:
+ #
+ # [foo]
+ # enable=true
+ # regex=\/usr\/bin\/nginx
+ # refresh=60
+ #
+ # and optionnaly:
+ #
+ # option1=opt1
+ #
+ if (hasattr(config, 'has_section') and
+ config.has_section(self.amp_name)):
+ logger.debug("AMP: Load {0} configuration".format(self.amp_name))
+ for param, _ in config.items(self.amp_name):
+ try:
+ self.configs[param] = config.get_float_value(self.amp_name, param)
+ except ValueError:
+ 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]))
+ else:
+ logger.warning("AMP: Can not find section {0} in the configuration file".format(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))
+ self.configs['enable'] = 'false'
+
+ if not self.enable():
+ logger.warning("AMP: {0} is disabled".format(self.amp_name))
+
+ def get(self, key):
+ """Generic method to get the item in the AMP configuration"""
+ if key in self.configs:
+ return self.configs[key]
+ else:
+ return None
+
+ def enable(self):
+ """Return True|False if the AMP is enabled in the configuration file (enable=true|false)."""
+ return self.get('enable').lower().startswith('true')
+
+ def regex(self):
+ """Return regular expression used to identified the current application."""
+ return self.get('regex')
+
+ def refresh(self):
+ """Return refresh time in seconds for the current application monitoring process."""
+ return self.get('refresh')
+
+ def should_update(self):
+ """Return True is the AMP should be updated:
+ - AMP is enable
+ - only update every 'refresh' seconds
+ """
+ return True
+
+ def set_result(self, result):
+ """Store the result (string) into the result key of the AMP"""
+ self.configs['result'] = str(result)
+
+ def result(self):
+ """ Return the result of the AMP (as a string)"""
+ return u(self.get('result'))
diff --git a/glances/amps/glances_nginx.py b/glances/amps/glances_nginx.py
new file mode 100644
index 00000000..33b49309
--- /dev/null
+++ b/glances/amps/glances_nginx.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+#
+# This file is part of Glances.
+#
+# Copyright (C) 2016 Nicolargo <nicolas@nicolargo.com>
+#
+# Glances is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Glances is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+"""Nginx AMP."""
+
+import requests
+
+from glances.logger import logger
+from glances.amps.glances_amp import GlancesAmp
+
+
+class Amp(GlancesAmp):
+
+ """Glances' Nginx AMP."""
+
+ def __init__(self, args=None):
+ """Init the AMP."""
+ super(Amp, self).__init__(args=args)
+
+ def update(self):
+ """Update the AMP"""
+
+ if self.should_update():
+ logger.debug('AMPS: Update {0} using status URL {1}'.format(self.amp_name, self.get('status_url')))
+ req = requests.get(self.get('status_url'))
+ if req.ok:
+ # u'Active connections: 1 \nserver accepts handled requests\n 1 1 1 \nReading: 0 Writing: 1 Waiting: 0 \n'
+ self.set_result(req.text)
+ else:
+ logger.debug('AMPS: Can not grab status URL {0} ({1})'.format(self.get('status_url'), req.reason))
+
+ return self.result()