Skip to content

Commit

Permalink
feat(postgresql): add missing parameters for PostgreSQL plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Adama DIENG authored and myii committed May 20, 2022
1 parent ce1cef0 commit b4e229c
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 9 deletions.
53 changes: 51 additions & 2 deletions collectd/files/postgresql.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,63 @@

LoadPlugin postgresql

<Plugin "postgresql">
<Plugin postgresql>
{%- for query in collectd_settings.plugins.postgresql.queries %}
<Query {{ query.name }}>
{%- 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 %}
<Result>
{%- 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 %}
</Result>
{%- endfor %}
</Query>
{%- endfor %}

{%- for db in collectd_settings.plugins.postgresql.databases %}
<Database {{ db.name }}>
{%- 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 %}
</Database>
{%- endfor %}
</Plugin>
7 changes: 6 additions & 1 deletion collectd/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'moduledirconfig': '/usr/share/collectd/modules',
'user': 'root',
'group': 'root',
'plugin_postgresql': {'pkg': 'postgresql-client'},
},
'RedHat': {
'config': '/etc/collectd.conf',
Expand All @@ -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',
Expand All @@ -29,6 +31,7 @@
'moduledirconfig': '/usr/local/share/collectd/modules',
'user': 'root',
'group': 'wheel',
'plugin_postgresql': {'pkg': 'postgresql-client'},
},
'Suse': {
'config': '/etc/collectd.conf',
Expand All @@ -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')) %}

Expand Down Expand Up @@ -147,7 +151,8 @@
'hosts_from_grains': {},
},
'postgresql': {
'databases': []
'databases': [],
'queries' : []
},
'powerdns': {
'socket': '/var/run/pdns.controlsocket'
Expand Down
7 changes: 6 additions & 1 deletion collectd/postgresql.sls
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
{%- from "collectd/map.jinja" import collectd_settings with context %}
{%- set postgresql_settings = collectd_settings.get('plugins:postgresql') %}
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
Expand All @@ -13,3 +16,5 @@ include:
- template: jinja
- watch_in:
- service: collectd-service
- require:
- pkg: collectd-postgresql-pkg-installed
3 changes: 2 additions & 1 deletion kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ suites:
state_top:
base:
'*':
- collectd.postgresql
- collectd._mapdata
- collectd
pillars:
Expand All @@ -304,7 +305,7 @@ suites:
'*':
- 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
39 changes: 35 additions & 4 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 30 additions & 0 deletions test/integration/default/controls/service.rb
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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(/<Plugin postgresql>/) }
its(:content) { should match(/<Database foo>/) }
its(:content) { should match(/<Database bar>/) }

its(:content) { should match(/<Query magic>/) }
its(:content) { should match(/<Query rt36_tickets>/) }
its(:content) { should match(/Statement */) }
end
end
Empty file.
40 changes: 40 additions & 0 deletions test/salt/pillar/default/collectd.sls
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit b4e229c

Please sign in to comment.