Skip to content

Commit b52d7ea

Browse files
marcinhlybinmajormoses
authored andcommitted
Add pgpass file and env variables support. (#36)
* Add pgpass file and env variables support. Default pgpass file path is ~/.pgpass Supported PG variables: * PGHOST * PGPORT * PGDATABASE * PGUSER * PGPASSWORD * Fix parentheses to pass RuboCop checks * Add usage and known issues * Read only first line from pgpass file * Add example with PGPASSWORD env variable
1 parent 382d6aa commit b52d7ea

16 files changed

+260
-76
lines changed

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,85 @@
2525

2626
## Usage
2727

28+
Use `--help` to see command arguments.
29+
30+
### Sample usage
31+
32+
#### Check if PostgreSQL is alive
33+
```
34+
$ check-postgres-alive.rb -d template1 -f /var/lib/postgresql/.pgpass
35+
CheckPostgres OK: Server version: {"version"=>"PostgreSQL 9.6.3 on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit"}
36+
```
37+
38+
### Check replication status
39+
```
40+
$ check-postgres-replication.rb -m psql1.local -s psql2.local -d template1 -w 5 -c 10
41+
CheckPostgresReplicationStatus OK: replication delayed by 0.0MB :: master:B0/B4031000 slave:B0/B4031000 m_segbytes:16777216
42+
```
43+
44+
### Check number of connections
45+
```
46+
$ export PGPASSWORD=this-is-secret-password
47+
$ check-postgres-connections.rb -a -w 80 -c 90 -d template1 -u sensu
48+
CheckPostgresConnections OK: PostgreSQL connections under threshold: 17%, 174 out of 997 connections
49+
```
50+
51+
### Default values
52+
53+
| Argument | Env variable | Value |
54+
|----------------|--------------|-----------|
55+
| -f, --pgpass | PGPASSFILE | ~/.pgpass |
56+
| -h, --hostname | PGHOST | localhost |
57+
| -P, --port | PGPORT | 5432 |
58+
| -d, --database | PGDATABASE | postgres |
59+
| -u, --user | PGUSER | postgres |
60+
| -p, --password | PGPASSWORD | |
61+
62+
Options precedence is following:
63+
1. command line arguments
64+
1. pgpass file
65+
1. env variables
66+
1. defaults
67+
68+
### Pgpass file
69+
70+
When file `~/.pgpass` is found it is used by default. You can also use `-f, --pgpass` command line argument or set `PGPASSFILE` env variable.
71+
72+
File format is:
73+
74+
```
75+
hostname:port:database:username:password
76+
```
77+
78+
Only first line is used. If database is set to `*` it is ommited.
79+
80+
You can ovveride `pgpass` values with command line arguments, e.g. `-h` for hostname.
81+
2882
## Installation
2983

30-
[Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html)
84+
```
85+
gem install sensu-plugins-postgres
86+
```
87+
88+
See [Installation and Setup](http://sensu-plugins.io/docs/installation_instructions.html) for details.
89+
90+
### Known issues
91+
92+
When using Sensu with `EMBEDDED_RUBY=true` and installing Postgres checks with `/usr/bin/sensu-install -p sensu-plugins-postgres` you might get following error:
93+
94+
```
95+
ERROR: Error installing sensu-plugins-postgres:
96+
ERROR: Failed to build gem native extension.
97+
[...]
98+
checking for PQconnectdb() in -lpq... no
99+
checking for PQconnectdb() in -llibpq... no
100+
checking for PQconnectdb() in -lms/libpq... no
101+
Can't find the PostgreSQL client library (libpq)
102+
*** extconf.rb failed ***
103+
```
104+
105+
The reason is that **libssl** library which comes with Sensu is incompatible with **libpq** library installed on your system. There is no easy way to fix it. You might want to install sensu-plugins-postgres globally with `gem install sensu-plugins-postgres`.
106+
107+
Checks are in `/usr/local/bin` directory.
31108

32109
## Notes

bin/check-postgres-alive.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,17 @@
2727
# for details.
2828
#
2929

30+
require 'sensu-plugins-postgres/pgpass'
3031
require 'sensu-plugin/check/cli'
3132
require 'pg'
3233

3334
class CheckPostgres < Sensu::Plugin::Check::CLI
35+
option :pgpass,
36+
description: 'Pgpass file',
37+
short: '-f FILE',
38+
long: '--pgpass',
39+
default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
40+
3441
option :user,
3542
description: 'Postgres User',
3643
short: '-u USER',
@@ -49,22 +56,23 @@ class CheckPostgres < Sensu::Plugin::Check::CLI
4956
option :database,
5057
description: 'Database schema to connect to',
5158
short: '-d DATABASE',
52-
long: '--database DATABASE',
53-
default: 'test'
59+
long: '--database DATABASE'
5460

5561
option :port,
5662
description: 'Database port',
5763
short: '-P PORT',
58-
long: '--port PORT',
59-
default: 5432
64+
long: '--port PORT'
6065

6166
option :timeout,
6267
description: 'Connection timeout (seconds)',
6368
short: '-T TIMEOUT',
6469
long: '--timeout TIMEOUT',
6570
default: nil
6671

72+
include Pgpass
73+
6774
def run
75+
pgpass
6876
con = PG.connect(host: config[:hostname],
6977
dbname: config[:database],
7078
user: config[:user],

bin/check-postgres-connections.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,17 @@
3232
# for details.
3333
#
3434

35+
require 'sensu-plugins-postgres/pgpass'
3536
require 'sensu-plugin/check/cli'
3637
require 'pg'
3738

3839
class CheckPostgresConnections < Sensu::Plugin::Check::CLI
40+
option :pgpass,
41+
description: 'Pgpass file',
42+
short: '-f FILE',
43+
long: '--pgpass',
44+
default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
45+
3946
option :user,
4047
description: 'Postgres User',
4148
short: '-u USER',
@@ -49,20 +56,17 @@ class CheckPostgresConnections < Sensu::Plugin::Check::CLI
4956
option :hostname,
5057
description: 'Hostname to login to',
5158
short: '-h HOST',
52-
long: '--hostname HOST',
53-
default: 'localhost'
59+
long: '--hostname HOST'
5460

5561
option :port,
5662
description: 'Database port',
5763
short: '-P PORT',
58-
long: '--port PORT',
59-
default: 5432
64+
long: '--port PORT'
6065

6166
option :database,
6267
description: 'Database name',
6368
short: '-d DB',
64-
long: '--db DB',
65-
default: 'postgres'
69+
long: '--db DB'
6670

6771
option :warning,
6872
description: 'Warning threshold number or % of connections. (default: 200 connections)',
@@ -91,8 +95,11 @@ class CheckPostgresConnections < Sensu::Plugin::Check::CLI
9195
long: '--timeout TIMEOUT',
9296
default: nil
9397

98+
include Pgpass
99+
94100
def run
95101
begin
102+
pgpass
96103
con = PG.connect(host: config[:hostname],
97104
dbname: config[:database],
98105
user: config[:user],

bin/check-postgres-query.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@
2929
# for details.
3030
#
3131

32+
require 'sensu-plugins-postgres/pgpass'
3233
require 'sensu-plugin/check/cli'
3334
require 'pg'
3435
require 'dentaku'
3536

3637
# Check PostgresSQL Query
3738
class CheckPostgresQuery < Sensu::Plugin::Check::CLI
39+
option :pgpass,
40+
description: 'Pgpass file',
41+
short: '-f FILE',
42+
long: '--pgpass',
43+
default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
44+
3845
option :user,
3946
description: 'Postgres User',
4047
short: '-u USER',
@@ -48,20 +55,17 @@ class CheckPostgresQuery < Sensu::Plugin::Check::CLI
4855
option :hostname,
4956
description: 'Hostname to login to',
5057
short: '-h HOST',
51-
long: '--hostname HOST',
52-
default: 'localhost'
58+
long: '--hostname HOST'
5359

5460
option :port,
5561
description: 'Database port',
5662
short: '-P PORT',
57-
long: '--port PORT',
58-
default: 5432
63+
long: '--port PORT'
5964

6065
option :database,
6166
description: 'Database name',
6267
short: '-d DB',
63-
long: '--db DB',
64-
default: 'postgres'
68+
long: '--db DB'
6569

6670
option :query,
6771
description: 'Database query to execute',
@@ -94,8 +98,11 @@ class CheckPostgresQuery < Sensu::Plugin::Check::CLI
9498
long: '--timeout TIMEOUT',
9599
default: nil
96100

101+
include Pgpass
102+
97103
def run
98104
begin
105+
pgpass
99106
con = PG.connect(host: config[:hostname],
100107
dbname: config[:database],
101108
user: config[:user],

bin/check-postgres-replication.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,17 @@
2626
# for details.
2727
#
2828

29+
require 'sensu-plugins-postgres/pgpass'
2930
require 'sensu-plugin/check/cli'
3031
require 'pg'
3132

3233
class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI
34+
option :pgpass,
35+
description: 'Pgpass file',
36+
short: '-f FILE',
37+
long: '--pgpass',
38+
default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
39+
3340
option(:master_host,
3441
short: '-m',
3542
long: '--master-host=HOST',
@@ -44,8 +51,7 @@ class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI
4451
option(:port,
4552
short: '-P',
4653
long: '--port=PORT',
47-
description: 'PostgreSQL port',
48-
default: 5432)
54+
description: 'PostgreSQL port')
4955

5056
option(:database,
5157
short: '-d',
@@ -90,6 +96,8 @@ class CheckPostgresReplicationStatus < Sensu::Plugin::Check::CLI
9096
default: nil,
9197
description: 'Connection timeout (seconds)')
9298

99+
include Pgpass
100+
93101
def compute_lag(master, slave, m_segbytes)
94102
m_segment, m_offset = master.split('/')
95103
s_segment, s_offset = slave.split('/')
@@ -100,6 +108,7 @@ def run
100108
ssl_mode = config[:ssl] ? 'require' : 'prefer'
101109

102110
# Establishing connection to the master
111+
pgpass
103112
conn_master = PG.connect(host: config[:master_host],
104113
dbname: config[:database],
105114
user: config[:user],

bin/metric-postgres-connections.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
# for details.
2929
#
3030

31+
require 'sensu-plugins-postgres/pgpass'
3132
require 'sensu-plugin/metric/cli'
3233
require 'pg'
3334
require 'socket'
3435

3536
class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
37+
option :pgpass,
38+
description: 'Pgpass file',
39+
short: '-f FILE',
40+
long: '--pgpass',
41+
default: "#{ENV['HOME']}/.pgpass"
42+
3643
option :user,
3744
description: 'Postgres User',
3845
short: '-u USER',
@@ -46,20 +53,17 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
4653
option :hostname,
4754
description: 'Hostname to login to',
4855
short: '-h HOST',
49-
long: '--hostname HOST',
50-
default: 'localhost'
56+
long: '--hostname HOST'
5157

5258
option :port,
5359
description: 'Database port',
5460
short: '-P PORT',
55-
long: '--port PORT',
56-
default: 5432
61+
long: '--port PORT'
5762

5863
option :database,
5964
description: 'Database name',
6065
short: '-d DB',
61-
long: '--db DB',
62-
default: 'postgres'
66+
long: '--db DB'
6367

6468
option :scheme,
6569
description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
@@ -72,9 +76,11 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
7276
long: '--timeout TIMEOUT',
7377
default: nil
7478

79+
include Pgpass
80+
7581
def run
7682
timestamp = Time.now.to_i
77-
83+
pgpass
7884
con = PG.connect(host: config[:hostname],
7985
dbname: config[:database],
8086
user: config[:user],

bin/metric-postgres-dbsize.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@
2828
# for details.
2929
#
3030

31+
require 'sensu-plugins-postgres/pgpass'
3132
require 'sensu-plugin/metric/cli'
3233
require 'pg'
3334
require 'socket'
3435

3536
class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
37+
option :pgpass,
38+
description: 'Pgpass file',
39+
short: '-f FILE',
40+
long: '--pgpass',
41+
default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
42+
3643
option :user,
3744
description: 'Postgres User',
3845
short: '-u USER',
@@ -46,20 +53,17 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
4653
option :hostname,
4754
description: 'Hostname to login to',
4855
short: '-h HOST',
49-
long: '--hostname HOST',
50-
default: 'localhost'
56+
long: '--hostname HOST'
5157

5258
option :port,
5359
description: 'Database port',
5460
short: '-P PORT',
55-
long: '--port PORT',
56-
default: 5432
61+
long: '--port PORT'
5762

5863
option :database,
5964
description: 'Database name',
6065
short: '-d DB',
61-
long: '--db DB',
62-
default: 'postgres'
66+
long: '--db DB'
6367

6468
option :scheme,
6569
description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
@@ -72,9 +76,11 @@ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
7276
long: '--timeout TIMEOUT',
7377
default: nil
7478

79+
include Pgpass
80+
7581
def run
7682
timestamp = Time.now.to_i
77-
83+
pgpass
7884
con = PG.connect(host: config[:hostname],
7985
dbname: config[:database],
8086
user: config[:user],

0 commit comments

Comments
 (0)