Skip to content

Commit

Permalink
feat(postgresql): postgresqlplugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Adama DIENG committed Apr 26, 2022
1 parent ce1cef0 commit 6373377
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 8 deletions.
56 changes: 53 additions & 3 deletions collectd/files/postgresql.conf
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,66 @@
# Modify the config that generates this file instead
#

LoadPlugin postgresql
<LoadPlugin postgresql>
</LoadPlugin>

<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.instancefrom is defined and result.instancefrom %}
InstancesFrom "{{ result.instancefrom }}"
{%- endif %}
{%- if result.valuefrom is defined and result.valuefrom %}
ValuesFrom "{{ result.valuefrom }}"
{%- endif %}
</Result>
{%- endfor %}
</Query>
{%- endfor %}

<Plugin "postgresql">
{%- for db in collectd_settings.plugins.postgresql.databases %}
<Database {{ db.name }}>
{%- if db.port is defined and db.port %}
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>
3 changes: 2 additions & 1 deletion collectd/map.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
'hosts_from_grains': {},
},
'postgresql': {
'databases': []
'databases': [],
'queries' : []
},
'powerdns': {
'socket': '/var/run/pdns.controlsocket'
Expand Down
38 changes: 34 additions & 4 deletions pillar.example
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,41 @@ 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'
valuefrom: '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'
instancefrom: 'type'
valuefrom: 'count'
powerdns:
socket: '/var/run/pdns.controlsocket'
protocols:
Expand Down
41 changes: 41 additions & 0 deletions test/salt/default/pillar/collectd.sls
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- 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'
valuefrom: 'count'
- name: 'rt36_tickets'
statement: 'mystatement'
param: 'hostname'
results:
- type: 'counter'
instanceprefix: 'rt36_tickets'
instancefrom: 'type'
valuefrom: 'count'

0 comments on commit 6373377

Please sign in to comment.