###########################################################################
#
# snmpd.conf
#
# - created by the snmpconf configuration program
#
###########################################################################
# SECTION: System Information Setup
#
# This section defines some of the information reported in
# the "system" mib group in the mibII tree.
# syslocation: The [typically physical] location of the system.
# Note that setting this value here means that when trying to
# perform an snmp SET operation to the sysLocation.0 variable will make
# the agent return the "notWritable" error code. IE, including
# this token in the snmpd.conf file will disable write access to
# the variable.
# arguments: location_string
syslocation xi'an
# syscontact: The contact information for the administrator
# Note that setting this value here means that when trying to
# perform an snmp SET operation to the sysContact.0 variable will make
# the agent return the "notWritable" error code. IE, including
# this token in the snmpd.conf file will disable write access to
# the variable.
# arguments: contact_string
syscontact [email]
[email protected][/email]
# sysservices: The proper value for the sysServices object.
# arguments: sysservices_number
sysservices 77
sysservices 0*1 + 0*2 + 0*4 + 0*8 + 0*64
sysservices 0*1 + 0*2 + 1*4 + 1*8 + 0*64
###########################################################################
# SECTION: Access Control Setup
#
# This section defines who is allowed to talk to your running
# snmp agent.
# rwuser: a SNMPv3 read-write user
# arguments: user [noauth|auth|priv] [restriction_oid]
rwuser rw3 noauth
# rwcommunity: a SNMPv1/SNMPv2c read-write access community name
# arguments: community [default|hostname|network/bits] [oid]
rwcommunity rw12
###########################################################################
# SECTION: Trap Destinations
#
# Here we define who the agent will send traps to.
# trap2sink: A SNMPv2c trap receiver
# arguments: host [community] [portnum]
trap2sink 202.117.129.75
UCD-DEMO-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, Integer32 FROM SNMPv2-SMI
ucdavis FROM UCD-SNMP-MIB;
ucdDemoMIB MODULE-IDENTITY
LAST-UPDATED "9912090000Z"
ORGANIZATION "University of California, Davis"
CONTACT-INFO
"The ucd-snmp-coders mailing list is the best place to
write for public questions about the ucd-snmp
project: [email]
[email protected][/email]
Primary Author: Wes Hardaker
postal: IT-DCAS
UCDavis
Davis CA 95616
phone: +1 530 754-7571
email: [email]
[email protected][/email]"
DESCRIPTION
"The UCD-SNMP Demonstration MIB."
REVISION "9912090000Z"
DESCRIPTION
"SMIv2 version converted from older MIB definitions."
::= { ucdavis 14 }
//该节点位于ucdavis下的第14个节点
ucdDemoMIBObjects OBJECT IDENTIFIER ::= { ucdDemoMIB 1 }
//在UCD-SNMP-MIB中定义,
// -- ucdDemoMIB OBJECT IDENTIFIER ::= { ucdavis 14 } - UCD-DEMO-MIB
//OID: ucdavis.14.1
ucdDemoPublic OBJECT IDENTIFIER ::= { ucdDemoMIBObjects 1 }
//OID: ucdavis.14.1.1
ucdDemoResetKeys OBJECT-TYPE
SYNTAX Integer32 (0..2147483647)
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"A set of value 1 to this object resets the
demonstration user's auth and priv keys to the
keys based on the P->Ku->Kul transformation of the
value of the ucdDemoPasspharse object.
Values other than 1 are ignored."
::= { ucdDemoPublic 1 }
//OID: ucdavis.14.1.1
ucdDemoPublicString OBJECT-TYPE
SYNTAX OCTET STRING (SIZE(0..1024))
MAX-ACCESS read-write
STATUS current
DESCRIPTION
"A publicly settable string that can be set for testing
snmpsets. This value has no real usage other than
testing purposes."
::= { ucdDemoPublic 2 }
ucdDemoUserList OBJECT-TYPE
SYNTAX OCTET STRING
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"The list of users affected by the ucdDemoResetKeys object."
::= { ucdDemoPublic 3 }
ucdDemoPassphrase OBJECT-TYPE
SYNTAX OCTET STRING
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"The demo passphrase that ucdDemoResetKeys changes each
users localized key to based on the P->Ku->Kul transformation."
::= { ucdDemoPublic 4 }
//定义了2个trap类型的节点
TheTrap1 NOTIFICATION-TYPE
STATUS current
DESCRIPTION
"This is 1st trap."
::= { ucdDemoPublic 5 }
//OID: ucdavis.14.1.5
TheTrap2 NOTIFICATION-TYPE
SYNTAX Integer32
MAX-ACCESS read-write
STATUS mandatory
DESCRIPTION
"This is 2nd trap."
::= { ucdDemoPublic 6 }
//OID: ucdavis.14.1.6
END
/* ucdDemoPublic.c */
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <config.h>
#include "mibincl.h"
#include "snmpv3.h"
#include "snmpusm.h"
#include "agent_read_config.h"
#include "ucdDemoPublic.h"
#include "util_funcs.h"
#define MYMAX 1024
#define MAXUSERS 10
int num=0;
static char demoUsers[MAXUSERS][MYMAX+1];
static char demopass[MYMAX+1];
char mynode_str[300];
void ucdDemo_parse_user(const char *word, char *line) {
if (num == MAXUSERS)
return;
if (strlen(line) > MYMAX)
return;
strcpy(demoUsers[num++], line);
}
void ucdDemo_parse_userpass(const char *word, char *line) {
if (strlen(line) > MYMAX)
return;
strcpy(demopass, line);
}
/* this variable defines function callbacks and type return information
for the ucdDemoPublic mib */
//与MIB中的定义相对应
struct variable2 ucdDemoPublic_variables[] = {
{ UCDDEMORESETKEYS , ASN_INTEGER , RWRITE, var_ucdDemoPublic, 1, { 1 } },
{ UCDDEMOPUBLICSTRING , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 2 } },
{ UCDDEMOUSERLIST , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 3 } },
{ UCDDEMOPASSPHRASE , ASN_OCTET_STR , RWRITE, var_ucdDemoPublic, 1, { 4 } },
{ 5, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {5}},
{ 6, ASN_INTEGER, RWRITE, var_ucdDemoPublic, 1, {6}}
};
/* Define the OID pointer to the top of the mib tree that we're
registering underneath */
oid ucdDemoPublic_variables_oid[] = { 1,3,6,1,4,1,2021,14,1,1 };
void init_ucdDemoPublic(void) {
REGISTER_MIB( "examples/ucdDemoPublic", ucdDemoPublic_variables,
variable2, ucdDemoPublic_variables_oid);
snmpd_register_config_handler("demoUser", ucdDemo_parse_user, NULL, "USER");
snmpd_register_config_handler("demoPass", ucdDemo_parse_userpass, NULL, "PASSPHASE");
//定义定时的trap发送
snmp_alarm_register(10, SA_REPEAT, snmploop_callback, NULL);
}
//定义自定义的定时trap动作
void snmploop_callback(unsigned int reg, void *clientarg){
send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 1);
}
unsigned char publicString[MYMAX+1];
long long_ret;
unsigned char *
var_ucdDemoPublic(
struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **write_method)
{
// static long long_ret;
static char string[MYMAX+1], *cp;
int i;
*write_method = 0; /* assume it isnt writable for the time being */
*var_len = sizeof(long_ret); /* assume an integer and change later if not */
if (header_generic(vp,name,length,exact,var_len,write_method))
return 0;
/* this is where we do the value assignments for the mib results. */
switch(vp->magic) {
case UCDDEMORESETKEYS:
*write_method = write_ucdDemoResetKeys;
//long_ret = 0;
return (unsigned char *) &long_ret;
case UCDDEMOPUBLICSTRING:
*write_method = write_ucdDemoPublicString;
*var_len = strlen(publicString);
return (unsigned char *) publicString;
case UCDDEMOUSERLIST:
cp = string;
for(i=0; i < num; i++) {
sprintf(cp, " %s", demoUsers[i]);
cp = cp + strlen(cp);
}
*var_len = strlen(string);
return (unsigned char *) string;
case UCDDEMOPASSPHRASE:
*var_len = strlen(demopass);
return (unsigned char *) demopass;
case 5:
long_ret=0;
*write_method = write_mynodetrap;
return (u_char *)&long_ret;
case 6:
/*
* This object is essentially "write-only".
* It only exists to trigger the sending of a v2 trap.
* Reading it will always return 0.
*/
long_ret = 0; /*FIX*/
*write_method = write_mynodetrap2;
return (u_char *)&long_ret;
default:
DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_ucdDemoPublic/n", vp->magic));
}
return 0;
}
int
write_ucdDemoResetKeys(
int action,
u_char *var_val,
u_char var_val_type,
size_t var_val_len,
u_char *statP,
oid *name,
size_t name_len)
{
/* variables we may use later */
//static long long_ret;
static unsigned char string[1500];
static oid objid[MAX_OID_LEN];
static struct counter64 c64;
int bigsize=1000;
unsigned char *engineID;
int engineIDLen;
int i;
struct usmUser *user;
if (var_val_type != ASN_INTEGER) {
printf("ASN_INTEGER/n");
DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys not ASN_INTEGER/n"));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long_ret)) {
DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoResetKeys: bad length/n"));
return SNMP_ERR_WRONGLENGTH;
}
if (action == COMMIT) {
int fd = open("/dev/lamp", O_RDWR, 0644);
if(fd<0){
printf("open /dev/lamp error/n");
return SNMP_ERR_NOERROR;
}
printf("new value: %x/n", *(long*)var_val);
if( *(long *)var_val == 0) write(fd,"00/n", 3);
if( *(long *)var_val == 1) write(fd,"01/n", 3);
if( *(long *)var_val == 2) write(fd,"10/n", 3);
if( *(long *)var_val == 3) write(fd,"11/n", 3);
close(fd);
long_ret = *((long *) var_val);
/*if (long_ret == 1) {
engineID = snmpv3_generate_engineID(&engineIDLen);
for(i=0; i < num; i++) {
user = usm_get_user(engineID, engineIDLen, demoUsers[i]);
if (user) {
usm_set_user_password(user, "userSetAuthPass", demopass);
usm_set_user_password(user, "userSetPrivPass", demopass);
}
}
reset the keys
}*/
}
return SNMP_ERR_NOERROR;
}
int
write_ucdDemoPublicString(
int action,
u_char *var_val,
u_char var_val_type,
size_t var_val_len,
u_char *statP,
oid *name,
size_t name_len)
{
if (var_val_type != ASN_OCTET_STR) {
DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString not ASN_OCTET_STR/n"));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > MYMAX) {
DEBUGMSGTL(("ucdDemoPublic","write to ucdDemoPublicString: bad length/n"));
return SNMP_ERR_WRONGLENGTH;
}
if (action == COMMIT) {
strcpy(publicString, var_val);
}
return SNMP_ERR_NOERROR;
}
int
write_mynodetrap(int action,
u_char * var_val,
u_char var_val_type,
size_t var_val_len,
u_char * statP, oid * name, size_t name_len)
{
long intval;
DEBUGMSGTL(("mynode", "write_mynodetrap entered: action=%d/n",
action));
switch (action) {
case RESERVE1:
/*
* The only acceptable value is the integer 1
*/
if (var_val_type != ASN_INTEGER) {
DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long)) {
DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
return SNMP_ERR_WRONGLENGTH;
}
intval = *((long *) var_val);
if (intval != 1) {
DEBUGMSGTL(("mynode", "wrong value %x", intval));
return SNMP_ERR_WRONGVALUE;
}
break;
case RESERVE2:
/*
* No resources are required....
*/
break;
case FREE:
/*
* ... so no resources need be freed
*/
break;
case ACTION:
/*
* Having triggered the sending of a trap,
* it would be impossible to revoke this,
* so we can't actually invoke the action here.
*/
break;
case UNDO:
/*
* We haven't done anything yet,
* so there's nothing to undo
*/
break;
case COMMIT:
/*
* Everything else worked, so it's now safe
* to trigger the trap.
* Note that this is *only* acceptable since
* the trap sending routines are "failsafe".
* (In fact, they can fail, but they return no
* indication of this, which is the next best thing!)
*/
DEBUGMSGTL(("mynode", "write_mynodetrap sending the trap/n",
action));
send_easy_trap(SNMP_TRAP_ENTERPRISESPECIFIC, 99);
DEBUGMSGTL(("mynode", "write_mynodetrap trap sent/n", action));
break;
}
return SNMP_ERR_NOERROR;
}
/*
* this documents how to send a SNMPv2 (and higher) trap via the
* send_v2trap() API.
*
* Coding SNMP-v2 Trap:
*
* The SNMPv2-Trap PDU contains at least a pair of object names and
* values: - sysUpTime.0 whose value is the time in hundredths of a
* second since the netwok management portion of system was last
* reinitialized. - snmpTrapOID.0 which is part of the trap group SNMPv2
* MIB whose value is the object-id of the specific trap you have defined
* in your own MIB. Other variables can be added to caracterize the
* trap.
*
* The function send_v2trap adds automaticallys the two objects but the
* value of snmpTrapOID.0 is 0.0 by default. If you want to add your trap
* name, you have to reconstruct this object and to add your own
* variable.
*
*/
int
write_mynodetrap2(int action,
u_char * var_val,
u_char var_val_type,
size_t var_val_len,
u_char * statP, oid * name, size_t name_len)
{
long intval;
/*
* these variales will be used when we send the trap
*/
oid objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };/* snmpTrapOID.0 */
oid demo_trap[] = { 1, 3, 6, 1, 4, 1, 2021, 13, 990 }; /*demo-trap */
oid mynode_string_oid[] =
{ 1, 3, 6, 1, 4, 1, 2021, 254, 1, 0 };
static struct variable_list var_trap;
static struct variable_list var_obj;
DEBUGMSGTL(("mynode", "write_mynodetrap2 entered: action=%d/n",
action));
switch (action) {
case RESERVE1:
/*
* The only acceptable value is the integer 1
*/
if (var_val_type != ASN_INTEGER) {
DEBUGMSGTL(("mynode", "%x not integer type", var_val_type));
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(long)) {
DEBUGMSGTL(("mynode", "wrong length %x", var_val_len));
return SNMP_ERR_WRONGLENGTH;
}
intval = *((long *) var_val);
if (intval != 1) {
DEBUGMSGTL(("mynode", "wrong value %x", intval));
return SNMP_ERR_WRONGVALUE;
}
break;
case RESERVE2:
/*
* No resources are required....
*/
break;
case FREE:
/*
* ... so no resources need be freed
*/
break;
case ACTION:
/*
* Having triggered the sending of a trap,
* it would be impossible to revoke this,
* so we can't actually invoke the action here.
*/
//mynode_int = intval;
//Temp_ret=intval;
break;
case UNDO:
/*
* We haven't done anything yet,
* so there's nothing to undo
*/
break;
case COMMIT:
/*
* Everything else worked, so it's now safe
* to trigger the trap.
* Note that this is *only* acceptable since
* the trap sending routines are "failsafe".
* (In fact, they can fail, but they return no
* indication of this, which is the next best thing!)
*/
/*
* trap definition objects
*/
//Temp_ret=intval;
var_trap.next_variable = &var_obj; /* next variable */
var_trap.name = objid_snmptrap; /* snmpTrapOID.0 */
var_trap.name_length = sizeof(objid_snmptrap) / sizeof(oid); /* number of sub-ids */
var_trap.type = ASN_OBJECT_ID;
var_trap.val.objid = demo_trap; /* demo-trap objid */
var_trap.val_len = sizeof(demo_trap); /* length in bytes (not number of subids!) */
/*
* additional objects
*/
strcpy(mynode_str, "HHHHHHHHHHH");
var_obj.next_variable = NULL; /* No more variables after this one */
var_obj.name = mynode_string_oid;
var_obj.name_length = sizeof(mynode_string_oid) / sizeof(oid); /* number of sub-ids */
var_obj.type = ASN_OCTET_STR; /* type of variable */
var_obj.val.string = mynode_str; /* value */
var_obj.val_len = strlen(mynode_str);
DEBUGMSGTL(("mynode", "write_mynodetrap2 sending the v2 trap/n",
action));
send_v2trap(&var_trap);
DEBUGMSGTL(("mynode", "write_mynodetrap2 v2 trap sent/n",
action));
break;
}
return SNMP_ERR_NOERROR;
}