From edddd5a6edd5c261cc22c9f366938a6e7b1f0f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Kut=C3=BD?= <6du1ro.n@gmail.com> Date: Sat, 13 May 2017 20:51:26 +0200 Subject: [PATCH] Add collectd exporter. --- metadata/service/collectd_exporter/single.yml | 7 +++ prometheus/collectd_exporter.sls | 56 +++++++++++++++++++ .../files/collectd_exporter.systemd.jinja | 18 ++++++ .../files/collectd_exporter.upstart.jinja | 30 ++++++++++ .../files/default-collectd_exporter.jinja | 3 + prometheus/files/default-prometheus.jinja | 2 +- prometheus/map.jinja | 26 +++++++++ prometheus/server.sls | 1 + 8 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 metadata/service/collectd_exporter/single.yml create mode 100644 prometheus/collectd_exporter.sls create mode 100644 prometheus/files/collectd_exporter.systemd.jinja create mode 100644 prometheus/files/collectd_exporter.upstart.jinja create mode 100644 prometheus/files/default-collectd_exporter.jinja diff --git a/metadata/service/collectd_exporter/single.yml b/metadata/service/collectd_exporter/single.yml new file mode 100644 index 0000000..c82300a --- /dev/null +++ b/metadata/service/collectd_exporter/single.yml @@ -0,0 +1,7 @@ +applications: + - collect_exporter +parameters: + prometheus: + collectd_exporter: + enabled: true + push_path: 'collectd' diff --git a/prometheus/collectd_exporter.sls b/prometheus/collectd_exporter.sls new file mode 100644 index 0000000..bd00603 --- /dev/null +++ b/prometheus/collectd_exporter.sls @@ -0,0 +1,56 @@ +{% from "prometheus/map.jinja" import collectd_exporter with context %} +{%- if collectd_exporter.enabled %} + +{%- if not (pillar.docker is defined and pillar.docker.host is defined) %} + +include: + - prometheus.common + +collectd_exporter_tarball: + archive.extracted: + - name: {{ collectd_exporter.dir.install }} + - source: {{ collectd_exporter.source }} + - source_hash: sha1={{ collectd_exporter.source_hash }} + - archive_format: tar + - if_missing: {{ collectd_exporter.dir.version_path }} + +collectd_exporter_bin_link: + file.symlink: + - name: /usr/bin/collectd_exporter + - target: {{ collectd_exporter.dir.version_path }}/collectd_exporter + - require: + - archive: collectd_exporter_tarball + +collectd_exporter_defaults: + file.managed: + - name: /etc/default/collectd_exporter + - source: salt://prometheus/files/default-collectd_exporter.jinja + - template: jinja + +collectd_exporter_service_unit: + file.managed: +{%- if grains.get('init') == 'systemd' %} + - name: /etc/systemd/system/collectd_exporter.service + - source: salt://prometheus/files/collectd_exporter.systemd.jinja +{%- elif grains.get('init') == 'upstart' %} + - name: /etc/init/collectd_exporter.conf + - source: salt://prometheus/files/collectd_exporter.upstart.jinja +{%- endif %} + - watch: + - file: collectd_exporter_defaults + - require_in: + - file: collectd_exporter_service + +collectd_exporter_service: + service.running: + - name: collectd_exporter + - enable: True + - reload: True + - watch: + - file: collectd_exporter_service_unit + - file: collectd_exporter_bin_link + +{%- endif %} + + +{%- endif %} diff --git a/prometheus/files/collectd_exporter.systemd.jinja b/prometheus/files/collectd_exporter.systemd.jinja new file mode 100644 index 0000000..8d5eff9 --- /dev/null +++ b/prometheus/files/collectd_exporter.systemd.jinja @@ -0,0 +1,18 @@ +[Unit] +Description="An exporter for collectd. It accepts collectd's binary network protocol as sent by collectd's network plugin and metrics in JSON format via HTTP POST as sent by collectd's write_http plugin, and transforms and exposes them for consumption by Prometheus." +Documentation=https://prometheus.io/docs/alerting/collectd_exporter/ +Wants=basic.target +After=basic.target network.target + +[Service] +User=prometheus +Group=prometheus +EnvironmentFile=/etc/default/collectd_exporter +ExecStart=/usr/bin/collectd_exporter $ARGS +ExecReload=/bin/kill -HUP $MAINPID +KillMode=process +Restart=always +RestartSec=42s + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/prometheus/files/collectd_exporter.upstart.jinja b/prometheus/files/collectd_exporter.upstart.jinja new file mode 100644 index 0000000..f16ae4c --- /dev/null +++ b/prometheus/files/collectd_exporter.upstart.jinja @@ -0,0 +1,30 @@ +# Prometheus Alert Manager (Upstart unit) +description "An exporter for collectd. It accepts collectd's binary network protocol as sent by collectd's network plugin and metrics in JSON format via HTTP POST as sent by collectd's write_http plugin, and transforms and exposes them for consumption by Prometheus." +start on runlevel [2345] +stop on runlevel [06] + +env ALERTMANAGER=/usr/bin/collectd_exporter +env USER=prometheus +env GROUP=prometheus +env DEFAULTS=/etc/default/collectd_exporter +env RUNDIR=/var/run/collectd_exporter +env PID_FILE=/var/run/collectd_exporter/collectd_exporter.pid +pre-start script + [ -e $DEFAULTS ] && . $DEFAULTS + + mkdir -p $RUNDIR || true + chmod 0750 $RUNDIR || true + chown $USER:$GROUP $RUNDIR || true +end script + +script + # read settings like GOMAXPROCS from "/etc/default/collectd_exporter", if available. + [ -e $DEFAULTS ] && . $DEFAULTS + + export GOMAXPROCS=${GOMAXPROCS:-2} + exec start-stop-daemon -c $USER -g $GROUP -p $PID_FILE -x $ALERTMANAGER -S -- $ARGS +end script + +respawn +respawn limit 10 10 +kill timeout 10 \ No newline at end of file diff --git a/prometheus/files/default-collectd_exporter.jinja b/prometheus/files/default-collectd_exporter.jinja new file mode 100644 index 0000000..a57210c --- /dev/null +++ b/prometheus/files/default-collectd_exporter.jinja @@ -0,0 +1,3 @@ +{%- from "prometheus/map.jinja" import collectd_exporter with context %} +# Set the command-line arguments to pass to the server. +ARGS="-collectd.listen-address {{ collectd_exporter.bind.address }}:{{ collectd_exporter.bind.port }} -web.collectd-push-path {{ collectd_exporter.push_path }} " diff --git a/prometheus/files/default-prometheus.jinja b/prometheus/files/default-prometheus.jinja index e925670..e32efcd 100644 --- a/prometheus/files/default-prometheus.jinja +++ b/prometheus/files/default-prometheus.jinja @@ -1,7 +1,7 @@ {%- from "prometheus/map.jinja" import server with context %} # Set the command-line arguments to pass to the server. -ARGS="-config.file {{server.dir.config}}/prometheus.yml -storage.local.path {{server.dir.storage}} -web.console.libraries {{server.dir.version_path}}/console_libraries -web.console.templates {{ server.dir.version_path }}/consoles -alertmanager.url {%- if server.alertmanager is defined %}{{server.alertmanager.get('host', 'localhost')}}:{{server.alertmanager.get('port', 9093)}}{%- endif %}" +ARGS="-config.file {{server.dir.config}}/prometheus.yml -storage.local.path {{server.dir.storage}} -web.console.libraries {{server.dir.version_path}}/console_libraries -web.console.templates {{ server.dir.version_path }}/consoles {%- if server.alertmanager is defined %} -alertmanager.url {{server.alertmanager.get('protocol', 'http')}}://{{server.alertmanager.get('host', 'localhost')}}:{{server.alertmanager.get('port', 9093)}}{%- endif %}" # Prometheus supports the following options: diff --git a/prometheus/map.jinja b/prometheus/map.jinja index 2a82cf3..4e8249a 100644 --- a/prometheus/map.jinja +++ b/prometheus/map.jinja @@ -68,3 +68,29 @@ Debian: {%- endload %} {%- set pushgateway = salt['grains.filter_by'](pushgateway_defaults, merge=salt['pillar.get']('prometheus:pushgateway')) %} + +{%- set collectd_exporter_version = pillar.prometheus.get('collectd_exporter', {}).get('version', '0.3.1') %} + +{%- load_yaml as collectd_exporter_defaults %} +{%- if pillar.docker is defined and pillar.docker.host is defined %} +Debian: + dir: + config: /srv/volumes/prometheus +{%- else %} +Debian: + dir: + config: /etc/prometheus + storage: /var/lib/prometheus-collectd_exporter/metrics + install: /opt + version_path: /opt/collectd_exporter-{{ collectd_exporter_version }}.linux-amd64 + version: {{ collectd_exporter_version }}.linux-amd64 + source: https://github.com/prometheus/collectd_exporter/releases/download/{{ collectd_exporter_version }}/collectd_exporter-{{ collectd_exporter_version }}.linux-amd64.tar.gz + source_hash: 6460a15dab512f7485d7974d115677e9406d53fd + bind: + address: '' + port: 25826 + push_path: '' +{%- endif %} +{%- endload %} + +{%- set collectd_exporter = salt['grains.filter_by'](collectd_exporter_defaults, merge=salt['pillar.get']('prometheus:collectd_exporter')) %} diff --git a/prometheus/server.sls b/prometheus/server.sls index dade5ce..8793c3f 100644 --- a/prometheus/server.sls +++ b/prometheus/server.sls @@ -61,6 +61,7 @@ prometheus_service: - file: prometheus_service_unit - file: {{server.dir.config}}/prometheus.yml - file: prometheus_bin_link + - file: {{server.dir.config}}/alerts.yml {%- endif %}