[Pgtreats-devel] [pgtreats commit] r267 - trunk/tools

svn-commit at lists.omniti.com svn-commit at lists.omniti.com
Wed Aug 3 17:10:44 EDT 2011


Author: depesz
Date: 2011-08-03 17:10:43 -0400 (Wed, 03 Aug 2011)
New Revision: 267

Modified:
   trunk/tools/system_monitoring-sample-config.cfg
   trunk/tools/system_monitoring.pl
Log:
1. Add handling of environment variables in config
2. modify sample config to use the new ability
3. modify pg queries to use COPY() to get results that are more easily parseable and loadable to db for analysis.


Modified: trunk/tools/system_monitoring-sample-config.cfg
===================================================================
--- trunk/tools/system_monitoring-sample-config.cfg	2011-07-22 21:11:37 UTC (rev 266)
+++ trunk/tools/system_monitoring-sample-config.cfg	2011-08-03 21:10:43 UTC (rev 267)
@@ -1,5 +1,8 @@
 # Global configuration, log directory
 GLOBAL.logdir=/home/depesz/monitoring
+GLOBAL.env.PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+GLOBAL.env.PGUSER=postgres
+GLOBAL.env.PGDATABASE=template1
 
 # Checks configuration
 check.iostat.type=persistent
@@ -32,31 +35,31 @@
 check.cpu.interval=10
 
 check.pg_stat_activity.type=periodic
-check.pg_stat_activity.exec=psql -qX -U postgres -d template1 -c 'select now(), * from pg_stat_activity where current_query <> $$<IDLE>$$'
+check.pg_stat_activity.exec=psql -qX -c 'COPY ( select now(), * from pg_stat_activity where current_query <> $$<IDLE>$$ ) TO STDOUT'
 check.pg_stat_activity.interval=30
 
 check.pg_locks.type=periodic
-check.pg_locks.exec=psql -qX -U postgres -d template1 -c 'select * from pg_locks'
+check.pg_locks.exec=psql -qX -c 'COPY ( select * from pg_locks ) TO STDOUT'
 check.pg_locks.interval=30
 
 check.pg_stat_database.type=periodic
-check.pg_stat_database.exec=psql -qX -U postgres -d template1 -c 'select * from pg_stat_database'
+check.pg_stat_database.exec=psql -qX -c 'COPY ( select * from pg_stat_database ) TO STDOUT'
 check.pg_stat_database.interval=300
 
 check.pg_stat_all_indexes.type=periodic
-check.pg_stat_all_indexes.exec=psql -qX -U postgres -d template1 -c 'select * from pg_stat_all_indexes'
+check.pg_stat_all_indexes.exec=psql -qX -c 'COPY ( select * from pg_stat_all_indexes ) TO STDOUT'
 check.pg_stat_all_indexes.interval=300
 
 check.pg_stat_all_tables.type=periodic
-check.pg_stat_all_tables.exec=psql -qX -U postgres -d template1 -c 'select * from pg_stat_all_tables'
+check.pg_stat_all_tables.exec=psql -qX -c 'COPY ( select * from pg_stat_all_tables ) TO STDOUT'
 check.pg_stat_all_tables.interval=300
 
 check.pg_statio_all_indexes.type=periodic
-check.pg_statio_all_indexes.exec=psql -qX -U postgres -d template1 -c 'select * from pg_statio_all_indexes'
+check.pg_statio_all_indexes.exec=psql -qX -c 'COPY ( select * from pg_statio_all_indexes ) TO STDOUT'
 check.pg_statio_all_indexes.interval=300
 
 check.pg_statio_all_tables.type=periodic
-check.pg_statio_all_tables.exec=psql -qX -U postgres -d template1 -c 'select * from pg_statio_all_tables'
+check.pg_statio_all_tables.exec=psql -qX -c 'COPY ( select * from pg_statio_all_tables ) TO STDOUT'
 check.pg_statio_all_tables.interval=300
 
 # Compress logs older than 1 hour

Modified: trunk/tools/system_monitoring.pl
===================================================================
--- trunk/tools/system_monitoring.pl	2011-07-22 21:11:37 UTC (rev 266)
+++ trunk/tools/system_monitoring.pl	2011-08-03 21:10:43 UTC (rev 267)
@@ -1,6 +1,9 @@
-#!/usr/bin/perl -w
+#!/usr/bin/env perl
 
 package main;
+use strict;
+use warnings;
+
 my $program = Monitoring->new();
 $program->run();
 
@@ -8,6 +11,7 @@
 
 package Monitoring;
 use strict;
+use warnings;
 use English qw( -no_match_vars );
 use Time::HiRes qw( time sleep );
 use POSIX qw( strftime );
@@ -101,11 +105,31 @@
     return;
 }
 
+sub set_env_for_check {
+    my $self = shift;
+    my $C    = shift;
+
+    for my $source ( $self, $C ) {
+        next unless $source->{ 'env' };
+        while ( my ( $key, $value ) = each %{ $source->{ 'env' } } ) {
+            if ( 0 == length $value ) {
+                delete $ENV{ $key };
+            }
+            else {
+                $ENV{ $key } = $value;
+            }
+        }
+    }
+    return;
+}
+
 sub run_check {
     my $self    = shift;
     my $C       = shift;
     my $command = $C->{ 'exec' };
 
+    $self->set_env_for_check( $C );
+
     my $mode = '-|';
     $mode = '<' if $command =~ s/\A\s*<\s*//;
 
@@ -235,10 +259,18 @@
             $self->{ 'logdir' } = $1;
             next;
         }
+        if ( $line =~ m{ \A GLOBAL\.env\.([^\s=]+) \s* = \s* (.*) \z }xmsi ) {
+            $self->{ 'env' }->{ $1 } = $2;
+            next;
+        }
         elsif ( $line =~ m{ \A check\.([A-Za-z0-9_]+)\.(type|exec|interval|ignore) \s* = \s* (\S.*) \z }xmsi ) {
             $self->{ 'pre_checks' }->{ $1 }->{ $2 } = $3;
             next;
         }
+        elsif ( $line =~ m{ \A check\.([A-Za-z0-9_]+)\.env\.([^\s=]+) \s* = \s* (.*) \z }xmsi ) {
+            $self->{ 'pre_checks' }->{ $1 }->{ 'env' }->{ $2 } = $3;
+            next;
+        }
         die "Unknown line: [ $line ]\n";
     }
     close $fh;
@@ -299,6 +331,8 @@
 
 =item * GLOBAL.logdir - full path to log directory
 
+=item * GLOBAL.env.* - setting environment variables
+
 =item * check.XXX.type - type of check with name XXX
 
 =item * check.XXX.exec - what should be executed to get data for check XXX
@@ -307,6 +341,8 @@
 
 =item * check.XXX.ignore - should output be ignored?
 
+=item * check.XXX.env.* - setting environment variables
+
 =back
 
 There are only two supported types:
@@ -322,6 +358,21 @@
 
 =back
 
+env parameters are used to set environment variables. You can set them
+globally for all checks, via GLOBAL.env., or for any given check itself -
+using check.XXX.env.
+
+For example:
+
+    GLOBAL.env.PGUSER=postgres
+
+Will set environment variable PGUSER to value postgres.
+
+If you'd want to make sure that given env variable is not set, you can use
+syntax with lack of value:
+
+    check.whatever.env.PGUSER=
+
 "exec" parameter is simply command line, to be run via shell, that will run
 the program.
 



More information about the Pgtreats-devel mailing list