--- /dev/null
+#!/usr/local/bin/perl
+#
+# This represents a view of hosts by host groups.
+# a single host, or a list of hosts by FDQN name.
+#
+# groupnames - a list of string names of all of the groups
+# group_hash - a hash of HostList objects keyed by group name
+#
+# + new() - constructor (sets instance vars to arguments passed in)
+# + gets/sets() - magical set/get functions (autoloaded based on func name)
+# + display() - output format and view
+#
+## + display_problems() - shows information only about hosts that have services
+## that indicate a problem
+#
+# + group - used to get at a specific HostList by name
+# + groups - used to get back a generic list of groups that are stored
+# + add - adds a specific host group to the instance
+#
+
+use Data::Dumper;
+
+use Spong::Host;
+use Spong::HostList;
+
+package Spong::HostGroups;
+
+# This constructor expects one of four different types of options. Either an
+# empty string specifying that no actual hostlist will be loaded, the string
+# "ALL" to load all the host grups, the string containing a group name that
+# represents a list of hosts, or a reference to an array that contains a list
+# of host groups
+# Note: "ALL" is used to signify all host groups, becuase there is a host group
+# called "all" that is already predefined.
+
+sub new {
+ my( $proto, $grps ) = @_;
+ my( $class ) = ref($proto) || $proto;
+ my( %groups, $groups, $group ) = ();
+ my $self = {};
+
+ if( $grps eq "" ) {
+ %groups = ();
+ } elsif( ref( $group ) eq "ARRAY" ) {
+ foreach $group ( @$grps ) {
+ my $object = Spong::HostList->new( $group );
+ if( $object ) { $groups{$group} = $object; }
+ }
+ } elsif ( $grps eq "ALL" ) {
+ foreach $group ( keys %main::GROUPS ) {
+ my $object = Spong::HostList->new( $group );
+ if( $object ) { $groups{$group} = $object; }
+ }
+ } else {
+ my $object = Spong::HostList->new( $grps );
+ if( $object ) { $groups{$grps} = $object; }
+ }
+
+ $self->{'group_hash'} = \%groups;
+
+ bless ($self,$class);
+ return $self;
+}
+
+
+# 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
+# hosthash function returns a reference however.
+
+sub group_hash { my $var = 'group_hash';
+ 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
+
+sub group {
+ return $_[0]->{'group_hash'}->{$_[1]}; }
+
+sub group_names {
+ return keys %{$_[0]->{'group_hash'}};
+}
+
+sub groups {
+ my $self = shift;
+ my( @tmp );
+
+ foreach( $self->group_names() ) { push( @tmp, $self->group( $_ ) ); }
+ return @tmp;
+}
+
+sub add {
+ my( $self, $hostlist ) = @_;
+ my $name = $hostlist->name();
+
+ $self->{'group_hash'}->{$name} = $hostlist;
+}
+
+# Display routines. Does both text and html, does rely on both the Host and
+# Service objects for some help.
+#
+# brief Hostname and "one" color reflecting overall status of the host
+# standard Table showing hosts vs services and the state of each service
+# full Records for each host showing detailed info about its state
+
+sub display {
+ my( $self, $type, $view ) = @_;
+
+ $self->display_text( $view ) if $type eq "text";
+ $self->display_html( $view ) if $type eq "html";
+}
+
+# This displays a summary of all the hosts in this list in a text format
+# suitable for displaying on dumb ascii terminals
+
+sub display_text {
+ my( $self, $format ) = @_;
+
+ if( $format eq "standard" ) {
+ my( %services, $host, $service, @names );
+
+ # Compute the total list of services running on the various hosts, and
+ # sort them alphabetically (except always put ping first).
+
+# foreach $host ( $self->hosts() ) {
+# foreach $service ( $host->service_names() ) { $services{$service}++;}}
+
+ if( grep( /^ping$/, keys %main::SERVICES ) ) { push( @names, "ping" ); }
+ foreach $service ( sort keys %main::SERVICES ) {
+ push( @names, $service ) unless $service eq "ping"; }
+
+ # Print the horizontal axis of the table (names of the services)
+
+ print "-"x30, "-"x(($#names+1)*3), "\n";
+ foreach $cnt ( 0..4 ) {
+ if( $cnt == 0 ) { print "Key: . = green, ? = purple "; }
+ if( $cnt == 1 ) { print " Y = yellow, R = red "; }
+ if( $cnt == 2 ) { print " B = blue "; }
+ if( $cnt == 3 ) { print " "x30; }
+ if( $cnt == 4 ) { print "Host:", " "x25; }
+ foreach $service ( @names ) {
+ if( length($service) - 5 + $cnt >= 0 ) {
+ print substr( $service, (length($service) - 5 + $cnt), 1 )," ";
+ } else {
+ print " ";
+ }
+ }
+ print "\n";
+ }
+ print "-"x30, "-"x(($#names+1)*3), "\n";
+
+ # Now go through each host, and fill in the table.
+
+ foreach $host ( $self->hosts() ) {
+ print substr( $host->name(), 0, 29 );
+ print " "x(30-length(substr($host->name(), 0, 29)));
+ foreach $service ( @names ) {
+ my $servobj = $host->service( $service );
+ if( $servobj ) {
+ $servobj->display_text( "really_brief" );
+ } else {
+ print " ";
+ }
+ }
+ print "\n";
+ }
+ print "\n";
+ } elsif( $format eq "full" ) {
+
+ # This goes through each host, and has each one print a textual record
+ # of the problem it is currently having, this would include a summary
+ # of the problem services, date/time the problem occurred, and contact
+ # information (as well as acknowledgments if they are available).
+
+ foreach $host ( $self->hosts() ) {
+ $host->display_problem( "text" ); print "\n";
+ }
+ }
+}
+
+# This displays a summary of all the hosts in this list in an HTML format
+# suitable for displaying on web clients capable of displaying tables.
+
+sub display_html {
+ my( $self, $format ) = @_;
+
+ if( $format eq "standard" ) {
+ my( %services, $group, $service, @names );
+
+ # Compute the total list of services running on the various hosts, and
+ # sort them alphabetically (except always put ping first).
+
+ if( grep( /^ping$/, keys %main::SERVICES ) ) { push( @names, "ping" ); }
+ foreach $service ( sort keys %main::SERVICES ) {
+ push( @names, $service ) unless $service eq "ping"; }
+
+ # Print the horizontal axis of the table (names of the services)
+
+ print "<table border=1 cellspacing=0 cellpadding=1";
+ print " bgcolor=" . $main::WWW_TITLE_COLOR if $main::WWW_TITLE_COLOR;
+ print "><tr>";
+ print "<td align=center width=80 nowrap><b>Host Group</b></td>\n";
+ foreach $service ( @names ) {
+ print "<td align=center valign=bottom width=25>\n";
+ print "<font size=-1><a href=\"!!WWWSPONG!!/help/$service\">";
+ print "$service</a></font></td>\n";
+ }
+
+ print "</tr>\n\n";
+
+ # Now go through each host group, and fill in the table.
+
+ foreach $group ( $self->groups() ) {
+ my $name = $group->name();
+ my $grname = $main::GROUPS{$name}->{'name'};
+
+ print "<tr><td align=left bgcolor=#ffffff nowrap>\n";
+ print "<a href=\"!!WWWSPONG!!/group/$name\">$grname</a></td>\n";
+
+ foreach $service ( @names ) {
+ my $color = $group->services_color( $service );
+
+ if( $color ) {
+ if( $main::WWW_USE_IMAGES == 1 ) {
+ print "<td align=center";
+ print " bgcolor=" . $main::WWW_CELL_COLOR
+ if $main::WWW_CELL_COLOR;
+ print ">";
+ my $alt = "";
+ print "<img src=\"!!WWWGIFS!!/$color.gif\" alt=\"$alt\" border=0>";
+ print "";
+ } else {
+ print "<td align=center bgcolor=\"";
+ print $main::WWW_COLOR{$col} . "\" width=25>";
+ print "<font color=\"" . $main::WWW_COLOR{$color} . "\">";
+ print "___</font>";
+ }
+ print "</td>";
+ } else {
+ if( $main::WWW_USE_IMAGES == 1 ) {
+ print "<td align=center width=25";
+ print " bgcolor=" . $main::WWW_CELL_COLOR
+ if $main::WWW_CELL_COLOR;
+ print "> - </td>\n";
+ } else {
+ print "<td align=center width=25";
+ print " bgcolor=" . $main::WWW_CELL_COLOR
+ if $main::WWW_CELL_COLOR;
+ print "> </td>\n";
+ }
+ }
+ }
+ }
+ print "</tr></table>";
+
+ } elsif( $format eq "full" ) {
+
+ }
+}
+
+1;