#!/usr/bin/perl

# Discover  (c) Copyright 1998 Mark Black 

# NOTE:  If you modify this code, you MUST change the version to
#        something over 100 to prevent it being replaced by future
#        versions of this script. 
$version = 2 ;

# Discover [-v]
# This script it used to automatically populate the matd.conf file.
# It discovers which monitored parameters are available on a host,
# and creates the matd.conf file accordingly.  It runs the monitoring
# scripts with the "-i" argument.  This provides the discover script 
# with information about the instances of a particular monitored 
# parameter on a host.  
#
# The jobid's used by the Discover script are fixed.  The jobid's 
# will be used to determine which parameters to try and discover.
# NOTE:  The Discover script will only add jobs to the matd.conf
# file.
# The following jobid's are reserved:
# 1001 - 1099  - These are for general monitored parameters
#                with only one instance.
# 1100 - 1199  - These are for partition instances.
#

# Deal with possible spaces in the input 
$input = "" ;
foreach $i (0 .. $#ARGV) {
   $input .= "@ARGV[$i] " ;
}
chop($input) ;

# Location of MAT tmp dir
$mattmp = "tmp" ;
$mathome = "/var/mat/" ;
$matdconf = "etc/matd.conf" ;

# Output the version if requested
if($input eq "-v") {
    print "$version\n" ;
    exit(0) ;
}


# jobMatch - A subroutine for searching the jobid's for
#            existing jobs.  Returns 1 on match, 0 otherwise.
#            This uses the "jobids" array
sub JobMatch {
    local($find) = pop(@_) ;
    local($i) = 0 ;
    foreach $i (@jobids) {
	if ( $find == $i ) {
	    return 1 ;
	}
    }
    return 0 ;
}

# Read in the current matd.conf file, and extract the jobid's
$jobids = "" ;
$indx = 0 ;
$cnt = 0 ;
$ext = open(CONFFILE, $matdconf) ;
if ($ext) {
    # File exists:  Read it
    @din = <CONFFILE> ;
    close(CONFFILE) ;
    foreach $line (@din) {
	if ( $line =~ m/^timed.*|^interval.*/ ) {
	    # We have a line of data.  Pull out the jobid
	    ($a, $tmp, $c) = split(/\s+/, $line, 3) ;
	    $jobids[$indx] = $tmp ;
	    $jobid2loc[$tmp] = $cnt ;
	    #print "cnt = $cnt, jobids = @jobids[$indx] \n" ;
	    $indx++ ;
	}
	$cnt++ ;
    }
} else {
    # Hmmm... the config file does not exist.  Lets make a new one
    open(CONFFILE, "> $matdconf") ;
    print CONFFILE "" ;
    print CONFFILE "# This file was generated by MAT.  Edit with care!\n" ;
    print CONFFILE "#\n" ;
    print CONFFILE "# The line format is follows:\n" ;
    print CONFFILE "#   interval  jobid  minutes  type#  timeout(sec)  UID  OnError  Logfilename  retention {Job specific data}\n" ;
    print CONFFILE "#   timed  jobid  min,s  hour,s  weekday,s  monthday,s  month,s  type#  timeout(sec)  UID  OnError  Logfilename  retention {Job specific data}\n" ;
    print CONFFILE "#   version data   {NOTE: This is not a user definable parameter}\n" ;
    print CONFFILE "# The following illustrates the form of the {Job specific data}\n" ;
    print CONFFILE "#    * Monitoring Jobs:    WarnFilename  Warn_Thres  Gflags  Monitor_What  {Optional Data}\n" ;
    print CONFFILE "#    * User Script Jobs:   ScriptFile\n" ;    
    print CONFFILE "#    * Cleanup Job:        Log_file_to_Clean  Lines_to_Retain\n" ;
    print CONFFILE "#      Replication Jobs:   ServerHost  ServerDir  ClientDir  Create_As  File_Umask  Dir_Umask\n" ;
    print CONFFILE "#      Backup Jobs:        ServerHost  ListFile  ErrorScript  VolumeSet\n" ;
    print CONFFILE "#      Time Sync Job:      ServerHost\n" ;
    print CONFFILE "# Those designated with a \"*\" are implemented in this release\n" ;
    print CONFFILE "# Comment lines start with a \"#\"\n" ;
    print CONFFILE "version 1 0 0 0 0 0 0 0 0\n" ;
    print CONFFILE "timed 1999 0 23 * * * 8 120 0 0 /var/mat/var/logcleaner 144\n" ;
    print CONFFILE "timed 1990 30 23 * * * 4 30 0 1 /var/mat/var/user1log 144 scripts/Discover\n" ;
    print "Creating new matd.conf \n" ;
    close(CONFFILE) ;
}


# We have the existing job data.  Now we are ready to discover
# new parameters to monitor.
$jobs2add = "" ;
print "Discovering  " ;

# Have we a Run Queue monitored parameter.  It's an internal function,so we have it
$got = &JobMatch(1001) ;
if (!$got) {
    $jobs2add .= sprintf("interval 1001 6 1 60 0 0 var/runlog 288 /tmp/none 0 0 1\n") ;
    print "1001 " ;
}

# Have we a CPU use monitored parameter.  It's an internal function,so we have it
$got = &JobMatch(1002) ;
if (!$got) {
 
   $jobs2add .= sprintf("interval 1002 6 1 60 0 0 var/cpulog 288 /tmp/none 0 0 2\n") ;
   print "1002 " ;
}

# Have we a Swap use monitored parameter.
$got = &JobMatch(1004) ;
if (!$got) {
    # The Swap monitored parameter was not found.  Check for an instance.
    $data = `scripts/monSwap -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1004 6 1 60 0 0 var/swaplog 288 /tmp/none 0 0 4\n") ;
	print "1004 " ;
    }
}

# Have we a Memory use monitored parameter
$got = &JobMatch(1005) ;
if (!$got) {
    # The Memory monitored parameter was not found.  Check for an instance.
    $data = `scripts/monMem -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1005 6 1 60 0 0 var/memlog 288 /tmp/none 0 0 5\n") ;
	print "1005 " ;
    }
}

# Have we a manditory_Processes monitored parameter.
$got = &JobMatch(1006) ;
if (!$got) {
    # The Manditory Proecesses monitored parameter was not found.  Check for an instance.
    $data = `scripts/monProcs -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1006 10 1 60 0 0 var/proclog 288 /tmp/none 0 0 6 etc/process_list\n") ;
	print "1006 " ;
    }
}

# Have we a Network Connectivity monitored parameter
$got = &JobMatch(1007) ;
if (!$got) {
    # The Network Connectivity monitored parameter was not found.  Check for an instance.
    $data = `scripts/monNetCon -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1007 10 1 60 0 0 var/netclog 288 /tmp/none 0 0 7 127.0.0.1\n") ;
	print "1007 " ;
    }
}

# Have we a Network Interface monitored parameter.
$got = &JobMatch(1008) ;
if (!$got) {
    # The Network Interface monitored parameter was not found.  Check for an instance.
    $data = `scripts/monEthNet -i` ;
    if ( $data =~ m/^1.*|^2.*|^3.*|^4.*|^5.*|^6.*|/ ) {
	$jobs2add .= sprintf("interval 1008 10 1 60 0 0 var/niclog 288 /tmp/none 0 0 8\n") ;
	print "1008 " ;
    }
}

# Have we a FTP monitored parameter
$got = &JobMatch(1009) ;
if (!$got) {
    # The FTP monitored parameter was not found.  Check for an instance.
    $data = `scripts/monFTP -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1009 10 1 60 0 0 var/ftplog 288 /tmp/none 0 0 9\n") ;
	print "1009 " ;
    }
}

# Have we a Network Connectivity monitored parameter
$got = &JobMatch(1010) ;
if (!$got) {
    # The Network Connectivity monitored parameter was not found.  Check for an instance.
    $data = `scripts/monMail -i` ;
    if ( $data =~ m/^1.*/ ) {
	$jobs2add .= sprintf("interval 1010 10 1 60 0 0 var/smtplog 288 /tmp/none 0 0 10\n") ;
	print "1010 " ;
    }
}

# Find out how many partition instances we have
chop($data = `scripts/monDisk -i`) ;
($numd, @partits) = split(/:/, $data) ;

# Find the number of monitored disks, and the next free jobid #.
$indx = 0 ;
foreach $val (1101..1199) {
    $got = &JobMatch($val) ;
    if(!$got) {
	# Don't have a break function!
	$indx = $val ;
	goto diskd ;
    }
}
diskd:

# Scan through the list of instances an add any that are not found 

$maxdisk = $indx - 1 ;
foreach $inst (@partits) {
    $found = 0 ;
    # Run through the jobs and look for a match
    foreach $loc (1101..$indx) {
	$tmp = @din[$jobid2loc[$loc]] ;
	@tmp2 = split(/\s+/, $tmp) ;
	$mntp = pop(@tmp2) ;
	#print "Loc = $loc   inst = $inst    Mount point = $mntp\n" ;
	if( $mntp eq $inst ) {
	    $found = 1 ;
	} 
    }

    if( $found == 0 ) {
	$maxdisk++ ;
	$dlog = $maxdisk - 1100 ;
	#print "Partition = $inst, mathome = $mathome, dlog = $dlog, maxdisk = $maxdisk  \n" ;
	$jobs2add .= sprintf("interval %s 15 1 60 0 0 var/disk%slog 288 /tmp/none 0 0 3 %s\n", $maxdisk, $dlog, $inst) ;
    }
}

print "\n" ;

# Append the new jobs to the file
open(CONFFILE, ">> $matdconf") ;
print "Writing config file....\n" ;
print CONFFILE $jobs2add ;
close(CONFFILE) ;

print $jobs2add ;
