下面是自己写的第一个perl脚本,在工作中逼着自己用新技术去做,学的会快一些,下面的脚本主要是从XML文件中读取配置,用到的是perl-XML-Simple模块,就像其名字,使用起来是够简单的。然后就是修改配置文件。
- #!/usr/bin/perl
- # start declaration statments
- use LWP::Simple;
- use XML::Simple;
- use Data::Dumper;
- use DBI;
- use Getopt::Long;
- use POSIX ':signal_h';
- # declare constant variables
- # declare constant variables of errno
- use constant config_error => 100;
- use constant vip_error => 101;
- use constant mask_error => 102;
- use constant broadcast_error => 103;
- use constant mode_error => 104;
- use constant heartbeat_error => 105;
- use constant delay_error => 106;
- use constant otherIP_error => 107;
- use constant host_error => 108;
- use constant mysql_conn_error => 201;
- use constant start_slave_error => 202;
- # declare constant variables of configuration files
- use constant hosts_file => "/etc/hosts";
- use constant hostname_file => "/etc/sysconfig/network";
- use constant ifcfg0_file => "/etc/sysconfig/network-scripts/ifcfg-eth0";
- use constant ifcfg1_file => "/etc/sysconfig/network-scripts/ifcfg-eth1";
- use constant ha_file => "/etc/ha.d/ha.cf";
- use constant hares_file => "/etc/ha.d/haresources";
- use constant mysql_file => "/etc/my.cnf";
- use constant mon_file => "/etc/mon/mon.cf";
- use constant config_file => "/root/sample.xml";
- # declare subroutine
- # ltrim return a string with whitespaces striped from the beginning of the input string
- sub ltrim;
- # rtrim return a string with whitespaces striped from the end of the input string
- sub rtrim;
- # trim return a string with whitespaces striped from the beginning and end of the input string
- sub trim;
- # load_config_file read configure items into a hash from the xml file which is defined as a constant variable named config_file and return the hash variable;
- sub load_config_file;
- # start_ha configure the configuration files of heartbeat, mon, mysql, etc., start the heartbeat, and it also do some work like configure hostname, start mysql replication thread, enable mysql binary log, etc.
- sub start_ha;
- # stop_ha configure the files of heartbeat, mon, mysql, etc. , stop the heartbeat, and it also do some work like stop mysql replication thread, disable mysql binary log, etc.
- sub stop_ha;
- # configure_hb configure the files of heartbeat and start the heartbeat
- sub configure_bh;
- # configure_mon modify the configuration files of mon
- sub configure_mon;
- # configure_host configure the host name and ip address
- sub configure_host;
- # configure_mysql modify the configuration file of mysql, that is /etc/my.cnf
- sub configure_mysql;
- # start_replication start the mysql replication thread
- sub start_replication;
- # stop_replication stop the mysql replication thread
- sub stop_replication;
- # start_binlog start the mysql binary log
- sub start_binlog;
- # stop_binlog stop the mysql binary log
- sub stop_binlog;
- # verify_ip verify whether the ip address is valid, use regular expression
- sub verify_ip;
- # verify_configuration verify all the configurations in the config_file
- sub verify_configuration;
- # connect_mysql connect to mysql and return the connection handler;
- sub connect_mysql;
- # disconnect_mysql disconnect to mysql and release the connection handler;
- sub disconnect_mysql;
- # end declaration statments
- # read all configurations into a hash
- our %config = load_config_file(config_file);
- #print Dumper(%config);
- verify_configuration;
- my $host = 'localhost';
- my $port = 3306;
- my $user = 'root';
- my $password = '123';
- my $db = connect_to_mysql($host, $port, $user, $password);
- stop_binlog($db);
- start_binlog($db);
- disconnect_mysql;
- if ($config{'mode'} eq "on")
- {
- start_ha;
- }
- elsif ($config{'mode'} eq "off")
- {
- stop_ha;
- }
- else
- {
- exit(mode_error);
- }
- # implementation subroutine
- # ltrim return a string with whitespaces striped from the beginning of the input string
- sub ltrim
- {
- my ($str) = @_;
- $str =~ s/^/s+//g;
- $str;
- }
- # rtrim return a string with whitespaces striped from the end of the input string
- sub rtrim
- {
- my ($str) = @_;
- $str =~ s//s$//g;
- $str;
- }
- # trim return a string with whitespaces striped from the beginning and end of the input string
- sub trim
- {
- my ($str) = @_;
- $str = ltrim(rtrim($str));
- $str;
- }
- # load_config_file read configure items into a hash from the xml file which is defined as a constant variable named config_file and return the hash variable;
- sub load_config_file
- {
- my ($file) = @_;
- my $doc = XMLin($file);
- foreach my $key (keys %$doc)
- {
- my $value = $doc->{$key};
- $config{lc(trim($key))} = lc(trim($value));
- }
- %config;
- }
- # start_ha configure the configuration files of heartbeat, mon, mysql, etc., start the heartbeat, and it also do some work like configure hostname, start mysql replication thread, enable mysql binary log, etc.
- sub start_ha
- {
- }
- # stop_ha configure the files of heartbeat, mon, mysql, etc. , stop the heartbeat, and it also do some work like stop mysql replication thread, disable mysql binary log, etc.
- sub stop_ha
- {
- }
- # verify_ip verify whether the ip address is valid, use regular expression
- sub verify_ip
- {
- my ($ipaddr) = @_;
- if ($ipaddr =~ /^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$/)
- {
- return "true";
- }
- else
- {
- return;
- }
- }
- # verify_configuration verify all the configurations in the config_file
- sub verify_configuration
- {
- if (defined(%config))
- {
- # verify vip configuration
- if (!verify_ip($config{'vip'}))
- {
- exit(vip_error);
- }
- # verify otherIP configuration, the otherIP is the IP address of the other Node which is in the ha cluster
- if (!verify_ip($config{'otherip'}))
- {
- exit(otherIP_error);
- }
- # verify netmask and broadcast address, this will be set to the virtual ip address netmask
- # if not configure the mask, the broadcast is also not configured.
- if ($config{'mask'} ne "null")
- {
- if (!verify_ip($config{'mask'}))
- {
- exit(mask_error);
- }
- if ($config{'broadcast'} ne "null")
- {
- if (!verify_ip($config{'broadcast'}))
- {
- exit(broadcast_error);
- }
- }
- }
- else
- {
- if ($config{'broadcast'} ne "null")
- {
- exit(broadcast_error);
- }
- } # if ($config{'mask'} ne "null")
- # verify mode, the mode can only be on or off,
- # on means enable ha
- # off means disable ha
- if (!($config{'mode'} =~ /^on$|^off$/))
- {
- exit(mode_error);
- }
- # verify host, the host can only be master or slave,
- # master means this server is the master server
- # slave means this server is the slave server
- if (!($config{'host'} =~ /^master$|^slave$/))
- {
- exit(host_error);
- }
- # verify heartbeat time value, this value is set for heartbeat
- if (!($config{'heartbeat'} =~ /^[0-9]{1,2}$/))
- {
- exit(heartbeat_error);
- }
- # verify heartbeat delay time value, this value is set for heartbeat
- if (!($config{'delaytime'} =~ /^[0-9]{1,3}$/))
- {
- exit(delay_error);
- }
- }
- else
- {
- exit(config_error);
- } # if (defined(%config))
- }
- # configure_hb configure the files of heartbeat and start the heartbeat
- sub configure_bh
- {
- }
- # configure_mon modify the configuration files of mon
- sub configure_mon
- {
- }
- # configure_host configure the host name and ip address
- sub configure_host
- {
- }
- # configure_mysql modify the configuration file of mysql, that is /etc/my.cnf
- sub configure_mysql
- {
- }
- # start_replication start the mysql replication thread
- sub start_replication
- {
- my ($dbh) = @_;
- my $stm = 0;
- if (!$dbh)
- {
- exit(mysql_conn_error);
- }
- # start slave
- eval
- {
- $stm = $dbh->prepare(q{start slave});
- $stm->execute();
- };
- if($@)
- {
- exit(start_slave_error);
- }
- }
- # stop_replication stop the mysql replication thread
- sub stop_replication
- {
- my ($dbh) = @_;
- my $stm = 0;
- if (!$dbh)
- {
- exit(mysql_conn_error);
- }
- # stop slave
- eval
- {
- $stm = $dbh->prepare(q{stop slave});
- $stm->execute();
- };
- if($@)
- {
- exit(stop_slave_error);
- }
- }
- # start_binlog start the mysql binary log
- sub start_binlog
- {
- my ($dbh) = @_;
- my $stm = 0;
- if (!$dbh)
- {
- exit(mysql_conn_error);
- }
- # start to record binary log
- eval
- {
- $stm = $dbh->prepare(q{set sql_log_bin = 1});
- $stm->execute();
- };
- if($@)
- {
- exit(stop_slave_error);
- }
- }
- # stop_binlog stop the mysql binary log
- sub stop_binlog
- {
- my ($dbh) = @_;
- my $stm = 0;
- if (!$dbh)
- {
- exit(mysql_conn_error);
- }
- # stop to record binary log
- eval
- {
- $stm = $dbh->prepare(q{set sql_log_bin = 0});
- $stm->execute();
- };
- if($@)
- {
- exit(stop_slave_error);
- }
- }
- # connect_mysql connect to mysql and return the connection handler;
- sub connect_to_mysql
- {
- my ($host, $port, $user, $password) = @_;
- my $dbh = 0;
- if (!($host && $port && $user && $password))
- {
- exit(mysql_conn_error);
- }
- my $mask = POSIX::SigSet->new( SIGALRM );
- my $action = POSIX::SigAction->new(
- sub { die "connect timeout" }, # the handler code ref
- $mask,
- # not using (perl 5.8.2 and later) 'safe' switch or sa_flags
- );
- # connect to mysql
- my $oldaction = POSIX::SigAction->new();
- sigaction( SIGALRM, $action, $oldaction );
- eval
- {
- alarm 3;
- $dbh = DBI->connect( "DBI:mysql:mysql:".$host."::$port", $user, $password);
- alarm 0;
- };
- alarm 0;
- sigaction( SIGALRM, $oldaction );
- if ($@)
- {
- print $@."/n";
- exit(mysql_conn_error);
- }
- elsif (!$dbh)
- {
- print "connect mysql failed".$DBI::errstr."/n";
- exit(mysql_conn_error);
- }
- return $dbh;
- }
- # disconnect_mysql disconnect to mysql and release the connection handler;
- sub disconnect_mysql
- {
- my ($dbh) = @_;
- if ($dbh)
- {
- $dbh->disconnect();
- }
- }
- # end implementations subroutine