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 }

In reply to Print subroutine problem by Shivaramakrishnan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.