<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># heartbeat-lib.pl
# Common functions for heartbeat tool configuration

BEGIN { push(@INC, ".."); };
use WebminCore;
&amp;init_config();

$ha_cf = $config{'ha_cf'} ? $config{'ha_cf'} : "$config{'ha_dir'}/ha.cf";
$haresources = $config{'haresources'} ? $config{'haresources'}
				      : "$config{'ha_dir'}/haresources";
$authkeys = $config{'authkeys'} ? $config{'authkeys'}
				: "$config{'ha_dir'}/authkeys";
$resource_d = $config{'resource_d'} ? $config{'resource_d'}
				    : "$config{'ha_dir'}/resource.d";

open(VERSION, "$module_config_directory/version");
chop($heartbeat_version = &lt;VERSION&gt;);
close(VERSION);

sub get_ha_config
{
local @rv;
local $lnum = 0;
open(CONF, $ha_cf);
while(&lt;CONF&gt;) {
	s/\s+$//;
	s/#.*$//;
	if (/^(\S+)\s+(\S.*)$/) {
		push(@rv, { 'name' =&gt; $1,
			    'value' =&gt; $2,
			    'line' =&gt; $lnum });
		}
	$lnum++;
	}
close(CONF);
return @rv;
}

# find(name, &amp;config)
sub find
{
local @rv;
foreach $c (@{$_[1]}) {
	if ($c-&gt;{'name'} eq $_[0]) {
		push(@rv, $c-&gt;{'value'});
		}
	}
return wantarray ? @rv : @rv==0 ? undef : $rv[0];
}

# find_struct(name, &amp;config)
sub find_struct
{
local @rv;
foreach $c (@{$_[1]}) {
	if ($c-&gt;{'name'} eq $_[0]) {
		push(@rv, $c);
		}
	}
return wantarray ? @rv : @rv==0 ? undef : $rv[0];
}

# save_directive(&amp;config, name, &amp;values)
sub save_directive
{
local $lref = &amp;read_file_lines($ha_cf);
local @old = &amp;find_struct($_[1], $_[0]);
for($i=0; $i&lt;@old || $i&lt;@{$_[2]}; $i++) {
	if ($i &gt;= @old) {
		# adding a directive
		push(@$lref, "$_[1]\t$_[2]-&gt;[$i]");
		push(@{$_[0]}, { 'name' =&gt; $_[1],
				 'value' =&gt; $_[2]-&gt;[$i],
				 'line' =&gt; scalar(@$lref)-1 });
		}
	elsif ($i &gt;= @{$_[2]}) {
		# removing a directive
		splice(@$lref, $old[$i]-&gt;{'line'}, 1);
		splice(@{$_[0]}, &amp;indexof($old[$i], @{$_[0]}), 1);
		&amp;renumber($_[0], $old[$i]-&gt;{'line'}, -1);
		}
	else {
		# updating a directive
		splice(@$lref, $old[$i]-&gt;{'line'}, 1, "$_[1]\t$_[2]-&gt;[$i]");
		$old[$i]-&gt;{'value'} = $_[2]-&gt;[$i];
		}
	}
}

# renumber(&amp;config, line, offset)
sub renumber
{
foreach $c (@{$_[0]}) {
	if ($c-&gt;{'line'} &gt; $_[1]) {
		$c-&gt;{'line'} += $_[2];
		}
	}
}

sub list_resources()
{
local @rv;
local $lnum = 0;
open(RES, $haresources);
while(&lt;RES&gt;) {
	s/\s+$//;
	s/#.*$//;
	local @res = split(/\s+/, $_);
	if (@res &gt; 0) {
		local $r = { 'node' =&gt; shift(@res),
			     'line' =&gt; $lnum };
		foreach $v (@res) {
			if ($v =~ /^[0-9\.\/]+$/) {
				push(@{$r-&gt;{'ips'}}, $v);
				}
			elsif ($v =~ /^IPaddr::(\S+)$/) {
				push(@{$r-&gt;{'ips'}}, $1);
				}
			else {
				push(@{$r-&gt;{'servs'}}, $v);
				}
			}
		push(@rv, $r);
		}
	$lnum++;
	}
close(RES);
return @rv;
}

sub create_resource
{
local $lref = &amp;read_file_lines($haresources);
push(@$lref, &amp;resource_line($_[0]));
&amp;flush_file_lines();
}

sub modify_resource
{
local $lref = &amp;read_file_lines($haresources);
$lref-&gt;[$_[0]-&gt;{'line'}] = &amp;resource_line($_[0]);
&amp;flush_file_lines();
}

sub delete_resource
{
local $lref = &amp;read_file_lines($haresources);
splice(@$lref, $_[0]-&gt;{'line'}, 1);
&amp;flush_file_lines();
}

sub resource_line
{
local @l = ( $_[0]-&gt;{'node'} );
push(@l, @{$_[0]-&gt;{'ips'}});
push(@l, @{$_[0]-&gt;{'servs'}});
return join(" ", @l);
}

sub get_auth_config
{
local $rv;
open(AUTH, $authkeys);
while(&lt;AUTH&gt;) {
	s/\r|\n//g;
	s/#.*$//;
	local @l = split(/\s+/, $_);
	if (@l &gt; 0) {
		$rv-&gt;{shift(@l)} = \@l;
		}
	}
close(AUTH);
return $rv;
}

sub save_auth_config
{
local %auth;
&amp;open_tempfile(AUTH, "&gt;$authkeys");
if ($_[0]-&gt;{'auth'}) {
	&amp;print_tempfile(AUTH, "auth ",join(" ", @{$_[0]-&gt;{'auth'}}),"\n");
	map { $auth{$_}++ } @{$_[0]-&gt;{'auth'}};
	}
foreach $k (keys %{$_[0]}) {
	if ($k ne 'auth') {
		&amp;print_tempfile(AUTH, "# ") if (!$auth{$k});
		&amp;print_tempfile(AUTH, "$k ",join(" ", @{$_[0]-&gt;{$k}}),"\n");
		}
	}
&amp;close_tempfile(AUTH);
}

# add two more functions (Christof Amelunxen, 22.08.2003)
sub check_status_resource {
	@ips = @_;
	$ifconfig="/sbin/ifconfig";
	@lines=qx|$ifconfig| or die("ifconfig does not seem to work: ".$!);
	foreach(@lines){
        if(/inet addr:([\d.]+)/){ 
		push(@realips,$1);
	}
	}
	$iplist = join (' ',@realips);
	foreach my $ip (@ips) {
		$ip =~ s/\/.*//;
		return 0 unless ( $iplist =~ m/$ip/);
	}		
	return 1;
}

sub get_resource {
	foreach(@_) {
		system("$config{req_resource_cmd} $_");
	}
}	

# version_atleast(v1, v2, v3)
sub version_atleast
{
local @vsp = split(/\./, $heartbeat_version);
local $i;
for($i=0; $i&lt;@vsp || $i&lt;@_; $i++) {
	return 0 if ($vsp[$i] &lt; $_[$i]);
	return 1 if ($vsp[$i] &gt; $_[$i]);
	}
return 1;	# same!
}

# apply_configuration()
# Apply the heartbeat configuration, and return undef on success or an error
# message on failure
sub apply_configuration
{
if ($config{'apply_cmd'}) {
	$out = &amp;backquote_logged("$config{'apply_cmd'} 2&gt;&amp;1 &lt;/dev/null");
	if ($?) {
		return $out;
		}
	}
else {
	local $pid = &amp;check_pid_file($config{'pid_file'});
	if ($pid) {
		kill(HUP, $pid);
		}
	else {
		return $text{'apply_epid'};
		}
	}
return undef;
}

1;

</pre></body></html>