#!/bin/sh  
# \
exec oagwish "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.8  2002/07/09 15:55:09  shang
# modified to use the log files that are named by dates (saves the searching time)
#
# Revision 1.7  2002/06/11 20:13:04  soliday
# Changed it so that it only searches the last six months.
#
# Revision 1.6  2002/06/11 20:03:10  soliday
# Added the Initialize search option.
#
# Revision 1.5  2000/12/06 22:43:20  soliday
# Added ability to apply filters.
#
# Revision 1.4  2000/12/05 20:31:07  soliday
# Added scriptAction review.
#
# Revision 1.3  2000/11/30 15:07:27  borland
# Added -log commandline option and support for general logDaemon log review.
# Added special statements for mpExecAudit log.
#
# Revision 1.2  2000/03/27 14:26:41  borland
# Now shows the procedure name in the printout.
#
# Revision 1.1  2000/03/18 16:26:24  borland
# First version.
#
#

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0  /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath

set log PEMErrorLog
set args $argv
APSStrictParseArguments {log}

set logDir /home/helios/oagData/logDaemonData

set CVSRevisionAuthor "\$Revision: 1.9 $ \$Author: soliday $"

set args $argv

proc SetStatus {text} {
    global statusText
    set statusText "$text ([clock format [clock seconds] -format %H:%M:%S])"
    update
}

proc APSCheckButtonAndEntryBoxFrame { widget args } {
    global apsContextHelp
    set parent ""
    set noPack 0
    set packOption "-side top"
    set label "NoLabel"
    set buttonList ""
    set variableList ""
    set entryVariableList ""
    set commandList ""
    set orientation vertical
    set allNone 0
    set contextHelp ""
    set relief ridge
    set limitPerRow 0
    set buttonNameList ""
    set gridPack ""
    set width 20
    APSStrictParseArguments {parent noPack packOption label buttonList  variableList orientation allNone commandList  contextHelp relief limitPerRow gridPack width entryVariableList}

    if {$commandList != "" &&
        [llength $buttonList] != [llength $commandList]} {
        puts stderr "commandList must be same length as buttonList"
        return
    }
    if {$contextHelp != ""} {
        set apsContextHelp($parent$widget) $contextHelp    
    }
    if {$orientation == "vertical"} {
        set packside top
        set opackside left
        set oanchor n
        set packOption "$packOption -fill y"
    } else {
        set packside left
        set opackside top
        set oanchor w
        set packOption "$packOption -fill x"
    }
    APSFrame $widget -parent $parent -packOption $packOption -noPack $noPack -label $label -orientation $orientation -relief $relief -gridPack $gridPack
    
    set variableIndex 0
    set buttonNumber 1
    set numberInRow $limitPerRow
    set row 0
    foreach button $buttonList {
        if {$limitPerRow && $numberInRow>=$limitPerRow} {
            set numberInRow 0
            frame $parent$widget.frame.row$row 
            pack $parent$widget.frame.row$row -fill y -fill x -side $opackside  -anchor $oanchor
            set parent1 $parent$widget.frame.row$row
            incr row
        } elseif !$limitPerRow {
            set parent1 $parent$widget.frame
        }
        set cmd [lindex $commandList $variableIndex]
        checkbutton $parent$widget.frame.button$buttonNumber -text $button  -variable [lindex $variableList $variableIndex] -anchor w  -command $cmd
        pack $parent$widget.frame.button$buttonNumber -side $packside -fill x  -in $parent1
        entry $parent$widget.frame.entry$buttonNumber -relief sunken -width $width -textvariable [lindex $entryVariableList $variableIndex]
        pack $parent$widget.frame.entry$buttonNumber -side $packside -fill x  -in $parent1
	lappend buttonNameList $parent$widget.frame.button$buttonNumber
        incr variableIndex
        incr buttonNumber
        incr numberInRow
    }
    if {$allNone} {
        set setBody {
            global $var
	    if {[set $var] == 0} {
            	$buttonName invoke
	    }
        }
        set setCmd [list foreach var $variableList buttonName $buttonNameList $setBody]
        set clrBody {
            global $var
	    if {[set $var] != 0} {
            	$buttonName invoke
	    }
        }
        set clrCmd [list foreach var $variableList buttonName $buttonNameList $clrBody]
        APSButton .all -parent $parent$widget.frame -text All -size small  -command $setCmd
        APSButton .none -parent $parent$widget.frame -text None -size small  -command $clrCmd
    }
}

proc SearchAndDisplayActivity {} {
    global startDay startMonth startYear startHour log
    global stopDay stopMonth stopYear stopHour logDir
    global actionList

    if [catch {APSConvertTimeToHours $startHour} hour] {
        SetStatus "Invalid starting hour: $startHour"
        return
    }
    if [catch {exec timeconvert \
                 -breakdown=year=$startYear,month=$startMonth,day=$startDay,hour=$hour} startTime] {
        SetStatus "Invalid starting time: $startTime"
        return
    }

    if [catch {APSConvertTimeToHours $stopHour} hour] {
        SetStatus "Invalid stopping hour: $stopHour"
        return
    }
    if [catch {exec timeconvert \
                 -breakdown=year=$stopYear,month=$stopMonth,day=$stopDay,hour=$hour} stopTime] {
        SetStatus "Invalid stopping time: $stopTime"
        return
    }
    
    set fileList  \
      [APSFindFilesBetweenDates -tailsOnly 0 -extensionList {"*"}\
         -directory $logDir/$log \
         -startDateList [APSFormatDate -year $startYear -month $startMonth \
                           -day $startDay -dateFormat list] \
         -endDateList [APSFormatDate -year $stopYear -month $stopMonth \
                         -day $stopDay -dateFormat list] ]
    if ![llength $fileList] {
        SetStatus "Log files for $log not found."
        return
    }
    
    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile $tmpFile.sdds
    switch $log {
        PEMErrorLog {
            if [catch {eval exec sddscombine $fileList -merge -pipe=out \
                         | sddsprocess -pipe -filter=column,Time,$startTime,$stopTime \
                         | sddssort -pipe -column=Time,decr \
                         | sddstimeconvert -pipe -break=column,Time,text=TimeStamp \
                         | sddsexpand -pipe \
                         | tee $tmpFile.sdds \
                         | sddsprintout -pipe=in $tmpFile \
                         -parameter=mpName,label=Procedure,end \
                         -parameter=TimeStamp -parameter=user,label=User,end \
                         -parameter=message,label=ErrorMessage} result] {
                SetStatus "$result"
                return
            }
        }
        mpExecAudit {
            if [catch {eval exec sddscombine $fileList -merge -pipe=out \
                         | sddsprocess -pipe -filter=column,Time,$startTime,$stopTime \
                         | sddssort -pipe -column=Time,decr \
                         | sddstimeconvert -pipe -break=column,Time,text=TimeStamp \
                         | sddsexpand -pipe \
                         | tee $tmpFile.sdds \
                         | sddsprintout -pipe=in $tmpFile \
                         -parameter=mpName,label=Procedure,end \
                         -parameter=TimeStamp -parameter=user,label=User,end \
                         -parameter=invokingHost,label=Host -parameter=action,label=Action } result] {
                SetStatus "$result"
                return
            }
        }
        scriptAction {
	    global actions display
	    set option ""
	    if {!($actions(All))} {
		foreach name "Error Abort Start initialize Finish" {
		    if {$actions($name)} {
			if {![llength $option]} {
			    set option "-match=column,Action=$name"
			} else {
			    append option ",Action=$name,|"
			}
		    }
		}
	    }
	    set option2 ""
	    set option3 ""
	    foreach name "Procedure Script TimeStamp User Host Action Status Parameters Stack" {
		if {$display($name)} {
		    lappend option2 "-parameter=$name"
		    if {$name != "TimeStamp"} {
			if {![llength $option3]} {
			    set option3 "-match=column,$name=$display(${name}Text)"
			} else {
			    append option3 ",$name=$display(${name}Text),&"
			}
		    }
		}
	    }
	    set option2 [join $option2 ",end "]
            global sortby
            if {$sortby(stack)} {
                set option4 "-column=Stack,incr -column=Time,decr"
            } else {
                set option4 "-column=Time,decr"
            }
            if [catch {eval exec sddscombine $fileList -merge -pipe=out \
			   | sddsprocess -pipe -filter=column,Time,$startTime,$stopTime \
			   $option $option3 \
			   | sddssort -pipe $option4 \
			   | sddstimeconvert -pipe -break=column,Time,text=TimeStamp \
			   | sddsexpand -pipe \
			   | tee $tmpFile.sdds \
			   | sddsprintout -pipe=in $tmpFile $option2} result] {
                SetStatus "$result"
                return
            }
        }
        default {
            if [catch {eval exec sddscombine $fileList -merge -pipe=out \
                         | sddsprocess -pipe -filter=column,Time,$startTime,$stopTime \
                         | sddssort -pipe -column=Time,decr \
                         | sddstimeconvert -pipe -break=column,Time,text=TimeStamp \
                         | sddsconvert -pipe -delete=col,Time -delete=parameter,NumberCombined \
                         | sddsexpand -pipe \
                         | tee $tmpFile.sdds \
                         | sddsprintout -pipe=in $tmpFile \
                         -parameter=*,end } result] {
                SetStatus "$result"
                return
            }
        }
    }
    APSFileDisplayWindow [APSUniqueName .] -comment \
      "$log log from $startYear/$startMonth/$startDay@$startHour to $stopYear/$stopMonth/$stopDay@$stopHour" \
      -width 132 -height 20 -deleteOnClose 1 -fileName $tmpFile \
      -sddsExportableFile $tmpFile.sdds
}

APSApplication . -name "LogReview:$log" -version $CVSRevisionAuthor \
  -overview "Displays logdaemon entries for the $log log."

set statusText Working...
APSScrolledStatus .status -parent .userFrame -textVariable statusText \
  -width 80


APSDateTimeAdjEntry .startTime -parent .userFrame \
    -dayVariable startDay -yearVariable startYear \
    -monthVariable startMonth -hourVariable startHour \
    -label "Start date/time: " -twoDigitYear 0 -defaultHour 0
set startHour 0
APSDateTimeAdjEntry .stopTime -parent .userFrame \
    -dayVariable stopDay -yearVariable stopYear \
    -monthVariable stopMonth -hourVariable stopHour \
    -label "Stop date/time: " -twoDigitYear 0 -defaultHour 24
set stopHour 24

if {$log == "scriptAction"} {
    set actions(Error) 1
    set actions(Abort) 1
    set actions(Start) 0
    set actions(initialize) 0
    set actions(Finish) 0
    set actions(All) 0
    set sortby(stack) 0
    pack [frame .userFrame.f1]
    APSCheckButtonFrame .actions \
	-parent .userFrame.f1 \
	-label "Search Actions:" \
	-orientation horizontal \
	-buttonList "Error Abort Start Initialize Finish *" \
	-variableList "actions(Error) actions(Abort) actions(Start) actions(initialize) actions(Finish) actions(All)"
    APSCheckButtonFrame .sort \
	-parent .userFrame.f1 \
	-label "Sort by:" \
	-orientation horizontal \
	-buttonList "Stack" \
	-variableList "sortby(stack)"
    set display(Procedure) 1
    set display(Script) 0
    set display(TimeStamp) 1
    set display(User) 1
    set display(Host) 0
    set display(Action) 1
    set display(Status) 1
    set display(Parameters) 1
    set display(Stack) 1
    set display(ProcedureText) "*"
    set display(ScriptText) "*"
    set display(TimeStampText) "*"
    set display(UserText) "*"
    set display(HostText) "*"
    set display(ActionText) "*"
    set display(StatusText) "*"
    set display(ParametersText) "*"
    set display(StackText) "*"
    pack [frame .userFrame.f2]
    APSCheckButtonAndEntryBoxFrame .display \
	-parent .userFrame.f2 \
	-label "Display:" \
	-limitPerRow 1 \
	-allNone 1 \
	-orientation horizontal \
	-buttonList {"Procedure " "Script    " "TimeStamp " "User      " "Host      " "Action    " "Status    " "Parameters" "Stack     "} \
	-variableList "display(Procedure) display(Script) display(TimeStamp) display(User) display(Host) display(Action) display(Status) display(Parameters) display(Stack)" \
	-entryVariableList "display(ProcedureText) display(ScriptText) display(TimeStampText) display(UserText) display(HostText) display(ActionText) display(StatusText) display(ParametersText) display(StackText)"
    .userFrame.f2.display.frame.entry3 configure -state disabled -relief ridge
	
}

APSButton .search -parent .userFrame -text "Search" \
  -contextHelp \
  "Searches for and displays activity for the system and date/time range of interest." \
  -command SearchAndDisplayActivity

SetStatus Ready.
