Permalink
Browse files

Merge pull request #1545 from l2isbad/haproxy_module

Haproxy module
  • Loading branch information...
2 parents 392042e + 43bd866 commit 642dc0ec79cb871f8f0f4e914af87e054409008f @ktsaou ktsaou committed on GitHub Jan 11, 2017
Showing with 254 additions and 0 deletions.
  1. +2 −0 conf.d/Makefile.am
  2. +8 −0 conf.d/health.d/haproxy.conf
  3. +78 −0 conf.d/python.d/haproxy.conf
  4. +1 −0 python.d/Makefile.am
  5. +165 −0 python.d/haproxy.chart.py
View
@@ -32,6 +32,7 @@ dist_pythonconfig_DATA = \
python.d/exim.conf \
python.d/fail2ban.conf \
python.d/freeradius.conf \
+ python.d/haproxy.conf \
python.d/hddtemp.conf \
python.d/ipfs.conf \
python.d/isc_dhcpd.conf \
@@ -59,6 +60,7 @@ dist_healthconfig_DATA = \
health.d/cpu.conf \
health.d/disks.conf \
health.d/entropy.conf \
+ health.d/haproxy.conf \
health.d/ipc.conf \
health.d/mdstat.conf \
health.d/memcached.conf \
@@ -0,0 +1,8 @@
+ template: haproxy_backend_server_status
+ on: haproxy_h.down
+ units: failed servers
+ every: 10s
+ lookup: average -10s
+ crit: $this > 0
+ info: Some of your backend servers is down!
+ to: sysadmin
@@ -0,0 +1,78 @@
+# netdata python.d.plugin configuration for haproxy
+#
+# This file is in YaML format. Generally the format is:
+#
+# name: value
+#
+# There are 2 sections:
+# - global variables
+# - one or more JOBS
+#
+# JOBS allow you to collect values from multiple sources.
+# Each source will have its own set of charts.
+#
+# JOB parameters have to be indented (using spaces only, example below).
+
+# ----------------------------------------------------------------------
+# Global Variables
+# These variables set the defaults for all JOBs, however each JOB
+# may define its own, overriding the defaults.
+
+# update_every sets the default data collection frequency.
+# If unset, the python.d.plugin default is used.
+# update_every: 1
+
+# priority controls the order of charts at the netdata dashboard.
+# Lower numbers move the charts towards the top of the page.
+# If unset, the default for python.d.plugin is used.
+# priority: 60000
+
+# retries sets the number of retries to be made in case of failures.
+# If unset, the default for python.d.plugin is used.
+# Attempts to restore the service are made once every update_every
+# and only if the module has collected values in the past.
+# retries: 5
+
+# ----------------------------------------------------------------------
+# JOBS (data collection sources)
+#
+# The default JOBS share the same *name*. JOBS with the same name
+# are mutually exclusive. Only one of them will be allowed running at
+# any time. This allows autodetection to try several alternatives and
+# pick the one that works.
+#
+# Any number of jobs is supported.
+#
+# All python.d.plugin JOBS (for all its modules) support a set of
+# predefined parameters. These are:
+#
+# job_name:
+# name: myname # the JOB's name as it will appear at the
+# # dashboard (by default is the job_name)
+# # JOBs sharing a name are mutually exclusive
+# update_every: 1 # the JOB's data collection frequency
+# priority: 60000 # the JOB's order on the dashboard
+# retries: 5 # the JOB's number of restoration attempts
+#
+# Additionally to the above, haproxy also supports the following:
+#
+# IMPORTANT: socket MUST BE readable AND writable by netdata user
+#
+# socket: 'path/to/haproxy/sock'
+#
+# OR
+# url: 'http://<ip.address>:<port>/<url>;csv;norefresh'
+# [user: USERNAME] only if stats auth is used
+# [pass: PASSWORD] only if stats auth is used
+
+# ----------------------------------------------------------------------
+# AUTO-DETECTION JOBS
+# only one of them will run (they have the same name)
+
+#via_url:
+# user : 'admin'
+# pass : 'password'
+# url : 'http://127.0.0.1:7000/haproxy_stats;csv;norefresh'
+
+#via_socket:
+# socket: '/var/run/haproxy/admin.sock'
@@ -18,6 +18,7 @@ dist_python_SCRIPTS = \
exim.chart.py \
fail2ban.chart.py \
freeradius.chart.py \
+ haproxy.chart.py \
hddtemp.chart.py \
ipfs.chart.py \
isc_dhcpd.chart.py \
@@ -0,0 +1,165 @@
+# -*- coding: utf-8 -*-
+# Description: haproxy netdata python.d module
+# Author: l2isbad
+
+from base import UrlService, SocketService
+
+# default module values (can be overridden per job in `config`)
+# update_every = 2
+priority = 60000
+retries = 60
+
+# charts order (can be overridden if you want less charts, or different order)
+ORDER = ['fbin', 'fbout', 'fscur', 'fqcur', 'bbin', 'bbout', 'bscur', 'bqcur', 'health_down']
+CHARTS = {
+ 'fbin': {
+ 'options': [None, "Kilobytes in", "kilobytes in/s", 'Frontend', 'haproxy_f.bin', 'line'],
+ 'lines': [
+ ]},
+ 'fbout': {
+ 'options': [None, "Kilobytes out", "kilobytes out/s", 'Frontend', 'haproxy_f.bout', 'line'],
+ 'lines': [
+ ]},
+ 'fscur': {
+ 'options': [None, "Sessions active", "sessions", 'Frontend', 'haproxy_f.scur', 'line'],
+ 'lines': [
+ ]},
+ 'fqcur': {
+ 'options': [None, "Session in queue", "sessions", 'Frontend', 'haproxy_f.qcur', 'line'],
+ 'lines': [
+ ]},
+ 'bbin': {
+ 'options': [None, "Kilobytes in", "kilobytes in/s", 'Backend', 'haproxy_b.bin', 'line'],
+ 'lines': [
+ ]},
+ 'bbout': {
+ 'options': [None, "Kilobytes out", "kilobytes out/s", 'Backend', 'haproxy_b.bout', 'line'],
+ 'lines': [
+ ]},
+ 'bscur': {
+ 'options': [None, "Sessions active", "sessions", 'Backend', 'haproxy_b.scur', 'line'],
+ 'lines': [
+ ]},
+ 'bqcur': {
+ 'options': [None, "Sessions in queue", "sessions", 'Backend', 'haproxy_b.qcur', 'line'],
+ 'lines': [
+ ]},
+ 'health_down': {
+ 'options': [None, "Servers in DOWN state", "failed servers", 'Health', 'haproxy_h.down', 'line'],
+ 'lines': [
+ ]}
+}
+
+
+class Service(UrlService, SocketService):
+ def __init__(self, configuration=None, name=None):
+ SocketService.__init__(self, configuration=configuration, name=name)
+ self.user = self.configuration.get('user')
+ self.password = self.configuration.get('pass')
+ self.request = 'show stat\n'
+ self.poll_method = (UrlService, SocketService)
+ self.order = ORDER
+ self.order_front = [_ for _ in ORDER if _.startswith('f')]
+ self.order_back = [_ for _ in ORDER if _.startswith('b')]
+ self.definitions = CHARTS
+ self.charts = True
+
+ def check(self):
+ if self.configuration.get('url'):
+ self.poll_method = self.poll_method[0]
+ url = self.configuration.get('url')
+ if not url.endswith(';csv;norefresh'):
+ self.error('Bad url(%s). Must be http://<ip.address>:<port>/<url>;csv;norefresh' % url)
+ return False
+ elif self.configuration.get('socket'):
+ self.poll_method = self.poll_method[1]
+ else:
+ self.error('No configuration is specified')
+ return False
+
+ if self.poll_method.check(self):
+ self.info('Plugin was started succesfully. We are using %s.' % self.poll_method.__name__)
+ return True
+
+ def create_charts(self, front_ends, back_ends):
+ for _ in range(len(front_ends)):
+ self.definitions['fbin']['lines'].append(['_'.join(['fbin', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
+ self.definitions['fbout']['lines'].append(['_'.join(['fbout', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'incremental', 1, 1024])
+ self.definitions['fscur']['lines'].append(['_'.join(['fscur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
+ self.definitions['fqcur']['lines'].append(['_'.join(['fqcur', front_ends[_]['# pxname']]), front_ends[_]['# pxname'], 'absolute'])
+
+ for _ in range(len(back_ends)):
+ self.definitions['bbin']['lines'].append(['_'.join(['bbin', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
+ self.definitions['bbout']['lines'].append(['_'.join(['bbout', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'incremental', 1, 1024])
+ self.definitions['bscur']['lines'].append(['_'.join(['bscur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
+ self.definitions['bqcur']['lines'].append(['_'.join(['bqcur', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
+ self.definitions['health_down']['lines'].append(['_'.join(['hdown', back_ends[_]['# pxname']]), back_ends[_]['# pxname'], 'absolute'])
+
+ def _get_data(self):
+ """
+ Format data received from http request
+ :return: dict
+ """
+ try:
+ raw_data = self.poll_method._get_raw_data(self).splitlines()
+ except Exception as e:
+ self.error(str(e))
+ return None
+
+ all_instances = [dict(zip(raw_data[0].split(','), raw_data[_].split(','))) for _ in range(1, len(raw_data))]
+
+ back_ends = list(filter(is_backend, all_instances))
+ front_ends = list(filter(is_frontend, all_instances))
+ servers = list(filter(is_server, all_instances))
+
+ if self.charts:
+ self.create_charts(front_ends, back_ends)
+ self.charts = False
+
+ to_netdata = dict()
+
+ for frontend in front_ends:
+ for _ in self.order_front:
+ to_netdata.update({'_'.join([_, frontend['# pxname']]): int(frontend[_[1:]]) if frontend.get(_[1:]) else 0})
+
+ for backend in back_ends:
+ for _ in self.order_back:
+ to_netdata.update({'_'.join([_, backend['# pxname']]): int(backend[_[1:]]) if backend.get(_[1:]) else 0})
+
+ for _ in range(len(back_ends)):
+ to_netdata.update({'_'.join(['hdown', back_ends[_]['# pxname']]):
+ len([server for server in servers if is_server_down(server, back_ends, _)])})
+
+ return to_netdata
+
+ def _check_raw_data(self, data):
+ """
+ Check if all data has been gathered from socket
+ :param data: str
+ :return: boolean
+ """
+ return not bool(data)
+
+def is_backend(backend):
+ try:
+ return backend['svname'] == 'BACKEND' and backend['# pxname'] != 'stats'
+ except Exception:
+ return False
+
+def is_frontend(frontend):
+ try:
+ return frontend['svname'] == 'FRONTEND' and frontend['# pxname'] != 'stats'
+ except Exception:
+ return False
+
+def is_server(server):
+ try:
+ return not server['svname'].startswith(('FRONTEND', 'BACKEND'))
+ except Exception:
+ return False
+
+def is_server_down(server, back_ends, _):
+ try:
+ return server['# pxname'] == back_ends[_]['# pxname'] and server['status'] != 'UP'
+ except Exception:
+ return False

0 comments on commit 642dc0e

Please sign in to comment.