|
| 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