FeistyLemur

7 days to die monitoring daemon.

Sep 9th, 2014
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.62 KB | None | 0 0
  1. ## This is a monitoring daemon for Alloc's 7 days to die Linux scripts.
  2. ## Requiremed modules: Proc::Daemon, Getopt::Long, File::Spec::Functions, Mail::Sendmail.
  3. ## Sendmail installation also required for notifications.  Install whatever package your distro warrants.
  4. ## Add to and redistribute this however you like.
  5.  
  6. #!/usr/bin/perl
  7.  
  8. use strict;
  9. use warnings;
  10. use Getopt::Long;
  11. use Proc::Daemon;
  12. use Cwd;
  13. use File::Spec::Functions;
  14. use Mail::Sendmail;
  15.  
  16.  
  17. ## Notification and other user settings.
  18. our $server='<instance>';                   #Instance name for your server.
  19. our $notify='<[email protected]>';         #Use the email you wish to send notifications to.
  20. our $outgoing='<[email protected]>';  #The outgoing mail address.
  21. our $smtp='<mail.whatever.something>';          #Your outgoing mail server.
  22. our $delay='<delay in seconds>';        #The time you want the daemon to wait before checking status.
  23.  
  24. my $pf = catfile(getcwd(), 'pidfile.pid');
  25. my $daemon = Proc::Daemon->new(
  26.     pid_file => $pf,
  27.     work_dir => getcwd()
  28. );
  29.  
  30. my $pid = $daemon->Status($pf);
  31. my $daemonize = 1;
  32.  
  33.  
  34. GetOptions(
  35.     'daemon!' => \$daemonize,
  36.     "start" => \&run,
  37.     "status" => \&status,
  38.     "stop" => \&stop,
  39. );
  40.  
  41. sub stop {
  42.     if ($pid) {
  43.         print "stopping pid $pid...\n";
  44.         if ($daemon->Kill_Daemon($pf)) {
  45.             print "Successfully stopped.\n";
  46.         }else{
  47.             print "Could not find $pid. Was it running?\n";
  48.         }
  49.     }else{
  50.         print "Not running, nothing to stop.\n";
  51.     }
  52. }
  53.  
  54. sub status {
  55.     if ($pid) {
  56.         print "Running with pid $pid.\n";
  57.     }else{
  58.         print "Not running.\n";
  59.     }
  60. }
  61.  
  62. sub run {
  63.     if (!$pid) {
  64.         print "Starting monitoring Daemon on $server, timeout $delay seconds...\n";
  65.         if ($daemonize) {
  66.             $daemon->Init;
  67.         }
  68.         while (1) {
  69.             my $status=`7dtd.sh status $server`;
  70.             if ($status=~/.*Status: NOT running.*/) {
  71.                 my $result=`7dtd.sh start $server`;
  72.                 if ($result=~/.*Failed!.*/) {
  73.                     &notify();
  74.                     $daemon->Kill_Daemon($pf);
  75.                 }
  76.             }
  77.             sleep($delay);
  78.         }
  79.     }else{
  80.         print "Already running with pid $pid\n";
  81.     }
  82. }
  83.  
  84. sub notify {
  85.     my %mail = (TO      => "$notify",
  86.                 From    => "$outgoing",
  87.                 Subject => "Server $server has failed.",
  88.                 Message => "Tried to start $server but failed.  I gave up.  Intervention is required.\n"
  89.                 );
  90.    
  91.     $mail{smtp} = "$smtp";
  92.     sendmail(%mail) or die $Mail::Sendmail::error;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment