Ad Widget

Collapse

Server Daemon Monitor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bdowns
    Junior Member
    • Oct 2007
    • 4

    #1

    Server Daemon Monitor

    For some reason, the server that Zabbix was installed on was having some stability issues where Zabbix and a few other services were dying. When that would happen, obviously, all of the other servers that Zabbix was monitoring wouldn't be monitored. I wrote a script to make sure that the daemon would always be running and if it wasn't able to, to alert me when it wasn't.

    I'm new to Perl and scripting in general, but wanted to submit this script in case it could be of any use to others. I'm sure it can be improved upon greatly.

    This is a copy and paste right from the terminal so the formatting looks a little messy.

    #!/usr/bin/perl -w

    #---------------------------------------------#
    # #
    # This script monitors the process for Zabbix #
    # and restarts it if it's not running. #
    # #
    # Part of the problem is that the .pid file #
    # isn't removed when the daemon stops, so we #
    # have to do that here too. #
    # #
    # Brian Downs
    # www.thedownscondition.com #
    # 2007-10-30 #
    # #
    #---------------------------------------------#

    use strict;

    # Variables
    my $domain = "put domain here";
    my $message = "put the body of the notification e-mail here";

    my (@pidList,@procList1,@procList2,@procList3,@addres s);
    my ($pidList,$pid,$list,$address);
    my ($zabbix);

    ## Subroutines
    # Function to start the zabbix daemon
    sub start_zabbix {
    my $pidfile = '/var/tmp/zabbix_server.pid';

    if ( -e $pidfile ) {

    # Make sure zabbix isn't running
    kill_zabbix();
    unlink($pidfile);
    }

    $zabbix = `/usr/local/bin/zabbix_server`;
    }

    # Function to kill the zabbix daemon
    sub kill_zabbix {
    @procList1 = `ps aux | grep zabbix_server | grep -v grep`;

    foreach $pid (@procList1) {
    if ($pid =~ /zabbix\s*(\d{0,5})/) {
    push(@pidList,$1);
    }
    }

    $list = join(' ',@pidList);
    `kill -9 $list`;
    }

    # Function to send e-mail notification
    sub mail_notification {

    # Input e-mail addresses here. Seperate by comma and escape the "@" symbol.
    @address = ("");

    foreach $address (@address) {
    open(MAIL, "|/usr/sbin/sendmail -t");

    # E-Mail Header
    print MAIL "To: $address\n";
    print MAIL "From: Zabbix_Server\@$domain\n";
    print MAIL "Subject: Unable to restart Zabbix.\n";

    # E-Mail Body
    print $message;

    close(MAIL);
    }
    }

    # Check to see if the Zabbix server is running
    @procList2 = `ps aux | grep zabbix_server | grep -v grep`;

    if (!@procList2) {
    print "Down - " . `date` ."\n";
    start_zabbix();

    # Check to see if it's been restarted
    @procList3 = `ps aux | grep zabbix_server | grep -v grep`;

    if (@procList3) {
    print "Zabbix server restarted successfully. " . `date`."\n";

    }
    else {
    print "Unable to restart Zabbix server, trying again.\n";
    start_zabbix();
    mail_notification();
    }
    }

    else {
    print "Up - " . `date` . "\n";
    }
Working...