Send email notification when keepalived dual-machine Hot Standby is enabled for Fault

Source: Internet
Author: User
Tags install perl

Send email notification when keepalived dual-machine Hot Standby is enabled for Fault
Currently, the project has used nginx to achieve load balancing, but there is only one nginx scheduler. The previous unexpected shutdown caused the entire service application to crash, this time, we are going to add a scheduler to achieve high site availability, which is often referred to as dual-host hot backup.
Master: 192.168.1.241
Backup: 192.168.1.242
Drifting IP Address: 192.168.1.243
1. keepalived software is installed on both the master and slave schedulers.
The installation process is as follows:
./Configure -- prefix =/usr/local/keepalived & make install
Cp/usr/local/keepalived/etc/rc. d/init. d/keepalived/etc/rc. d/init. d/
Cp/usr/local/keepalived/etc/sysconfig/
Mkdir/etc/keepalived
Cp/usr/local/keepalived/etc/keepalived. conf/etc/keepalived/
Cp/usr/local/keepalived/sbin/keepalived/usr/sbin/
Service keepalived start
You can start the test with the Command service keepalived start to check whether the process can be started. Check the log.
2. Configure the master scheduler as follows:
! Configuration File for keepalived
Global_defs {
Router_id LVS_DEVEL
}
Vrrp_sync_group VG_1 {
Group {
VI_1
}
Notify_master/etc/keepalived/sendmail. pl
}
Vrrp_instance VI_1 {
State MASTER
Interface eth0
Virtual_router_id 51
Priority100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.243
}
}
2. Configure the Backup Scheduler as follows:
! Configuration File for keepalived
Global_defs {
Router_id LVS_DEVEL
}
Vrrp_sync_group VG_1 {
Group {
VI_1
}
Notify_master/etc/keepalived/sendmail. pl
}
Vrrp_instance VI_1 {
State BACKUP
Interface eth0
Virtual_router_id 51
Priority 99
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Virtual_ipaddress {
192.168.1.243
}
}
3. The perl script sendmail. pl for sending emails is as follows:
Note that the title of the email sent by the master and slave nodes is inconsistent, as long as you can identify the server on which the drifting IP address is located.

#! /Usr/bin/perl-wuse Net: SMTP_auth; use strict; my $ mailhost = 'smtp .163.com '; my $ mailfrom = 'test @ 163.com '; my @ mailto = ('1970 @ 139.com '); my $ subject = 'keepalived up on backup'; my $ text = "body \ n the second line is located here. "; My $ user = 'test @ 163.com '; my $ passwd = 'xxxxxxx'; & SendMail (); ############################## Send notice mail ###### ######################## sub SendMail () {my $ smtp = Net: SMTP_auth-> new ($ mailhost, Timeout => 120, Debug => 1) or die "Error. \ n "; $ smtp-> auth ('login', $ user, $ passwd); foreach my $ mailto (@ mailto) {$ smtp-> mail ($ mailfrom ); $ smtp-> to ($ mailto); $ smtp-> data (); $ smtp-> datasend ("To: $ mailto \ n "); $ smtp-> datasend ("From: $ mailfrom \ n"); $ smtp-> datasend ("Subject: $ subject \ n "); $ smtp-> datasend ("\ n"); $ smtp-> datasend ("$ text \ n"); $ smtp-> dataend ();} $ smtp-> quit ;}
Note:
A. Since the built-in sending and email mechanism of keeplived is a weakness, if port 25 is not enabled locally, it cannot be used to send emails, wondering if it can be implemented through custom scripts, we were lucky to have adopted the authentication method.
B. Other configuration instructions are not detailed. There are a lot of information on the Internet.
4. Test keepalived.

Both the master and slave schedulers enable port 80, and the test content on the two servers is inconsistent, which makes the test easier.

##########################

# Required installation modules

# Use Net: SMTP

# Authen: SASL

##########################

# $ Stmp-> auth ('user', 'pass ');

# Most SMTP servers require user authentication to prevent spam/spam.

# This method requires an additional installation module: Authen: SASL, which may not be provided by the system

##########################

# Debug => 1

# This code is used for testing, so Debug is enabled. Generally, once the test is completed, it will be closed if it is used.

Note: You can run:/etc/keepalived/sendmail. pl on the command line interface to check whether the email can be sent successfully. If the email fails, install the Net: SMTP_auth module.

Installation Method:

Yum-y install perl-CPAN
Cpan Net: SMTP_auth

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.