Skip to content

Instantly share code, notes, and snippets.

@dan82840
Last active September 28, 2018 09:04
Show Gist options
  • Save dan82840/9ff98febf5423ca4033cb82d3e2798fd to your computer and use it in GitHub Desktop.
Save dan82840/9ff98febf5423ca4033cb82d3e2798fd to your computer and use it in GitHub Desktop.
-- Test-MIB.my
Test-MIB DEFINITIONS ::= BEGIN
IMPORTS
OBJECT-GROUP, MODULE-COMPLIANCE, NOTIFICATION-GROUP
FROM SNMPv2-CONF
enterprises, Integer32, Unsigned32, OBJECT-TYPE, MODULE-IDENTITY,
NOTIFICATION-TYPE
FROM SNMPv2-SMI
DisplayString
FROM SNMPv2-TC;
-- October 09, 2002 at 14:50 GMT
-- 1.3.6.1.4.1.16535
Test MODULE-IDENTITY
LAST-UPDATED "200210091450Z" -- October 09, 2002 at 14:50 GMT
ORGANIZATION
""
CONTACT-INFO
""
DESCRIPTION
"Video's Server MIB."
::= { enterprises 16535 }
-- Node definitions
-- This part will include all details about the Test.
-- 1.3.6.1.4.1.16535.1
Time OBJECT IDENTIFIER ::= { Test 1 }
-- 1.3.6.1.4.1.16535.1.1
GetTime OBJECT-TYPE
SYNTAX DisplayString (SIZE (0..100))
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"Example : 2013/4/11"
::= { Time 1 }
END
-- Test-MIB.my
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "Test.h"
#include <time.h>
/** Initializes the Test module */
void
init_Test(void)
{
oid GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 16535, 1, 1 };
DEBUGMSGTL(("Test", "Initializing\n"));
netsnmp_register_scalar(netsnmp_create_handler_registration
("GetTime", handle_GetTime, GetTime_oid,
OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY));
}
int
handle_GetTime(netsnmp_mib_handler *handler,
netsnmp_handler_registration *reginfo,
netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests)
{
/*
* We are never called for a GETNEXT if it's registered as a
* "instance", as it's "magically" handled for us.
*/
/*
* a instance handler also only hands us one request at a time, so
* we don't need to loop over a list of requests; we'll only get one.
*/
time_t t;
switch (reqinfo->mode) {
case MODE_GET:
time(&t);
char szTime[100];
snprintf(szTime, sizeof(szTime), "%s", ctime(&t));
snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR,
/*
* XXX: a pointer to the scalar's data
*/ (unsigned char *)szTime,
/*
* XXX: the length of the data in bytes
*/ strlen(szTime));
break;
default:
/*
* we should never get here, so this is a really bad error
*/
snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n",
reqinfo->mode);
return SNMP_ERR_GENERR;
}
return SNMP_ERR_NOERROR;
}
static int keep_running = 1;
RETSIGTYPE stop_server(int __attribute__((unused)) a) {
keep_running = 0;
}
int main()
{
int agentx_subagent=1; /* change this if you want to be a SNMP master agent */
int background = 0; /* change this if you want to run in the background */
int syslog = 1; /* change this if you want to use syslog */
const char *app_name = "Test";
/* print log errors to syslog or stderr */
if (syslog)
snmp_enable_calllog();
else
snmp_enable_stderrlog();
/* we're an agentx subagent? */
if (agentx_subagent) {
/* make us a agentx client. */
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
}
/* run in background, if requested */
if (background && netsnmp_daemonize(1, !syslog)) {
exit(1);
}
/* initialize tcpip, if necessary */
SOCK_STARTUP;
/* initialize the agent library */
init_agent(app_name);
/* initialize your mib code here */
init_Test();
/* initialize vacm/usm access control */
//if (!agentx_subagent) {
// init_vacm_vars();
// init_usmUser();
//}
/* Test will be used to read Test.conf files. */
init_snmp("Test");
/* If we're going to be a snmp master agent, initial the ports */
if (!agentx_subagent) {
init_master_agent(); /* open the port to listen on (defaults to udp:161) */
}
/* In case we recevie a request to stop (kill -TERM or kill -INT) */
keep_running = 1;
while (keep_running) {
/* if you use select(), see snmp_select_info() in snmp_api(3) */
/* --- OR --- */
agent_check_and_process(1);/* block every 1 second */
}
/* at shutdown time */
snmp_shutdown(app_name);
/* deinitialize your mib code here */
/* shutdown the agent library */
//shutdown_agent();
SOCK_CLEANUP;
return 0;
}
/*
* Note: this file originally auto-generated by mib2c using
* $
*/
#ifndef TEST_H
#define TEST_H
/*
* function declarations
*/
void init_Test(void);
Netsnmp_Node_Handler handle_GetTime;
#endif /* TEST_H */
#!/bin/sh
#
gcc -fno-strict-aliasing -g -O2 -Ulinux -Dlinux=linux -D_REENTRANT \
-D_GNU_SOURCE -fno-strict-aliasing -pipe -Wdeclaration-after-statement \
-I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 \
-I/usr/include/gdbm -I/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/CORE -I. \
-I/usr/local/include -o Test Test.c -L/usr/local/lib -lnetsnmp -lnetsnmpmibs -lnetsnmpagent \
-Wl,-E -Wl,-rpath,/usr/lib64/perl5/5.8.8/x86_64-linux-thread-multi/CORE -lnetsnmp -lcrypto
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment