CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Print subroutine help required.

 



Shiva1987
New User

Apr 5, 2013, 9:46 AM

Post #1 of 3 (276 views)
Print subroutine help required. Can't Post

Hello,
I am a newbie with some bash experience.
I am trying to write a script to monitor postgresql database.
The script is working fine,but I would like to have see a human readable output here.

Currently the script inputs the queries to be performed
Ex:
./check_postgres.pl --buffers_alloc --exclusivelock --activeconn

Output I get :
17212root@host:

which is 172 is for --buffers_alloc ,1 is for --exclusivelock and 2 is for active connections.

Output I would like to see :
Buffers_allocated:172 Exclusive_Locks:2 Active_Connections:2
root@host:

That is all in one line with description:value description:value and \n at the end.

I am not able to get it in this way.Any help is greatly appreciated.Need to get it working soon as I am planning to use this for prod monitoring.
Attachments: post_check.txt (1.41 KB)


BillKSmith
Veteran

Apr 5, 2013, 1:30 PM

Post #2 of 3 (262 views)
Re: [Shiva1987] Print subroutine help required. [In reply to] Can't Post


Code
print 
"Buffers_allocated:$Buffers_allocated "
."Exclusive_Locks:$Exclusive_Locks "
."Active_Connections:$Active_Connections "
."root@host: "
;

Good Luck,
Bill

(This post was edited by BillKSmith on Apr 5, 2013, 1:31 PM)


wickedxter
User

Apr 5, 2013, 1:50 PM

Post #3 of 3 (259 views)
Re: [Shiva1987] Print subroutine help required. [In reply to] Can't Post

try this: I added an extra arg to the calling of the query_database sub and its added to the output of the info returned info..


Code
#!/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_stat_database},
# Locks
"exclusivelock" => qq{SELECT COUNT(*) FROM pg_locks WHERE mode='ExclusiveLock'},
# Checkpoints
"buffers_alloc" => qq{SELECT buffers_alloc FROM pg_stat_bgwriter},
);

GetOptions(
'help!' => \&usage,
'user=s' => \$user,
'database=s' => \$database,

'activeconn' => sub { print query_database($querys{activeconn},'--activeconn') },
'exclusivelock' => sub { print query_database($querys{exclusivelock},'--exclusivelock') },
'buffers_alloc' => sub { print query_database($querys{buffers_alloc},'--buffers_alloc') }
) or die "$0: try --help for more information\n";

sub query_database {
my $query = shift;
my $type_info = shift;

my $dbh = DBI->connect("dbi:Pg:dbname=$database;host=$hostname",$user);
my $sth = $dbh->prepare("$query") or die $|;
$sth->execute;

my @array = $sth->fetchrow_array;

return "$array[0] $type_info ";
}

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 }


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives