Permalink
Browse files

Fix commit for archive-queue check

Previous commit, for some reason, didn't contain these files
  • Loading branch information...
1 parent 229ac1d commit 2bdf642ef87b14664e96215e12195e7ddbf93e4d @depesz depesz committed Jun 28, 2012
Showing with 34 additions and 101 deletions.
  1. +17 −0 doc/omnipitr-monitor.pod
  2. +12 −2 lib/OmniPITR/Program/Monitor.pm
  3. +5 −99 lib/OmniPITR/Program/Monitor/Check.pm
View
@@ -33,6 +33,12 @@ between calls to checks
=item * --verbose (-v) shows information about reading lines from log files.
+=item * --psql-path (-pp) path to psql - used when getting data from
+database
+
+=item * --temp-dir (-t) - Where to create temporary files (defaults to /tmp
+or I<$TMPDIR> environment variable location)
+
=item * options depend on which check being performed.
Most checks don't have any options. List of options for check is supplied in
@@ -95,6 +101,17 @@ than x seconds.
Requires path to log from I<omnipitr-archive>
+=head3 archive-queue
+
+Shows number of xlog files that are waiting to be archived. This, in normal
+circumstances, shouldn't be larger than 1, and usually should be 0.
+
+This check will need to connect to master database, so you might need to
+provide database connection parameters and/or psql path.
+
+Information about these options is in USAGE section at the beginning of this
+manual.
+
=head3 last-restore-age
When was the last WAL segment restored. Returns value in seconds being interval
@@ -45,6 +45,7 @@ sub run {
$O->setup(
'state-dir' => $check_state_dir,
'log' => $self->{ 'log' },
+ 'psql' => sub { return $self->psql( @_ ) },
);
$O->get_args();
@@ -361,7 +362,10 @@ Function which handles reading of base arguments ( i.e. without options specific
sub read_args {
my $self = shift;
- my %args = ();
+ my %args = (
+ 'temp-dir' => $ENV{ 'TMPDIR' } || '/tmp',
+ 'psql-path' => 'psql',
+ );
croak( 'Error while reading command line arguments. Please check documentation in doc/omnipitr-archive.pod' )
unless GetOptions(
@@ -370,9 +374,15 @@ sub read_args {
'check|c=s',
'state-dir|s=s',
'verbose|v',
+ 'database|d=s',
+ 'host|h=s',
+ 'port|p=i',
+ 'username|U=s',
+ 'temp-dir|t=s',
+ 'psql-path|pp=s',
);
- for my $key ( qw( check state-dir verbose ) ) {
+ for my $key ( qw( check state-dir verbose database host port username temp-dir psql-path ) ) {
next unless defined $args{ $key };
$self->{ $key } = $args{ $key };
}
@@ -36,6 +36,8 @@ Afterwards, it calls ->setup() function, passing (as hash):
=item * log - log object
+=item * psql - coderef which will run given query via psql, and return whole output as scalar
+
=back
Afterwards, omnipitr-monitor will run "get_args" method (if it's defined), to get all necessary options from command line - options specifically for this check.
@@ -65,7 +67,7 @@ Sets check for work - receives state-dir and log object from omnipitr-monitor.
sub setup {
my $self = shift;
my %args = @_;
- for my $key ( qw( log state-dir ) ) {
+ for my $key ( qw( log state-dir psql ) ) {
croak( "$key not given in call to ->setup()." ) unless defined $args{ $key };
$self->{ $key } = $args{ $key };
}
@@ -95,109 +97,13 @@ sub log { return shift->{ 'log' }; }
=head1 psql()
-Runs given query via psql - assumes there is $self->{ 'psql-path' }.
-
-Uses also:
-
-=over
-
-=item * username
-
-=item * database
-
-=item * port
-
-=item * host
-
-=item
-
-optional keys from $self.
-
-On first run it will cache psql call arguments, so if you'd change them on
-subsequent calls, you have to delete $self->{'psql'}.
-
-In case of errors, it raises fatal error.
-
-Otherwise returns stdout of the psql.
+Runs given query via psql.
=cut
sub psql {
- my $self = shift;
- my $query = shift;
-
- unless ( $self->{ 'psql' } ) {
- my @psql = ();
- push @psql, $self->{ 'psql-path' };
- push @psql, '-qAtX';
- push @psql, ( '-U', $self->{ 'username' } ) if $self->{ 'username' };
- push @psql, ( '-d', $self->{ 'database' } ) if $self->{ 'database' };
- push @psql, ( '-h', $self->{ 'host' } ) if $self->{ 'host' };
- push @psql, ( '-p', $self->{ 'port' } ) if $self->{ 'port' };
- push @psql, '-c';
- $self->{ 'psql' } = \@psql;
- }
-
- $self->prepare_temp_directory();
-
- my @command = ( @{ $self->{ 'psql' } }, $query );
-
- $self->log->time_start( $query ) if $self->verbose;
- my $status = run_command( $self->{ 'temp-dir' }, @command );
- $self->log->time_finish( $query ) if $self->verbose;
-
- $self->log->fatal( 'Running [%s] via psql failed: %s', $query, $status ) if $status->{ 'error_code' };
-
- return $status->{ 'stdout' };
-}
-
-=head1 find_tablespaces()
-
-Helper function. Takes no arguments. Uses pg_tblspc directory and returns
-a hashref of the physical locations of tablespaces.
-Keys in the hashref are tablespace OIDs (link names in pg_tblspc). Values
-are hashrefs with two keys:
-
-=over
-
-=item * pg_visible - what is the path to tablespace that PostgreSQL sees
-
-=item * real_path - what is the real absolute path to tablespace directory
-
-=back
-
-The two can be different in case tablespace got moved and symlinked back to
-original location, or if tablespace path itself contains symlinks.
-
-=cut
-
-sub get_tablespaces {
my $self = shift;
-
- # Identify any tablespaces and get those
- my $tablespace_dir = File::Spec->catfile( $self->{ 'data-dir' }, "pg_tblspc" );
- my %tablespaces;
-
- return unless -e $tablespace_dir;
-
- my @pgfiles;
- opendir( my $dh, $tablespace_dir ) or $self->log->fatal( "Unable to open tablespace directory $tablespace_dir" );
-
- # Push onto our list the locations that are pointed to by the pg_tblspc symlinks
- foreach my $filename ( readdir $dh ) {
- next if $filename !~ /^\d+$/; # Filename should be all numeric
- my $full_name = File::Spec->catfile( $tablespace_dir, $filename );
- next if !-l $full_name; # It should be a symbolic link
- my $pg_visible = readlink $full_name;
- my $real_path = Cwd::abs_path( $full_name );
- $tablespaces{ $filename } = {
- 'pg_visible' => $pg_visible,
- 'real_path' => $real_path,
- };
- }
- closedir $dh;
-
- return \%tablespaces;
+ return $self->{ 'psql' }->( @_ );
}
1;

0 comments on commit 2bdf642

Please sign in to comment.