From 646cda70a7a8d63b6acd52e0f264237978cb1d5b Mon Sep 17 00:00:00 2001 From: Jan Willamowius Date: Tue, 20 Sep 2005 14:24:17 +0000 Subject: [PATCH] display list of hosts sorted alphabetically (patch by Andrew Ruthven) --- src/lib/Spong/HostList.pm | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/lib/Spong/HostList.pm b/src/lib/Spong/HostList.pm index abb089f..6dba884 100755 --- a/src/lib/Spong/HostList.pm +++ b/src/lib/Spong/HostList.pm @@ -69,11 +69,30 @@ sub new { $self->{'hosthash'} = \%hosts; $self->{'hostnames'} = [ @names ]; + $self->{'sorted_hostnames'} = [ sort byname @names ]; bless $self; return $self; } +# Sort the list of hostnames by hostname. +sub byname { + # Work out the display-name attrib if it's defined. + my ($display_a) = $main::HOSTS{$a}->{'display_name'}; + my ($display_b) = $main::HOSTS{$b}->{'display_name'}; + if ( ! $display_a ) { + # Otherwise use the hostname if $WWW_FQDN or short name + $display_a = $main::WWW_FQDN ? $a : (split( /\./, $a ))[0]; + } + if ( ! $display_b ) { + # Otherwise use the hostname if $WWW_FQDN or short name + $display_b = $main::WWW_FQDN ? $b : (split( /\./, $b ))[0]; + } + + return lc($display_a) cmp lc($display_b); +} + + # Get/Set methods, hostnames() is a little fancy in that it return a list # rather then just the list reference (easier for others to deal with). The @@ -85,6 +104,8 @@ sub hosthash { my $var = 'hosthash'; if( defined $_[1] ) { $_[0]->{$var} = $_[1]; } return $_[0]->{$var}; } sub hostnames { my $var = 'hostnames'; if( defined $_[1] ) { $_[0]->{$var} = $_[1]; } return @{$_[0]->{$var}}; } +sub sorted_hostnames { my $var = 'sorted_hostnames'; + if( defined $_[1] ) { $_[0]->{$var} = $_[1]; } return @{$_[0]->{$var}}; } # Some specific functions that get at and manipulate the data in the instance # vars in more convenient ways @@ -100,12 +121,22 @@ sub hosts { return @tmp; } +sub sorted_hosts { + my $self = shift; + my( @tmp ); + + foreach( $self->sorted_hostnames() ) { push( @tmp, $self->host( $_ ) ); } + return @tmp; +} + sub add { my( $self, $host ) = @_; my $name = $host->name(); $self->{'hosthash'}->{$name} = $host; push( @{$self->{'hostnames'}}, $name ); + push( @{ $self->{'sorted_hostnames'} }, $name); + $self->{'sorted_hostnames'} = [ sort byname @{ $self->{'sorted_hostnames'} } ]; } # Calculate summary color for a service @@ -276,7 +307,7 @@ sub display_html { # Now go through each host, and fill in the table. - foreach $host ( $self->hosts() ) { + foreach $host ( $self->sorted_hosts() ) { my $hostname = $host->name(); # Use display-name attrib if defined my $display = $main::HOSTS{$hostname}->{'display_name'}; @@ -335,13 +366,12 @@ sub display_html { # of the problem services, date/time the problem occurred, and contact # information (as well as acknowledgments if they are available). - foreach $host ( $self->hosts() ) { + foreach $host ( $self->sorted_hosts() ) { $host->display_problem( "html" ); print "\n"; } } } - # These methods all display summary information about only those machines # that are currently registering a problem, or that have services that have # not been updated (showing purple) - it also relays information about -- 2.30.2