| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
. ../support/simple_eval_tools.sh |
| 4 |
|
| 5 |
HEADER 'AgentX subagent in Perl (using NetSNMP::agent)' |
| 6 |
|
| 7 |
SKIPIF NETSNMP_DISABLE_SNMPV2C |
| 8 |
SKIPIFNOT USING_AGENTX_MASTER_MODULE |
| 9 |
SKIPIFNOT USING_AGENTX_SUBAGENT_MODULE |
| 10 |
SKIPIFNOT HAVE_SIGHUP |
| 11 |
# make sure the toolkit has been configured --with-perl-modules |
| 12 |
[ -d ${SNMP_UPDIR}/perl/blib/arch/auto/NetSNMP/agent ] || SKIP Not --with-perl-modules |
| 13 |
|
| 14 |
# |
| 15 |
# Begin test |
| 16 |
# |
| 17 |
|
| 18 |
snmp_version=v2c |
| 19 |
TESTCOMMUNITY=testcommunity |
| 20 |
. ./Sv2cconfig |
| 21 |
|
| 22 |
TESTOID=.1.3.6.1.4.1.8872.9999.9999.1.0 |
| 23 |
TESTOID2=.1.3.6.1.4.1.8872.9999.9999.2.0 |
| 24 |
AGENTX_SOCKET=${SNMP_TMPDIR}/agentx_socket |
| 25 |
PERL_SUBAGENT=${SNMP_TMPDIR}/hello.pl |
| 26 |
PERL_SUBAGENT_CONF=${SNMP_TMPDIR}/hello.conf |
| 27 |
|
| 28 |
cat >$PERL_SUBAGENT <<END |
| 29 |
#!${SNMP_PERLPROG} |
| 30 |
# minimal AgentX subagent in Perl |
| 31 |
|
| 32 |
use blib qw(${SNMP_UPDIR}/perl); |
| 33 |
use NetSNMP::OID qw(:all); |
| 34 |
use NetSNMP::agent qw(:all); |
| 35 |
use NetSNMP::ASN qw(:all); |
| 36 |
use NetSNMP::agent::default_store; |
| 37 |
use NetSNMP::default_store qw(:all); |
| 38 |
use SNMP; |
| 39 |
|
| 40 |
## double-fork daemon |
| 41 |
my \$pid; |
| 42 |
unless (\$pid = fork) { |
| 43 |
unless (fork) { |
| 44 |
## start main code |
| 45 |
my \$regat = '.1.3.6.1.4.1.8872.9999.9999'; # netSnmpPlayPen |
| 46 |
my \$myoid = \$regat . ".1.0"; # the "hello world" OID |
| 47 |
my \$myoid2 = \$regat . ".2.0"; # the "hello again world" OID |
| 48 |
|
| 49 |
my \$pidfile = shift; # take PID file as first argument |
| 50 |
open(PIDFILE, ">\$pidfile") or die "Couldn't open \$pidfile"; |
| 51 |
print PIDFILE \$\$; |
| 52 |
close(PIDFILE); |
| 53 |
|
| 54 |
sub my_snmp_handler { |
| 55 |
my (\$handler, \$registration_info, \$request_info, \$requests) = @_; |
| 56 |
for(my \$request = \$requests; \$request; \$request = \$request->next()) { |
| 57 |
if (\$request_info->getMode() == MODE_GET) { |
| 58 |
if (\$request->getOID() == new NetSNMP::OID(\$myoid)) { |
| 59 |
\$request->setValue(ASN_OCTET_STR, "hello world"); |
| 60 |
last; |
| 61 |
} elsif (\$request->getOID() == new NetSNMP::OID(\$myoid2)) { |
| 62 |
\$request->setValue(ASN_OCTET_STR, "hello again world"); |
| 63 |
last; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
sub shut_it_down { \$running = 0; } |
| 69 |
|
| 70 |
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, |
| 71 |
NETSNMP_DS_AGENT_NO_ROOT_ACCESS, 1); |
| 72 |
my \$agent = new NetSNMP::agent('Name' => 'hello', 'AgentX' => 1); |
| 73 |
my \$regoid = new NetSNMP::OID(\$regat); |
| 74 |
\$agent->register("hello", \$regoid, \&my_snmp_handler); |
| 75 |
|
| 76 |
\$SIG{'INT'} = \&shut_it_down; |
| 77 |
\$SIG{'QUIT'} = \&shut_it_down; |
| 78 |
\$running = 1; |
| 79 |
while(\$running) { |
| 80 |
\$agent->agent_check_and_process(1); |
| 81 |
} |
| 82 |
\$agent->shutdown(); |
| 83 |
## end main code |
| 84 |
exit 0; |
| 85 |
} |
| 86 |
exit 0; |
| 87 |
} |
| 88 |
waitpid(\$pid, 0); |
| 89 |
END |
| 90 |
|
| 91 |
echo "agentxsocket $AGENTX_SOCKET" > $PERL_SUBAGENT_CONF |
| 92 |
|
| 93 |
CONFIGAGENT master agentx |
| 94 |
CONFIGAGENT agentxsocket $AGENTX_SOCKET |
| 95 |
|
| 96 |
STARTAGENT |
| 97 |
|
| 98 |
# start perl subagent |
| 99 |
/usr/bin/env SNMPCONFPATH="${SNMP_TMPDIR}${SNMP_ENV_SEPARATOR}${SNMP_TMP_PERSISTENTDIR}" $SNMP_PERLPROG $PERL_SUBAGENT $SNMP_AGENTX_PID_FILE >$SNMP_AGENTX_LOG_FILE 2>&1 |
| 100 |
DELAY |
| 101 |
|
| 102 |
## 1) SNMP GET against mib object implemented by the perl subagent |
| 103 |
CAPTURE "snmpget -$snmp_version -c $TESTCOMMUNITY $SNMP_TRANSPORT_SPEC:$SNMP_TEST_DEST$SNMP_SNMPD_PORT $TESTOID" |
| 104 |
CHECK "hello world" |
| 105 |
|
| 106 |
## 2) persistency: reconfigure master agent (HUP), re-test |
| 107 |
HUPAGENT |
| 108 |
CAPTURE "snmpget -$snmp_version -c $TESTCOMMUNITY $SNMP_TRANSPORT_SPEC:$SNMP_TEST_DEST$SNMP_SNMPD_PORT $TESTOID2" |
| 109 |
CHECK "hello again world" |
| 110 |
|
| 111 |
## stop |
| 112 |
STOPAGENT |
| 113 |
STOPPROG $SNMP_AGENTX_PID_FILE |
| 114 |
|
| 115 |
FINISHED |