search on fdb changed to binary search; added object tools of type "server-side command"
authorVictor Kirhenshtein <victor@netxms.org>
Thu, 27 Jan 2011 17:28:39 +0000 (17:28 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Thu, 27 Jan 2011 17:28:39 +0000 (17:28 +0000)
include/nms_cscp.h
include/nxtools.h
src/libnetxms/nxcp.cpp
src/libnxcl/server.cpp
src/server/core/fdb.cpp
src/server/core/session.cpp
src/server/include/nms_core.h
src/server/include/nms_topo.h

index e6ceea2..b7d5170 100644 (file)
@@ -364,7 +364,7 @@ typedef struct
 #define CMD_DEPLOY_AGENT_POLICY     0x008E
 #define CMD_LOG_DATA                0x008F
 #define CMD_GET_OBJECT_TOOL_DETAILS 0x0090
-//#define CMD_LOCK_OBJECT_TOOLS       0x0091
+#define CMD_EXECUTE_SERVER_COMMAND  0x0091
 //#define CMD_UNLOCK_OBJECT_TOOLS     0x0092
 #define CMD_UPDATE_OBJECT_TOOL      0x0093
 #define CMD_DELETE_OBJECT_TOOL      0x0094
index 2763a0a..3b460e0 100644 (file)
@@ -1,6 +1,6 @@
 /* 
 ** NetXMS - Network Management System
-** Copyright (C) 2003, 2004, 2005 Victor Kirhenshtein
+** Copyright (C) 2003-2011 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
 ** along with this program; if not, write to the Free Software
 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **
-** $module: nxtools.h
+** File: nxtools.h
 **
 **/
 
@@ -34,6 +34,7 @@
 #define TOOL_TYPE_TABLE_AGENT       3
 #define TOOL_TYPE_URL               4
 #define TOOL_TYPE_COMMAND           5
+#define TOOL_TYPE_SERVER_SCRIPT     6
 
 
 //
index ce04029..b96f375 100644 (file)
@@ -188,7 +188,7 @@ TCHAR LIBNETXMS_EXPORTABLE *NXCPMessageCodeName(WORD wCode, TCHAR *pszBuffer)
       _T("CMD_DEPLOY_AGENT_POLICY"),
       _T("CMD_LOG_DATA"),
       _T("CMD_GET_OBJECT_TOOL_DETAILS"),
-      _T("0x0091"),    // was: CMD_LOCK_OBJECT_TOOLS
+      _T("CMD_EXECUTE_SERVER_COMMAND"),
       _T("0x0092"),    // was: CMD_UNLOCK_OBJECT_TOOLS
       _T("CMD_UPDATE_OBJECT_TOOL"),
       _T("CMD_DELETE_OBJECT_TOOL"),
index 331cea7..d9617d4 100644 (file)
@@ -321,3 +321,27 @@ DWORD LIBNXCL_EXPORTABLE NXCSetServerConfigCLOB(NXC_SESSION hSession, const TCHA
    
    return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
 }
+
+
+//
+// Run server script for object
+//
+
+DWORD LIBNXCL_EXPORTABLE NXCExecuteServerCommand(NXC_SESSION hSession, DWORD nodeId, const TCHAR *command)
+{
+   CSCPMessage msg;
+   DWORD dwRqId;
+
+   dwRqId = ((NXCL_Session *)hSession)->CreateRqId();
+
+   // Build request message
+       msg.SetCode(CMD_EXECUTE_SERVER_COMMAND);
+   msg.SetId(dwRqId);
+       msg.SetVariable(VID_OBJECT_ID, name);
+       msg.SetVariable(VID_COMMAND, value);
+
+   // Send request
+   ((NXCL_Session *)hSession)->SendMsg(&msg);
+   
+   return ((NXCL_Session *)hSession)->WaitForRCC(dwRqId);
+}
index 2e52629..f9bdd7d 100644 (file)
@@ -114,12 +114,17 @@ void ForwardingDatabase::addEntry(FDB_ENTRY *entry)
 // Returns interface index or 0 if MAC address not found\r
 //\r
 \r
+static int EntryComparator(const void *p1, const void *p2)\r
+{\r
+       return memcmp(((FDB_ENTRY *)p1)->macAddr, ((FDB_ENTRY *)p2)->macAddr, MAC_ADDR_LENGTH);\r
+}\r
+\r
 DWORD ForwardingDatabase::findMacAddress(const BYTE *macAddr)\r
 {\r
-       for(int i = 0; i < m_fdbSize; i++)\r
-               if (!memcmp(macAddr, m_fdb[i].macAddr, MAC_ADDR_LENGTH))\r
-                       return m_fdb[i].ifIndex;\r
-       return 0;\r
+       FDB_ENTRY key;\r
+       memcpy(key.macAddr, macAddr, MAC_ADDR_LENGTH);\r
+       FDB_ENTRY *entry = (FDB_ENTRY *)bsearch(&key, m_fdb, m_fdbSize, sizeof(FDB_ENTRY), EntryComparator);\r
+       return (entry != NULL) ? entry->ifIndex : 0;\r
 }\r
 \r
 \r
@@ -142,6 +147,16 @@ bool ForwardingDatabase::isSingleMacOnPort(DWORD ifIndex)
 \r
 \r
 //\r
+// Sort FDB\r
+//\r
+\r
+void ForwardingDatabase::sort()\r
+{\r
+       qsort(m_fdb, m_fdbSize, sizeof(FDB_ENTRY), EntryComparator);\r
+}\r
+\r
+\r
+//\r
 // FDB walker's callback\r
 //\r
 \r
@@ -230,5 +245,6 @@ ForwardingDatabase *GetSwitchForwardingDatabase(Node *node)
        ForwardingDatabase *fdb = new ForwardingDatabase();\r
        node->CallSnmpEnumerate(_T(".1.3.6.1.2.1.17.1.4.1.1"), Dot1dPortTableHandler, fdb);\r
        node->CallSnmpEnumerate(_T(".1.3.6.1.2.1.17.4.3.1.1"), FDBHandler, fdb);\r
+       fdb->sort();\r
        return fdb;\r
 }\r
index e4c19c4..95eea18 100644 (file)
@@ -1,6 +1,6 @@
 /* 
 ** NetXMS - Network Management System
-** Copyright (C) 2003-2010 Victor Kirhenshtein
+** Copyright (C) 2003-2011 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
@@ -1218,6 +1218,9 @@ void ClientSession::ProcessingThread()
                        case CMD_LIST_IMAGES:
                                listLibraryImages(pMsg);
                                break;
+                       case CMD_EXECUTE_SERVER_COMMAND:
+                               executeServerCommand(pMsg);
+                               break;
          default:
             // Pass message to loaded modules
             for(i = 0; i < g_dwNumModules; i++)
@@ -10904,3 +10907,52 @@ void ClientSession::listLibraryImages(CSCPMessage *request)
        msg.SetVariable(VID_RCC, rcc);
        sendMessage(&msg);
 }
+
+
+//
+// Execute server side command on object
+//
+
+static THREAD_RESULT THREAD_CALL RunCommand(void *arg)
+{
+       _tsystem((TCHAR *)arg);
+       free(arg);
+       return THREAD_OK;
+}
+
+void ClientSession::executeServerCommand(CSCPMessage *request)
+{
+       CSCPMessage msg;
+
+       msg.SetId(request->GetId());
+       msg.SetCode(CMD_REQUEST_COMPLETED);
+
+       DWORD nodeId = request->GetVariableLong(VID_OBJECT_ID);
+       NetObj *object = FindObjectById(nodeId);
+       if (object != NULL)
+       {
+               if (object->CheckAccessRights(m_dwUserId, OBJECT_ACCESS_CONTROL))
+               {
+                       if (object->Type() == OBJECT_NODE)
+                       {
+                               TCHAR *cmd = request->GetVariableStr(VID_COMMAND);
+                               ThreadCreate(RunCommand, 0, cmd);
+                               msg.SetVariable(VID_RCC, RCC_SUCCESS);
+                       }
+                       else
+                       {
+                               msg.SetVariable(VID_RCC, RCC_INCOMPATIBLE_OPERATION);
+                       }
+               }
+               else
+               {
+                       msg.SetVariable(VID_RCC, RCC_ACCESS_DENIED);
+               }
+       }
+       else
+       {
+               msg.SetVariable(VID_RCC, RCC_INVALID_OBJECT_ID);
+       }
+
+       sendMessage(&msg);
+}
index 62d0832..a58fe04 100644 (file)
@@ -558,6 +558,7 @@ private:
        void updateLibraryImage(CSCPMessage *request);
        void listLibraryImages(CSCPMessage *request);
        void deleteLibraryImage(CSCPMessage *request);
+       void executeServerCommand(CSCPMessage *request);
 
 public:
    ClientSession(SOCKET hSocket, DWORD dwHostAddr);
index 189235a..bfc15b5 100644 (file)
@@ -91,6 +91,7 @@ public:
 
        void addEntry(FDB_ENTRY *entry);
        void addPortMapping(PORT_MAPPING_ENTRY *entry);
+       void sort();
 
        void incRefCount() { m_refCount++; }
        void decRefCount();