Added possibility to process SNMP traps by modules
authorVictor Kirhenshtein <victor@netxms.org>
Thu, 10 Mar 2005 09:35:56 +0000 (09:35 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Thu, 10 Mar 2005 09:35:56 +0000 (09:35 +0000)
src/server/core/snmptrap.cpp
src/server/include/nxmodule.h

index 99d81e4..65eb36b 100644 (file)
@@ -227,23 +227,33 @@ static void ProcessTrap(SNMP_PDU *pdu, struct sockaddr_in *pOrigin)
       }
       else     // Process unmatched traps
       {
-         // Build trap's parameters string
-         dwBufSize = pdu->GetNumVariables() * 4096 + 16;
-         pszTrapArgs = (TCHAR *)malloc(sizeof(TCHAR) * dwBufSize);
-         pszTrapArgs[0] = 0;
-         for(i = 0, dwBufPos = 0; i < pdu->GetNumVariables(); i++)
+         // Pass trap to loaded modules
+         for(i = 0; i < g_dwNumModules; i++)
+            if (g_pModuleList[i].pfTrapHandler != NULL)
+               if (g_pModuleList[i].pfTrapHandler(pdu, pNode))
+                  break;   // Message was processed by the module
+
+         // Handle unprocessed traps
+         if (i == g_dwNumModules)
          {
-            pVar = pdu->GetVariable(i);
-            dwBufPos += _sntprintf(&pszTrapArgs[dwBufPos], dwBufSize - dwBufPos, _T("%s%s == '%s'"),
-                                   (i == 0) ? _T("") : _T("; "),
-                                   pVar->GetName()->GetValueAsText(), 
-                                   pVar->GetValueAsString(szBuffer, 512));
-         }
+            // Build trap's parameters string
+            dwBufSize = pdu->GetNumVariables() * 4096 + 16;
+            pszTrapArgs = (TCHAR *)malloc(sizeof(TCHAR) * dwBufSize);
+            pszTrapArgs[0] = 0;
+            for(i = 0, dwBufPos = 0; i < pdu->GetNumVariables(); i++)
+            {
+               pVar = pdu->GetVariable(i);
+               dwBufPos += _sntprintf(&pszTrapArgs[dwBufPos], dwBufSize - dwBufPos, _T("%s%s == '%s'"),
+                                      (i == 0) ? _T("") : _T("; "),
+                                      pVar->GetName()->GetValueAsText(), 
+                                      pVar->GetValueAsString(szBuffer, 512));
+            }
 
-         // Generate default event for unmatched traps
-         PostEvent(EVENT_SNMP_UNMATCHED_TRAP, pNode->Id(), "ss", 
-                   pdu->GetTrapId()->GetValueAsText(), pszTrapArgs);
-         free(pszTrapArgs);
+            // Generate default event for unmatched traps
+            PostEvent(EVENT_SNMP_UNMATCHED_TRAP, pNode->Id(), "ss", 
+                      pdu->GetTrapId()->GetValueAsText(), pszTrapArgs);
+            free(pszTrapArgs);
+         }
       }
       MutexUnlock(m_mutexTrapCfgAccess);
    }
index 4cb30a1..5e6d700 100644 (file)
 
 
 //
-// Forward declaration of client session class
+// Forward declaration of server classes
 //
 
 class ClientSession;
+class Node;
 
 
 //
@@ -50,6 +51,7 @@ typedef struct
    DWORD dwFlags;
    void (* pfMain)(void);  // Pointer to module's main()
    BOOL (* pfCommandHandler)(DWORD dwCommand, CSCPMessage *pMsg, ClientSession *pSession);
+   BOOL (* pfTrapHandler)(SNMP_PDU *pdu, Node *pNode);
    HMODULE hModule;
 } NXMODULE;