###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::Control::newsyslog - psgconf control class for controlling log rotation/pruning for FreeBSD
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Control::newsyslog;

use PSGConf::Action::GenerateFile::Literal;

use PSGConf::Data::Boolean;
use PSGConf::Data::Integer;
use PSGConf::Data::List;

###############################################################################
### policies
###############################################################################

sub _add_files
{
	my ($psgconf, $mode, $flags, @files) = @_;
	my ($file, $fullcmd);

	$fullcmd = " " . $mode . " " . 
		$psgconf->data_obj('newsyslog_file_cnt')->get() .  " " .
		$flags;

	foreach $file ( @files ) {

		# FIXME: Need to check to make sure the file is not already
		# in the list.
		$psgconf->data_obj('newsyslog_files')->add (
			$file . $fullcmd
		);
	}
}

sub _policy_add_syslog_files
{
	my ($self, $psgconf) = @_;
	my (@logs);
 
	return
		if ($psgconf->data_obj('syslog_enable')->equals('false')
			|| $psgconf->data_obj('newsyslog_enable')->equals('false'));
 
	map {
		push @logs, $_ if ( -f $_ );
	} keys %{$psgconf->data_obj('syslog')->get()};

	&_add_files ($psgconf, '644', '* $D0 J', @logs );
}

sub _policy_add_ftp_files 
{
     my ($self, $psgconf) = @_;

	return
		if ($psgconf->data_obj('anon_ftp_enable')->equals('false')
			|| $psgconf->data_obj('newsyslog_enable')->equals('false'));

	&_add_files ($psgconf, '644', '* $D0 JN', ( $psgconf->data_obj('log_dir')->get() . '/xferlog' ));
}

sub _policy_add_www_files 
{
     my ($self, $psgconf) = @_;
	my (@logs);

	return
		if ($psgconf->data_obj('www_enable')->equals('false')
			|| $psgconf->data_obj('newsyslog_enable')->equals('false'));

     @logs = (
          $psgconf->data_obj('www_log_dir')->get() . '/suexec',
          $psgconf->data_obj('www_log_dir')->get() . '/error_log',
          $psgconf->data_obj('www_log_dir')->get() . '/access_log'
     );

     map { push @logs, $_; } values %{$psgconf->data_obj('www_vh_access_log')->get()};

     push @logs, $psgconf->data_obj('www_log_dir')->get() . '/ssl_engine_log'
          if ( $psgconf->data_obj('www_vh_ssl')->count() );  
          
     push @logs, $psgconf->data_obj('www_jk_logfile')->get()
          if ( $psgconf->data_obj('www_jk_enable')->equals('true'));

     ###
     ### FIXME: Need to add the bluestem and anyother logs not in
     ### the stock psgconf modules.  This really screams to be put
     ### into a method that other Control modules can call from w/i
     ### one of their policies.
     ###

	&_add_files ( $psgconf, '644', '* $D0 JN', @logs );
}

###############################################################################
###
###  new() constructor
###
###############################################################################

sub new {
	my ($class, $psgconf) = @_;
	my ($self);
  
	$self = {};
  
	bless($self, $class);
  
	$self->{name} = 'newsyslog';
	$self->{enable} = $self->{name} . '_enable';
	$self->{packages} = $self->{name} . '_packages';

	$psgconf->register_data(
		'newsyslog_enable'	=> PSGConf::Data::Boolean->new(
							'value' => 'false'
						),
		'newsyslog_file_cnt'	=> PSGConf::Data::Integer->new(
							'value' => 14
						),
		'newsyslog_files'		=> PSGConf::Data::List->new()
	);

	### 
	### Rotate the syslog/ftp/apache logs.  Do not need to do the
	### TSM logs as dsmc does it for us (by setting the retention
	### in the config files).
	### 
	$psgconf->register_policy($self,
		newsyslog_add_syslog_files		=> '_policy_add_syslog_files',
		newsyslog_add_ftp_files		=> '_policy_add_ftp_files',
		newsyslog_add_www_files		=> '_policy_add_www_files'
	);

	return $self;
}

###############################################################################
###
###  decide() method
###
###  generate /etc/newsyslog.conf based on files specified in psg.conf
###
###############################################################################

sub decide {
	my ($self, $psgconf) = @_;
	my ($conffile);
	
	return
		if ($psgconf->data_obj('newsyslog_enable')->equals('false'));

	### create /etc/newsyslog.conf
	$psgconf->register_actions(
		PSGConf::Action::GenerateFile::Literal->new(
			name		=> '/etc/newsyslog.conf',
			content	=> join("\n", @{$psgconf->data_obj('newsyslog_files')->get()}) . "\n",
		)
	)
}

1;

__END__

=head1 NAME

PSGConf::Control::newsyslog - psgconf control class for controlling log rotation/pruning for Solaris

=head1 SYNOPSIS

In F<psgconf_modules>:

  Control PSGConf::Control::newsyslog

=head1 DESCRIPTION

The B<PSGConf::Control::newsyslog> module provides a B<psgconf> control object
for rotating system logs using the newsyslog facility.
It supports the following methods:

=over 4

=item new()

The constructor.  Its parameter is a reference to the B<PSGConf>
object.  It registers the following data objects:

=over 4

=item I<newsyslog_enable>

A B<PSGConf::Data::Boolean> object to enable/disable this feature

=item I<newsyslog_file_cnt>

A B<PSGConf::Data::Integer> object for the number of files we need to
keep.  The default is 14.

=item I<newsyslog_files>

A B<PSGConf::Data::List> object of the commands to use to configure
C<newsyslog>.

=back

The constructor also registers the following policy methods:

=over 4

=item I<newsyslog_add_syslog_files>

Requests that the C<syslog> files be rotated.

=item I<newsyslog_add_ftp_files>

Requests that the C<anon_ftp> log files be rotated.

=item I<newsyslog_add_www_files>

Requests that the C<apache> log files be rotated.

=back

=item decide()

Registers a B<PSGConf::Action::GenerateFile::Literal> Action object to
generate the F</etc/newsyslog.conf> file.

=back

=head1 SEE ALSO

C<CE<lt>newsyslog(8)E<gt>>

L<perl>

L<PSGConf>

L<PSGConf::Action::GenerateFile::Literal>

L<PSGConf::Data::List>

L<PSGConf::Data::Integer>

L<PSGConf::Data::Boolean>

L<psgconf-intro>

=cut


syntax highlighted by Code2HTML, v. 0.9.1