Guest User

pg_xlog_watchdog

a guest
Oct 18th, 2011
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.29 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. @date=localtime(time);
  4. $date[5]+=1900;$date[4]=sprintf("%02d",++$date[4]);
  5. $date[3]=sprintf("%02d",$date[3]);$date[2]=sprintf("%02d",$date[2]);
  6. $date[1]=sprintf("%02d",$date[1]);$date[0]=sprintf("%02d",$date[0]);
  7. my $pref="$date[5]-$date[4]-$date[3] $date[2]:$date[1]:$date[0] WATCHDOG ";
  8.  
  9. open LOG, '+>>', '/var/log/postgresql/watchdog.log';
  10.  
  11.  
  12. sub copy()
  13. {
  14.     printf LOG $pref . "OK: $ARGV[0]\n";
  15.     exit system("cp /var/lib/postgresql/9.0/main/pg_xlog/$ARGV[0] /var/lib/postgresql/9.0/main/archive/");
  16. }
  17.  
  18. if(!$ARGV[0])
  19. {
  20.     printf LOG $pref . "No input args. Exit.\n";
  21.     exit 1;
  22. }
  23.  
  24. if ($ARGV[0]=~m/\.backup$/) { copy(); }
  25.  
  26. if (! -e "/var/lib/postgresql/9.0/main/pg_xlog/$ARGV[0]")
  27. {
  28.     printf LOG $pref . "WARN: no such file $ARGV[0]. Ignore\n";
  29.     exit 0;
  30. }
  31.  
  32. $senders_n=`ps aux|grep sender|grep -v grep|wc -l`;
  33. chomp($senders_n);
  34. $senders_max=`cat /etc/postgresql/9.0/main/slaves.list|egrep -v '^\$|^#'|wc -l`;
  35. chomp($senders_max);
  36.  
  37. if ($senders_max==0) { copy(); }
  38.  
  39. if ($senders_n < $senders_max)
  40. {
  41.     printf LOG $pref . 'ERROR: not enough senders(' .$senders_n . ' of ' . $senders_max . ')' . "\n";
  42.     exit 1;
  43. }
  44. else
  45. {
  46.  
  47.     $connected=`ps aux|grep postg|grep sender|grep -v grep|awk '{ print \$16 }'|sed 's/([0-9]*)//g'|sort`;
  48.     chomp($connected);
  49.     $slaves=`cat /etc/postgresql/9.0/main/slaves.list |egrep -v '^#'|awk '{ system("host " \$1)}'|awk '{ print \$4 }'|sort`;
  50.     chomp($slaves);
  51.     if ($connected ne $slaves)
  52.     {
  53.     printf LOG $pref . "ERROR: slaves list mistmatch(unlisted slave detected)\n";
  54.     exit 1;
  55.     }
  56. }
  57.  
  58. $min_pos=`ps aux|grep postgres|grep sender|grep -v grep|awk '{ print \$18 }'|sort|head -n 1`;
  59. chomp($min_pos);
  60. $min_pos=~s/\///;
  61. $min_pos=~s/[\w\d]{6}$//;
  62. $min_pos_unhex=hex($min_pos);
  63. #printf LOG "Min pos: " . $min_pos . "\n";
  64.  
  65. $filename=$ARGV[0];
  66. $filename=~s/(^[\w\d]{13})//;
  67. $filename=~s/([0]{6})//;
  68. #$filename=~s/([\w\d]$)//;
  69.  
  70. chomp($filename);
  71. $filename_unhex=hex($filename);
  72. #printf LOG "Filename:" .  $filename . "\n";
  73.  
  74. if ($min_pos_unhex > $filename_unhex)
  75. {
  76.     #printf LOG $pref . "DEBUG trying to archive $filename($filename_unhex). Current $min_pos($min_pos_unhex)\n";
  77.     copy();
  78. }
  79. else
  80. {
  81.     printf LOG $pref . "ERROR Some senders still use this(or previous) file $ARGV[0]\n";
  82.     exit 1;
  83. }
  84.  
Add Comment
Please, Sign In to add comment