📄 system_mib.c
字号:
#endif // !HAVE_EXECV
#endif // !HAVE_UNAME
#ifdef HAVE_GETHOSTNAME
gethostname(sysName,sizeof(sysName));
#else
#ifdef HAVE_UNAME
strncpy(sysName,utsName.nodename,sizeof(sysName));
#else
#if HAVE_EXECV
sprintf(extmp.command,"%s -n",UNAMEPROG);
/* setup defaults */
extmp.type = EXECPROC;
extmp.next = NULL;
exec_command(&extmp);
strncpy(sysName,extmp.output, sizeof(sysName));
sysName[strlen(sysName)-1] = 0; /* chomp new line */
#endif /* HAVE_EXECV */
#endif /* HAVE_UNAME */
#endif /* HAVE_GETHOSTNAME */
/* register ourselves with the agent to handle our mib tree */
REGISTER_MIB("mibII/system", system_variables, variable2, \
system_variables_oid);
if ( ++system_module_count == 3 )
REGISTER_SYSOR_ENTRY( system_module_oid,
"The MIB module for SNMPv2 entities");
/* register our config handlers */
snmpd_register_config_handler("syslocation", system_parse_config_sysloc,
NULL, "location");
snmpd_register_config_handler("syscontact", system_parse_config_syscon,
NULL,"contact-name");
snmpd_register_config_handler("sysservices", system_parse_config_sysServices,
NULL,"NUMBER");
}
/*
header_system(...
Arguments:
vp IN - pointer to variable entry that points here
name IN/OUT - IN/name requested, OUT/name found
length IN/OUT - length of IN/OUT oid's
exact IN - TRUE if an exact match was requested
var_len OUT - length of variable or 0 if function returned
write_method
*/
int
header_system(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **write_method)
{
#define SYSTEM_NAME_LENGTH 8
oid newname[MAX_OID_LEN];
int result;
DEBUGMSGTL(("mibII/system", "var_system: "));
DEBUGMSGOID(("mibII/system", name, *length));
DEBUGMSG(("mibII/system"," %d\n", exact));
memcpy((char *)newname, (char *)vp->name, vp->namelen * sizeof(oid));
newname[SYSTEM_NAME_LENGTH] = 0;
result = snmp_oid_compare(name, *length, newname, vp->namelen + 1);
if ((exact && (result != 0)) || (!exact && (result >= 0)))
return(MATCH_FAILED);
memcpy( (char *)name,(char *)newname, (vp->namelen + 1) * sizeof(oid));
*length = vp->namelen + 1;
*write_method = 0;
*var_len = sizeof(long); /* default to 'long' results */
return(MATCH_SUCCEEDED);
}
/*********************
*
* System specific implementation functions
* (actually common!)
*
*********************/
#ifdef USING_MIBII_SYSORTABLE_MODULE
extern struct timeval sysOR_lastchange;
#endif
u_char *
var_system(struct variable *vp,
oid *name,
size_t *length,
int exact,
size_t *var_len,
WriteMethod **write_method)
{
struct timeval now, diff;
if (header_system(vp, name, length, exact, var_len, write_method) == MATCH_FAILED )
return NULL;
switch (vp->magic){
case VERSION_DESCR:
*var_len = strlen(version_descr);
*write_method = writeSystem;
return (u_char *)version_descr;
case VERSIONID:
*var_len = version_id_len*sizeof(version_id[0]);
return (u_char *)version_id;
case UPTIME:
gettimeofday(&now, NULL);
now.tv_sec--;
now.tv_usec += 1000000L;
diff.tv_sec = now.tv_sec - starttime.tv_sec;
diff.tv_usec = now.tv_usec - starttime.tv_usec;
if (diff.tv_usec > 1000000L){
diff.tv_usec -= 1000000L;
diff.tv_sec++;
}
long_return = ((diff.tv_sec * 100) + (diff.tv_usec / 10000));
return ((u_char *) &long_return);
case SYSCONTACT:
*var_len = strlen(sysContact);
*write_method = writeSystem;
return (u_char *)sysContact;
case SYSTEMNAME:
*var_len = strlen(sysName);
*write_method = writeSystem;
return (u_char *)sysName;
case SYSLOCATION:
*var_len = strlen(sysLocation);
*write_method = writeSystem;
return (u_char *)sysLocation;
case SYSSERVICES:
#if NO_DUMMY_VALUES
if (!sysServicesConfiged)
return NULL;
#endif
long_return = sysServices;
return (u_char *)&long_return;
#ifdef USING_MIBII_SYSORTABLE_MODULE
case SYSORLASTCHANGE:
diff.tv_sec = sysOR_lastchange.tv_sec - 1 - starttime.tv_sec;
diff.tv_usec =
sysOR_lastchange.tv_usec + 1000000L - starttime.tv_usec;
if (diff.tv_usec > 1000000L){
diff.tv_usec -= 1000000L;
diff.tv_sec++;
}
if ((diff.tv_sec * 100) + (diff.tv_usec / 10000) < 0)
long_return = 0;
else
long_return = ((diff.tv_sec * 100) + (diff.tv_usec / 10000));
return ((u_char *) &long_return);
#endif
default:
DEBUGMSGTL(("snmpd", "unknown sub-id %d in var_system\n", vp->magic));
}
return NULL;
}
int
writeSystem(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)
{
u_char *cp;
char *buf = NULL, *oldbuf = NULL;
int count;
switch((char)name[7]){
case VERSION_DESCR:
buf = version_descr;
oldbuf = oldversion_descr;
break;
case SYSCONTACT:
buf = sysContact;
oldbuf = oldsysContact;
break;
case SYSTEMNAME:
buf = sysName;
oldbuf = oldsysName;
break;
case SYSLOCATION:
buf = sysLocation;
oldbuf = oldsysLocation;
break;
default:
return SNMP_ERR_GENERR; /* ??? */
}
switch ( action ) {
case RESERVE1: /* Check values for acceptability */
if (var_val_type != ASN_OCTET_STR){
snmp_log(LOG_ERR, "not string\n");
return SNMP_ERR_WRONGTYPE;
}
if (var_val_len > sizeof(version_descr)-1){
snmp_log(LOG_ERR, "bad length\n");
return SNMP_ERR_WRONGLENGTH;
}
for(cp = var_val, count = 0; count < (int)var_val_len; count++, cp++){
if (!isprint(*cp)){
snmp_log(LOG_ERR, "not print %x\n", *cp);
return SNMP_ERR_WRONGVALUE;
}
}
break;
case RESERVE2: /* Allocate memory and similar resources */
/* Using static strings, so nothing needs to be done */
break;
case ACTION: /* Perform the SET action (if reversible) */
/* Save the old value, in case of UNDO */
strcpy( oldbuf, buf);
memcpy( buf, var_val, var_val_len);
buf[var_val_len] = 0;
break;
case UNDO: /* Reverse the SET action and free resources */
strcpy( buf, oldbuf);
oldbuf[0] = 0;
break;
case COMMIT: /* Confirm the SET, performing any irreversible actions,
and free resources */
case FREE: /* Free any resources allocated */
/* No resources have been allocated, but "empty" the 'oldbuf' */
oldbuf[0] = 0;
break;
}
return SNMP_ERR_NOERROR;
} /* end of writeSystem */
/*********************
*
* Internal implementation functions - None
*
*********************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -