2012-10-13

透過Silent Install的模式進行ITM Agent的安裝和配置

當需要安裝ITM Agent數量變多的時候,我們可能會希望讓系統自行安裝和配置,甚至於順便啟動服務。

下列提供的作法共分為七個步驟,分別是:

1.安裝Unix OS Agent、AIX Premium Agent和Unix LOG Agent
2.將原先kul_config檔案備份並將定義好的kul_config檔案拷貝至/opt/IBM/ITM/config目錄下
3.檢查Unix LOG Agent欲檢查的目錄及檔案是否存在
4.將原先syslog.conf檔案備份並將定義好的syslog.config檔案拷貝至/etc目錄底下
5.重新啟動syslog daemon
6.將ITM Agent安裝檔刪除
7.僅啟動AIX Premium和Unix LOG Agent,故必須把Unix OS Agent自動帶起服務的項目先Disable




#!/usr/bin/perl -w
use strict;
##################################################
## Target                                                                                 ##
## 1.Install & Config ul,ux,px agent                                        ##
## 2.Replace ITM config file:kul_configfile                              ##
## 3.Check directory and files for ul agent                          ##
## 4.Replace syslog entry to /etc/syslog.conf                            ##
## 5.Start syslogd but ITM agent                                          ##
## 6.Delete temp ul ux px agent directory                               ##
## 7.Disable ux agent from /etc/rc.itm1 file                              ##
## ##
## Date:   2012/10/12         ##
## Ver:    1.7         ##
## Author: Jammy Yu ##
##################################################

## Initial setup
my $source_dir = '/TWSE';
my $install_dir = '/opt/IBM/ITM';
my $env_dir = '/etc';

## Package source
my %agent_relation = (
"ux" => "$source_dir/ITM_V6.2.3_Agent_Multiplatform",
"ul" => "$source_dir/ITM_V6.2.3_Agent_Multiplatform",
"px" => "$source_dir/ITM_Agents_SystemP_V6.2.2",
);

## LOG record
open(LOG, ">$source_dir/agent_install.log") || die "$!\n";

## Start Program
my $start_time = `date +%Y-%m-%d_%H:%M:%S`;

print LOG "Program start time:$start_time\n";

##################################################
## 1.Install & Config ul,ux,px agent                                 ##
##################################################
while (my ($agent_name, $agent_source_dir) = each (%agent_relation))
{
   ## Check source agent directory
   if (!-e "$agent_source_dir")
   {
      print"ERROR: Please check source agent directories are aready under the $source_dir directory\n";
      exit 1;
   }
   ## Chmod agent Directory
   `chmod -R 777 $agent_source_dir`;
   print LOG "Chmod agent Directory:$agent_source_dir to 777\n";
   ## Install agent
   `$agent_source_dir/install.sh -q -h $install_dir -p $agent_source_dir/$agent_name\_silent_install.txt`;
   print LOG "Install agent name:$agent_name,\t Source agent dir:$agent_source_dir\n";

   ## Config agent
   `$install_dir/bin/itmcmd config -A -p $agent_source_dir/silent_config.txt $agent_name`;
   print LOG "Config agent name:$agent_name\n";
}
print LOG "-" x 80,"\n";

##################################################
## 2.Replace /opt/IBM/ITM/config/kul_configfile                 ##
##################################################
my $itmconfig_dir = '/opt/IBM/ITM/config';
my $command;
if (-e "$itmconfig_dir/kul_configfile")
{
   $command = "/usr/bin/mv $itmconfig_dir/kul_configfile $itmconfig_dir/kul_configfile.bak_$start_time";
   `$command`;
   $command = "/usr/bin/cp -f $source_dir/kul_configfile $itmconfig_dir/kul_configfile";
   `$command`;
   $command = "/usr/bin/chmod 777 $itmconfig_dir/kul_configfile";
   `$command`;
}
else
{
   $command = "/usr/bin/cp -f $source_dir/kul_configfile $itmconfig_dir/kul_configfile";
   `$command`;
   $command = "/usr/bin/chmod 777 $itmconfig_dir/kul_configfile";
   `$command`;
}

##################################################
## 3.check directory and files for ul agent                                 ##
##################################################
my %file_relation = (
        "/var/log/syslog/critical_files" => "D",
        "/var/log/syslog/auth_files" => "D",
        "/var/log/syslog/user_files" => "D",
        "/var/log/syslog/sftp_files" => "D",
        "/var/log/syslog/critical" => "F",
        "/var/log/syslog/auth" => "F",
        "/var/log/syslog/user" => "F",
        "/var/log/syslog/sftp" => "F",
);

while (my ($path, $type) = each (%file_relation))
{
=cut
   ## Build directory and files
   if ( $type eq 'D' )
   {
      build_dir($path);
   }
   elsif ( $type eq 'F' )
   {
      build_file($path);
   }
=cut
   ## Check directory and files
   if (-e "$path" )
   {
      print LOG "Type:$type,PATH:$path\t check: Exist!\n";
   }
   else
   {
      print LOG "Type:$type,PATH:$path\t check: Doesn't Exist!\n";
   }
}
print LOG "-" x 80,"\n";

##################################################
## 4.Replace syslog entry to /etc/syslog.conf                         ##
##################################################
if (-e "$env_dir/syslog.conf")
{
   $command = "/usr/bin/cp -f $env_dir/syslog.conf $env_dir/syslog.conf.bak_$start_time";
   `$command`;
   $command = "/usr/bin/cat $source_dir/syslog_build.config > $env_dir/syslog.conf";
   `$command`;
}
else
{
   $command = "/usr/bin/touch $env_dir/syslog.conf";
   `$command`;
   $command = "/usr/bin/cat $source_dir/syslog_build.config > $env_dir/syslog.conf";
   `$command`;
}


##################################################
## 5.Start syslogd but ITM agent                                 ##
##################################################
=cut
`$install_dir/bin/itmcmd agent start px`;
`$install_dir/bin/itmcmd agent start ul`;
=cut

`/usr/bin/stopsrc -s syslogd`;
`/usr/bin/startsrc -s syslogd`;


## Agent status
my $agent_status = `$install_dir/bin/cinfo -r`;
print LOG "ITM install agent status:$agent_status\n";
print LOG "-" x 80,"\n";
my $agent_status = `$install_dir/bin/cinfo -i`;
print LOG "ITM install package:$agent_status\n";
print LOG "-" x 80,"\n";
my $agent_status = `ps -ef | grep syslogd | grep -v grep`;
print LOG "Syslog daemon status:$agent_status\n";
print LOG "-" x 80,"\n";

##################################################
## 6.Delete temp ul ux px agent directory                           ##
##################################################
my $host = `hostname`;
chomp($host);

if ($host ne "WKLPAR")
{
   opendir(DIR, "$source_dir") || die "Can't open dir\n";
   while( defined(my $filename = readdir(DIR)) )
   {
      next if ($filename eq '.' || $filename eq '..');
 
      if (-d "$source_dir/$filename")
      {
         `rm -rf "$source_dir/$filename"`;
         print LOG "Delete directory:$source_dir/$filename\n";
      }
   }
   close(DIR);
}

## End Program
my $end_time = `date +%Y-%m-%d_%H:%M:%S`;
print LOG "Program end time:$end_time\n";

close(LOG);

##################################################
## 7.Disable ux agent from /etc/rc.itm1 file                         ##
##################################################
if (-e "$env_dir/rc.itm1")
{
   $command = "/usr/bin/cp -f $env_dir/rc.itm1 $env_dir/rc.itm1.bak_$start_time";
   `$command`;
}

open(TMP, "$env_dir/rc.itm1.bak_$start_time") || die "$!\n";

open(FHD, "> $env_dir/rc.itm1") || die "$!\n";

while ( <TMP> )
{
   chomp;
   if ( /ux/ )
   {
      $_ =~ s/$_/#$_/;
      print FHD "$_\n";
   }
   else
   {
      print FHD "$_\n";
   }
}

close(FHD);

close(TMP);

##################################################
## Subroutine                                            ##
##################################################
sub build_dir
{
   my $directory = shift;
   my $command;

   if (-e $directory)
   {
      $command = "/usr/bin/mv $directory $directory.bak_$start_time";
      `$command`;
      $command = "/usr/bin/mkdir -p $directory";
      `$command`;
   }
   else
   {
      $command = "/usr/bin/mkdir -p $directory";
      `$command`;
   }

   return;
}

sub build_file
{
   my $file = shift;
   my $command;
   if (-e $file)
   {
      $command = "/usr/bin/mv $file $file.bak_$start_time";
      `$command`;
      $command = "/usr/bin/touch $file";
      `$command`;
   }
   else
   {
      $command = "/usr/bin/touch $file";
      `$command`;
   }

   return;
}