From 82d71045e753bd18586b4a8e60a4750444874ba7 Mon Sep 17 00:00:00 2001 From: Adama DIENG Date: Fri, 15 Apr 2022 10:21:22 +0200 Subject: [PATCH 1/2] feat(collectd): add missing parameters for PostgreSQL plugin --- collectd/files/postgresql.conf | 53 +++++++++++++++++++- collectd/map.jinja | 9 +++- collectd/postgresql.sls | 6 +++ kitchen.yml | 3 +- pillar.example | 39 ++++++++++++-- test/integration/default/controls/service.rb | 30 +++++++++++ test/salt/default/pillar/collectd.sls | 0 test/salt/pillar/default/collectd.sls | 40 +++++++++++++++ 8 files changed, 171 insertions(+), 9 deletions(-) delete mode 100644 test/salt/default/pillar/collectd.sls create mode 100644 test/salt/pillar/default/collectd.sls diff --git a/collectd/files/postgresql.conf b/collectd/files/postgresql.conf index ca272a72..f2a5a83d 100644 --- a/collectd/files/postgresql.conf +++ b/collectd/files/postgresql.conf @@ -8,14 +8,63 @@ LoadPlugin postgresql - + +{%- for query in collectd_settings.plugins.postgresql.queries %} + + {%- if query.statement is defined and query.statement %} + Statement "{{ query.statement }}" + {%- endif %} + {%- if query.param is defined and query.param %} + Param "{{ query.param }}" + {%- endif %} + {%- for result in query.results %} + + {%- if result.type is defined and result.type %} + Type {{ result.type }} + {%- endif %} + {%- if result.instanceprefix is defined and result.instanceprefix %} + InstancePrefix "{{ result.instanceprefix }}" + {%- endif %} + {%- if result.instancesfrom is defined and result.instancesfrom %} + InstancesFrom "{{ result.instancesfrom }}" + {%- endif %} + {%- if result.valuesfrom is defined and result.valuesfrom %} + ValuesFrom "{{ result.valuesfrom }}" + {%- endif %} + + {%- endfor %} + +{%- endfor %} + {%- for db in collectd_settings.plugins.postgresql.databases %} + {%- if db.host is defined and db.host %} Host "{{ db.host }}" + {%- endif %} + {%- if db.port is defined and db.port %} Port "{{ db.port }}" + {%- endif %} + {%- if db.user is defined and db.user %} User "{{ db.user }}" + {%- endif %} + {%- if db.pass is defined and db.pass %} Password "{{ db.pass }}" - Database "{{ db.name }}" + {%- endif %} + {%- if db.sslmode is defined and db.sslmode %} + SSLMode "{{ db.sslmode }}" + {%- endif %} + {%- if db.krbsrvname is defined and db.krbsrvname %} + KRBSrvName "{{ db.krbsrvname }}" + {%- endif %} + {%- if db.service is defined and db.service %} + Service "{{ db.service }}" + {%- endif %} + {%- if db.interval is defined and db.interval %} + Interval "{{ db.interval }}" + {%- endif %} + {%- for query in db.queries %} + Query {{ query }} + {%- endfor %} {%- endfor %} diff --git a/collectd/map.jinja b/collectd/map.jinja index 6959c319..de06033b 100644 --- a/collectd/map.jinja +++ b/collectd/map.jinja @@ -9,6 +9,7 @@ 'moduledirconfig': '/usr/share/collectd/modules', 'user': 'root', 'group': 'root', + 'plugin_postgresql': {'pkg': 'postgresql-client'}, }, 'RedHat': { 'config': '/etc/collectd.conf', @@ -19,6 +20,7 @@ 'moduledirconfig': '/usr/share/collectd/modules', 'user': 'root', 'group': 'root', + 'plugin_postgresql': {'pkg': 'collectd-postgresql'}, }, 'FreeBSD': { 'config': '/usr/local/etc/collectd.conf', @@ -29,6 +31,7 @@ 'moduledirconfig': '/usr/local/share/collectd/modules', 'user': 'root', 'group': 'wheel', + 'plugin_postgresql': {'pkg': 'postgresql-client'}, }, 'Suse': { 'config': '/etc/collectd.conf', @@ -39,6 +42,7 @@ 'moduledirconfig': '/usr/share/collectd/modules', 'user': 'root', 'group': 'root', + 'plugin_postgresql': {'pkg': 'collectd-plugin-postgresql'}, }, }, merge=salt['pillar.get']('collectd:lookup')) %} @@ -147,7 +151,8 @@ 'hosts_from_grains': {}, }, 'postgresql': { - 'databases': [] + 'databases': [], + 'queries' : [] }, 'powerdns': { 'socket': '/var/run/pdns.controlsocket' @@ -246,4 +251,4 @@ {% do default_settings.collectd.update(os_map) %} {# Update settings defaults from pillar data #} -{% set collectd_settings = salt['pillar.get']('collectd', default=default_settings.collectd, merge=True) %} +{% set collectd_settings = salt['pillar.get']('collectd', default=default_settings.collectd, merge=True) %} \ No newline at end of file diff --git a/collectd/postgresql.sls b/collectd/postgresql.sls index 8261ed53..1e3a455a 100644 --- a/collectd/postgresql.sls +++ b/collectd/postgresql.sls @@ -4,6 +4,10 @@ include: - collectd +collectd-postgresql-pkg-installed: + pkg.installed: + - name: {{ collectd_settings.plugin_postgresql.pkg }} + {{ collectd_settings.plugindirconfig }}/postgresql.conf: file.managed: - source: salt://collectd/files/postgresql.conf @@ -13,3 +17,5 @@ include: - template: jinja - watch_in: - service: collectd-service + - require: + - pkg: collectd-postgresql-pkg-installed \ No newline at end of file diff --git a/kitchen.yml b/kitchen.yml index 568c42e7..dd10a3a3 100644 --- a/kitchen.yml +++ b/kitchen.yml @@ -298,13 +298,14 @@ suites: '*': - collectd._mapdata - collectd + - collectd.postgresql pillars: top.sls: base: '*': - collectd pillars_from_files: - collectd.sls: test/salt/default/pillar/collectd.sls + collectd.sls: test/salt/pillar/default/collectd.sls verifier: inspec_tests: - path: test/integration/default diff --git a/pillar.example b/pillar.example index 4744e44d..0c425841 100644 --- a/pillar.example +++ b/pillar.example @@ -126,11 +126,42 @@ collectd: slavenotifications: true postgresql: databases: - - host: 'localhost' + - name: 'foo' + host: 'hostname' port: '5432' - user: 'myuser' - pass: 'mypass' - name: 'mydb' + user: 'username' + pass: 'secret' + sslmode: 'prefer' + krbsrvname: 'kerberos_service_name' + service: 'service_name' + queries: + - 'magic' + - name: 'bar' + service: 'service_name' + queries: + - 'backend' + - 'rt36_tickets' + queries: + - name: 'magic' + statement: "SELECT magic FROM wizard WHERE host = $1;" + param: 'hostname' + results: + - type: 'gauge' + instanceprefix: 'magic' + valuesfrom: 'magic' + - name: 'rt36_tickets' + statement: | + SELECT COUNT(type) AS count, type + FROM (SELECT CASE + WHEN resolved = 'epoch' THEN 'open' + ELSE 'resolved' END AS type + FROM tickets) type + GROUP BY type; + results: + - type: 'counter' + instanceprefix: 'rt36_tickets' + instancesfrom: 'type' + valuesfrom: 'count' powerdns: socket: '/var/run/pdns.controlsocket' protocols: diff --git a/test/integration/default/controls/service.rb b/test/integration/default/controls/service.rb index 3e7e1dfa..461894e1 100644 --- a/test/integration/default/controls/service.rb +++ b/test/integration/default/controls/service.rb @@ -1,5 +1,15 @@ # frozen_string_literal: true +config_plugin_file = + case platform[:family] + when 'redhat', 'fedora' + '/etc/collectd.d/postgresql.conf' + when 'suse' + '/usr/lib64/collectd/postgresql.conf' + else + '/etc/collectd/plugins/postgresql.conf' + end + control 'Collectd service' do title 'should be running and enabled' @@ -8,4 +18,24 @@ it { should be_enabled } it { should be_running } end + + describe file(config_plugin_file) do + its('owner') { should eq 'root' } + its('group') { should eq 'root' } + it { should be_readable.by('others') } + it { should_not be_writable.by('others') } + it { should_not be_executable.by('others') } + end + + describe file(config_plugin_file) do + it { should exist } + it { should be_file } + its(:content) { should match(//) } + its(:content) { should match(//) } + its(:content) { should match(//) } + + its(:content) { should match(//) } + its(:content) { should match(//) } + its(:content) { should match(/Statement */) } + end end diff --git a/test/salt/default/pillar/collectd.sls b/test/salt/default/pillar/collectd.sls deleted file mode 100644 index e69de29b..00000000 diff --git a/test/salt/pillar/default/collectd.sls b/test/salt/pillar/default/collectd.sls new file mode 100644 index 00000000..6531e581 --- /dev/null +++ b/test/salt/pillar/default/collectd.sls @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- +# vim: ft=yaml +--- +collectd: + FQDNLookup: true + plugins: + postgresql: + databases: + - name: 'foo' + host: 'hostname' + port: '5432' + user: 'username' + pass: 'secret' + sslmode: 'prefer' + krbsrvname: 'kerberos_service_name' + service: 'service_name' + interval: '3600' + queries: + - 'magic' + - name: 'bar' + service: 'service_name' + queries: + - 'rt36_tickets' + - 'backend' + queries: + - name: 'magic' + statement: 'mystatement' + param: 'hostname' + results: + - type: 'gauge' + instanceprefix: 'magic' + valuesfrom: 'count' + - name: 'rt36_tickets' + statement: 'mystatement' + param: 'hostname' + results: + - type: 'counter' + instanceprefix: 'rt36_tickets' + instancesfrom: 'type' + valuesfrom: 'count' From 31fa9fe67adfc1353ef949f1fa2513256723729b Mon Sep 17 00:00:00 2001 From: Adama DIENG Date: Tue, 24 May 2022 11:55:48 +0200 Subject: [PATCH 2/2] fix(pre-commit): run end-of-file-fixer & trailing-whitespace [skip ci] * https://gitlab.com/myii/collectd-formula/-/jobs/2492752465 -- before * https://gitlab.com/myii/collectd-formula/-/jobs/2498876164 -- after --- collectd/files/postgresql.conf | 22 +++++++++++----------- collectd/map.jinja | 2 +- collectd/openldap.sls | 1 - collectd/openvpn.sls | 1 - collectd/postgresql.sls | 3 ++- collectd/rabbitmq.sls | 1 - collectd/redis.sls | 1 - collectd/tcpconns.sls | 1 - collectd/zookeeper.conf | 1 - 9 files changed, 14 insertions(+), 19 deletions(-) diff --git a/collectd/files/postgresql.conf b/collectd/files/postgresql.conf index f2a5a83d..210f6690 100644 --- a/collectd/files/postgresql.conf +++ b/collectd/files/postgresql.conf @@ -13,52 +13,52 @@ LoadPlugin postgresql {%- if query.statement is defined and query.statement %} Statement "{{ query.statement }}" - {%- endif %} + {%- endif %} {%- if query.param is defined and query.param %} Param "{{ query.param }}" {%- endif %} - {%- for result in query.results %} + {%- for result in query.results %} {%- if result.type is defined and result.type %} Type {{ result.type }} {%- endif %} {%- if result.instanceprefix is defined and result.instanceprefix %} InstancePrefix "{{ result.instanceprefix }}" - {%- endif %} + {%- endif %} {%- if result.instancesfrom is defined and result.instancesfrom %} InstancesFrom "{{ result.instancesfrom }}" - {%- endif %} + {%- endif %} {%- if result.valuesfrom is defined and result.valuesfrom %} ValuesFrom "{{ result.valuesfrom }}" {%- endif %} {%- endfor %} - + {%- endfor %} {%- for db in collectd_settings.plugins.postgresql.databases %} {%- if db.host is defined and db.host %} Host "{{ db.host }}" - {%- endif %} + {%- endif %} {%- if db.port is defined and db.port %} Port "{{ db.port }}" - {%- endif %} + {%- endif %} {%- if db.user is defined and db.user %} User "{{ db.user }}" - {%- endif %} + {%- endif %} {%- if db.pass is defined and db.pass %} Password "{{ db.pass }}" - {%- endif %} + {%- endif %} {%- if db.sslmode is defined and db.sslmode %} SSLMode "{{ db.sslmode }}" - {%- endif %} + {%- endif %} {%- if db.krbsrvname is defined and db.krbsrvname %} KRBSrvName "{{ db.krbsrvname }}" {%- endif %} {%- if db.service is defined and db.service %} Service "{{ db.service }}" - {%- endif %} + {%- endif %} {%- if db.interval is defined and db.interval %} Interval "{{ db.interval }}" {%- endif %} diff --git a/collectd/map.jinja b/collectd/map.jinja index de06033b..9eb9fae6 100644 --- a/collectd/map.jinja +++ b/collectd/map.jinja @@ -251,4 +251,4 @@ {% do default_settings.collectd.update(os_map) %} {# Update settings defaults from pillar data #} -{% set collectd_settings = salt['pillar.get']('collectd', default=default_settings.collectd, merge=True) %} \ No newline at end of file +{% set collectd_settings = salt['pillar.get']('collectd', default=default_settings.collectd, merge=True) %} diff --git a/collectd/openldap.sls b/collectd/openldap.sls index 38021275..c5c0a341 100644 --- a/collectd/openldap.sls +++ b/collectd/openldap.sls @@ -12,4 +12,3 @@ include: - template: jinja - watch_in: - service: collectd-service - diff --git a/collectd/openvpn.sls b/collectd/openvpn.sls index 3d77f9b4..0964ee70 100644 --- a/collectd/openvpn.sls +++ b/collectd/openvpn.sls @@ -12,4 +12,3 @@ include: - template: jinja - watch_in: - service: collectd-service - diff --git a/collectd/postgresql.sls b/collectd/postgresql.sls index 1e3a455a..ab27e358 100644 --- a/collectd/postgresql.sls +++ b/collectd/postgresql.sls @@ -4,6 +4,7 @@ include: - collectd +# Install the required dependency package corresponding to the OS collectd-postgresql-pkg-installed: pkg.installed: - name: {{ collectd_settings.plugin_postgresql.pkg }} @@ -18,4 +19,4 @@ collectd-postgresql-pkg-installed: - watch_in: - service: collectd-service - require: - - pkg: collectd-postgresql-pkg-installed \ No newline at end of file + - pkg: collectd-postgresql-pkg-installed diff --git a/collectd/rabbitmq.sls b/collectd/rabbitmq.sls index ca12f88e..6df01b83 100644 --- a/collectd/rabbitmq.sls +++ b/collectd/rabbitmq.sls @@ -20,4 +20,3 @@ collectd-rabbitmq-module: - template: jinja - watch_in: - service: collectd-service - diff --git a/collectd/redis.sls b/collectd/redis.sls index 381c1772..2c4ca220 100644 --- a/collectd/redis.sls +++ b/collectd/redis.sls @@ -15,4 +15,3 @@ libhiredis0.13: - template: jinja - watch_in: - service: collectd-service - diff --git a/collectd/tcpconns.sls b/collectd/tcpconns.sls index 4b62773a..d7582b56 100644 --- a/collectd/tcpconns.sls +++ b/collectd/tcpconns.sls @@ -12,4 +12,3 @@ include: - template: jinja - watch_in: - service: collectd-service - diff --git a/collectd/zookeeper.conf b/collectd/zookeeper.conf index c0f683b2..bf26ba7b 100644 --- a/collectd/zookeeper.conf +++ b/collectd/zookeeper.conf @@ -12,4 +12,3 @@ include: - template: jinja - watch_in: - service: collectd-service -