# Copyright 05/10/01 Sun Microsystems, Inc. All Rights Reserved.
#pragma ident  "@(#)agent-stats-d.rul	1.2 01/05/10 Sun Microsystems"


proc ragst100 {action ruleId {rowName ""} {rowIndex 0} {matchList ""} } {
	set estatus warning
	switch $action {
                condition {
			set memUsed [ getMyValue $ruleId $rowName "" ]
			if [ catch { meminfo } result ] {
			  ddl print error "Unable to get physical memory information\n"
			  return $estatus
		        }
			set physicalMem [ expr ([ lindex $result 0 ]*1024) ]
#
# physicalMem is in MB, so *1024 to convert it to KB for comparision
# The idea is to use 27% of RAM or 70000KB, which ever is greater as the alarm limit
# we get 27% from this calculation (70000/(256KB*1024))*100
# Similar calculation for the warning limit too
#
			set EditErrLimit [ getRuleParm EDIT $ruleId $rowName critical ]
                        if { $EditErrLimit == "" } {
                          set ErrMemlimit [ expr ($physicalMem*0.27) ]
                          set Errlimit [ expr ( $ErrMemlimit > 70000 )?$ErrMemlimit:70000]
                        } else {
                          set Errlimit $EditErrLimit
                        }
                        set EditWarnLimit [ getRuleParm EDIT $ruleId $rowName alert ]
			if { $EditWarnLimit == "" } {
                        set WarnMemlimit [ expr ($physicalMem*0.23) ]
                        set Warnlimit [ expr ( $WarnMemlimit > 60000 )?$WarnMemlimit:60000]
			} else {
			  set Warnlimit $EditWarnLimit
			}
			ddl print debug "EditErrLimit $EditErrLimit EditWarnLimit $EditWarnLimit"
			ddl print debug "memUsed $memUsed physicalMem $physicalMem Warnlimit $Warnlimit Errlimit $Errlimit"
			if { $memUsed > $Errlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Errlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "error-gt"
			  return 1
			} elseif { $memUsed > $Warnlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Warnlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "warning-gt"
			  return 1
			}
		}
		init {
                        return ""
                }
                open {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
                  set lim [ getRuleParm DYN $ruleId $rowName threshold "" ]
                  if { $estatus == "error-gt" } {
		    set msg [ format [ getRuleMsg iragst100msg01 ] "$lim" ]
                  } elseif { $estatus == "warning-gt" } {
                    set msg [ format [ getRuleMsg iragst100msg ] "$lim" ]
                  }
                  setRuleText $ruleId $rowName $estatus $msg ""
                  ddl print debug "$msg\n"
                  return $estatus
		}
		continue {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
		  return $estatus
		}
		ack {
                        return ""
                }
		fix {
                        return ""
                }
                close {
                        return ok
                }
	}

}

proc ragst101 {action ruleId {rowName ""} {rowIndex 0} {matchList ""} } {
	set estatus warning
	switch $action {
                condition {
			set memUsed [ getMyValue $ruleId $rowName "" ]
			if [ catch { meminfo } result ] {
			  ddl print error "Unable to get physical memory information\n"
			  return $estatus
		        }
			set physicalMem [ expr ([ lindex $result 0 ]*1024) ]
#
# physicalMem is in MB, so *1024 to convert it to KB for comparision
# The idea is to use 19% of RAM or 50000KB, which ever is greater as the alarm limit
# we get 19% from this calculation (50000/(256KB*1024))*100
#
			set EditErrLimit [ getRuleParm EDIT $ruleId $rowName critical ]
                        set EditWarnLimit [ getRuleParm EDIT $ruleId $rowName alert ]
			if { $EditWarnLimit == "" } {
                        set WarnMemlimit [ expr ($physicalMem*0.19) ]
                        set Warnlimit [ expr ( $WarnMemlimit > 50000 )?$WarnMemlimit:50000]
			} else {
			  set Warnlimit $EditWarnLimit
			}
			ddl print debug "EditErrLimit $EditErrLimit EditWarnLimit $EditWarnLimit"
			ddl print debug "memUsed $memUsed physicalMem $physicalMem Warnlimit $Warnlimit"
			if { ($EditErrLimit != "") && ($memUsed > $EditErrLimit) } {
			  setRuleParm DYN $ruleId $rowName threshold "$EditErrLimit"
			  setRuleParm DYN $ruleId $rowName rulestate "error-gt"
			  return 1
			} elseif { $memUsed > $Warnlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Warnlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "warning-gt"
			  return 1
			}
		}
		init {
                        return ""
                }
                open {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
                  set lim [ getRuleParm DYN $ruleId $rowName threshold "" ]
                  if { $estatus == "error-gt" } {
                    set msg [ format [ getRuleMsg iragst100msg01 ] "$lim" ]
                  } elseif { $estatus == "warning-gt" } {
                    set msg [ format [ getRuleMsg iragst100msg ] "$lim" ]
                  }
                  setRuleText $ruleId $rowName $estatus $msg ""
                  ddl print debug "$msg\n"
                  return $estatus
		}
		continue {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
		  return $estatus
		}
		ack {
                        return ""
                }
		fix {
                        return ""
                }
                close {
                        return ok
                }
	}
}


proc ragst102 {action ruleId {rowName ""} {rowIndex 0} {matchList ""} } {
	set estatus warning
	switch $action {
                condition {
			set memUsed [ getMyValue $ruleId $rowName "" ]
			if [ catch { meminfo } result ] {
			  ddl print error "Unable to get physical memory information\n"
			  return $estatus
		        }
			set physicalMem [ expr ([ lindex $result 0 ]*1024) ]
#
# physicalMem is in MB, so *1024 to convert it to KB for comparision
# The idea is to use 29% of RAM or 75000KB, which ever is greater as the alarm limit
# we get 29% from this calculation (75000/(256KB*1024))*100
# Similar calculation for the warning limit too
#
			set EditErrLimit [ getRuleParm EDIT $ruleId $rowName critical ]
                        if { $EditErrLimit == "" } {
                          set ErrMemlimit [ expr ($physicalMem*0.29) ]
                          set Errlimit [ expr ( $ErrMemlimit > 75000 )?$ErrMemlimit:75000]
                        } else {
                          set Errlimit $EditErrLimit
                        }
                        set EditWarnLimit [ getRuleParm EDIT $ruleId $rowName alert ]
			if { $EditWarnLimit == "" } {
                        set WarnMemlimit [ expr ($physicalMem*0.25) ]
                        set Warnlimit [ expr ( $WarnMemlimit > 65000 )?$WarnMemlimit:65000]
			} else {
			  set Warnlimit $EditWarnLimit
			}
			ddl print debug "EditErrLimit $EditErrLimit EditWarnLimit $EditWarnLimit"
			ddl print debug "memUsed $memUsed physicalMem $physicalMem Warnlimit $Warnlimit Errlimit $Errlimit"
			if { $memUsed > $Errlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Errlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "error-gt"
			  return 1
			} elseif { $memUsed > $Warnlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Warnlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "warning-gt"
			  return 1
			}
		}
		init {
                        return ""
                }
                open {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
                  set lim [ getRuleParm DYN $ruleId $rowName threshold "" ]
		  set msg [ format [ getRuleMsg iragst100msg ] "$lim" ]
                  setRuleText $ruleId $rowName $estatus $msg ""
                  ddl print debug "$msg\n"
                  return $estatus
		}
		continue {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
		  return $estatus
		}
		ack {
                        return ""
                }
		fix {
                        return ""
                }
                close {
                        return ok
                }
	}

}

proc ragst103 {action ruleId {rowName ""} {rowIndex 0} {matchList ""} } {
	set estatus warning
	switch $action {
                condition {
			set memUsed [ getMyValue $ruleId $rowName "" ]
			if [ catch { meminfo } result ] {
			  ddl print error "Unable to get physical memory information\n"
			  return $estatus
		        }
			set physicalMem [ expr ([ lindex $result 0 ]*1024) ]
#
# physicalMem is in MB, so *1024 to convert it to KB for comparision
# The idea is to use 21% of RAM or 55000KB, which ever is greater as the alarm limit
# we get 21% from this calculation (55000/(256KB*1024))*100
#
			set EditErrLimit [ getRuleParm EDIT $ruleId $rowName critical ]
                        set EditWarnLimit [ getRuleParm EDIT $ruleId $rowName alert ]
			if { $EditWarnLimit == "" } {
                        set WarnMemlimit [ expr ($physicalMem*0.21) ]
                        set Warnlimit [ expr ( $WarnMemlimit > 55000 )?$WarnMemlimit:55000]
			} else {
			  set Warnlimit $EditWarnLimit
			}
			ddl print debug "EditErrLimit $EditErrLimit EditWarnLimit $EditWarnLimit"
			ddl print debug "memUsed $memUsed physicalMem $physicalMem Warnlimit $Warnlimit"
			if { ($EditErrLimit != "") && ($memUsed > $EditErrLimit) } {
			  setRuleParm DYN $ruleId $rowName threshold "$EditErrLimit"
			  setRuleParm DYN $ruleId $rowName rulestate "error-gt"
			  return 1
			} elseif { $memUsed > $Warnlimit } {
			  setRuleParm DYN $ruleId $rowName threshold "$Warnlimit"
			  setRuleParm DYN $ruleId $rowName rulestate "warning-gt"
			  return 1
			}
		}
		init {
                        return ""
                }
                open {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
                  set lim [ getRuleParm DYN $ruleId $rowName threshold "" ]
                  set msg [ format [ getRuleMsg iragst100msg ] "$lim" ]
                  setRuleText $ruleId $rowName $estatus $msg ""
                  ddl print debug "$msg\n"
                  return $estatus
		}
		continue {
		  set estatus [ getRuleParm DYN $ruleId $rowName rulestate "" ]
		  return $estatus
		}
		ack {
                        return ""
                }
		fix {
                        return ""
                }
                close {
                        return ok
                }
	}
}

