用perl读取XML并且修改配置文件

下面是自己写的第一个perl脚本,在工作中逼着自己用新技术去做,学的会快一些,下面的脚本主要是从XML文件中读取配置,用到的是perl-XML-Simple模块,就像其名字,使用起来是够简单的。然后就是修改配置文件。
  1. #!/usr/bin/perl
  2. # start declaration statments
  3. use LWP::Simple;
  4. use XML::Simple;
  5. use Data::Dumper;
  6. use DBI;
  7. use Getopt::Long;
  8. use POSIX ':signal_h';
  9. # declare constant variables
  10. # declare constant variables of errno
  11. use constant config_error        => 100;
  12. use constant vip_error           => 101;
  13. use constant mask_error          => 102;
  14. use constant broadcast_error     => 103;
  15. use constant mode_error          => 104;
  16. use constant heartbeat_error     => 105;
  17. use constant delay_error         => 106;
  18. use constant otherIP_error       => 107;
  19. use constant host_error          => 108;
  20. use constant mysql_conn_error    => 201;
  21. use constant start_slave_error   => 202;
  22. # declare constant variables of configuration files
  23. use constant hosts_file          => "/etc/hosts";
  24. use constant hostname_file       => "/etc/sysconfig/network";
  25. use constant ifcfg0_file         => "/etc/sysconfig/network-scripts/ifcfg-eth0";
  26. use constant ifcfg1_file         => "/etc/sysconfig/network-scripts/ifcfg-eth1";
  27. use constant ha_file             => "/etc/ha.d/ha.cf";
  28. use constant hares_file          => "/etc/ha.d/haresources";
  29. use constant mysql_file          => "/etc/my.cnf";
  30. use constant mon_file            => "/etc/mon/mon.cf";
  31. use constant config_file         => "/root/sample.xml";
  32. # declare subroutine
  33. # ltrim return a string with whitespaces striped from the beginning of the input string
  34. sub ltrim;
  35. # rtrim return a string with whitespaces striped from the end of the input string
  36. sub rtrim;
  37. # trim return a string with whitespaces striped from the beginning and end of the input string
  38. sub trim;
  39. # 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; 
  40. sub load_config_file;
  41. # 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.
  42. sub start_ha;
  43. # 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.
  44. sub stop_ha;
  45. # configure_hb configure the files of heartbeat and start the heartbeat
  46. sub configure_bh;
  47. # configure_mon modify the configuration files of mon
  48. sub configure_mon;
  49. # configure_host configure the host name and ip address
  50. sub configure_host;
  51. # configure_mysql modify the configuration file of mysql, that is /etc/my.cnf
  52. sub configure_mysql;
  53. # start_replication start the mysql replication thread
  54. sub start_replication;
  55. # stop_replication stop the mysql replication thread
  56. sub stop_replication;
  57. # start_binlog start the mysql binary log
  58. sub start_binlog;
  59. # stop_binlog stop the mysql binary log
  60. sub stop_binlog;
  61. # verify_ip verify whether the ip address is valid, use regular expression
  62. sub verify_ip;
  63. # verify_configuration verify all the configurations in the config_file
  64. sub verify_configuration;
  65. # connect_mysql connect to mysql and return the connection handler;
  66. sub connect_mysql;
  67. # disconnect_mysql disconnect to mysql and release the connection handler;
  68. sub disconnect_mysql;
  69. # end declaration statments
  70. # read all configurations into a hash
  71. our %config = load_config_file(config_file);
  72. #print Dumper(%config);
  73. verify_configuration;
  74. my $host = 'localhost';
  75. my $port = 3306;
  76. my $user = 'root';
  77. my $password = '123';
  78. my $db = connect_to_mysql($host, $port, $user, $password);
  79. stop_binlog($db);
  80. start_binlog($db);
  81. disconnect_mysql;
  82. if ($config{'mode'} eq "on")
  83. {
  84.     start_ha;
  85. }
  86. elsif ($config{'mode'} eq "off")
  87. {
  88.     stop_ha;
  89. }
  90. else
  91. {
  92.     exit(mode_error);
  93. }
  94. # implementation subroutine
  95. # ltrim return a string with whitespaces striped from the beginning of the input string
  96. sub ltrim
  97. {
  98.     my ($str) = @_;
  99.     $str =~ s/^/s+//g;
  100.     $str;
  101. }
  102. # rtrim return a string with whitespaces striped from the end of the input string
  103. sub rtrim
  104. {
  105.     my ($str) = @_;
  106.     $str =~ s//s$//g;
  107.     $str;
  108. }
  109. # trim return a string with whitespaces striped from the beginning and end of the input string
  110. sub trim
  111. {
  112.     my ($str) = @_;
  113.     $str = ltrim(rtrim($str));
  114.     $str;
  115. }
  116. # 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; 
  117. sub load_config_file
  118. {
  119.     my ($file) = @_;
  120.     my $doc = XMLin($file);
  121.     foreach my $key (keys %$doc)
  122.     {
  123.         my $value = $doc->{$key};
  124.         $config{lc(trim($key))} = lc(trim($value));
  125.     }
  126.     %config;
  127. }
  128. # 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.
  129. sub start_ha
  130. {
  131. }
  132. # 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.
  133. sub stop_ha
  134. {
  135. }
  136. # verify_ip verify whether the ip address is valid, use regular expression
  137. sub verify_ip
  138. {
  139.     my ($ipaddr) = @_;
  140.     if ($ipaddr =~ /^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$/)
  141.     {
  142.         return "true";
  143.     }
  144.     else
  145.     {
  146.         return;
  147.     }
  148. }
  149. # verify_configuration verify all the configurations in the config_file
  150. sub verify_configuration
  151. {
  152.     if (defined(%config))
  153.     {
  154.         # verify vip configuration
  155.         if (!verify_ip($config{'vip'}))
  156.         {
  157.             exit(vip_error); 
  158.         }
  159.         
  160.         # verify otherIP configuration, the otherIP is the IP address of the other Node which is in the ha cluster
  161.         if (!verify_ip($config{'otherip'}))
  162.         {
  163.             exit(otherIP_error);
  164.         }
  165.         # verify netmask and broadcast address, this will be set to the virtual ip address netmask
  166.         # if not configure the mask, the broadcast is also not configured. 
  167.         if ($config{'mask'} ne "null")
  168.         {
  169.             if (!verify_ip($config{'mask'}))
  170.             {
  171.                 exit(mask_error);
  172.             }
  173.             if ($config{'broadcast'} ne "null")
  174.             {
  175.                 if (!verify_ip($config{'broadcast'}))
  176.                 {
  177.                     exit(broadcast_error);
  178.                 }
  179.                 
  180.             }
  181.         }
  182.         else
  183.         {
  184.             if ($config{'broadcast'} ne "null")
  185.             {
  186.                 exit(broadcast_error);
  187.             }
  188.         } # if ($config{'mask'} ne "null")
  189.         # verify mode, the mode can only be on or off, 
  190.         # on means enable ha
  191.         # off means disable ha
  192.         if (!($config{'mode'} =~ /^on$|^off$/))
  193.         {
  194.             exit(mode_error);
  195.         }
  196.         # verify host, the host can only be master or slave, 
  197.         # master means this server is the master server
  198.         # slave means this server is the slave server 
  199.         if (!($config{'host'} =~ /^master$|^slave$/))
  200.         {
  201.             exit(host_error);
  202.         }
  203.         
  204.         # verify heartbeat time value, this value is set for heartbeat
  205.         if (!($config{'heartbeat'} =~ /^[0-9]{1,2}$/))
  206.         {
  207.             exit(heartbeat_error);
  208.         }
  209.         # verify heartbeat delay time value, this value is set for heartbeat
  210.         if (!($config{'delaytime'} =~ /^[0-9]{1,3}$/))
  211.         {
  212.             exit(delay_error);
  213.         }
  214.     }
  215.     else
  216.     {
  217.         exit(config_error);
  218.     } # if (defined(%config))
  219. }
  220. # configure_hb configure the files of heartbeat and start the heartbeat
  221. sub configure_bh
  222. {
  223. }
  224. # configure_mon modify the configuration files of mon
  225. sub configure_mon
  226. {
  227. }
  228. # configure_host configure the host name and ip address
  229. sub configure_host
  230. {
  231. }
  232. # configure_mysql modify the configuration file of mysql, that is /etc/my.cnf
  233. sub configure_mysql
  234. {
  235. }
  236. # start_replication start the mysql replication thread
  237. sub start_replication
  238. {
  239.     my ($dbh) = @_;
  240.     my $stm = 0;
  241.     
  242.     if (!$dbh)
  243.     {
  244.         exit(mysql_conn_error);
  245.     }
  246.     # start slave
  247.     eval
  248.     {
  249.         $stm = $dbh->prepare(q{start slave});
  250.         $stm->execute();
  251.     };
  252.     if($@)
  253.     {
  254.         exit(start_slave_error);
  255.     }
  256. }
  257. # stop_replication stop the mysql replication thread
  258. sub stop_replication
  259. {
  260.     my ($dbh) = @_;
  261.     my $stm = 0;
  262.     
  263.     if (!$dbh)
  264.     {
  265.         exit(mysql_conn_error);
  266.     }
  267.     # stop slave
  268.     eval
  269.     {
  270.         $stm = $dbh->prepare(q{stop slave});
  271.         $stm->execute();
  272.     };
  273.     if($@)
  274.     {
  275.         exit(stop_slave_error);
  276.     }
  277. }
  278. # start_binlog start the mysql binary log
  279. sub start_binlog
  280. {
  281.     my ($dbh) = @_;
  282.     my $stm = 0;
  283.     
  284.     if (!$dbh)
  285.     {
  286.         exit(mysql_conn_error);
  287.     }
  288.     # start to record binary log
  289.     eval
  290.     {
  291.         $stm = $dbh->prepare(q{set sql_log_bin = 1});
  292.         $stm->execute();
  293.     };
  294.     if($@)
  295.     {
  296.         exit(stop_slave_error);
  297.     }
  298. }
  299. # stop_binlog stop the mysql binary log
  300. sub stop_binlog
  301. {
  302.     my ($dbh) = @_;
  303.     my $stm = 0;
  304.     if (!$dbh)
  305.     {
  306.         exit(mysql_conn_error);
  307.     }
  308.     # stop to record binary log
  309.     eval
  310.     {
  311.         $stm = $dbh->prepare(q{set sql_log_bin = 0});
  312.         $stm->execute();
  313.     };
  314.     if($@)
  315.     {
  316.         exit(stop_slave_error);
  317.     }
  318. }
  319. # connect_mysql connect to mysql and return the connection handler;
  320. sub connect_to_mysql
  321. {
  322.     my ($host, $port, $user, $password) = @_;
  323.     my $dbh = 0;
  324.     if (!($host && $port && $user && $password))
  325.     {
  326.         exit(mysql_conn_error);
  327.     }
  328.     my $mask = POSIX::SigSet->new( SIGALRM );
  329.     my $action = POSIX::SigAction->new(
  330.                                     sub { die "connect timeout" },        # the handler code ref
  331.                                     $mask,
  332.                                     # not using (perl 5.8.2 and later) 'safe' switch or sa_flags
  333.                                     );
  334.     
  335.     # connect to mysql
  336.     my $oldaction = POSIX::SigAction->new();
  337.     sigaction( SIGALRM, $action, $oldaction );
  338.     eval 
  339.     {
  340.         alarm 3;
  341.         $dbh = DBI->connect( "DBI:mysql:mysql:".$host."::$port", $user, $password);
  342.         alarm 0;
  343.     };
  344.     alarm 0;
  345.     sigaction( SIGALRM, $oldaction );
  346.     if ($@)
  347.     {
  348.         print $@."/n";
  349.         exit(mysql_conn_error);
  350.     }
  351.     elsif (!$dbh)
  352.     {
  353.         print "connect mysql failed".$DBI::errstr."/n";
  354.         exit(mysql_conn_error);
  355.     }
  356.     return $dbh;
  357. }
  358. # disconnect_mysql disconnect to mysql and release the connection handler;
  359. sub disconnect_mysql
  360. {
  361.     my ($dbh) = @_;
  362.     if ($dbh)
  363.     {
  364.         $dbh->disconnect();
  365.     }
  366. }
  367. # end implementations subroutine
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值