2 # Common functions for heartbeat tool configuration
4 BEGIN { push(@INC, ".."); };
8 $ha_cf = $config{'ha_cf'} ? $config{'ha_cf'} : "$config{'ha_dir'}/ha.cf";
9 $haresources = $config{'haresources'} ? $config{'haresources'}
10 : "$config{'ha_dir'}/haresources";
11 $authkeys = $config{'authkeys'} ? $config{'authkeys'}
12 : "$config{'ha_dir'}/authkeys";
13 $resource_d = $config{'resource_d'} ? $config{'resource_d'}
14 : "$config{'ha_dir'}/resource.d";
16 open(VERSION, "$module_config_directory/version");
17 chop($heartbeat_version = <VERSION>);
28 if (/^(\S+)\s+(\S.*)$/) {
29 push(@rv, { 'name' => $1,
43 foreach $c (@{$_[1]}) {
44 if ($c->{'name'} eq $_[0]) {
45 push(@rv, $c->{'value'});
48 return wantarray ? @rv : @rv==0 ? undef : $rv[0];
51 # find_struct(name, &config)
55 foreach $c (@{$_[1]}) {
56 if ($c->{'name'} eq $_[0]) {
60 return wantarray ? @rv : @rv==0 ? undef : $rv[0];
63 # save_directive(&config, name, &values)
66 local $lref = &read_file_lines($ha_cf);
67 local @old = &find_struct($_[1], $_[0]);
68 for($i=0; $i<@old || $i<@{$_[2]}; $i++) {
71 push(@$lref, "$_[1]\t$_[2]->[$i]");
72 push(@{$_[0]}, { 'name' => $_[1],
73 'value' => $_[2]->[$i],
74 'line' => scalar(@$lref)-1 });
76 elsif ($i >= @{$_[2]}) {
77 # removing a directive
78 splice(@$lref, $old[$i]->{'line'}, 1);
79 splice(@{$_[0]}, &indexof($old[$i], @{$_[0]}), 1);
80 &renumber($_[0], $old[$i]->{'line'}, -1);
83 # updating a directive
84 splice(@$lref, $old[$i]->{'line'}, 1, "$_[1]\t$_[2]->[$i]");
85 $old[$i]->{'value'} = $_[2]->[$i];
90 # renumber(&config, line, offset)
93 foreach $c (@{$_[0]}) {
94 if ($c->{'line'} > $_[1]) {
95 $c->{'line'} += $_[2];
104 open(RES, $haresources);
108 local @res = split(/\s+/, $_);
110 local $r = { 'node' => shift(@res),
113 if ($v =~ /^[0-9\.\/]+$/) {
114 push(@{$r->{'ips'}}, $v);
116 elsif ($v =~ /^IPaddr::(\S+)$/) {
117 push(@{$r->{'ips'}}, $1);
120 push(@{$r->{'servs'}}, $v);
133 local $lref = &read_file_lines($haresources);
134 push(@$lref, &resource_line($_[0]));
140 local $lref = &read_file_lines($haresources);
141 $lref->[$_[0]->{'line'}] = &resource_line($_[0]);
147 local $lref = &read_file_lines($haresources);
148 splice(@$lref, $_[0]->{'line'}, 1);
154 local @l = ( $_[0]->{'node'} );
155 push(@l, @{$_[0]->{'ips'}});
156 push(@l, @{$_[0]->{'servs'}});
157 return join(" ", @l);
163 open(AUTH, $authkeys);
167 local @l = split(/\s+/, $_);
169 $rv->{shift(@l)} = \@l;
179 &open_tempfile(AUTH, ">$authkeys");
180 if ($_[0]->{'auth'}) {
181 &print_tempfile(AUTH, "auth ",join(" ", @{$_[0]->{'auth'}}),"\n");
182 map { $auth{$_}++ } @{$_[0]->{'auth'}};
184 foreach $k (keys %{$_[0]}) {
186 &print_tempfile(AUTH, "# ") if (!$auth{$k});
187 &print_tempfile(AUTH, "$k ",join(" ", @{$_[0]->{$k}}),"\n");
190 &close_tempfile(AUTH);
193 # add two more functions (Christof Amelunxen, 22.08.2003)
194 sub check_status_resource {
196 $ifconfig="/sbin/ifconfig";
197 @lines=qx|$ifconfig| or die("ifconfig does not seem to work: ".$!);
199 if(/inet addr:([\d.]+)/){
203 $iplist = join (' ',@realips);
204 foreach my $ip (@ips) {
206 return 0 unless ( $iplist =~ m/$ip/);
213 system("$config{req_resource_cmd} $_");
217 # version_atleast(v1, v2, v3)
220 local @vsp = split(/\./, $heartbeat_version);
222 for($i=0; $i<@vsp || $i<@_; $i++) {
223 return 0 if ($vsp[$i] < $_[$i]);
224 return 1 if ($vsp[$i] > $_[$i]);
229 # apply_configuration()
230 # Apply the heartbeat configuration, and return undef on success or an error
232 sub apply_configuration
234 if ($config{'apply_cmd'}) {
235 $out = &backquote_logged("$config{'apply_cmd'} 2>&1 </dev/null");
241 local $pid = &check_pid_file($config{'pid_file'});
246 return $text{'apply_epid'};