#!/bin/sh
# the next line restarts using scotty -*- tcl -*- \
exec scotty2.1.11 "$0" "$@"

package require Tnm 2.1

##
## IfLoadEventProc is called whenever an ifLoad event is received.
##

proc IfLoadEventProc {session ifIndex ifDescr ifOperStatus ifLoad} {
    global out
    puts $out [format "%s %3d %6.2f %% %6s (%s)" \
	    [clock seconds] $ifIndex $ifLoad $ifOperStatus $ifDescr]
    flush $out
}

##
## The following proc takes samples of one day and writes them to
## a file which contains the starting date of the sample.
##

proc MonitorIfLoadDaily { s interval } {

    global tnm out

    set clock [clock seconds]
    set file "[$s cget -address]-[clock format $clock -format "%b-%d"]"
    set out [open $file w+]
    set secs [expr ((($clock / 86400) + 1) * 86400) - $clock]
    set iterations [expr ($secs / $interval) + 1]
    
    puts $out "# Interface load (based on scotty $tnm(version))"
    puts $out "#"
    puts $out "# Agent:		[$s cget -address]"
    puts $out "# Start:		[clock format [clock seconds]]"
    puts $out "# Interval:		$interval"
    puts $out "# Iterations:		$iterations"
    puts $out "#"
    puts $out "# Description of the columns in this file:"
    puts $out "#"
    puts $out "# 1:	Seconds since 1970."
    puts $out "# 2:	Interface index."
    puts $out "# 3:	Interface load."
    puts $out "# 4:	%"
    puts $out "# 5:	Interface status (either up or down)."
    puts $out "# 6:	Interface description."
    puts $out ""

    Tnm_MonitorIfLoad $s $interval $iterations
    event bind ifLoad IfLoadEventProc
    job wait
    close $out
}

##
## Some examples to set up monitoring jobs. Make sure to use IP
## addresses as this will make more readable output.
##

mib load rfc1213.mib

if {$argc < 2 || $argc > 3} {
    puts stderr {usage: ifload <ip> <seconds> [<community>]}
    exit 1
}

set host [lindex $argv 0]
set interval [lindex $argv 1]
set community [expr {$argc == 3 ? [lindex $argv 2] : "public"}]

set code [catch {snmp session -address $host -community $community} s]
if $code {
    puts stderr $s
    exit 1
}

while 1 {
    set code [catch {MonitorIfLoadDaily $s $interval} msg]
    if $code {	
	puts stderr $msg
	exit 1
    }
}
