Simple example ?
Corrado Giacomini
c.giacomini____acresearch.com
Fri Oct 6 16:34:09 CEST 2000
Hello All,
I have recently downloaded AGENT++ I have read the the documentation
and had a look in the agent examples
but still I ham a little bit confused about how things should be done.
Does anybody has a small example of an agent implementing a table which
has row added through a set ?
attached you will find what I have currently wrote, is still in
progress..
-corrado
-------------- next part --------------
#include <Customer.h>
//////////////////////////////////////////////////////////////////////////////////////
//
// customerId
//
customerId::customerId(const Oidx& oid) :
MibLeaf(oid, READCREATE, new SnmpInt32())
{
}
customerId::~customerId()
{
}
MibEntryPtr
customerId::clone()
{
MibEntryPtr other = new customerId(oid);
((customerId*)other)->replace_value(value->clone());
((customerId*)other)->set_reference_to_table(my_table);
return other;
}
boolean
customerId::value_ok(const Vbx& vb)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerName
//
customerName::customerName(const Oidx& oid) :
MibLeaf(oid, READCREATE, new OctetStr(""))
{
}
customerName::~customerName()
{
}
MibEntryPtr
customerName::clone()
{
MibEntryPtr other = new customerName(oid);
((customerName*)other)->replace_value(value->clone());
((customerName*)other)->set_reference_to_table(my_table);
return other;
}
void
customerName::get_request(Request* req, int ind)
{
// place instrumentation code (manipulating "value") here
MibLeaf::get_request(req, ind);
}
boolean
customerName::value_ok(const Vbx& vb)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerSurname
//
customerSurname::customerSurname(const Oidx& oid) :
MibLeaf(oid, READCREATE, new OctetStr())
{
}
customerSurname::~customerSurname()
{
}
MibEntryPtr
customerSurname::clone()
{
MibEntryPtr other = new customerSurname(oid);
((customerSurname*)other)->replace_value(value->clone());
((customerSurname*)other)->set_reference_to_table(my_table);
return other;
}
boolean
customerSurname::value_ok(const Vbx&)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerAddress
//
customerAddress::customerAddress(const Oidx&) :
MibLeaf(oid, READCREATE, new OctetStr())
{
}
customerAddress::~customerAddress()
{
}
MibEntryPtr
customerAddress::clone()
{
MibEntryPtr other = new customerAddress(oid);
((customerAddress*)other)->replace_value(value->clone());
((customerAddress*)other)->set_reference_to_table(my_table);
return other;
}
boolean
customerAddress::value_ok(const Vbx&)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerPhone
//
customerPhone::customerPhone(const Oidx&) :
MibLeaf(oid, READCREATE, new OctetStr())
{
}
customerPhone::~customerPhone()
{
}
MibEntryPtr
customerPhone::clone()
{
MibEntryPtr other = new customerPhone(oid);
((customerPhone*)other)->replace_value(value->clone());
((customerPhone*)other)->set_reference_to_table(my_table);
return other;
}
boolean
customerPhone::value_ok(const Vbx&)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerRowStatus
//
customerRowStatus::customerRowStatus(const Oidx& id) :
snmpRowStatus(id, READCREATE)
{
}
customerRowStatus::~customerRowStatus()
{
}
boolean
customerRowStatus::value_ok(const Vbx&)
{
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////
//
// customerTableEntry
//
customerTableEntry* customerTableEntry::instance = 0;
customerTableEntry::customerTableEntry():
MibTable(oidCustomerTableEntry)
{
// This table object is a singleton. In order to access it use
// the static pointer customerTableEntry::instance.
instance = this;
add_col(new customerId (colCustomerId));
add_col(new customerName (colCustomerName));
add_col(new customerSurname (colCustomerSurname));
add_col(new customerAddress (colCustomerAddress));
add_col(new customerPhone (colCustomerPhone));
add_col(new customerRowStatus (colCustomerRowStatus));
//
// add one row just to see something at the beginin
//
add_entry(1001,"Pippo","Mc Pippis","Main Street n1","0493331234");
add_entry(1002,"Minni","DeMinni", "Main Street n2","0391380477");
}
customerTableEntry::~customerTableEntry()
{
}
void
customerTableEntry::set_row(MibTableRow* row, int customerId, char* customerName,
char* customerSurname, char* customerAddress,
char* customerPhone)
{
row->get_nth(0)->replace_value(new SnmpInt32(customerId));
row->get_nth(0)->replace_value(new OctetStr(customerName));
row->get_nth(1)->replace_value(new OctetStr(customerSurname));
row->get_nth(2)->replace_value(new OctetStr(customerAddress));
row->get_nth(3)->replace_value(new OctetStr(customerPhone));
}
void
customerTableEntry::row_delete(MibTableRow* row, const Oidx& index)
{
// The row 'row' with 'index' will be deleted.
// Place any necessary actions here.
}
MibTableRow *
customerTableEntry::add_entry (int customerId, char* customerName,
char* customerSurname, char* customerAddress,
char* customerPhone)
{
Oidx index; // create Index
index = customerId;
if(contains(index)) { // check if we already have row with that index
// error
return 0;
}
else {
LOG_BEGIN(EVENT_LOG | 1);
LOG("adding row....");
LOG(index.get_printable());
LOG_END;
// Add the row with specified index and set row values
MibTableRow *row = add_row(index);
set_row(row, customerId, customerName, customerSurname,
customerAddress,customerPhone);
return row;
}
}
boolean
customerTableEntry::contains(Oidx index)
{
OidListCursor<MibTableRow> cur;
for (cur.init(&content); cur.get(); cur.next()) {
if (strcmp(cur.get()->get_index().get_printable(),
index.get_printable()) == 0) {
return TRUE;
}
}
return FALSE;
}
void
customerTableEntry::get_request(Request* req, int ind)
{
}
int
customerTableEntry::commit_set_request(Request* req, int ind)
{
return SNMP_ERROR_SUCCESS;
}
int
customerTableEntry::prepare_set_request(Request* req, int& ind)
{
return SNMP_ERROR_SUCCESS;
}
int
customerTableEntry::undo_set_request(Request* req, int& ind)
{
return SNMP_ERROR_SUCCESS;
}
void
customerTableEntry::cleanup_set_request(Request* req, int& ind)
{
}
//////////////////////////////////////////////////////////////////////////////////////
//
//
//
customerMib::customerMib() : MibGroup(oidCustomerMibGroup, "CustomerMibGroup")
{
add(new customerTableEntry());
}
customerMib::~customerMib()
{
}
-------------- next part --------------
#ifndef _Customer_H_
#define _Customer_H_
#include <mib.h>
#include <snmp_textual_conventions.h>
#include <notification_originator.h>
#include <log.h>
#include <sim_mib.h>
#define oidCustomerMibGroup "1.3.6.1.2.1.6915.1"
#define oidCustomerTable "1.3.6.1.2.1.6915.1.1"
#define oidCustomerTableEntry "1.3.6.1.2.1.6915.1.1.1"
#define oidCustomerId "1.3.6.1.2.1.6915.1.1.1.1"
#define colCustomerId "1"
#define oidCustomerName "1.3.6.1.2.1.6915.1.1.1.2"
#define colCustomerName "2"
#define oidCustomerSurname "1.3.6.1.2.1.6915.1.1.1.3"
#define colCustomerSurname "3"
#define oidCustomerAddress "1.3.6.1.2.1.6915.1.1.1.4"
#define colCustomerAddress "4"
#define oidCustomerPhone "1.3.6.1.2.1.6915.1.1.1.5"
#define colCustomerPhone "5"
#define oidCustomerRowStatus "1.3.6.1.2.1.6915.1.1.1.6"
#define colCustomerRowStatus "6"
class customerId : public MibLeaf {
public:
customerId(const Oidx&);
virtual ~customerId();
virtual MibEntryPtr clone();
virtual boolean value_ok(const Vbx&);
};
class customerName : public MibLeaf {
public:
customerName(const Oidx&);
virtual ~customerName();
virtual MibEntryPtr clone();
virtual void get_request(Request*, int);
virtual boolean value_ok(const Vbx&);
};
class customerSurname : public MibLeaf {
public:
customerSurname(const Oidx&);
virtual ~customerSurname();
virtual MibEntryPtr clone();
virtual boolean value_ok(const Vbx&);
};
class customerAddress : public MibLeaf {
public:
customerAddress(const Oidx&);
virtual ~customerAddress();
virtual MibEntryPtr clone();
virtual boolean value_ok(const Vbx&);
};
class customerPhone : public MibLeaf {
public:
customerPhone(const Oidx&);
virtual ~customerPhone();
virtual MibEntryPtr clone();
virtual boolean value_ok(const Vbx&);
};
class customerRowStatus : public snmpRowStatus {
public:
customerRowStatus(const Oidx&);
virtual ~customerRowStatus();
virtual boolean value_ok(const Vbx&);
};
class customerTableEntry : public MibTable {
public:
customerTableEntry();
virtual ~customerTableEntry();
static customerTableEntry* instance;
virtual boolean contains(Oidx index);
virtual void set_row( MibTableRow* r,
int p0, //customerId
char* p1, //Name
char* p2, //Surname
char* p3, //Address
char* p4); //Phone //rowStatus
virtual MibTableRow* add_entry (int p0, //Id
char* p1, //Name
char* p2, //Surname
char* p3, //Address
char* p4); //Phone
virtual void row_delete(MibTableRow* row, const Oidx& index) ;
int prepare_set_request(Request* req, int& ind);
int undo_set_request(Request* req, int& ind);
void cleanup_set_request(Request* req, int& ind);
int commit_set_request(Request* req, int ind);
void get_request(Request* req, int ind);
};
class customerMib: public MibGroup
{
public:
customerMib();
virtual ~customerMib();
};
#endif
-------------- next part --------------
# Linux
CFLAGS = -g -Wall
# Digital Unix
#CFLAGS = -g -pthreads
# Solaris
#CFLAGS = -g -mt
CLINK = -c -o
CC = cc
# Linux
CPP = g++
# Digital Unix
#CPP = cxx
# Solaris
#CPP = CC
RM = rm -f
INCLUDE = -I. -I../../../include \
-I../../../../snmp++/include -I../../../../snmp++/src
SNMPLIBPATH = ../../../../snmp++/lib
AGENTLIBPATH = ../../../lib
RSAEUROLIBPATH = ../../../../rsaeuro/install/unix
# Digital Unix 3.2 and 4.0
#LIB = -L$(AGENTLIBPATH) -L$(SNMPLIBPATH) -L$(RSAEUROLIBPATH) \
# -lagent++ -lsnmp++ -lrsaeuro -lpthread
# SOLARIS 2.6
#LIB = -L$(AGENTLIBPATH) -L$(SNMPLIBPATH) -L$(RSAEUROLIBPATH) \
# -lagent++ -lsnmp++ -lrsaeuro -lsocket -lnsl
# Linux
LIB = -L$(AGENTLIBPATH) -L$(SNMPLIBPATH) -L$(RSAEUROLIBPATH) \
-lpthread -lagent++ -lsnmp++ -lrsaeuro #-lg++
AGENTOBJ = Customer.o agent.o
EXECUTABLE = agent
HEADERS = ../include/Customer.h
all: $(EXECUTABLE)
clean:
$(RM) *.o *~ ../include/*~ $(EXECUTABLE)
clobber: clean
$(RM) $(EXECUTABLE)
#compile rules
.SUFFIXES: .cpp
$(EXECUTABLE): $(AGENTOBJ) $(SNMPLIBPATH)/libsnmp++.a \
$(AGENTLIBPATH)/libagent++.a
$(CPP) $(CFLAGS) -o $@ $(AGENTOBJ) $(LIB)
%.o: %.cpp $(HEADERS)
$(RM) $@
$(CPP) $(CFLAGS) $(CLINK) $@ $(INCLUDE) $<
-------------- next part --------------
#include <stdlib.h>
#include <signal.h>
#include "agent++.h"
#include "log.h"
#include "snmp_group.h"
#include "system_group.h"
#include "snmp_target_mib.h"
#include "snmp_notification_mib.h"
#include "snmp_community_mib.h"
#include "notification_originator.h"
#include "oid_def.h"
#include "Customer.h"
#include "agentpp_simulation_mib.h"
// globals:
u_short port;
Mib* mib;
RequestList* reqList;
static void sig(int signo)
{
if ((signo == SIGTERM) || (signo == SIGINT) ||
(signo == SIGSEGV)) {
printf ("\n");
switch (signo) {
case SIGSEGV: {
printf ("Segmentation fault, aborting.\n");
exit(1);
}
case SIGTERM:
case SIGINT: {
delete mib;
printf ("User abort\n");
exit(0);
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Function : init_signals
//
// Description :
//
void init_signals()
{
//
// Setup signal handler for SIGTERM SIGINT SIGSEGV
//
signal (SIGTERM, sig);
signal (SIGINT, sig);
signal (SIGSEGV, sig);
}
////////////////////////////////////////////////////////////////////////////////
//
// Function : init_mib
//
// Description :
//
void init_mib(Mib& mib)
{
//mib.add(new snmpGroup());
mib.add(new customerMib());
//mib.add(new snmp_notification_mib()); // this is need to send Traps
}
////////////////////////////////////////////////////////////////////////////////
//
// Function : main
//
// Description :
//
int main (int argc, char* argv[])
{
//
// very simply get the port number as argument if any
//
if( argc > 1 )
port = atoi(argv[1]);
else
port = 4700;
#ifndef _NO_LOGGING
DefaultLog::log->set_filter(ERROR_LOG, 5);
DefaultLog::log->set_filter(WARNING_LOG, 5);
DefaultLog::log->set_filter(EVENT_LOG, 5);
DefaultLog::log->set_filter(INFO_LOG, 5);
DefaultLog::log->set_filter(DEBUG_LOG, 6);
#endif
//
// Open snmpsession with specified port number
//
int status;
Snmpx snmp(status, port);
if (status == SNMP_CLASS_SUCCESS) {
LOG_BEGIN(EVENT_LOG | 1);
LOG("main: SNMP listen port");
LOG(port);
LOG_END;
}
else {
LOG_BEGIN(ERROR_LOG | 0);
LOG("main: SNMP port init failed");
LOG(status);
LOG_END;
exit(1);
}
//
// Create the agent MIB
// Call Mib initialization functions
// load persitent objects from disk
// add supported objects
//
mib = new Mib();
mib->init();
init_mib(*mib);
//
// register signal handler for some signals
//
init_signals();
//
// register the RequestList to be used for answering request
// ( requestList for outgoing requests )
//
reqList = new RequestList();
mib->set_request_list(reqList);
//
// Set the Snmpx session to be used for incoming requests
//
reqList->set_snmp(&snmp);
/*
Vbx* vbs = 0;
coldStartOid coldOid;
NotificationOriginator no;
UdpAddress dest("127.0.0.1/162");
no.add_v1_trap_destination(dest);
no.generate(vbs, 0, coldOid, "", "");
*/
Request* req;
for (;;) {
//
// Wait a given time (in secs) for an incoming request
//
req = reqList->receive(120);
if (req) {
mib->process_request(req); // process the request
}
else {
mib->cleanup(); // cleanup the MIB
}
}
return 0;
}
More information about the AGENTPP
mailing list