[AGENT++] how to remove not longer needed rows from a table while update()?

Claus Klein claus.klein at arcormail.de
Sun Nov 7 22:38:55 CET 2010


Hi,

i am testing with a simple MIB table which shows the content of the / 
etc/hosts file.

While updating this table, it may happens, that one or more rows have  
to be deleted
because the are not longer at the file.

This is how i try to do it:

void netSnmpHostsEntry::update(Request* req) {
     // This table needs to be updated.
     //--AgentGen BEGIN=netSnmpHostsEntry::update
     static unsigned long currentRequest = 0;

     if(currentRequest == req->get_request_id()) {
         return;
     }
     currentRequest = req->get_request_id();

     LOG_BEGIN(EVENT_LOG | 1);
     LOG("netSnmpHostsEntry::update");
     LOG_END;

     start_synch();
     std::set<std::string> hostsSet;
     {
         loopctx->filep = fopen(HOSTS_FILE, "r");
         if(!loopctx->filep) {
             perror("fopen: " HOSTS_FILE);
         } else {
             while(loopctx->filep) {

                 // . . .

                 if(strlen(loopctx->hostname)) {
                     MibTableRow* row =  
add_row(Oidx::from_string(loopctx->hostname));
                     if(row) {
                         row->get_nth(nNetSnmpHostAddressType)- 
 >replace_value(new NS_SNMP SnmpInt32(loopctx- 
 >theaddr.get_ip_version() + 1));
                         row->get_nth(nNetSnmpHostAddress)- 
 >replace_value(loopctx->theaddr.clone_as_hex());
                         row->get_nth(nNetSnmpHostRowStatus)- 
 >replace_value(new NS_SNMP SnmpInt32(rowActive));

                         hostsSet.insert(std::string(loopctx- 
 >hostname));    // mark this row as found at the host file
                     }
                 } else {
                     loopctx->current_ptr = NULL;    // end of line!
                 }
             }   // end while
         } /* we're out of data */

         // Now, check if each row is still at the hosts file
         OidListCursor<MibTableRow> cur;

         for(cur.init(&content); cur.get(); cur.next()) {
             Oidx index = cur.get()->get_index();
             OctetStr hostName = index.as_string();  //HACK: this  
returns ".localhost"! ck
             std::string name(hostName.get_printable_clear() + 1);   // 
HACK: get off the '.' char

             if(hostsSet.end() == hostsSet.find(name)) {
                 // if not found in set, the current index (hostname)  
is not longer at the hosts file!
                 remove_row(index);
                 break;  // FIXME: we can't continue with that  
iterator! ck		<<<<<<<<<<<?
             }

         }
     }
     end_synch();
     //--AgentGen END
}

________________

I have to questions:
1.) The cur loop iterater crashes if I continue after remove_row(). Is  
there a better way to do it?
2.) I would like to find a simpler way to get the hostname as string  
from row index.

With Regards,

Claus







More information about the AGENTPP mailing list