Index: openacs.org-dev/packages/monitoring/tcl/monitoring-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs.org-dev/packages/monitoring/tcl/monitoring-procs.tcl,v
diff -u -r1.1.1.1 -r1.1.1.2
--- openacs.org-dev/packages/monitoring/tcl/monitoring-procs.tcl	9 Jul 2002 17:35:09 -0000	1.1.1.1
+++ openacs.org-dev/packages/monitoring/tcl/monitoring-procs.tcl	8 Oct 2002 15:47:14 -0000	1.1.1.2
@@ -1,9 +1,9 @@
 # /packages/monitoring/tcl/monitoring-procs.tcl
 ad_library {
      
-     @author jbank@arsdigita.com [jbank@arsdigita.com]
-     @creation-date Mon Jan 29 16:50:23 2001
-     @cvs-id
+    @author jbank@arsdigita.com [jbank@arsdigita.com]
+    @creation-date Mon Jan 29 16:50:23 2001
+    @cvs-id $Id$
 }
 
 if { [llength [info proc apm_package_id_from_key]] == 0 } {
@@ -26,13 +26,15 @@
     a descriptive quantifier. 
 } {
     if {$num < 0} {
-	return [format "%5.1f Kb" [util_commify_number $num]]
+        return [format "%5.1f Kb" [util_commify_number $num]]
     } else {
-	switch [expr {([string length $num] - 1) / 3}] {
-	    0   { return [append num  " Kb"] }
-	    1   { return [format "%5.1f Mb" [expr {$num / 1000.0}]] }
-	    default { return [format "%5.1f Gb" [util_commify_number [expr {$num / 1000000.0}]]] }
-}   }   }
+        switch [expr {([string length $num] - 1) / 3}] {
+            0   { return [append num  " Kb"] }
+            1   { return [format "%5.1f Mb" [expr {$num / 1000.0}]] }
+            default { return [format "%5.1f Gb" [util_commify_number [expr {$num / 1000000.0}]]] }
+        }   
+    }   
+}
 
 
 ad_proc ad_monitor_top {} { 
@@ -54,121 +56,120 @@
     set proc_var_list [list pid username threads priority nice proc_size \
                        resident_memory state cpu_total_time cpu_pct command]
     # location of the desired top function
-    set top_location [ad_parameter -package_id [monitoring_pkg_id] TopLocation monitoring]
-
+    set top_location [ad_parameter -package_id [monitoring_pkg_id] TopLocation monitoring "/usr/local/bin/top"]
+    set top_options [ad_parameter -package_id [monitoring_pkg_id] TopOptions monitoring "-bn 1"]
+    
     # make sure we have a path to top and that the file exists
     if { [empty_string_p $top_location] } {
-	ns_log Error "ad_monitor_top: cannot find top; TopLocation parameter in monitoring 
-                      is not defined"
-	return
+        ns_log Error "ad_monitor_top: cannot find top; TopLocation parameter in monitoring is not defined"
+        return
     } elseif { ![file exists $top_location] } {
-	ns_log Error "ad_monitor_top: Specified location for top ($top_location) does not exist"
-	return
+        ns_log Error "ad_monitor_top: Specified location for top ($top_location) does not exist"
+        return
     }
 
-    # set top_output [exec $top_location]
-    if [catch { set top_output [exec $top_location] } errmsg] {
+    if [catch { set top_output [eval "exec $top_location $top_options"] } errmsg] {
         # couldn't exec top at TopLocation
         if { ![file exists $top_location] } {
-                ad_return_error "top not found" "
-    The top procedure could not be found at $top_location:
-    <blockquote><pre> $errmsg </pre></blockquote>"
-                return
+            ns_log Error "ad_monitor_top: top not found $top_location: $errmsg"
+            return
         }
-        ad_return_error "insufficient top permissions" "
-    The top procedure at $top_location cannot be run:
-    <blockquote><pre> $errmsg </pre></blockquote>"
+        ns_log Error "ad_monitor_top: top could not be run - $errmsg"
         return
     }
     # else top execution went ok
     set top_list [split $top_output "\n"]
 
     ## Run through the output of top, grab header lines and leave the rest. 
     # number of header lines grabbed
-    set ctr 0   	
+    set ctr 0       
  
-    # greet the database
-    
-
     # id for this iteration of top-parsing
-    set top_id [db_string top_id_select "select ad_monitoring_top_top_id.nextval from dual"]
+    set top_id [db_nextval ad_monitoring_top_top_id]
 
     # have we reached the list of process info yet?    
-    set procflag 0	
+    set procflag 0  
     foreach line $top_list {
-
-	if { $procflag > 0 } {
-	    #compress multiple spaces
-    	    regsub -all {[ ]+} [string trim $line] " " line    
+        if { $procflag > 0 } {
+            #compress multiple spaces
+            regsub -all {[ ]+} [string trim $line] " " line    
             set proc_list [split $line]
+            ns_log Notice "line=$line"
 
-	    #skip blank lines
+            #skip blank lines
             if { [llength $proc_list] < 2 } { continue } 
             if { [llength $proc_list] < 11 } { 
-		ns_log Notice "skipping top process line: 
+                ns_log Notice "skipping top process line: 
                      element list shorter than variable list."
-		continue 
-	    }
-		
-	    #set proc-related vars
- 	    set pid       [lindex $proc_list 0]
-	    set username  [lindex $proc_list 1]
-	    set threads   [lindex $proc_list 2]
-	    set priority  [lindex $proc_list 3]
-	    set nice      [lindex $proc_list 4]
-	    set proc_size [lindex $proc_list 5]
-	    set resident_memory [lindex $proc_list 6]
-	    set state           [lindex $proc_list 7]
-	    set cpu_total_time  [lindex $proc_list 8]
-	    set cpu_pct   [lindex $proc_list 9]
-	    set command   [lindex $proc_list 10]
+                continue 
+            }
+        
+            #set proc-related vars
+            #vinodk: top (procps v.2.0.7 has different columns)
+            #set pid       [lindex $proc_list 0]
+            #set username  [lindex $proc_list 1]
+            #set threads   [lindex $proc_list 2]
+            #set priority  [lindex $proc_list 3]
+            #set nice      [lindex $proc_list 4]
+            #set proc_size [lindex $proc_list 5]
+            #set resident_memory [lindex $proc_list 6]
+            #set state           [lindex $proc_list 7]
+            #set cpu_total_time  [lindex $proc_list 8]
+            #set cpu_pct   [lindex $proc_list 9]
+            #set command   [lindex $proc_list 10]
 
-	    db_dml top_proc_info_insert "
-	    insert into ad_monitoring_top_proc
-	    (proc_id, top_id, pid, command, username, 
-	      threads, priority, nice, proc_size, 
-	      resident_memory, state, cpu_total_time, cpu_pct) 
- 	    values 
-	    (ad_monitoring_top_proc_proc_id.nextval, :top_id, :pid, 
-	      :command, :username, :threads, :priority, :nice, :proc_size,  
-	      :resident_memory, :state, :cpu_total_time, :cpu_pct)" 
+            set pid       [lindex $proc_list 0]
+            set username  [lindex $proc_list 1]
+            set threads   0
+            set priority  [lindex $proc_list 2]
+            set nice      [lindex $proc_list 3]
+            set proc_size [lindex $proc_list 4]
+            set resident_memory [lindex $proc_list 5]
+            set state           [lindex $proc_list 7]
+            set cpu_total_time  [lindex $proc_list 10]
+            set cpu_pct   [lindex $proc_list 8]
+            set command   [lindex $proc_list 11]
 
-	    continue   
+            set proc_id [db_nextval ad_monitoring_top_proc_proc_id]
 
-	} elseif { [regexp -nocase {(.*PID.USERNAME.*)} $line match top_header] } {
-	    ## this is the start of proc info lines
+            db_dml top_proc_info_insert "*SQL*"
+
+            continue   
+
+        } elseif { [regexp -nocase {(.*PID.USER.*)} $line match top_header] } {
+            ns_log notice "vk1"
+            ## this is the start of proc info lines
             incr procflag
 
-	    continue
+            continue
 
         } elseif { [regexp -nocase {load av[a-z]*: (.*)} $line match load] } {
-	    ## this is the load header 
-	    incr ctr
+            ## this is the load header 
+            incr ctr
 
-	    # remove commas, multiple spaces
-    	    regsub -all {,} [string trim $load] "" load   
-    	    regsub -all {[ ]+} $load " " load             
+            # remove commas, multiple spaces
+            regsub -all {,} [string trim $load] "" load   
+            regsub -all {[ ]+} $load " " load             
 
-    	    set load_list [split $load " "]
-    
-    	    # We keep all three load avgs, ignore the time at the end of the line
-    	    set load_1  [lindex load_list 0]
-	    set load_5  [lindex load_list 1]
-	    set load_15 [lindex load_list 2]
+            set load_list [split $load " "]
 
-	    # send out any high-load alerts
-   	    if { $load_5 > [ad_parameter -package_id [monitoring_pkg_id] LoadAverageAlertThreshold monitoring 2.0] } {
-              wd_email_notify_list "High Load Average on [ad_url]" \
-                  "The load average over the last 5 minutes for [ad_url] was $load_5"
-    	     } 
+            # We keep all three load avgs, ignore the time at the end of the line
+            set load_1  [lindex $load_list 0]
+            set load_5  [lindex $load_list 1]
+            set load_15 [lindex $load_list 2]
 
-	} elseif { [regexp -nocase {mem[a-z]*: (.*)} $line match memory] } {
-	    ## this is the memory header 
-	    incr ctr
+            # send out any high-load alerts
+            if { $load_5 > [ad_parameter -package_id [monitoring_pkg_id] LoadAverageAlertThreshold monitoring 2.0] } {
+                wd_email_notify_list "High Load Average on [ad_url]" \
+                        "The load average over the last 5 minutes for [ad_url] was $load_5"
+            } 
+
+        } elseif { [regexp -nocase {mem[a-z]*: (.*)} $line match memory] } {
+            ## this is the memory header 
             foreach mem [split $memory ","] {
                 regexp {^ *([^ ]*) (.*)} $mem match amount type
                 set amount [string trim [string toupper $amount]]
-               	# convert all mem values to Kilobytes 
+                # convert all mem values to Kilobytes 
                 regsub {K$} $amount "" amount
                 regsub {M$} $amount "000" amount
 
@@ -178,29 +179,52 @@
                     "free" { set memory_free $amount }
                     "swap free" { set memory_swap_free $amount }
                     "swap in use" { set memory_swap_in_use $amount }
+                    "total" { set memory_real $amount }                 
                 }
             } 
-	}
 
-	if {$ctr == 2 } {
- 	    ## we have all the header information we currently store.
-	    ## we should also store the rest of top's output... 
-	    ## some of it can be gotten from the zoom package. 
-	    db_dml top_misc_info_insert "insert into ad_monitoring_top
-             (top_id, timestamp, timehour, load_avg_1, load_avg_5, load_avg_15,
-              memory_real, memory_free, memory_swap_in_use, memory_swap_free)
-            values
-             (:top_id, sysdate, to_char(sysdate, 'HH24'), :load_1, :load_5, :load_15,
-              :memory_real, :memory_free, :memory_swap_in_use, :memory_swap_free)"
-	    incr ctr
-	}
-    # end of the foreach loop
+            # my version of top has Mem and Swap on different lines
+            # don't increment ctr until we get both
+            if {[info exists memory_real] && [info exists memory_swap_free]} {
+                incr ctr
+            }
+        } elseif { [regexp -nocase {swa[a-z]*: (.*)} $line match memory] } {
+            ## this is the swap header 
+            foreach mem [split $memory ","] {
+                regexp {^ *([^ ]*) (.*)} $mem match amount type
+                set amount [string trim [string toupper $amount]]
+                # convert all mem values to Kilobytes 
+                regsub {K$} $amount "" amount
+                regsub {M$} $amount "000" amount
+
+                set type [string trim [string tolower $type]]
+                switch $type {
+                    "free" { set memory_swap_free $amount }
+                    "used" { set memory_swap_in_use $amount }
+                }
+            } 
+
+            # my version of top has Mem and Swap on different lines
+            # don't increment ctr until we get both
+            if {[info exists memory_real] && [info exists memory_swap_free]} {
+                incr ctr
+            }
+        }
+        
+        if {$ctr == 2 } {
+            ## we have all the header information we currently store.
+            ## we should also store the rest of top's output... 
+            ## some of it can be gotten from the zoom package. 
+            db_dml top_misc_info_insert "*SQL"
+            incr ctr
+        }
+        # end of the foreach loop
     }
 
     if { $ctr < 2 } {
-	# didn't even get load and memory lines from top
-	ns_log Error "ad_monitor_top: Cannot parse output from top"
-	return
+        # didn't even get load and memory lines from top
+        ns_log Error "ad_monitor_top: Cannot parse output from top"
+        return
     }
 }
 
@@ -224,15 +248,15 @@
 
     # Are we even doing this?
     if {[ad_parameter -package_id [monitoring_pkg_id] AutoAnalyzeP monitoring 0]==0} {
-	ns_log notice "ad_monitoring_analyze_tables: Not Analyzing Tables"
-	return
+        ns_log notice "ad_monitoring_analyze_tables: Not Analyzing Tables"
+        return
     }
     
     #how many days should a complete scan take
     set numdays [ad_parameter -package_id [monitoring_pkg_id] NumDaysToEstAllRows monitoring 7]
     if {$numdays < 1} {
-	ns_log error "ad_monitoring_analyze_tables: Parameter NumDaysToEstAllRows cannot be less than 1."
-	return
+        ns_log error "ad_monitoring_analyze_tables: Parameter NumDaysToEstAllRows cannot be less than 1."
+        return
     }
     
     # grab a handle
@@ -262,27 +286,27 @@
     
     # Now we go through each item
     foreach table_item  $alltables {
-	# Variables to be used JAva
+        # Variables to be used JAva
 
-	set entry_id [lindex $table_item 2]
-	set table_name [lindex $table_item 0]
-	set percent [lindex $table_item 1]
-
-	#The string to execute.  
-	set execstr "analyze table :table_name estimate statistics sample :percent percent"
-	
+        set entry_id [lindex $table_item 2]
+        set table_name [lindex $table_item 0]
+        set percent [lindex $table_item 1]
+        
+        #The string to execute.  Don't use bind vars since you can't bind on table name.
+        set execstr "analyze table $table_name estimate statistics sample $percent percent"
+    
         # for some reason this failed.. probably cause the table doesn't exists
         # anymore
-	ns_log notice "Analyzing $table_name.."
-	if {[catch {db_dml table_analyze $execstr} errmsg]} {
-	    # Look up the table in user_tables
-	    if { ![db_table_exists $table_name] } {
-	        # The table wasn't listed.. so we disable this entry
-		db_dml one_table_entry_disable_monitoring "update ad_monitoring set enabled_p='f' 
-		    where table_entry_id=:entry_id" 
+        ns_log notice "Analyzing $table_name.."
+        if {[catch {db_dml table_analyze $execstr} errmsg]} {
+            # Look up the table in user_tables
+            if { ![db_table_exists $table_name] } {
+                # The table wasn't listed.. so we disable this entry
+                db_dml one_table_entry_disable_monitoring "update ad_monitoring set enabled_p='f' 
+                where table_entry_id=:entry_id" 
             } else {
-		ns_log error "ad_monitoring_analyze_tables: Error while executing: $execstr"
-	    }
+                ns_log error "ad_monitoring_analyze_tables: Error while executing: $execstr"
+            }
         } else {
             #Change the last estimated, and continue looping
             db_dml one_table_entry_update_last_estimated "
@@ -294,5 +318,3 @@
     } 
     db_release_unused_handles
 } 
-
-