#
#   File:	fscan-d.rul
#   Author:	Haitian Zhou
#   Version: 	1.7 02/12/17 17:28:02
#
#   Copyright (c) 1997 Halcyon Inc.
#
#   Rule for File Scanning Module
#

proc filename { action ruleId {rowName ""} {rowindex 0} {matchlist ""} } {
    set activeNode [ ilookup -d "" internal fscanstats ]
    if { $activeNode == "" } {
	set activeNode [ locate *fscanstats ]
	define internal fscanstats $activeNode
    }

    set access [  toe_send $activeNode lookup -d "" internal file_access ]

    switch $action {
	condition {
	    if { $access == "" } {
		return 0
	    } else {
	        return 1
	    }
	}
	open {
	    switch $access {
		RDERR {
		    set msg "[ getValue 0 ] is not readable"
		}
		OPERR {
		    set state "error"
		    set msg "[ getValue 0 ] cannot be opened"
		}
		ACERR {
		    set msg "[ getValue 0 ] is not accessable"
		}
		default {
		    set state "ok"
		    set msg ""
		}
	    }
	    setRuleText $ruleId $rowName "warning" "$msg"
	    return "warning"
	}
    }
}


#
# Total matching rule
#
proc rCompare_match { action ruleId {rowName ""} {rowIndex 0} {matchlist ""} } {

    # first call rCompare rule to get the status
    set status [ rCompare $action $ruleId $rowName $rowIndex {} ]

    # get the reraise flag from the rule editing parameter
    set reraise [ getRuleParm EDIT $ruleId $rowName reraise ]

    if { $reraise != 1 } {
	#
	# The rule for this row is not reraising, so we should return
	# immidiately. But we need set the old-data value before return.
	# Because when the user changes the reraise flag to reraise alarm,
	# the agent will this rule and it enters the "continue" flag.
	# If the old-data is not the same as data, this rule will raise
	# a false alarm.
	#
	define old-data $rowIndex [ ilookup data $rowIndex ]

	#
	# reset the total number
	#
	if { $action == "ack" } {
	    if { [ getRuleParm EDIT $ruleId $rowName reset ] == "1" } {
		toe_send [ locate fscanstats ] define pmatches $rowName 0
	    }
	}

	return $status
    }

    switch $action {
	open {
	    define old-data $rowIndex [ ilookup data $rowIndex ]
	}

	continue {
	    set oldstatus [ getRuleCond $ruleId $rowName ]

	    set oldval [ ilookup old-data $rowIndex ]
	    set val [ ilookup data $rowIndex ]

	    #
	    # If the status changes in the continue section, the
	    # agent will close the current event and fire a new event.
	    # So this rule only process the case when status is not change.
	    #
	    if { $val != $oldval && $status == $oldstatus } {
		writeEvent C $ruleId $rowName $status {} {}
		openEvent $ruleId $rowName $status

		#
		# fire action command
		#
		pushAlarmParameters $rowName
		runActionCommand $status $rowName "" _resolveStatusParams
		popAlarmParameters
	    }

	    define old-data $rowIndex $val
	}

	ack { 
	    if { [ getRuleParm EDIT $ruleId $rowName reset ] == "1" } {
		toe_send [ locate fscanstats ] define pmatches $rowName 0
	    }
	}
    }

    return $status
}

