sensu-plugins-postgres-mrtrotl 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # metric-postgres-locks
6
+ #
7
+ # DESCRIPTION:
8
+ #
9
+ # This plugin collects postgres database lock metrics
10
+ #
11
+ # OUTPUT:
12
+ # metric data
13
+ #
14
+ # PLATFORMS:
15
+ # Linux
16
+ #
17
+ # DEPENDENCIES:
18
+ # gem: sensu-plugin
19
+ # gem: pg
20
+ #
21
+ # USAGE:
22
+ # ./metric-postgres-locks.rb -u db_user -p db_pass -h db_host -d db
23
+ #
24
+ # NOTES:
25
+ #
26
+ # LICENSE:
27
+ # Copyright (c) 2012 Kwarter, Inc <platforms@kwarter.com>
28
+ # Author Gilles Devaux <gilles.devaux@gmail.com>
29
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
30
+ # for details.
31
+ #
32
+
33
+ require 'sensu-plugins-postgres/pgpass'
34
+ require 'sensu-plugin/metric/cli'
35
+ require 'pg'
36
+ require 'socket'
37
+
38
+ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
39
+ option :pgpass,
40
+ description: 'Pgpass file',
41
+ short: '-f FILE',
42
+ long: '--pgpass',
43
+ default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
44
+
45
+ option :user,
46
+ description: 'Postgres User',
47
+ short: '-u USER',
48
+ long: '--user USER'
49
+
50
+ option :password,
51
+ description: 'Postgres Password',
52
+ short: '-p PASS',
53
+ long: '--password PASS'
54
+
55
+ option :hostname,
56
+ description: 'Hostname to login to',
57
+ short: '-h HOST',
58
+ long: '--hostname HOST'
59
+
60
+ option :port,
61
+ description: 'Database port',
62
+ short: '-P PORT',
63
+ long: '--port PORT'
64
+
65
+ option :database,
66
+ description: 'Database name',
67
+ short: '-d DB',
68
+ long: '--db DB'
69
+
70
+ option :scheme,
71
+ description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
72
+ long: '--scheme SCHEME',
73
+ default: "#{Socket.gethostname}.postgresql"
74
+
75
+ option :timeout,
76
+ description: 'Connection timeout (seconds)',
77
+ short: '-T TIMEOUT',
78
+ long: '--timeout TIMEOUT',
79
+ default: 10,
80
+ proc: proc(&:to_i)
81
+
82
+ include Pgpass
83
+
84
+ def run
85
+ timestamp = Time.now.to_i
86
+
87
+ locks_per_type = Hash.new(0)
88
+ pgpass
89
+ con = PG.connect(host: config[:hostname],
90
+ dbname: config[:database],
91
+ user: config[:user],
92
+ password: config[:password],
93
+ port: config[:port],
94
+ connect_timeout: config[:timeout])
95
+ request = [
96
+ 'SELECT mode, count(mode) AS count FROM pg_locks',
97
+ "WHERE database = (SELECT oid FROM pg_database WHERE datname = '#{config[:database]}')",
98
+ 'GROUP BY mode'
99
+ ]
100
+
101
+ con.exec(request.join(' ')) do |result|
102
+ result.each do |row|
103
+ lock_name = row['mode'].downcase.to_sym
104
+ locks_per_type[lock_name] = row['count']
105
+ end
106
+ end
107
+
108
+ locks_per_type.each do |lock_type, count|
109
+ output "#{config[:scheme]}.locks.#{config[:database]}.#{lock_type}", count, timestamp
110
+ end
111
+
112
+ ok
113
+ end
114
+ end
@@ -0,0 +1,119 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: false
3
+
4
+ # metrics-postgres-relation-size.rb
5
+ # DESCRIPTION:
6
+ #
7
+ # This plugin finds the total size of the largest tables.
8
+ #
9
+ # https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-DBOBJECT
10
+ #
11
+ # OUTPUT:
12
+ # metric data
13
+ #
14
+ # PLATFORMS:
15
+ # Linux
16
+ #
17
+ # DEPENDENCIES:
18
+ # gem: sensu-plugin
19
+ # gem: pg
20
+ #
21
+ # USAGE:
22
+ # ./metric-postgres-relation-size.rb -u db_user -p db_pass -h db_host -d db
23
+ #
24
+ # NOTES:
25
+ #
26
+ # LICENSE:
27
+ # Copyright (c) 2020 Airbrake Technologies, Inc <support@airbrake.io>
28
+ # Author Patrick Humpal <patrick@netvilla.net>
29
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
30
+ # for details.
31
+ #
32
+
33
+ require 'sensu-plugins-postgres/pgpass'
34
+ require 'sensu-plugin/metric/cli'
35
+ require 'pg'
36
+ require 'socket'
37
+
38
+ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
39
+ option :pgpass,
40
+ description: 'Pgpass file',
41
+ short: '-f FILE',
42
+ long: '--pgpass',
43
+ default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
44
+
45
+ option :user,
46
+ description: 'Postgres User',
47
+ short: '-u USER',
48
+ long: '--user USER'
49
+
50
+ option :password,
51
+ description: 'Postgres Password',
52
+ short: '-p PASS',
53
+ long: '--password PASS'
54
+
55
+ option :hostname,
56
+ description: 'Hostname to login to',
57
+ short: '-h HOST',
58
+ long: '--hostname HOST'
59
+
60
+ option :port,
61
+ description: 'Database port',
62
+ short: '-P PORT',
63
+ long: '--port PORT'
64
+
65
+ option :database,
66
+ description: 'Database name',
67
+ short: '-d DB',
68
+ long: '--db DB'
69
+
70
+ option :scheme,
71
+ description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
72
+ long: '--scheme SCHEME',
73
+ default: "#{Socket.gethostname}.postgresql"
74
+
75
+ option :timeout,
76
+ description: 'Connection timeout (seconds)',
77
+ short: '-T TIMEOUT',
78
+ long: '--timeout TIMEOUT',
79
+ default: 10,
80
+ proc: proc(&:to_i)
81
+
82
+ option :limit,
83
+ description: 'Limit query to this many results',
84
+ short: '-L LIMIT',
85
+ login: '--limit LIMIT',
86
+ default: 25
87
+
88
+ include Pgpass
89
+
90
+ def run
91
+ timestamp = Time.now.to_i
92
+ pgpass
93
+ con = PG.connect(host: config[:hostname],
94
+ dbname: config[:database],
95
+ user: config[:user],
96
+ password: config[:password],
97
+ port: config[:port],
98
+ connect_timeout: config[:timeout])
99
+
100
+ # https://wiki.postgresql.org/wiki/Disk_Usage
101
+ request = [
102
+ "SELECT nspname || '.' || relname AS relation,
103
+ pg_total_relation_size(C.oid) AS total_size
104
+ FROM pg_class C
105
+ LEFT JOIN pg_namespace N on (N.oid = C.relnamespace)
106
+ WHERE nspname NOT IN ('pg_catalog', 'information_schema')
107
+ ORDER BY pg_total_relation_size(C.oid) DESC
108
+ LIMIT '#{config[:limit]}'"
109
+ ]
110
+
111
+ con.exec(request.join(' ')) do |result|
112
+ result.each do |row|
113
+ output "#{config[:scheme]}.size.#{config[:database]}.#{row['relation']}", row['total_size'], timestamp
114
+ end
115
+ end
116
+
117
+ ok
118
+ end
119
+ end
@@ -0,0 +1,113 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # metric-postgres-statsbgwriter
6
+ #
7
+ # DESCRIPTION:
8
+ #
9
+ # This plugin collects postgres database bgwriter metrics
10
+ #
11
+ # OUTPUT:
12
+ # metric data
13
+ #
14
+ # PLATFORMS:
15
+ # Linux
16
+ #
17
+ # DEPENDENCIES:
18
+ # gem: sensu-plugin
19
+ # gem: pg
20
+ #
21
+ # USAGE:
22
+ # ./metric-postgres-statsbgwriter.rb -u db_user -p db_pass -h db_host -d db
23
+ #
24
+ # NOTES:
25
+ #
26
+ # LICENSE:
27
+ # Copyright (c) 2012 Kwarter, Inc <platforms@kwarter.com>
28
+ # Author Gilles Devaux <gilles.devaux@gmail.com>
29
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
30
+ # for details.
31
+ #
32
+
33
+ require 'sensu-plugins-postgres/pgpass'
34
+ require 'sensu-plugin/metric/cli'
35
+ require 'pg'
36
+ require 'socket'
37
+
38
+ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
39
+ option :pgpass,
40
+ description: 'Pgpass file',
41
+ short: '-f FILE',
42
+ long: '--pgpass',
43
+ default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
44
+
45
+ option :user,
46
+ description: 'Postgres User',
47
+ short: '-u USER',
48
+ long: '--user USER'
49
+
50
+ option :password,
51
+ description: 'Postgres Password',
52
+ short: '-p PASS',
53
+ long: '--password PASS'
54
+
55
+ option :hostname,
56
+ description: 'Hostname to login to',
57
+ short: '-h HOST',
58
+ long: '--hostname HOST'
59
+
60
+ option :port,
61
+ description: 'Database port',
62
+ short: '-P PORT',
63
+ long: '--port PORT'
64
+
65
+ option :scheme,
66
+ description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
67
+ long: '--scheme SCHEME',
68
+ default: "#{Socket.gethostname}.postgresql"
69
+
70
+ option :timeout,
71
+ description: 'Connection timeout (seconds)',
72
+ short: '-T TIMEOUT',
73
+ long: '--timeout TIMEOUT',
74
+ default: 10,
75
+ proc: proc(&:to_i)
76
+
77
+ include Pgpass
78
+
79
+ def run
80
+ timestamp = Time.now.to_i
81
+ pgpass
82
+ con = PG.connect(host: config[:hostname],
83
+ dbname: 'postgres',
84
+ user: config[:user],
85
+ password: config[:password],
86
+ port: config[:port],
87
+ connect_timeout: config[:timeout])
88
+ request = [
89
+ 'select checkpoints_timed, checkpoints_req,',
90
+ 'checkpoint_write_time, checkpoint_sync_time,',
91
+ 'buffers_checkpoint, buffers_clean,',
92
+ 'maxwritten_clean, buffers_backend, buffers_backend_fsync',
93
+ 'buffers_alloc',
94
+ 'from pg_stat_bgwriter'
95
+ ]
96
+ con.exec(request.join(' ')) do |result|
97
+ result.each do |row|
98
+ output "#{config[:scheme]}.bgwriter.checkpoints_timed", row['checkpoints_timed'], timestamp
99
+ output "#{config[:scheme]}.bgwriter.checkpoints_req", row['checkpoints_req'], timestamp
100
+ output "#{config[:scheme]}.bgwriter.checkpoints_write_time", row['checkpoint_write_time'], timestamp
101
+ output "#{config[:scheme]}.bgwriter.checkpoints_sync_time", row['checkpoint_sync_time'], timestamp
102
+ output "#{config[:scheme]}.bgwriter.buffers_checkpoint", row['buffers_checkpoint'], timestamp
103
+ output "#{config[:scheme]}.bgwriter.buffers_clean", row['buffers_clean'], timestamp
104
+ output "#{config[:scheme]}.bgwriter.maxwritten_clean", row['maxwritten_clean'], timestamp
105
+ output "#{config[:scheme]}.bgwriter.buffers_backend", row['buffers_backend'], timestamp
106
+ output "#{config[:scheme]}.bgwriter.buffers_backend_fsync", row['buffers_backend_fsync'], timestamp
107
+ output "#{config[:scheme]}.bgwriter.buffers_alloc", row['buffers_alloc'], timestamp
108
+ end
109
+ end
110
+
111
+ ok
112
+ end
113
+ end
@@ -0,0 +1,124 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # metric-postgres-statsdb
6
+ #
7
+ # DESCRIPTION:
8
+ #
9
+ # This plugin collects postgres database metrics from the pg_stat_database table
10
+ #
11
+ # OUTPUT:
12
+ # metric data
13
+ #
14
+ # PLATFORMS:
15
+ # Linux
16
+ #
17
+ # DEPENDENCIES:
18
+ # gem: sensu-plugin
19
+ # gem: pg
20
+ #
21
+ # USAGE:
22
+ # ./metric-postgres-statsdb.rb -u db_user -p db_pass -h db_host -d db
23
+ #
24
+ # NOTES:
25
+ # Requires PSQL `track_counts` `track_io_timing` for some metrics enabled
26
+ #
27
+ # LICENSE:
28
+ # Copyright (c) 2012 Kwarter, Inc <platforms@kwarter.com>
29
+ # Author Gilles Devaux <gilles.devaux@gmail.com>
30
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
31
+ # for details.
32
+ #
33
+
34
+ require 'sensu-plugins-postgres/pgpass'
35
+ require 'sensu-plugin/metric/cli'
36
+ require 'pg'
37
+ require 'socket'
38
+
39
+ class PostgresStatsDBMetrics < Sensu::Plugin::Metric::CLI::Graphite
40
+ option :pgpass,
41
+ description: 'Pgpass file',
42
+ short: '-f FILE',
43
+ long: '--pgpass',
44
+ default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
45
+
46
+ option :user,
47
+ description: 'Postgres User',
48
+ short: '-u USER',
49
+ long: '--user USER'
50
+
51
+ option :password,
52
+ description: 'Postgres Password',
53
+ short: '-p PASS',
54
+ long: '--password PASS'
55
+
56
+ option :hostname,
57
+ description: 'Hostname to login to',
58
+ short: '-h HOST',
59
+ long: '--hostname HOST'
60
+
61
+ option :port,
62
+ description: 'Database port',
63
+ short: '-P PORT',
64
+ long: '--port PORT'
65
+
66
+ option :database,
67
+ description: 'Database to connect on',
68
+ short: '-d DB',
69
+ long: '--db DB',
70
+ default: 'postgres'
71
+
72
+ option :all_databases,
73
+ description: 'Get stats for all available databases',
74
+ short: '-a',
75
+ long: '--all-databases',
76
+ boolean: true,
77
+ default: false
78
+
79
+ option :scheme,
80
+ description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
81
+ long: '--scheme SCHEME',
82
+ default: "#{Socket.gethostname}.postgresql"
83
+
84
+ option :timeout,
85
+ description: 'Connection timeout (seconds)',
86
+ short: '-T TIMEOUT',
87
+ long: '--timeout TIMEOUT',
88
+ default: 10,
89
+ proc: proc(&:to_i)
90
+
91
+ include Pgpass
92
+
93
+ def run
94
+ timestamp = Time.now.to_i
95
+ pgpass
96
+ con = PG.connect(host: config[:hostname],
97
+ dbname: config[:database],
98
+ user: config[:user],
99
+ password: config[:password],
100
+ port: config[:port],
101
+ connect_timeout: config[:timeout])
102
+
103
+ query = 'SELECT * FROM pg_stat_database'
104
+ params = []
105
+ unless config[:all_databases]
106
+ query += ' WHERE datname = $1'
107
+ params.push config[:database]
108
+ end
109
+
110
+ con.exec_params(query, params) do |result|
111
+ result.each do |row|
112
+ database = row['datname']
113
+
114
+ row.each do |key, value|
115
+ next if %w[datid datname stats_reset].include?(key)
116
+
117
+ output "#{config[:scheme]}.statsdb.#{database}.#{key}", value.to_s, timestamp
118
+ end
119
+ end
120
+ end
121
+
122
+ ok
123
+ end
124
+ end
@@ -0,0 +1,122 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: false
3
+
4
+ #
5
+ # metric-postgres-statsio
6
+ #
7
+ # DESCRIPTION:
8
+ #
9
+ # This plugin collects postgres database IO metrics
10
+ #
11
+ # OUTPUT:
12
+ # metric data
13
+ #
14
+ # PLATFORMS:
15
+ # Linux
16
+ #
17
+ # DEPENDENCIES:
18
+ # gem: sensu-plugin
19
+ # gem: pg
20
+ #
21
+ # USAGE:
22
+ # ./metric-postgres-statsio.rb -u db_user -p db_pass -h db_host -d db -s scope
23
+ #
24
+ # NOTES:
25
+ # Requires PSQL `track_io_timing` enabled
26
+ #
27
+ # LICENSE:
28
+ # Copyright (c) 2012 Kwarter, Inc <platforms@kwarter.com>
29
+ # Author Gilles Devaux <gilles.devaux@gmail.com>
30
+ # Released under the same terms as Sensu (the MIT license); see LICENSE
31
+ # for details.
32
+ #
33
+
34
+ require 'sensu-plugins-postgres/pgpass'
35
+ require 'sensu-plugin/metric/cli'
36
+ require 'pg'
37
+ require 'socket'
38
+
39
+ class PostgresStatsIOMetrics < Sensu::Plugin::Metric::CLI::Graphite
40
+ option :pgpass,
41
+ description: 'Pgpass file',
42
+ short: '-f FILE',
43
+ long: '--pgpass',
44
+ default: ENV['PGPASSFILE'] || "#{ENV['HOME']}/.pgpass"
45
+
46
+ option :user,
47
+ description: 'Postgres User',
48
+ short: '-u USER',
49
+ long: '--user USER'
50
+
51
+ option :password,
52
+ description: 'Postgres Password',
53
+ short: '-p PASS',
54
+ long: '--password PASS'
55
+
56
+ option :hostname,
57
+ description: 'Hostname to login to',
58
+ short: '-h HOST',
59
+ long: '--hostname HOST'
60
+
61
+ option :port,
62
+ description: 'Database port',
63
+ short: '-P PORT',
64
+ long: '--port PORT'
65
+
66
+ option :database,
67
+ description: 'Database name',
68
+ short: '-d DB',
69
+ long: '--db DB'
70
+
71
+ option :scope,
72
+ description: 'Scope, see http://www.postgresql.org/docs/9.2/static/monitoring-stats.html',
73
+ short: '-s SCOPE',
74
+ long: '--scope SCOPE',
75
+ default: 'user'
76
+
77
+ option :scheme,
78
+ description: 'Metric naming scheme, text to prepend to $queue_name.$metric',
79
+ long: '--scheme SCHEME',
80
+ default: "#{Socket.gethostname}.postgresql"
81
+
82
+ option :timeout,
83
+ description: 'Connection timeout (seconds)',
84
+ short: '-T TIMEOUT',
85
+ long: '--timeout TIMEOUT',
86
+ default: 10,
87
+ proc: proc(&:to_i)
88
+
89
+ include Pgpass
90
+
91
+ def run
92
+ timestamp = Time.now.to_i
93
+ pgpass
94
+ con = PG.connect(host: config[:hostname],
95
+ dbname: config[:database],
96
+ user: config[:user],
97
+ password: config[:password],
98
+ port: config[:port],
99
+ connect_timeout: config[:timeout])
100
+ request = [
101
+ 'select sum(heap_blks_read) as heap_blks_read, sum(heap_blks_hit) as heap_blks_hit,',
102
+ 'sum(idx_blks_read) as idx_blks_read, sum(idx_blks_hit) as idx_blks_hit,',
103
+ 'sum(toast_blks_read) as toast_blks_read, sum(toast_blks_hit) as toast_blks_hit,',
104
+ 'sum(tidx_blks_read) as tidx_blks_read, sum(tidx_blks_hit) as tidx_blks_hit',
105
+ "from pg_statio_#{config[:scope]}_tables"
106
+ ]
107
+ con.exec(request.join(' ')) do |result|
108
+ result.each do |row|
109
+ output "#{config[:scheme]}.statsio.#{config[:database]}.heap_blks_read", row['heap_blks_read'], timestamp
110
+ output "#{config[:scheme]}.statsio.#{config[:database]}.heap_blks_hit", row['heap_blks_hit'], timestamp
111
+ output "#{config[:scheme]}.statsio.#{config[:database]}.idx_blks_read", row['idx_blks_read'], timestamp
112
+ output "#{config[:scheme]}.statsio.#{config[:database]}.idx_blks_hit", row['idx_blks_hit'], timestamp
113
+ output "#{config[:scheme]}.statsio.#{config[:database]}.toast_blks_read", row['toast_blks_read'], timestamp
114
+ output "#{config[:scheme]}.statsio.#{config[:database]}.toast_blks_hit", row['toast_blks_hit'], timestamp
115
+ output "#{config[:scheme]}.statsio.#{config[:database]}.tidx_blks_read", row['tidx_blks_read'], timestamp
116
+ output "#{config[:scheme]}.statsio.#{config[:database]}.tidx_blks_hit", row['tidx_blks_hit'], timestamp
117
+ end
118
+ end
119
+
120
+ ok
121
+ end
122
+ end