Re: scwish complains: couldn't connect to display.. xhost + done

sjepson@sjepson.amteva.com
Tue, 14 May 96 11:01 EDT

Hello again,

Here is a simple example to demonstrate the problem I am having with the
getnext command. Again for some reason when the manager issues a getnext
command to the agent the manager sends a second request to the agent which then
is typically filled with junk or the next mib's value defined under the
parent object. So the first request is successfull and the second request by
the manager is bogus. I guess the second request is issued by the manager because
it still thinks there are other instances left to get, due to the agent's response.
It's quite possible that I am the cause of the error since this stuff is new to
me. However myself and two others here at work have given it a look over and can't seem to
find the problem. Thanks to Juergen for being so responsive to everyone's questions.
I know it must eat up a lot of his time.

Sorry this is not a UU file. I could possiblly encode it if people are having problems
with mail translations.

I am using scotty 2.02, tcl 7.5a, tk 4.1a

tkined 1.3.4 and HP OpenView NNM 3.3 were used as the managers

############################################ CUT HERE ###############################################

#!/bin/sh
##
## SNMP Agent. This file only does the startup. All MIB
## implementations are done in extra files that are sourced from
## this file.
##
## Copyright (c) 1994, 1995
##
## J. Schoenwaelder
## TU Braunschweig, Germany
## Institute for Operating Systems and Computer Networks
##
##

# Tcl sees the next lines as an assignment to variable `kludge'.
# For sh, the two shifts cancel the effect of the set, and then we
# run scotty on this script.

set kludge { $*
shift
shift
if test -f ../scotty -a -f ../scotty ; then
exec ../scotty -nf $0 $*
else
exec scotty -nf $0 $*
fi
}

##
## Report all errors to standard error including the complete backtrace.
##

proc tkerror { msg } {
global errorInfo
syslog error $msg
puts stderr $errorInfo
}

##
## Expand the auto_path to auto load script files when needed.
##

set auto_path [concat $scotty_library/agents $auto_path]

if {[file exists tclIndex]} {
set auto_path [concat . $auto_path]
}

##
## Load useful MIB definitions.
##

mib load test.mib
mib load obj1.mib
mib load obj2.mib

##
## Create a usec agent based on the current user guessed from the
## environment.
##

proc CreateUSECAgent { port {interp {}} } {
global env
set ip [netdb host address [exec /usr/ucb/hostname]]
if {[info exists env(USER)]} {
set user $env(USER)
} elseif {[info exists env(LOGNAME)]} {
set user $env(LOGNAME)
} else {
set user "uhura"
}
set session [snmp session -port $port -address $ip -user $user \
-community "public" -agent $interp -retries 0]
return $session
}

##
## Create a Tcl interpreter that can be used to evaluate arbitrary
## scripts send from potentially unsecure managers. You can run the
## agent with an unsafe Tcl interpreter by uncommenting the return
## command below. But it will be your own risk to do so. You have
## been warned.
##

proc CreateSafeInterp {} {

# following return uncommented by deepak
# return ""

if {[catch {interp create -safe } interp]} {
puts stderr "Running the agent requires the safe Tcl extension (stcl)."
exit 42
}

# List of commands allowed in the safe Tcl interpreter. Note, the
# http command allows to write to local files. This is no problem
# if you run the agent on a restricted account.

# Safe Tcl/Tk commands.

$interp alias tkerror tkerror
$interp alias puts puts stderr

# Safe Scotty commands.

$interp alias getdate getdate
$interp alias getclock getclock
$interp alias icmp icmp
$interp alias http http
$interp alias syslog syslog
$interp alias mib mib

return $interp
}

proc AGENT_GetObject { varList } {

## This function looks at the incoming mib
## and based on the oid it returns the object
## which the mib was invoked under. This
## is needed because the mibs under each object
## share the same names (inheritance).
## The magic number seven is the oid level which
## the objects are defined. Simplified for
## test example.

set oid [lindex $varList 0]

set oidList [split $oid . ]

set objectOid [lindex $oidList 7]

switch -exact $objectOid {
1 {return "obj1"}
2 {return "obj2"}
default {return "NULL"}
}

}

proc AGENT_GetAllObjects {} {
## Yeah, Yeah. I know this is bad but this is
## just a simple example. :-)

return "obj1 obj2"
}

proc AGENT_GetAllMibs {} {
## Yeah, Yeah. I know this is bad but this is
## just a simple example. :-)

return "item1 item2"
}

proc AGENT_GetMibValue { item varList } {

## In our porject we are connecting to an object
## and requesting the information with which we
## should set the mib value to. However, for this example
## the following will return us different values
## based on the request.

set object [AGENT_GetObject $varList]

switch -exact $object {
obj1 { switch -exact $item {
item1 { return "Cow" }
item2 { return "Horse" }
default {return "Unknown item"}
}
}
obj2 { switch -exact $item {
item1 { return "Dog" }
item2 { return "Cat" }
default {return "Unknown item"}
}
}
default {return "Unknown Obj"}
}
}

proc AGENT_TestInit {session} {

set fullPath "iso.org.dod.internet.private.enterprises.amteva"
set interp [$session cget -agent]
$interp alias AGENT_GetMibValue AGENT_GetMibValue

foreach CurMib [AGENT_GetAllMibs] {
foreach CurObject [AGENT_GetAllObjects] {

## full path names are needed since mib names are not unique
## unless fully qualified.
$session instance $fullPath.$CurObject.$CurMib.0 $CurMib
$session bind $fullPath.$CurObject.$CurMib.0 \
get "set $CurMib \[ AGENT_GetMibValue $CurMib %V \]"
}
}
}

##
## Startup the agent on port 161.
##

set agent "CreateUSECAgent 161"

set interp [CreateSafeInterp]

if [catch {eval $agent $interp} session] {
puts stderr "Failed to setup SNMP agent: $session"
exit 42
}

AGENT_TestInit $session


# snmp watch on

$session bind 1.3 get { puts "get ->Varbind: %V\n" }
$session bind 1.3 set { puts "set ->Varbind: %V\n" }
$session bind 1.3 create { puts "create ->Varbind: %V\n" }
$session bind 1.3 check { puts "check ->Varbind: %V\n" }
$session bind 1.3 commit { puts "commit ->Varbind: %V\n" }
$session bind 1.3 rollback { puts "rollback ->Varbind: %V\n" }

$session bind "" begin { puts "begin ->SNMP Packet Type: %T\nError Status: %E\nError Index: %I\nVarbind: %V\n" }
$session bind "" end { puts "end ->SNMP Packet Type: %T\nError Status: %E\nError Index: %I\nVarbind: %V\n" }

puts stdout "\nagent started using the following parameters:\n[$session configure]"

#####
################################### TEST MIBS #######################################################################
#####

-- File: test.mib

AMTEVA-MIB DEFINITIONS ::= BEGIN

IMPORTS
MODULE-IDENTITY, OBJECT-TYPE,
enterprises2
FROM SNMPv2-SMI;

amteva OBJECT IDENTIFIER
::= { enterprises 1727 }

-- the amteva group

END

######################################## CUT HERE ##########################################

-- File: obj1.mib

AMTEVA-OBJ1-MIB DEFINITIONS ::= BEGIN

IMPORTS
MODULE-IDENTITY, OBJECT-TYPE,
DisplayString
FROM SNMPv2-TC
amteva
FROM AMTEVA-MIB;

obj1 OBJECT IDENTIFIER
::= { amteva 1 }

-- the app group

item1 OBJECT-TYPE
SYNTAX DisplayString
ACCESS read-write
STATUS mandatory
DESCRIPTION
"Item 1"
::= { obj1 1 }

item2 OBJECT-TYPE
SYNTAX DisplayString
ACCESS read-write
STATUS mandatory
DESCRIPTION
"Item 2"
::= { obj1 2 }

END

######################################### CUT HERE ##########################################

-- File: obj2.mib

AMTEVA-OBJ2-MIB DEFINITIONS ::= BEGIN

IMPORTS
MODULE-IDENTITY, OBJECT-TYPE,
DisplayString
FROM SNMPv2-TC
amteva
FROM AMTEVA-MIB;

obj2 OBJECT IDENTIFIER
::= { amteva 2 }

-- the app group

item1 OBJECT-TYPE
SYNTAX DisplayString
ACCESS read-write
STATUS mandatory
DESCRIPTION
"Item 1"
::= { obj2 1 }

item2 OBJECT-TYPE
SYNTAX DisplayString
ACCESS read-write
STATUS mandatory
DESCRIPTION
"Item 2"
::= { obj2 2 }

END