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'