Skip to content

Commit 4e18e89

Browse files
phumpalmajormoses
andauthored
Find the total size of largest tables (#155)
* Find the total size of largest tables * update changelog * appease the cops Co-authored-by: Ben Abrams <me@benabrams.it>
1 parent 989798a commit 4e18e89

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44
This CHANGELOG follows the format listed [here](https://github.com/sensu-plugins/community/blob/master/HOW_WE_CHANGELOG.md).
55

66
## [Unreleased]
7+
### Added
8+
- new `bin/metric-postgres-relation-size.rb` find largest tables (@phumpal)
79

810
## [4.1.0] - 2020-06-04
911
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* bin/metric-postgres-statsio.rb
2323
* bin/check-postgres-query.rb
2424
* bin/metrics-postgres-query.rb
25+
* bin/metric-postgres-relation-size.rb
2526

2627
## Usage
2728

bin/metric-postgres-relation-size.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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: nil
80+
81+
option :limit,
82+
description: 'Limit query to this many results',
83+
short: '-L LIMIT',
84+
login: '--limit LIMIT',
85+
default: 25
86+
87+
include Pgpass
88+
89+
def run
90+
timestamp = Time.now.to_i
91+
pgpass
92+
con = PG.connect(host: config[:hostname],
93+
dbname: config[:database],
94+
user: config[:user],
95+
password: config[:password],
96+
port: config[:port],
97+
connect_timeout: config[:timeout])
98+
99+
# https://wiki.postgresql.org/wiki/Disk_Usage
100+
request = [
101+
"SELECT nspname || '.' || relname AS relation,
102+
pg_total_relation_size(C.oid) AS total_size
103+
FROM pg_class C
104+
LEFT JOIN pg_namespace N on (N.oid = C.relnamespace)
105+
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
106+
ORDER BY pg_total_relation_size(C.oid) DESC
107+
LIMIT '#{config[:limit]}'"
108+
]
109+
110+
con.exec(request.join(' ')) do |result|
111+
result.each do |row|
112+
output "#{config[:scheme]}.size.#{config[:database]}.#{row['relation']}", row['total_size'], timestamp
113+
end
114+
end
115+
116+
ok
117+
end
118+
end

0 commit comments

Comments
 (0)