Esempio n. 1
0
static void
setup_mib_flags(int resolve_oids) {

#ifdef DEBUG
/*  snmp_set_mib_warnings (2); */
#endif /* DEBUG  */

  if (resolve_oids)
    {
      setenv ("MIBS", "ALL", 1);
    }

#ifdef HAVE_NETSNMP_INIT_MIB
  netsnmp_init_mib ();
#else
  init_mib ();
#endif

  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
    }				/* we want OIDs to appear in numeric form */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
    }				/* we want enums to appear in numeric form as integers */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_FULL_OID);
    }				/* we want to full numeric OID to be printed, including prefix */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_DONT_PRINT_UNITS);
    }
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_RANDOM_ACCESS);
    }				/* so we can use sysContact.0 instead of system.sysContact.0  */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
    }				/* so we can use sysContact.0 instead of system.sysContact.0  */
}
Esempio n. 2
0
GkrellmMonitor *
gkrellm_init_plugin(void)
{
    readers = NULL;

#ifdef DEBUG_SNMP
    debug_register_tokens("all");
    snmp_set_do_debugging(1);
#endif /* DEBUG_SNMP */

    init_mib();
    
    mon = &plugin_mon;
    return &plugin_mon;
}
Esempio n. 3
0
int
snmp_printer_info(char *hostname, char *community, char **manufacturer,
		char **model, char **description, char **serial_no,
		char ***command_set, char **uri)
{
	char *tmp = NULL;

	init_snmp("network-printer-probe");
	init_mib();

	if (snmp_brother_printer_info(hostname, community, manufacturer, model,
			description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_ricoh_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_lexmark_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_xerox_phaser_printer_info(hostname, community,
			manufacturer, model, description, serial_no,
			command_set, uri) == 0) {
		return (0);
	} else if (snmp_qms_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set, uri) == 0) {
		return (0);
	} else if (snmp_oki_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_hp_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_ppm_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_prt_printer_info(hostname, community, manufacturer,
			model, description, serial_no, command_set) == 0) {
		return (0);
	} else if (snmp_host_resource_printer_info(hostname, community,
			manufacturer, model, description, serial_no,
			command_set) == 0) {
		return (0);
	}

	return (-1);
}
PEGASUS_NAMESPACE_BEGIN

void snmpDeliverTrap_netsnmp::initialize()
{
    PEG_METHOD_ENTER(TRC_IND_HANDLER, "snmpDeliverTrap_netsnmp::initialize");

    // Defined default MIB modules (in net-snmp-config.h) do not need to be
    // loaded and loading them can cause some stderr;
    // use environment variable MIBS to override the default MIB modules.
    // If there is no MIBS environment variable, add it in.
    char* envVar;
    envVar = getenv("MIBS");

    if (envVar == NULL)
    {
        putenv("MIBS=");
    }

    // Initialize the mib reader
    netsnmp_set_mib_directory("");
    init_mib();

    // Initializes the SNMP library
    init_snmp("snmpIndicationHandler");

    // don't load/save persistent file
#ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
    netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
        NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
#endif

    // windows32 specific initialization (is a NOOP on unix)
    SOCK_STARTUP;

    PEG_METHOD_EXIT();
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
    char *hostname = NULL;
    struct protoent *p;
    struct protox *tp = NULL;	/* for printing cblocks & stats */
    int allprotos = 1;
    char *community = NULL;
    char *argp;
    struct snmp_session session;
    int dest_port = SNMP_PORT;
    int timeout = SNMP_DEFAULT_TIMEOUT;
    int version = SNMP_VERSION_1;
    int arg;

    init_mib();
    /*
     * Usage: snmpnetstatwalk -v 1 [-q] hostname community ...      or:
     * Usage: snmpnetstat [-v 2 ] [-q] hostname noAuth     ...
     */
    for(arg = 1; arg < argc; arg++){
	if (argv[arg][0] == '-'){
	    switch(argv[arg][1]){
              case 'V':
                fprintf(stderr,"UCD-snmp version: %s\n", VersionInfo);
                exit(0);
                break;

	      case 'h':
		usage();
		exit(0);

	      case 'd':
		snmp_set_dump_packet(1);
		break;

	      case 'q':
		snmp_set_quick_print(1);
		break;

	      case 'D':
                debug_register_tokens(&argv[arg][2]);
		snmp_set_do_debugging(1);
		break;
	      case 'p':
		if (argv[arg][2] != 0) dest_port = atoi(argv[arg]+2);
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else dest_port = atoi(argv[arg]);
		break;

	      case 't':
		if (argv[arg][2] != 0) timeout = atoi(argv[arg]+2);
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else timeout = atoi(argv[arg]);
		timeout *= 1000000;
		break;

	      case 'c':
		if (argv[arg][2] != 0) community = argv[arg]+2;
		else if (++arg == argc) {
		    usage();
		    exit(1);
		}
		else community = argv[arg];
		break;

	      case 'v':
		if (argv[arg][2] != 0) argp = argv[arg]+2;
		else if (arg == argc) {
		    usage();
		    exit(1);
		}
		else argp = argv[arg];
		if (!strcmp(argp,"1"))
		    version = SNMP_VERSION_1;
		else if (!strcmp(argp,"2c"))
		    version = SNMP_VERSION_2c;
		else {
		    fprintf(stderr, "Invalid version: %s\n", argp);
		    usage();
		    exit(1);
		}
		break;

	      case 'a':
		aflag++;
		break;

	      case 'i':
		iflag++;
		break;

	      case 'o':
		oflag++;
		break;

	      case 'n':
		nflag++;
		break;

	      case 'r':
		rflag++;
		break;

	      case 's':
		sflag++;
		break;

	      case 'P':
		if (++arg == argc) {
		    usage();
		    exit(1);
		}
		if ((tp = name2protox(argv [arg])) == NULLPROTOX) {
		  fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
			  argv [arg]);
		  exit(1);
		}
		allprotos = 0;
		tp->pr_wanted = 1;
		break;

	      case 'I':
		iflag++;
		if (*(intrface = argv[arg] + 2) == 0) {
		  if (++arg == argc) {
		      usage();
		      exit(1);
		  }
		  if ((intrface = argv[arg]) == 0)
		    break;
		}
		break;

	      default:
		printf("invalid option: -%c\n", argv[arg][1]);
		break;
	    }
	    continue;
	}
	if (hostname == NULL){
	    hostname = argv[arg];
	} else if ((version == SNMP_VERSION_1 || version == SNMP_VERSION_2c)
                   && community == NULL){
	    community = argv[arg]; 
	} else if (isdigit(argv[arg][0])) {
            interval = atoi(argv[arg]);
            if (interval <= 0){
		usage();
		exit(1);
	    }
	    iflag++;
	} else {
	    usage();
	    exit(1);
	}
    }
    
    if (!hostname ||
	((version == SNMP_VERSION_1 || version == SNMP_VERSION_2c) && !community)) {
	usage();
	exit(1);
    }

    snmp_sess_init(&session);
    session.peername = hostname;
    session.remote_port = dest_port;
    session.timeout = timeout;
    if (version == SNMP_VERSION_1 || version == SNMP_VERSION_2c){
        session.version = version;
        session.community = (u_char *)community;
        session.community_len = strlen((char *)community);
    }

    SOCK_STARTUP;

    /* open an SNMP session */
    Session = snmp_open(&session);
    if (Session == NULL){
      /* diagnose snmp_open errors with the input struct snmp_session pointer */
        snmp_sess_perror("snmpnetstat", &session);
        SOCK_CLEANUP;
	exit(1);
    }

    /*
     * Keep file descriptors open to avoid overhead
     * of open/close on each call to get* routines.
     */
    sethostent(1);
    setnetent(1);
    setprotoent(1);
    setservent(1);

    if (iflag) {
	intpr(interval);
    }
    if (oflag) {
	intpro(interval);
    }
    if (rflag) {
	if (sflag)
	    rt_stats();
	else
	    routepr();
    }
    
    if (iflag || rflag || oflag)
	;
    else {

    while ((p = getprotoent46())) {
	for (tp = protox; tp->pr_name; tp++) {
	    if (strcmp(tp->pr_name, p->p_name) == 0)
		break;
	}
	if (tp->pr_name == 0 || (tp->pr_wanted == 0 && allprotos == 0))
	    continue;
	if (sflag) {
	    if (tp->pr_stats)
		(*tp->pr_stats)();
	} else
	    if (tp->pr_cblocks)
		(*tp->pr_cblocks)(tp->pr_name);
    }
    } /* ! iflag, rflag, oflag */

    endprotoent();
    endservent();
    endnetent();
    endhostent();

    snmp_close(Session);

    SOCK_CLEANUP;
    return 0;
}
Esempio n. 6
0
int
main(int argc, char *argv[])
{
    char           *hostname = NULL;
    struct protoent *p;
    struct protox  *tp = NULL;  /* for printing cblocks & stats */
    int             allprotos = 1;
    char           *community = NULL;
    char           *argp;
    netsnmp_session session;
    int             timeout = SNMP_DEFAULT_TIMEOUT;
    int             version = SNMP_DEFAULT_VERSION;
    int             arg;

#ifndef DISABLE_MIB_LOADING
    init_mib();
#endif /* DISABLE_MIB_LOADING */
    /*
     * Usage: snmpnetstatwalk -v 1 [-q] hostname community ...      or:
     * Usage: snmpnetstat [-v 2 ] [-q] hostname noAuth     ...
     */
    while ((arg = getopt(argc, argv, "VhdqD:t:c:v:aionrsP:I:")) != EOF) {
        switch (arg) {
        case 'V':
            fprintf(stderr, "NET-SNMP version: %s\n",
                    netsnmp_get_version());
            exit(0);
            break;

        case 'h':
            usage();
            exit(0);

        case 'd':
            snmp_set_dump_packet(1);
            break;

        case 'q':
            snmp_set_quick_print(1);
            break;

        case 'D':
            debug_register_tokens(optarg);
            snmp_set_do_debugging(1);
            break;

        case 't':
            timeout = atoi(optarg);
            timeout *= 1000000;
            break;

        case 'c':
            community = optarg;
            break;

        case 'v':
            argp = optarg;
            version = -1;
#ifndef DISABLE_SNMPV1
            if (!strcasecmp(argp, "1"))
                version = SNMP_VERSION_1;
#endif
#ifndef DISABLE_SNMPV2C
            if (!strcasecmp(argp, "2c"))
                version = SNMP_VERSION_2c;
#endif
            if (version == -1) {
                fprintf(stderr, "Invalid version: %s\n", argp);
                usage();
                exit(1);
            }
            break;

        case 'a':
            aflag++;
            break;

        case 'i':
            iflag++;
            break;

        case 'o':
            oflag++;
            break;

        case 'n':
            nflag++;
            break;

        case 'r':
            rflag++;
            break;

        case 's':
            sflag++;
            break;

        case 'P':
            if ((tp = name2protox(optarg)) == NULLPROTOX) {
                fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
                        optarg);
                exit(1);
            }
            allprotos = 0;
            tp->pr_wanted = 1;
            break;

        case 'I':
            iflag++;
            intrface = optarg;
            break;

        default:
            exit(1);
            break;
        }
        continue;
    }

    init_snmp("snmpapp");
    snmp_enable_stderrlog();
    if (version == SNMP_DEFAULT_VERSION) {
        version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
		                     NETSNMP_DS_LIB_SNMPVERSION);
        if (!version) {
            switch (DEFAULT_SNMP_VERSION) {
#ifndef DISABLE_SNMPV1
            case 1:
                version = SNMP_VERSION_1;
                break;
#endif
#ifndef DISABLE_SNMPV2C
            case 2:
                version = SNMP_VERSION_2c;
                break;
#endif
            case 3:
                version = SNMP_VERSION_3;
                break;
            }
#ifndef DISABLE_SNMPV1
        } else if (version == NETSNMP_DS_SNMP_VERSION_1) {
                          /* Bogus value. version1 = 0 */
            version = SNMP_VERSION_1;
#endif
        }
    }
    if (optind < argc) {
        hostname = argv[optind++];
    }
    else {
        fprintf(stderr, "Missing host name.\n");
        exit(1);
    }
    if (community == NULL) {
	community = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
		                          NETSNMP_DS_LIB_COMMUNITY);
    }
    if (optind < argc && isdigit(argv[optind][0])) {
        interval = atoi(argv[optind++]);
        if (interval <= 0) {
            usage();
            exit(1);
        }
        iflag++;
    }
    if (optind < argc) {
        usage();
        exit(1);
    }


    snmp_sess_init(&session);
    session.peername = hostname;
    session.timeout = timeout;
#if !defined(DISABLE_SNMPV1) || !defined(DISABLE_SNMPV2C)
    if (version != SNMP_VERSION_3) {
        if (!community) {
            fprintf(stderr, "Missing community name.\n");
            exit(1);
        }
        session.version = version;
        session.community = (u_char *) community;
        session.community_len = strlen(community);
    }
#endif

    SOCK_STARTUP;

    /*
     * open an SNMP session 
     */
    Session = snmp_open(&session);
    if (Session == NULL) {
        /*
         * diagnose snmp_open errors with the input netsnmp_session pointer 
         */
        snmp_sess_perror("snmpnetstat", &session);
        SOCK_CLEANUP;
        exit(1);
    }

    /*
     * Keep file descriptors open to avoid overhead
     * of open/close on each call to get* routines.
     */
    sethostent(1);
    setnetent(1);
    setprotoent(1);
    setservent(1);

    if (iflag) {
        intpr(interval);
    }
    if (oflag) {
        intpro(interval);
    }
    if (rflag) {
        if (sflag)
            rt_stats();
        else
            routepr();
    }

    if (!(iflag || rflag || oflag)) {
        while ((p = getprotoent46())) {
            for (tp = protox; tp->pr_name; tp++) {
                if (strcmp(tp->pr_name, p->p_name) == 0)
                    break;
            }
            if (tp->pr_name == 0 || (tp->pr_wanted == 0 && allprotos == 0))
                continue;
            if (sflag) {
                if (tp->pr_stats)
                    (*tp->pr_stats) ();
            } else if (tp->pr_cblocks)
                (*tp->pr_cblocks) (tp->pr_name);
        }
    }                           /* ! iflag, rflag, oflag */

    endprotoent();
    endservent();
    endnetent();
    endhostent();

    snmp_close(Session);

    SOCK_CLEANUP;
    return 0;
}
Esempio n. 7
0
void vacm_parse_view (char *token,
                      char *param)
{
    char *name, *type, *subtree, *mask;
    int inclexcl;
    struct vacm_viewEntry *vp;
    oid suboid[MAX_OID_LEN];
    int suboid_len = 0;
    u_char viewMask[sizeof (vp->viewMask)];
    int i;

    init_mib();
    name = strtok (param, " \t\n");
    if (!name) {
        config_perror("missing NAME parameter");
        return;
    }
    type = strtok (NULL, " \n\t");
    if (!type) {
        config_perror("missing TYPE parameter");
        return;
    }
    subtree = strtok(NULL, " \t\n");
    if (!subtree) {
        config_perror("missing SUBTREE parameter");
        return;
    }
    mask = strtok(NULL, " \t\n");

    if (strcmp(type, "included") == 0) inclexcl = SNMP_VIEW_INCLUDED;
    else if (strcmp(type, "excluded") == 0) inclexcl = SNMP_VIEW_EXCLUDED;
    else {
        config_perror("TYPE must be included/excluded?");
        return;
    }
    suboid_len = MAX_OID_LEN;
    if (!read_objid(subtree, suboid, &suboid_len)) {
        config_perror("bad SUBTREE object id");
        return;
    }
    if (mask) {
        int val;
        i = 0;
        for (mask = strtok(mask, "."); mask; mask = strtok(NULL, ":")) {
            if (i >= sizeof(viewMask)) {
                config_perror("MASK too long");
                return;
            }
            if (sscanf(mask, "%x", &val) == 0) {
                config_perror("invalid MASK");
                return;
            }
            viewMask[i] = val;
            i++;
        }
    }
    else {
        for (i = 0; i < sizeof(viewMask); i++)
            viewMask[i] = 0xff;
    }
    vp = vacm_createViewEntry(name, suboid, suboid_len);
    memcpy(vp->viewMask, viewMask, sizeof(viewMask));
    vp->viewType = inclexcl;
    vp->viewStorageType = SNMP_STORAGE_PERMANENT;
    vp->viewStatus = SNMP_ROW_ACTIVE;
    free (vp->reserved);
    vp->reserved = NULL;
}
Esempio n. 8
0
void init(const QString &appname)
{
	init_snmp(appname.latin1());
	init_mib();
}
Esempio n. 9
0
int SnmpInit(void) {
	syslog(LOG_DEBUG,"*** Initialise SNMP ***");

  init_snmp("CrateTest");									  // I never saw this name used !?!
	init_mib();																// init MIB processing
  if(!read_module("WIENER-CRATE-MIB")) {		// read specific mibs
    syslog(LOG_ERR,"Unable to load SNMP MIB file \"%s\"","WIENER-CRATE-MIB");
    return 0;
  }
  syslog(LOG_DEBUG,"*** Translate OIDs ***");
	
  lengthSysMainSwitch = MAX_OID_LEN;
  if(!get_node(S_sysMainSwitch,oidSysMainSwitch,&lengthSysMainSwitch)) {
		syslog(LOG_ERR,"OID \"sysMainSwitch.0\"not found"); return false;
  } 

  lengthOutputMeasurementSenseVoltageAll = MAX_OID_LEN;
  if(!get_node(S_outputMeasurementSenseVoltageAll,oidOutputMeasurementSenseVoltageAll,&lengthOutputMeasurementSenseVoltageAll)) {
		syslog(LOG_ERR,"OID \"sysMainSwitch.0\"not found"); return false;
  } 

  lengthFanAirTemperature = MAX_OID_LEN;
  if(!get_node(S_fanAirTemperature,oidFanAirTemperature,&lengthFanAirTemperature)) {
		syslog(LOG_ERR,"OID \"fanAirTemperature.0\"not found"); return false;
  } 

  for(int j = 0; j < 8; j++)
  {
	lengthSensorTemperature[j] = MAX_OID_LEN;
	if(!get_node(S_sensorTemperature[j],oidSensorTemperature[j],&lengthSensorTemperature[j])) {
		syslog(LOG_ERR,"OID \"sensorTemperature.0\"not found"); return false;
  } 
  }

  for(int j = 0; j < 12; j++)
  {
	lengthOutputMeasurementSenseVoltage[j] = MAX_OID_LEN;
	if(!get_node(S_outputMeasurementSenseVoltage[j],oidOutputMeasurementSenseVoltage[j],&lengthOutputMeasurementSenseVoltage[j])) {
		syslog(LOG_ERR,"OID \"outputMeasurementSenseVoltage.1\"not found"); return false;
	 }
	lengthOutputVoltage[j] = MAX_OID_LEN;
	if(!get_node(S_outputVoltage[j],oidOutputVoltage[j],&lengthOutputVoltage[j])) {
		syslog(LOG_ERR,"OID \"outputVoltage.1\"not found"); return false;
	}
	lengthOutputMeasurementCurrent[j] = MAX_OID_LEN;
		if(!get_node(S_outputMeasurementCurrent[j],oidOutputMeasurementCurrent[j],&lengthOutputMeasurementCurrent[j])) {
			syslog(LOG_ERR,"OID \"outputMeasurementCurrent.1\"not found"); return false;
		}
	lengthOutputStatus[j] = MAX_OID_LEN;
		if(!get_node(S_outputStatus[j],oidOutputStatus[j],&lengthOutputStatus[j])) {
			syslog(LOG_ERR,"OID \"outputStatus.1\"not found"); return false;
		} 
	lengthChannelSwitch[j] = MAX_OID_LEN;
		if(!get_node(S_outputSwitch[j],oidChannelSwitch[j],&lengthChannelSwitch[j])) {
			syslog(LOG_ERR,"OID \"channelSwitch.1\"not found"); return false;
		} 
//	lengthOutputRampUp[j] = MAX_OID_LEN;
//		if(!get_node(S_outputRampUp[j],oidOutputRampUp[j],&lengthOutputRampUp[j])) {
//			syslog(LOG_ERR,"OID \"outputRampUp.1\"not found"); return false;
//		} 
//	lengthOutputRampDown[j] = MAX_OID_LEN;
//		if(!get_node(S_outputRampDown[j],oidOutputRampDown[j],&lengthOutputRampDown[j])) {
//			syslog(LOG_ERR,"OID \"outputRampDown.1\"not found"); return false;
//		} 
  }


  syslog(LOG_DEBUG,"*** Initialise SNMP done ***");
  SOCK_STARTUP;															// only in main thread
  return 1;
}
Esempio n. 10
0
static void
setup_mib_flags(int resolve_oids, char *custom_mibs) {

#ifdef DEBUG
/*  snmp_set_mib_warnings (2); */
#endif /* DEBUG  */
/* We do not want warning for normal users. Should be set with an argument on the CLI maybe?
 * snmp_set_mib_warnings (1); */

  if (custom_mibs)
    {
     setenv ("MIBDIRS", custom_mibs, 1);
    }

  if (resolve_oids)
    {
     setenv ("MIBS", "ALL", 1);
    }

  if (!resolve_oids) {
    if (!netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC)) {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID, NETSNMP_OID_OUTPUT_NUMERIC);
    }
  }

  /* DOCSIS vendors are notorious for supplying MIBs with invalid syntax */
  netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_PARSE_LABEL, 1);

#ifdef HAVE_NETSNMP_INIT_MIB
  netsnmp_init_mib ();
#else
  init_mib ();
#endif
  
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_NUMERIC_OIDS);
    }				/* we want OIDs to appear in numeric form */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
    }				/* we want enums to appear in numeric form as integers */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_FULL_OID))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_PRINT_FULL_OID);
    }				/* we want to full numeric OID to be printed, including prefix */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PRINT_UNITS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_DONT_PRINT_UNITS);
    }
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_RANDOM_ACCESS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_RANDOM_ACCESS);
    }				/* so we can use sysContact.0 instead of system.sysContact.0  */
  if (!netsnmp_ds_get_boolean
      (NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_NUMERIC_TIMETICKS))
    {
      netsnmp_ds_toggle_boolean (NETSNMP_DS_LIBRARY_ID,
				 NETSNMP_DS_LIB_NUMERIC_TIMETICKS);
    }				/* so we can use sysContact.0 instead of system.sysContact.0  */
}
Esempio n. 11
0
int
main(int argc, char *argv[])
{
    int	arg,i;
    int ret;
    u_short dest_port = 161;
    int dont_fork = 0, dont_zero_log = 0;
    char logfile[300];
    char *cptr, **argvptr;

    logfile[0] = 0;
    optconfigfile = NULL;
    dontReadConfigFiles = 0;
    
#ifdef LOGFILE
    strcpy(logfile,LOGFILE);
#endif

    /*
     * usage: snmpd
     */
    for(arg = 1; arg < argc; arg++){
	if (argv[arg][0] == '-'){
	    switch(argv[arg][1]){
                case 'c':
		    if (++arg == argc) usage(argv[0]);
                    optconfigfile = strdup(argv[arg]);
                    break;
                case 'C':
                    dontReadConfigFiles = 1;
                    break;
		case 'd':
		    snmp_dump_packet++;
		    verbose = 1;
		    break;
		case 'q':
		    snmp_set_quick_print(1);
		    break;
		case 'D':
                    debug_register_tokens(&argv[arg][2]);
		    snmp_set_do_debugging(1);
		    break;
                case 'p':
		    if (++arg == argc) usage(argv[0]);
                    dest_port = atoi(argv[arg]);
		    if (dest_port <= 0) usage(argv[0]);
                    break;
		case 'a':
		    log_addresses++;
		    break;
		case 'V':
		    verbose = 1;
		    break;
		case 'f':
		    dont_fork = 1;
		    break;
                case 'l':
		    if (++arg == argc) usage(argv[0]);
                    strcpy(logfile,argv[arg]);
                    break;
                case 'L':
                    logfile[0] = 0;
                    break;
                case 'A':
                    dont_zero_log = 1;
                    break;
                case 'h':
                    usage(argv[0]);
                    break;
                case 'H':
                    init_agent();            /* register our .conf handlers */
#ifdef HAVE_MIB
                    register_mib_handlers(); /* snmplib .conf handlers */
#endif
                    fprintf(stderr, "Configuration directives understood:\n");
                    read_config_print_usage("  ");
                    break;
                case 'v':
                    printf("\nUCD-snmp version:  %s\n",VersionInfo);
                    printf("Author:            Wes Hardaker\n");
                    printf("Email:             [email protected]\n\n");
                    exit (0);
                case '-':
                  switch(argv[arg][2]){
                    case 'v': 
                      printf("\nUCD-snmp version:  %s\n",VersionInfo);
                      printf("Author:            Wes Hardaker\n");
                      printf("Email:             [email protected]\n\n");
                      exit (0);
                    case 'h':
                      usage(argv[0]);
                      exit(0);
                  }
		default:
		    printf("invalid option: %s\n", argv[arg]);
                    usage(argv[0]);
		    break;
	    }
	    continue;
	}
    }
    /* initialize a argv set to the current for restarting the agent */
    argvrestartp = (char **) malloc((argc+2) * sizeof (char *));
    argvptr = argvrestartp;
    for(i=0, ret = 1; i < argc; i++) {
      ret += strlen(argv[i])+1;
    }
    argvrestart = (char *) malloc((ret));
    argvrestartname = (char *) malloc(strlen(argv[0])+1);
    strcpy(argvrestartname,argv[0]);
    for(cptr = argvrestart,i = 0; i < argc; i++) {
      strcpy(cptr,argv[i]);
      *(argvptr++) = cptr;
      cptr += strlen(argv[i]) + 1;
    }
    *cptr = 0;
    *argvptr = NULL;

    /* open the logfile if necessary */
    if (logfile[0]) {
      close(1);
      open(logfile,O_WRONLY|O_CREAT| ((dont_zero_log) ? O_APPEND : O_TRUNC),
           0644);
      close(2);
      dup(1);
      close(0);
    }
#ifdef USE_LIBWRAP
    openlog("snmpd", LOG_CONS, LOG_AUTH|LOG_INFO);
#endif
    setvbuf (stdout, NULL, _IOLBF, BUFSIZ);
    printf ("%s UCD-SNMP version %s\n", sprintf_stamp (NULL), VersionInfo);
    if (!dont_fork && fork() != 0)   /* detach from shell */
      exit(0);
    init_agent();            /* register our .conf handlers */
#ifdef HAVE_MIB
    register_mib_handlers(); /* snmplib .conf handlers */
    read_premib_configs();   /* read pre-mib-reading .conf handlers */
    init_mib();              /* initialize the mib structures */
#endif
    update_config(0);        /* read in config files and register HUP */
#ifdef PERSISTENTFILE
    /* read in the persistent information cache */
    read_config_with_type(PERSISTENTFILE, "snmpd");
    unlink(PERSISTENTFILE);  /* nuke it now that we've read it */
#endif
#ifdef HAVE_SNMP2P
    init_snmp2p( dest_port );
#endif
    
    printf("Opening port(s): "); 
    fflush(stdout);
    if (( ret = open_port( dest_port )) > 0 )
        sd_handlers[ret-1] = snmp_read_packet;   /* Save pointer to function */
#ifdef HAVE_SNMP2P
    open_ports_snmp2p( );
#endif
    printf("\n");
    fflush(stdout);

    /* get current time (ie, the time the agent started) */
    gettimeofday(&starttime, NULL);
    starttime.tv_sec--;
    starttime.tv_usec += 1000000L;

    /* send coldstart trap via snmptrap(1) if possible */
    send_easy_trap (0, 0);
    signal(SIGTERM, SnmpdShutDown);
    signal(SIGINT, SnmpdShutDown);

    memset(addrCache, 0, sizeof(addrCache));
    receive(sdlist);
#include "mib_module_shutdown.h"
    DEBUGMSGTL(("snmpd", "sending shutdown trap\n"));
    SnmpTrapNodeDown();
    DEBUGMSGTL(("snmpd", "Bye...\n"));
    return 0;
}
Esempio n. 12
0
/*
 * Generic SNMP object fetcher
 *
 * st=1 GET
 * st=2 WALK
 */
void _php3_snmp(INTERNAL_FUNCTION_PARAMETERS, int st) {
	pval *a1, *a2, *a3;
	struct snmp_session session, *ss;
	struct snmp_pdu *pdu=NULL, *response;
	struct variable_list *vars;
	char *community, *hostname;
    char *objid;
    oid name[MAX_NAME_LEN];
    int name_length;
    int status, count,rootlen=0,gotroot=0;
	oid root[MAX_NAME_LEN];
	char buf[2048];
	int keepwalking=1;
	static int mib_init=0;

	if (getParameters(ht, 3, &a1, &a2, &a3) == FAILURE) {
		WRONG_PARAM_COUNT;
	}
	convert_to_string(a1);
	convert_to_string(a2);
	convert_to_string(a3);

	hostname=a1->value.str.val;
	community=a2->value.str.val;
	objid=a3->value.str.val;

	if (!mib_init) {
		init_mib();
		mib_init=1;
	}

	if (st==2) { /* walk */
		rootlen = MAX_NAME_LEN;
		if (strlen(objid)) { /* on a walk, an empty string means top of tree - no error */
			if (read_objid(objid, root, &rootlen)) {
				gotroot = 1;
			} else {
				php3_error(E_WARNING,"Invalid object identifier: %s\n", objid);
			}
		}
    	if (gotroot == 0) {
			memmove((char *)root, (char *)objid_mib, sizeof(objid_mib));
        	rootlen = sizeof(objid_mib) / sizeof(oid);
        	gotroot = 1;
    	}
	}

	memset(&session, 0, sizeof(struct snmp_session));
	session.peername = hostname;

	session.version = SNMP_VERSION_1;
	session.community = (u_char *)community;
	session.community_len = strlen((char *)community);
	session.retries = SNMP_DEFAULT_RETRIES;
	session.timeout = SNMP_DEFAULT_TIMEOUT;

	session.authenticator = NULL;
	snmp_synch_setup(&session);
	ss = snmp_open(&session);
	if (ss == NULL){
		php3_error(E_WARNING,"Couldn't open snmp\n");
		RETURN_FALSE;
	}
	if (st==2) {
		memmove((char *)name, (char *)root, rootlen * sizeof(oid));
		name_length = rootlen;
		/* prepare result array */
		array_init(return_value);	
	}

	while(keepwalking) {
		keepwalking=0;
		if (st==1) pdu = snmp_pdu_create(GET_REQ_MSG);
		else if (st==2) pdu = snmp_pdu_create(GETNEXT_REQ_MSG);

		if (st==1) {
			name_length = MAX_NAME_LEN;
			if (!read_objid(objid, name, &name_length)) {
				php3_error(E_WARNING,"Invalid object identifier: %s\n", objid);
				RETURN_FALSE;
			}
		}
		snmp_add_null_var(pdu, name, name_length);

retry:
		status = snmp_synch_response(ss, pdu, &response);
		if (status == STAT_SUCCESS) {
			if (response->errstat == SNMP_ERR_NOERROR) {
				for(vars = response->variables; vars; vars = vars->next_variable) {
					if (st==2 && (vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid))))
						continue;       /* not part of this subtree */

					sprint_value(buf,vars->name, vars->name_length, vars);
#if 0
					Debug("snmp response is: %s\n",buf);
#endif
					if (st==1) {
						RETVAL_STRING(buf,1);
					} else if (st==2) {
						/* Add to returned array */
						add_next_index_string(return_value,buf,1);
						if (vars->type != SNMP_ENDOFMIBVIEW && vars->type != SNMP_NOSUCHOBJECT && vars->type != SNMP_NOSUCHINSTANCE) {
							memmove((char *)name, (char *)vars->name,vars->name_length * sizeof(oid));
							name_length = vars->name_length;
							keepwalking = 1;
						}
					}
				}	
			} else {
				if (st!=2 || response->errstat != SNMP_ERR_NOSUCHNAME) {
					php3_error(E_WARNING,"Error in packet.\nReason: %s\n", snmp_errstring(response->errstat));
					if (response->errstat == SNMP_ERR_NOSUCHNAME) {
						for(count=1, vars = response->variables; vars && count != response->errindex;
							vars = vars->next_variable, count++);
						if (vars) sprint_objid(buf,vars->name, vars->name_length);
						php3_error(E_WARNING,"This name does not exist: %s\n",buf);
					}
					if (st==1) {
						if ((pdu = snmp_fix_pdu(response, GET_REQ_MSG)) != NULL) goto retry;
					} else if (st==2) {
						if ((pdu = snmp_fix_pdu(response, GETNEXT_REQ_MSG)) != NULL) goto retry;
					}
					RETURN_FALSE;
				}
			}
		} else if (status == STAT_TIMEOUT) {
			php3_error(E_WARNING,"No Response from %s\n", hostname);
			RETURN_FALSE;
		} else {    /* status == STAT_ERROR */
			php3_error(E_WARNING,"An error occurred, Quitting\n");
			RETURN_FALSE;
		}
		if (response) snmp_free_pdu(response);
	} /* keepwalking */
	snmp_close(ss);
}