From 34ada49bf7cebda3d2c26c9e1b5bcd81e44b5a44 Mon Sep 17 00:00:00 2001 From: Alexander Weidinger Date: Fri, 26 Apr 2019 13:09:43 +0200 Subject: [PATCH] feat(exporters): added node_exporter --- docs/README.rst | 6 ++ kitchen.yml | 1 + pillar.example | 5 ++ prometheus/defaults.yaml | 4 + prometheus/exporters/init.sls | 14 ++++ prometheus/exporters/node/clean.sls | 28 +++++++ prometheus/exporters/node/init.sls | 73 +++++++++++++++++++ prometheus/osfamilymap.yaml | 9 ++- .../default/controls/packages_spec.rb | 4 + .../default/controls/services_spec.rb | 5 ++ 10 files changed, 148 insertions(+), 1 deletion(-) create mode 100644 prometheus/exporters/init.sls create mode 100644 prometheus/exporters/node/clean.sls create mode 100644 prometheus/exporters/node/init.sls diff --git a/docs/README.rst b/docs/README.rst index 0a5b930..c9997d4 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -92,3 +92,9 @@ dependency on ``prometheus.service.clean`` via include list. This state will remove the prometheus package and has a depency on ``prometheus.config.clean`` via include list. +``prometheus.exporters`` +^^^^^^^^^^^^^^^^^^^^^^^^ + +This state will manage prometheus exporters according to Pillar ``prometheus:exporters``. +It includes sub-states like ``prometheus.exporters.node``. +If you don't want to use Pillar data for this you may use the sub-states directly. diff --git a/kitchen.yml b/kitchen.yml index 54fa570..6624b42 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -42,6 +42,7 @@ provisioner: base: '*': - prometheus + - prometheus.exporters pillars: top.sls: base: diff --git a/pillar.example b/pillar.example index 0e6a6e2..d1715b5 100644 --- a/pillar.example +++ b/pillar.example @@ -62,3 +62,8 @@ prometheus: static_configs: - targets: ['localhost:9090'] + + exporters: + node: + args: + web.listen-address: ":9110" diff --git a/prometheus/defaults.yaml b/prometheus/defaults.yaml index 17dc5f6..c8581cd 100644 --- a/prometheus/defaults.yaml +++ b/prometheus/defaults.yaml @@ -9,3 +9,7 @@ prometheus: service: name: prometheus sysrc: False + exporters: + node: + pkg: prometheus-node-exporter + service: prometheus-node-exporter diff --git a/prometheus/exporters/init.sls b/prometheus/exporters/init.sls new file mode 100644 index 0000000..1e5daf5 --- /dev/null +++ b/prometheus/exporters/init.sls @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- set sls_config_file = tplroot ~ '.config.file' %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + +{%- if prometheus.exporters.keys()|length > 0 %} +include: +{%- for name in prometheus.exporters.keys()|list %} + - .{{ name }} +{%- endfor %} +{%- endif %} diff --git a/prometheus/exporters/node/clean.sls b/prometheus/exporters/node/clean.sls new file mode 100644 index 0000000..cbd4030 --- /dev/null +++ b/prometheus/exporters/node/clean.sls @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + +prometheus-exporters-node-service-dead: + service.dead: + - name: {{ prometheus.exporters.node.service }} + - enable: False + +prometheus-exporters-node-pkg-removed: + pkg.removed: + - name: {{ prometheus.exporters.node.pkg }} + - require: + - service: prometheus-exporters-node-service-dead + +{# FreeBSD #} +{%- if salt['grains.get']('os_family') == 'FreeBSD' %} +{%- for parameter in ['args', 'listen_address', 'textfile_dir'] %} +prometheus-exporters-node-args-{{ parameter }}: + sysrc.absent: + - name: node_exporter_{{ parameter }} + - require: + - service: prometheus-exporters-node-service-dead +{%- endfor %} +{%- endif %} diff --git a/prometheus/exporters/node/init.sls b/prometheus/exporters/node/init.sls new file mode 100644 index 0000000..e0cff6e --- /dev/null +++ b/prometheus/exporters/node/init.sls @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- from tplroot ~ "/map.jinja" import prometheus with context %} + +{%- macro concat_args(args) %} +{%- if args|length > 0 %} +{%- for k,v in args -%} +{%- if not k or not v %}{% continue %}{% endif -%} + --{{ k }}={{ v }} +{%- if not loop.last %} {% endif -%} +{%- endfor -%} +{%- endif -%} +{%- endmacro %} + +prometheus-exporters-node-pkg-installed: + pkg.installed: + - name: {{ prometheus.exporters.node.pkg }} + +{%- if 'args' in prometheus.exporters.node %} +{%- set args = prometheus.exporters.node.get('args', {}) -%} + +{# FreeBSD #} +{%- if salt['grains.get']('os_family') == 'FreeBSD' %} +{%- if 'web.listen-address' in args.keys() %} +{%- set value = args.pop('web.listen-address') %} +prometheus-exporters-node-args-web-listen-address: + sysrc.managed: + - name: node_exporter_listen_address + - value: {{ value }} + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} + +{%- if 'collector.textfile.directory' in args.keys() %} +{%- set value = args.pop('collector.textfile.directory') %} +prometheus-exporters-node-args-collector-textfile-directory: + sysrc.managed: + - name: node_exporter_textfile_dir + - value: {{ value }} + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} + +prometheus-exporters-node-args: + sysrc.managed: + - name: node_exporter_args + # service node_exporter restart tended to hang on FreeBSD + # https://github.com/saltstack/salt/issues/44848#issuecomment-487016414 + - value: "{{ concat_args(args|dictsort) }} >/dev/null 2>&1" + - watch_in: + - service: prometheus-exporters-node-service-running + +{# Debian #} +{%- elif salt['grains.get']('os_family') == 'Debian'%} +prometheus-exporters-node-args: + file.managed: + - name: {{ prometheus.exporters.node.config_file }} + - contents: | + ARGS="{{ concat_args(args) }}" + - watch_in: + - service: prometheus-exporters-node-service-running +{%- endif %} +{%- endif %} + +prometheus-exporters-node-service-running: + service.running: + - name: {{ prometheus.exporters.node.service }} + - enable: True + - watch: + - pkg: prometheus-exporters-node-pkg-installed diff --git a/prometheus/osfamilymap.yaml b/prometheus/osfamilymap.yaml index b460866..66e11d5 100644 --- a/prometheus/osfamilymap.yaml +++ b/prometheus/osfamilymap.yaml @@ -10,7 +10,10 @@ # you will need to provide at least an empty dict in this file, e.g. # osfamilymap: {} --- -Debian: {} +Debian: + exporters: + node: + config_file: /etc/default/prometheus-node-exporter RedHat: {} @@ -27,6 +30,10 @@ FreeBSD: config_file: /usr/local/etc/prometheus.yml service: sysrc: True + exporters: + node: + pkg: node_exporter + service: node_exporter OpenBSD: rootgroup: wheel diff --git a/test/integration/default/controls/packages_spec.rb b/test/integration/default/controls/packages_spec.rb index 70d9cbe..87849de 100644 --- a/test/integration/default/controls/packages_spec.rb +++ b/test/integration/default/controls/packages_spec.rb @@ -4,4 +4,8 @@ control 'Prometheus package' do describe package('prometheus') do it { should be_installed } end + + describe package('prometheus-node-exporter') do + it { should be_installed } + end end diff --git a/test/integration/default/controls/services_spec.rb b/test/integration/default/controls/services_spec.rb index aecbca4..d3d79ce 100644 --- a/test/integration/default/controls/services_spec.rb +++ b/test/integration/default/controls/services_spec.rb @@ -6,4 +6,9 @@ control 'Prometheus service' do it { should be_enabled } it { should be_running } end + + describe service('prometheus-node-exporter') do + it { should be_enabled } + it { should be_running } + end end