Shivaramakrishnan has asked for the wisdom of the Perl Monks concerning the following question:
Hi , I am a newbie to perl with some bash experience.I am trying to get a monitoring script for postgres database.I was able to get the script working with no issues.But I am having problems with output form.Here is the current output that I get.
./check_postgres.pl --buffers_alloc --exclusivelock --activeconn 17212root@host:
which is to be read as 172 for buffers_alloc 1 for exclusivelock and 2 for activeconn.
I would like to have my output as./check_postgres.pl --buffers_alloc --exclusivelock --activeconn ------
Buffers_Alloc:172 Exclusive_lock:1 Active_Conn:2
root@host:
That is Description:Value Description:Value followed by next line at the end. I am trying to fix this from past three days but not able to succeed.Any help is greatly appreciated.I need to get this working as soon as possible.#!/usr/bin/perl use strict; use DBI; use Getopt::Long; use Sys::Hostname; my $database = 'atloffice'; my $user = 'postgres'; my $hostname = '10.28.128.143'; my %querys = ( # State "activeconn" => qq{SELECT SUM(numbackends) FROM pg_sta +t_database}, # Locks "exclusivelock" => qq{SELECT COUNT(*) FROM pg_loc +ks WHERE mode='ExclusiveLock'}, # Checkpoints "buffers_alloc" => qq{SELECT buffers_alloc FROM p +g_stat_bgwriter}, ); GetOptions( 'help!' => \&usage, 'user=s' => \$user, 'database=s' => \$database, 'activeconn' => sub { print query_database($querys{activeconn}) }, 'exclusivelock' => sub { print query_database($querys{exclusiveloc +k}) }, 'buffers_alloc' => sub { print query_database($querys{buffers_allo +c}) } ) or die "$0: try --help for more information\n"; sub query_database { my $query = shift(@_); my $dbh = DBI->connect("dbi:Pg:dbname=$database;host=$hostname +",$user); my $sth = $dbh->prepare("$query") or die $|; $sth->execute; while (my @array = $sth->fetchrow_array) { return @array[0]; } } sub usage { print "[-] $0 script to monitor PostgreSQL databases,\n"; print "Usage: $0 [--OPTION]\n"; while ( my($key, undef) = each %querys ) { print "\t--".$key."\n"; } exit 0 } if (!$ARGV) { usage }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Print subroutine problem
by NetWallah (Canon) on Apr 05, 2013 at 17:58 UTC | |
by Shivaramakrishnan (Initiate) on Apr 05, 2013 at 18:40 UTC | |
by Shivaramakrishnan (Initiate) on Apr 05, 2013 at 18:47 UTC | |
by CountOrlok (Friar) on Apr 05, 2013 at 18:54 UTC | |
Re: Print subroutine problem
by CountOrlok (Friar) on Apr 05, 2013 at 17:56 UTC |
Back to
Seekers of Perl Wisdom