Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- @date=localtime(time);
- $date[5]+=1900;$date[4]=sprintf("%02d",++$date[4]);
- $date[3]=sprintf("%02d",$date[3]);$date[2]=sprintf("%02d",$date[2]);
- $date[1]=sprintf("%02d",$date[1]);$date[0]=sprintf("%02d",$date[0]);
- my $pref="$date[5]-$date[4]-$date[3] $date[2]:$date[1]:$date[0] WATCHDOG ";
- open LOG, '+>>', '/var/log/postgresql/watchdog.log';
- sub copy()
- {
- printf LOG $pref . "OK: $ARGV[0]\n";
- exit system("cp /var/lib/postgresql/9.0/main/pg_xlog/$ARGV[0] /var/lib/postgresql/9.0/main/archive/");
- }
- if(!$ARGV[0])
- {
- printf LOG $pref . "No input args. Exit.\n";
- exit 1;
- }
- if ($ARGV[0]=~m/\.backup$/) { copy(); }
- if (! -e "/var/lib/postgresql/9.0/main/pg_xlog/$ARGV[0]")
- {
- printf LOG $pref . "WARN: no such file $ARGV[0]. Ignore\n";
- exit 0;
- }
- $senders_n=`ps aux|grep sender|grep -v grep|wc -l`;
- chomp($senders_n);
- $senders_max=`cat /etc/postgresql/9.0/main/slaves.list|egrep -v '^\$|^#'|wc -l`;
- chomp($senders_max);
- if ($senders_max==0) { copy(); }
- if ($senders_n < $senders_max)
- {
- printf LOG $pref . 'ERROR: not enough senders(' .$senders_n . ' of ' . $senders_max . ')' . "\n";
- exit 1;
- }
- else
- {
- $connected=`ps aux|grep postg|grep sender|grep -v grep|awk '{ print \$16 }'|sed 's/([0-9]*)//g'|sort`;
- chomp($connected);
- $slaves=`cat /etc/postgresql/9.0/main/slaves.list |egrep -v '^#'|awk '{ system("host " \$1)}'|awk '{ print \$4 }'|sort`;
- chomp($slaves);
- if ($connected ne $slaves)
- {
- printf LOG $pref . "ERROR: slaves list mistmatch(unlisted slave detected)\n";
- exit 1;
- }
- }
- $min_pos=`ps aux|grep postgres|grep sender|grep -v grep|awk '{ print \$18 }'|sort|head -n 1`;
- chomp($min_pos);
- $min_pos=~s/\///;
- $min_pos=~s/[\w\d]{6}$//;
- $min_pos_unhex=hex($min_pos);
- #printf LOG "Min pos: " . $min_pos . "\n";
- $filename=$ARGV[0];
- $filename=~s/(^[\w\d]{13})//;
- $filename=~s/([0]{6})//;
- #$filename=~s/([\w\d]$)//;
- chomp($filename);
- $filename_unhex=hex($filename);
- #printf LOG "Filename:" . $filename . "\n";
- if ($min_pos_unhex > $filename_unhex)
- {
- #printf LOG $pref . "DEBUG trying to archive $filename($filename_unhex). Current $min_pos($min_pos_unhex)\n";
- copy();
- }
- else
- {
- printf LOG $pref . "ERROR Some senders still use this(or previous) file $ARGV[0]\n";
- exit 1;
- }
Add Comment
Please, Sign In to add comment