/old/rsyslog/files/syslog.pl

https://github.com/jwiegley/puppet-config · Perl · 46 lines · 33 code · 10 blank · 3 comment · 10 complexity · edd1b18bdedd66aa55af64cd383bda8b MD5 · raw file

  1. #!/usr/bin/perl
  2. use strict;
  3. use Sys::Syslog qw( :DEFAULT setlogsock );
  4. use IO::Handle;
  5. chdir '/'; # Avoid the possibility of our working directory resulting in
  6. # keeping an otherwise unused filesystem in use
  7. # Double-fork to avoid leaving a zombie process behind
  8. exit if (fork());
  9. exit if (fork());
  10. sleep 1 until getppid() == 1;
  11. openlog $ARGV[1], 'cons', 'pid', $ARGV[2];
  12. my $replicate = 0;
  13. if ($ARGV[4] ne "false") {
  14. open(REPLICATE, ">> $ARGV[4]") || die "Can't write to $ARGV[4]: $!";
  15. REPLICATE->autoflush(1);
  16. $replicate = 1;
  17. }
  18. while (1) {
  19. open(FIFO, "< $ARGV[0]") || die "Can't read from $ARGV[0]: $!";
  20. my $log;
  21. while ($log = <FIFO>) {
  22. my $priority = $ARGV[3];
  23. if ($log =~ /(err|fatal)/i) {
  24. $priority = "error";
  25. }
  26. elsif ($log =~ /warning/i) {
  27. $priority = "warning";
  28. }
  29. syslog $priority, $log;
  30. if ($replicate) {
  31. print REPLICATE $log;
  32. }
  33. }
  34. close FIFO;
  35. }
  36. closelog