Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing parameters in postgresql plugin config #157

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion collectd/openldap.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ include:
- template: jinja
- watch_in:
- service: collectd-service

1 change: 0 additions & 1 deletion collectd/openvpn.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ include:
- template: jinja
- watch_in:
- service: collectd-service

7 changes: 7 additions & 0 deletions collectd/postgresql.sls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
include:
- collectd

# Install the required dependency package corresponding to the OS
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 +18,5 @@ include:
- template: jinja
- watch_in:
- service: collectd-service
- require:
- pkg: collectd-postgresql-pkg-installed
1 change: 0 additions & 1 deletion collectd/rabbitmq.sls
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ collectd-rabbitmq-module:
- template: jinja
- watch_in:
- service: collectd-service

1 change: 0 additions & 1 deletion collectd/redis.sls
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ libhiredis0.13:
- template: jinja
- watch_in:
- service: collectd-service

1 change: 0 additions & 1 deletion collectd/tcpconns.sls
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ include:
- template: jinja
- watch_in:
- service: collectd-service

1 change: 0 additions & 1 deletion collectd/zookeeper.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ include:
- template: jinja
- watch_in:
- service: collectd-service

3 changes: 2 additions & 1 deletion kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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'