#!@@PERL@@
# -*- cperl -*-
#
# Copyright (C) 2009 Magnus Hagander, Redpill Linpro AB
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 dated June,
# 1991.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.

=head1 NAME

postgres_tuples_ - Plugin to monitor PostgreSQL tuple access.

=head1 CONFIGURATION

Configuration is done through libpq environment variables, for example
PGUSER, PGDATABASE, etc. For more information, see L<Munin::Plugin::Pgsql>.

To monitor a specific database, link to postgres_tuples_<databasename>.

=head1 SEE ALSO

L<Munin::Plugin::Pgsql>

=head1 MAGIC MARKERS

 #%# family=auto
 #%# capabilities=autoconf suggest

=head1 AUTHOR

Magnus Hagander <magnus@hagander.net>, Redpill Linpro AB

=head1 COPYRIGHT/License.

Copyright (c) 2009 Magnus Hagander, Redpill Linpro AB

All rights reserved. This program is free software; you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; version 2
dated June, 1991.

=cut

use strict;
use warnings;

use Munin::Plugin::Pgsql;

my $pg = Munin::Plugin::Pgsql->new(
    basename  => 'postgres_tuples_',
    title     => 'PostgreSQL tuple access',
    info      => 'Tuple access',
    vlabel    => 'Tuples / sec',
    basequery => [
        "SELECT COALESCE(sum(seq_tup_read),0) AS seqread, COALESCE(sum(idx_tup_fetch),0) AS idxfetch,"
            . " COALESCE(sum(n_tup_ins),0) AS inserted, COALESCE(sum(n_tup_upd),0) AS updated,"
            . " COALESCE(sum(n_tup_del),0) AS deleted, COALESCE(sum(n_tup_hot_upd),0) AS hotupdated FROM pg_stat_user_tables",
        [
            8.2,
            "SELECT COALESCE(sum(seq_tup_read),0) AS seqread, COALESCE(sum(idx_tup_fetch),0) AS idxfetch,"
                . " COALESCE(sum(n_tup_ins),0) AS inserted, COALESCE(sum(n_tup_upd),0) AS updated,"
                . " COALESCE(sum(n_tup_del),0) AS deleted FROM pg_stat_user_tables",
        ],
    ],
    paramdatabase => 1,
    pivotquery    => 1,
    configquery   => [
        "VALUES ('seqread','Tuples sequentally read'), ('idxfetch','Tuples index fetched'), ('inserted','Tuples inserted'),"
            . " ('updated','Tuples updated'),('deleted','Tuples deleted'),('hotupdated','Tuples HOT updated')",
        [
            8.2,
            "VALUES ('seqread','Tuples sequentally read'), ('idxfetch','Tuples index fetched'), ('inserted','Tuples inserted'),"
                . " ('updated','Tuples updated'),('deleted','Tuples deleted')",
        ], [
            8.1,
            "SELECT 'seqread','Tuples sequentially read' UNION ALL SELECT 'idxfetch','Tuples index fetched' UNION ALL "
                . "SELECT 'inserted','Tuples inserted' UNION ALL SELECT 'updated','Tuples updated' UNION ALL "
                . "SELECT 'deleted','Tuples deleted'",
        ],
    ],
    suggestquery =>
        "SELECT datname FROM pg_database WHERE datallowconn AND NOT datistemplate AND NOT datname='postgres' ORDER BY 1 LIMIT 10",
    graphtype => 'DERIVE',
    graphmin => 0,
);

$pg->Process();
