Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## This is a monitoring daemon for Alloc's 7 days to die Linux scripts.
- ## Requiremed modules: Proc::Daemon, Getopt::Long, File::Spec::Functions, Mail::Sendmail.
- ## Sendmail installation also required for notifications. Install whatever package your distro warrants.
- ## Add to and redistribute this however you like.
- #!/usr/bin/perl
- use strict;
- use warnings;
- use Getopt::Long;
- use Proc::Daemon;
- use Cwd;
- use File::Spec::Functions;
- use Mail::Sendmail;
- ## Notification and other user settings.
- our $server='<instance>'; #Instance name for your server.
- our $smtp='<mail.whatever.something>'; #Your outgoing mail server.
- our $delay='<delay in seconds>'; #The time you want the daemon to wait before checking status.
- my $pf = catfile(getcwd(), 'pidfile.pid');
- my $daemon = Proc::Daemon->new(
- pid_file => $pf,
- work_dir => getcwd()
- );
- my $pid = $daemon->Status($pf);
- my $daemonize = 1;
- GetOptions(
- 'daemon!' => \$daemonize,
- "start" => \&run,
- "status" => \&status,
- "stop" => \&stop,
- );
- sub stop {
- if ($pid) {
- print "stopping pid $pid...\n";
- if ($daemon->Kill_Daemon($pf)) {
- print "Successfully stopped.\n";
- }else{
- print "Could not find $pid. Was it running?\n";
- }
- }else{
- print "Not running, nothing to stop.\n";
- }
- }
- sub status {
- if ($pid) {
- print "Running with pid $pid.\n";
- }else{
- print "Not running.\n";
- }
- }
- sub run {
- if (!$pid) {
- print "Starting monitoring Daemon on $server, timeout $delay seconds...\n";
- if ($daemonize) {
- $daemon->Init;
- }
- while (1) {
- my $status=`7dtd.sh status $server`;
- if ($status=~/.*Status: NOT running.*/) {
- my $result=`7dtd.sh start $server`;
- if ($result=~/.*Failed!.*/) {
- ¬ify();
- $daemon->Kill_Daemon($pf);
- }
- }
- sleep($delay);
- }
- }else{
- print "Already running with pid $pid\n";
- }
- }
- sub notify {
- my %mail = (TO => "$notify",
- From => "$outgoing",
- Subject => "Server $server has failed.",
- Message => "Tried to start $server but failed. I gave up. Intervention is required.\n"
- );
- $mail{smtp} = "$smtp";
- sendmail(%mail) or die $Mail::Sendmail::error;
- }
Advertisement
Add Comment
Please, Sign In to add comment