⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 system_mib.c

📁 ucd-snmp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  System MIB group implementation - system.c * */#include <config.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <sys/types.h>#if HAVE_WINSOCK_H#include <winsock.h>#endif#ifdef HAVE_SYS_TIME_H#include <sys/time.h>#endif#include <ctype.h>#if HAVE_UTSNAME_H#include <utsname.h>#else#if HAVE_SYS_UTSNAME_H#include <sys/utsname.h>#endif#endif#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if HAVE_DMALLOC_H#include <dmalloc.h>#endif#include "mibincl.h"#include "util_funcs.h"#include "system_mib.h"#include "struct.h"#include "read_config.h"#include "agent_read_config.h"#include "system.h"#include "sysORTable.h"#include "default_store.h"	/*********************	 *	 *  Kernel & interface information,	 *   and internal forward declarations	 *	 *********************/#define SYS_STRING_LEN	256char version_descr[ SYS_STRING_LEN ] = VERS_DESC;char sysContact[    SYS_STRING_LEN ] = SYS_CONTACT;char sysName[       SYS_STRING_LEN ] = SYS_NAME;char sysLocation[   SYS_STRING_LEN ] = SYS_LOC;char oldversion_descr[ SYS_STRING_LEN ];char oldsysContact[    SYS_STRING_LEN ];char oldsysName[       SYS_STRING_LEN ];char oldsysLocation[   SYS_STRING_LEN ];int sysServices=72;int sysServicesConfiged=0;extern oid version_id[];extern int version_id_len;static int sysContactSet = 0, sysLocationSet = 0, sysNameSet = 0;WriteMethod writeSystem;int header_system(struct variable *,oid *, size_t *, int, size_t *, WriteMethod **);#ifdef WIN32void getSysDescr();#endif	/*********************	 *	 *  snmpd.conf config parsing	 *	 *********************/void system_parse_config_sysloc(const char *token, char *cptr){  char tmpbuf[1024];  if (strlen(cptr) >= sizeof(sysLocation)) {      snprintf(tmpbuf, 1024,"syslocation token too long (must be < %d):\n\t%s",	       sizeof(sysLocation), cptr);      config_perror(tmpbuf);  }  if (strcmp(token, "psyslocation") == 0) {      if (sysLocationSet < 0) {	  /*  This is bogus (and shouldn't happen anyway) -- the sysLocation	      is already configured read-only.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysLocation.0\n");	  return;      } else {	  sysLocationSet++;      }  } else {      if (sysLocationSet > 0) {	  /*  This is bogus (and shouldn't happen anyway) -- we already read a	      persistent value of sysLocation, which we should ignore in	      favour of this one.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysLocation.0\n");	  /*  Fall through and copy in this value.  */      }            sysLocationSet = -1;  }    if (strcmp(cptr,"\"\"") == 0) {      sysLocation[0] = '\0';  } else if (strlen(cptr) < sizeof(sysLocation)) {      strcpy(sysLocation,cptr);  } }void system_parse_config_syscon(const char *token, char *cptr){  char tmpbuf[1024];  if (strlen(cptr) >= sizeof(sysContact)) {      snprintf(tmpbuf, 1024,"syscontact token too long (must be < %d):\n\t%s",	       sizeof(sysContact), cptr);      config_perror(tmpbuf);  }  if (strcmp(token, "psyscontact") == 0) {      if (sysContactSet < 0) {	  /*  This is bogus (and shouldn't happen anyway) -- the sysContact	      is already configured read-only.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysContact.0\n");	  return;      } else {	  sysContactSet++;      }  } else {      if (sysContactSet > 0) {	  /*  This is bogus (and shouldn't happen anyway) -- we already read a	      persistent value of sysContact, which we should ignore in favour	      of this one.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysContact.0\n");	  /*  Fall through and copy in this value.  */      }      sysContactSet = -1;  }    if (strcmp(cptr,"\"\"") == 0) {      sysContact[0] = '\0';  } else if (strlen(cptr) < sizeof(sysContact)) {      strcpy(sysContact,cptr);  } }void system_parse_config_sysname(const char *token, char *cptr){  char tmpbuf[1024];  if (strlen(cptr) >= sizeof(sysName)) {      snprintf(tmpbuf, 1024,"sysname token too long (must be < %d):\n\t%s",	       sizeof(sysName), cptr);      config_perror(tmpbuf);  }  if (strcmp(token, "psysname") == 0) {      if (sysNameSet < 0) {	  /*  This is bogus (and shouldn't happen anyway) -- the sysName	      is already configured read-only.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysName.0\n");	  return;      } else {	  sysNameSet++;      }  } else {       if (sysNameSet > 0) {	  /*  This is bogus (and shouldn't happen anyway) -- we already read a	      persistent value of sysName, which we should ignore in favour	      of this one.  */	  snmp_log(LOG_WARNING,		   "ignoring attempted override of read-only sysName.0\n");	  /*  Fall through and copy in this value.  */      }           sysNameSet = -1;  }    if (strcmp(cptr,"\"\"") == 0) {      sysName[0] = '\0';  } else if (strlen(cptr) < sizeof(sysName)) {      strcpy(sysName,cptr);  } }void system_parse_config_sysServices(const char *token, char *cptr){  sysServices = atoi(cptr);  sysServicesConfiged = 1;}	/*********************	 *	 *  Initialisation & common implementation functions	 *	 *********************//* define the structure we're going to ask the agent to register our   information at */struct variable2 system_variables[] = {    {VERSION_DESCR, ASN_OCTET_STR, RONLY, var_system, 1, {1}},    {VERSIONID, ASN_OBJECT_ID, RONLY, var_system, 1, {2}},    {UPTIME, ASN_TIMETICKS, RONLY, var_system, 1, {3}},    {SYSCONTACT, ASN_OCTET_STR, RWRITE, var_system, 1, {4}},    {SYSTEMNAME, ASN_OCTET_STR, RWRITE, var_system, 1, {5}},    {SYSLOCATION, ASN_OCTET_STR, RWRITE, var_system, 1, {6}},    {SYSSERVICES, ASN_INTEGER, RONLY, var_system, 1, {7}},    {SYSORLASTCHANGE, ASN_TIMETICKS, RONLY, var_system, 1, {8}}};/* Define the OID pointer to the top of the mib tree that we're   registering underneath */oid system_variables_oid[] = { SNMP_OID_MIB2,1 };oid system_module_oid[]    = { SNMP_OID_SNMPMODULES,1 };int system_module_oid_len  = sizeof( system_module_oid ) / sizeof( oid );int system_module_count    = 0;static intsystem_store(int a, int b, void *c, void *d){  char line[SNMP_MAXBUF_SMALL];  if (sysLocationSet > 0) {      snprintf(line, SNMP_MAXBUF_SMALL, "psyslocation %s", sysLocation);      snmpd_store_config(line);  }  if (sysContactSet > 0) {      snprintf(line, SNMP_MAXBUF_SMALL, "psyscontact %s", sysContact);      snmpd_store_config(line);  }  if (sysNameSet > 0) {      snprintf(line, SNMP_MAXBUF_SMALL, "psysname %s", sysName);      snmpd_store_config(line);  }  return 0;}void init_system_mib(void){#ifdef HAVE_UNAME  struct utsname utsName;  uname(&utsName);  sprintf(version_descr, "%s %s %s %s %s", utsName.sysname, utsName.nodename,          utsName.release, utsName.version, utsName.machine);#else#if HAVE_EXECV  struct extensible extmp;  /* set default values of system stuff */  sprintf(extmp.command,"%s -a",UNAMEPROG);  /* setup defaults */  extmp.type = EXECPROC;  extmp.next = NULL;  exec_command(&extmp);  strncpy(version_descr,extmp.output, sizeof(version_descr));  version_descr[strlen(version_descr)-1] = 0; /* chomp new line */#else#if WIN32  getSysDescr();#else  strcpy(version_descr, "unknown" );#endif /* WIN32 */#endif#endif#ifdef HAVE_GETHOSTNAME

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -