intermediate commit - containers save/load broken !!!!
authorVictor Kirhenshtein <victor@netxms.org>
Mon, 23 Apr 2012 22:25:18 +0000 (22:25 +0000)
committerVictor Kirhenshtein <victor@netxms.org>
Mon, 23 Apr 2012 22:25:18 +0000 (22:25 +0000)
include/netxmsdb.h
include/nxclapi.h
include/nxevent.h
sql/events.in
sql/schema.in
src/server/core/template.cpp
src/server/include/nms_events.h
src/server/include/nms_objects.h
src/server/tools/nxdbmgr/nxdbmgr.h
src/server/tools/nxdbmgr/upgrade.cpp

index ac363ca..a04af10 100644 (file)
@@ -23,6 +23,6 @@
 #ifndef _netxmsdb_h
 #define _netxmsdb_h
 
-#define DB_FORMAT_VERSION   252
+#define DB_FORMAT_VERSION   253
 
 #endif
index a7d585b..8cf9da4 100644 (file)
@@ -1,7 +1,7 @@
 /* 
 ** NetXMS - Network Management System
 ** Client Library API
-** Copyright (C) 2003-2011 Victor Kirhenshtein
+** Copyright (C) 2003-2012 Victor Kirhenshtein
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU Lesser General Public License as published by
@@ -251,6 +251,20 @@ typedef void * NXC_SESSION;
 
 
 //
+// Template flags
+//
+
+#define TF_AUTO_APPLY            0x00000001
+
+
+//
+// Container flags
+//
+
+#define CF_AUTO_BIND             0x00000001
+
+
+//
 // Interface flags
 //
 
index fe8a584..b47192a 100644 (file)
 #define EVENT_8021X_AUTH_TIMEOUT          61
 #define EVENT_INTERFACE_UNEXPECTED_UP     62
 #define EVENT_INTERFACE_EXPECTED_DOWN     63
+#define EVENT_CONTAINER_AUTOBIND          64
+#define EVENT_CONTAINER_AUTOUNBIND        65
+#define EVENT_TEMPLATE_AUTOAPPLY          66
+#define EVENT_TEMPLATE_AUTOREMOVE         67
 
 #define EVENT_SNMP_UNMATCHED_TRAP         500
 #define EVENT_SNMP_COLD_START             501
index 9a9ffb6..06ebdc9 100644 (file)
@@ -705,6 +705,54 @@ INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)
                '   1) Interface index#0D#0A' CONCAT
                '   2) Interface name'
        );
+INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES
+       (
+               EVENT_CONTAINER_AUTOBIND, 'SYS_CONTAINER_AUTOBIND',
+               EVENT_SEVERITY_NORMAL, 1,
+               'Node %2 automatically bound to container %4',
+               'Generated when node bound to container object by autobind rule.#0D#0A' CONCAT
+               'Parameters:#0D#0A' CONCAT
+               '   1) Node ID#0D#0A' CONCAT
+               '   2) Node name#0D#0A' CONCAT
+               '   3) Container ID#0D#0A' CONCAT
+               '   4) Container name'
+       );
+INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES
+       (
+               EVENT_CONTAINER_AUTOUNBIND, 'SYS_CONTAINER_AUTOUNBIND',
+               EVENT_SEVERITY_NORMAL, 1,
+               'Node %2 automatically unbound from container %4',
+               'Generated when node unbound from container object by autobind rule.#0D#0A' CONCAT
+               'Parameters:#0D#0A' CONCAT
+               '   1) Node ID#0D#0A' CONCAT
+               '   2) Node name#0D#0A' CONCAT
+               '   3) Container ID#0D#0A' CONCAT
+               '   4) Container name'
+       );
+INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES
+       (
+               EVENT_TEMPLATE_AUTOAPPLY, 'SYS_TEMPLATE_AUTOAPPLY',
+               EVENT_SEVERITY_NORMAL, 1,
+               'Template %4 automatically applied to node %2',
+               'Generated when template applied to node by autoapply rule.#0D#0A' CONCAT
+               'Parameters:#0D#0A' CONCAT
+               '   1) Node ID#0D#0A' CONCAT
+               '   2) Node name#0D#0A' CONCAT
+               '   3) Template ID#0D#0A' CONCAT
+               '   4) Template name'
+       );
+INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES
+       (
+               EVENT_TEMPLATE_AUTOREMOVE, 'SYS_TEMPLATE_AUTOREMOVE',
+               EVENT_SEVERITY_NORMAL, 1,
+               'Template %4 automatically removed from node %2',
+               'Generated when template removed from node by autoapply rule.#0D#0A' CONCAT
+               'Parameters:#0D#0A' CONCAT
+               '   1) Node ID#0D#0A' CONCAT
+               '   2) Node name#0D#0A' CONCAT
+               '   3) Template ID#0D#0A' CONCAT
+               '   4) Template name'
+       );
 
 
 /*
index 1fe5652..39b3289 100644 (file)
@@ -381,7 +381,7 @@ CREATE TABLE containers
        id integer not null,
        category integer not null,
        object_class integer not null,
-       enable_auto_bind integer not null,
+       flags integer not null,
        auto_bind_filter SQL_TEXT not null,
        PRIMARY KEY(id)
 ) TABLE_TYPE;
@@ -428,7 +428,7 @@ CREATE TABLE templates
 (
        id integer not null,
        version integer not null,
-       enable_auto_apply integer not null,
+       flags integer not null,
        apply_filter SQL_TEXT not null,
        PRIMARY KEY(id)
 ) TABLE_TYPE;
index 2b263b6..a60da6b 100644 (file)
@@ -41,6 +41,7 @@ Template::Template() : NetObj()
 {
        m_dcObjects = new ObjectArray<DCObject>(8, 16, true);
    m_dwDCILockStatus = INVALID_INDEX;
+       m_flags = 0;
    m_dwVersion = 0x00010000;  // Initial version is 1.0
        m_applyFilter = NULL;
        m_applyFilterSource = NULL;
@@ -58,6 +59,7 @@ Template::Template(const TCHAR *pszName) : NetObj()
    nx_strncpy(m_szName, pszName, MAX_OBJECT_NAME);
        m_dcObjects = new ObjectArray<DCObject>(8, 16, true);
    m_dwDCILockStatus = INVALID_INDEX;
+       m_flags = 0;
    m_dwVersion = 0x00010000;  // Initial version is 1.0
        m_applyFilter = NULL;
        m_applyFilterSource = NULL;
@@ -81,11 +83,13 @@ Template::Template(ConfigEntry *config) : NetObj()
        // Name and version
        nx_strncpy(m_szName, config->getSubEntryValue(_T("name"), 0, _T("Unnamed Template")), MAX_OBJECT_NAME);
        m_dwVersion = config->getSubEntryValueUInt(_T("version"), 0, 0x00010000);
+       m_flags = config->getSubEntryValueUInt(_T("flags"), 0, 0);
 
        // Auto-apply filter
        m_applyFilter = NULL;
        m_applyFilterSource = NULL;
-       setAutoApplyFilter(config->getSubEntryValue(_T("filter")));
+       if (m_flags & TF_AUTO_APPLY)
+               setAutoApplyFilter(config->getSubEntryValue(_T("filter")));
 
        // Data collection
        m_dcObjects = new ObjectArray<DCObject>(8, 16, true);
@@ -170,7 +174,7 @@ BOOL Template::CreateFromDB(DWORD dwId)
    if (!loadCommonProperties())
       return FALSE;
 
-   _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("SELECT version,enable_auto_apply,apply_filter FROM templates WHERE id=%d"), dwId);
+   _sntprintf(szQuery, sizeof(szQuery) / sizeof(TCHAR), _T("SELECT version,flags,apply_filter FROM templates WHERE id=%d"), dwId);
    hResult = DBSelect(g_hCoreDB, szQuery);
    if (hResult == NULL)
       return FALSE;     // Query failed
@@ -183,14 +187,14 @@ BOOL Template::CreateFromDB(DWORD dwId)
    }
 
    m_dwVersion = DBGetFieldULong(hResult, 0, 0);
-       if (DBGetFieldLong(hResult, 0, 1))
+       m_flags = DBGetFieldULong(hResult, 0, 1);
+       if (m_flags & TF_AUTO_APPLY)
        {
                m_applyFilterSource = DBGetField(hResult, 0, 2, NULL, 0);
                if (m_applyFilterSource != NULL)
                {
                        TCHAR error[256];
 
-                       DecodeSQLString(m_applyFilterSource);
                        m_applyFilter = NXSLCompile(m_applyFilterSource, error, 256);
                        if (m_applyFilter == NULL)
                                nxlog_write(MSG_TEMPLATE_SCRIPT_COMPILATION_ERROR, EVENTLOG_WARNING_TYPE, "dss", m_dwId, m_szName, error);
@@ -250,51 +254,60 @@ BOOL Template::CreateFromDB(DWORD dwId)
 
 BOOL Template::SaveToDB(DB_HANDLE hdb)
 {
-   TCHAR *pszQuery, *pszEscScript, query2[256];
-   DWORD i, len;
-
-   // Lock object's access
    LockData();
 
-   saveCommonProperties(hdb);
+   if (!saveCommonProperties(hdb))
+       {
+               UnlockData();
+               return FALSE;
+       }
 
-   // Form and execute INSERT or UPDATE query
-       pszEscScript = (m_applyFilterSource != NULL) ? EncodeSQLString(m_applyFilterSource) : _tcsdup(_T("#00"));
-       len = (DWORD)_tcslen(pszEscScript) + 256;
-       pszQuery = (TCHAR *)malloc(sizeof(TCHAR) * len);
+       DB_STATEMENT hStmt;
    if (IsDatabaseRecordExist(hdb, _T("templates"), _T("id"), m_dwId))
        {
-      _sntprintf(pszQuery, len, _T("UPDATE templates SET version=%d,enable_auto_apply=%d,apply_filter='%s' WHERE id=%d"),
-                 m_dwVersion, (m_applyFilterSource != NULL) ? 1 : 0, pszEscScript, m_dwId);
+               hStmt = DBPrepare(hdb, _T("UPDATE templates SET version=?,flags=?,apply_filter=? WHERE id=?"));
        }
    else
        {
-      _sntprintf(pszQuery, len, _T("INSERT INTO templates (id,version,enable_auto_apply,apply_filter) VALUES (%d,%d,%d,'%s')"),
-                          m_dwId, m_dwVersion, (m_applyFilterSource != NULL) ? 1 : 0, pszEscScript);
+               hStmt = DBPrepare(hdb, _T("INSERT INTO templates (version,flags,apply_filter,id) VALUES (?,?,?,?)"));
        }
-       free(pszEscScript);
-   DBQuery(hdb, pszQuery);
-       free(pszQuery);
-
-   // Update members list
-   _sntprintf(query2, 256, _T("DELETE FROM dct_node_map WHERE template_id=%d"), m_dwId);
-   DBQuery(hdb, query2);
-   LockChildList(FALSE);
-   for(i = 0; i < m_dwChildCount; i++)
-   {
-      _sntprintf(query2, 256, _T("INSERT INTO dct_node_map (template_id,node_id) VALUES (%d,%d)"), m_dwId, m_pChildList[i]->Id());
-      DBQuery(hdb, query2);
-   }
-   UnlockChildList();
+       if (hStmt == NULL)
+       {
+               UnlockData();
+               return FALSE;
+       }
+
+       DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, m_dwVersion);
+       DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, m_flags);
+       DBBind(hStmt, 3, DB_SQLTYPE_TEXT, m_applyFilterSource, DB_BIND_STATIC);
+       DBBind(hStmt, 4, DB_SQLTYPE_INTEGER, m_dwId);
+       BOOL success = DBExecute(hStmt);
+       DBFreeStatement(hStmt);
+
+       if (success)
+       {
+               TCHAR query[256];
+
+               // Update members list
+               _sntprintf(query, 256, _T("DELETE FROM dct_node_map WHERE template_id=%d"), m_dwId);
+               DBQuery(hdb, query);
+               LockChildList(FALSE);
+               for(DWORD i = 0; i < m_dwChildCount; i++)
+               {
+                       _sntprintf(query, 256, _T("INSERT INTO dct_node_map (template_id,node_id) VALUES (%d,%d)"), m_dwId, m_pChildList[i]->Id());
+                       DBQuery(hdb, query);
+               }
+               UnlockChildList();
 
-   // Save access list
-   saveACLToDB(hdb);
+               // Save access list
+               saveACLToDB(hdb);
+       }
 
    UnlockData();
 
    // Save data collection items
        lockDciAccess();
-   for(i = 0; i < (DWORD)m_dcObjects->size(); i++)
+   for(int i = 0; i < m_dcObjects->size(); i++)
       m_dcObjects->get(i)->saveToDB(hdb);
        unlockDciAccess();
 
@@ -303,7 +316,7 @@ BOOL Template::SaveToDB(DB_HANDLE hdb)
    m_bIsModified = FALSE;
        UnlockData();
 
-   return TRUE;
+   return success;
 }
 
 
@@ -749,7 +762,7 @@ void Template::CreateMessage(CSCPMessage *pMsg)
 {
    NetObj::CreateMessage(pMsg);
    pMsg->SetVariable(VID_TEMPLATE_VERSION, m_dwVersion);
-       pMsg->SetVariable(VID_AUTO_APPLY, (WORD)((m_applyFilterSource != NULL) ? 1 : 0));
+       pMsg->SetVariable(VID_FLAGS, m_flags);
        pMsg->SetVariable(VID_APPLY_FILTER, CHECK_NULL_EX(m_applyFilterSource));
 }
 
@@ -767,31 +780,27 @@ DWORD Template::ModifyFromMessage(CSCPMessage *pRequest, BOOL bAlreadyLocked)
    if (pRequest->IsVariableExist(VID_TEMPLATE_VERSION))
       m_dwVersion = pRequest->GetVariableLong(VID_TEMPLATE_VERSION);
 
+   // Change flags
+   if (pRequest->IsVariableExist(VID_FLAGS))
+               m_flags = pRequest->GetVariableLong(VID_FLAGS);
+
    // Change apply filter
-       if (pRequest->IsVariableExist(VID_AUTO_APPLY))
+       if (pRequest->IsVariableExist(VID_APPLY_FILTER))
        {
-               if (pRequest->GetVariableShort(VID_AUTO_APPLY))
+               safe_free(m_applyFilterSource);
+               delete m_applyFilter;
+               m_applyFilterSource = pRequest->GetVariableStr(VID_APPLY_FILTER);
+               if (m_applyFilterSource != NULL)
                {
-                       safe_free(m_applyFilterSource);
-                       delete m_applyFilter;
-                       m_applyFilterSource = pRequest->GetVariableStr(VID_APPLY_FILTER);
-                       if (m_applyFilterSource != NULL)
-                       {
-                               TCHAR error[256];
+                       TCHAR error[256];
 
-                               m_applyFilter = NXSLCompile(m_applyFilterSource, error, 256);
-                               if (m_applyFilter == NULL)
-                                       nxlog_write(MSG_TEMPLATE_SCRIPT_COMPILATION_ERROR, EVENTLOG_WARNING_TYPE, "dss", m_dwId, m_szName, error);
-                       }
-                       else
-                       {
-                               m_applyFilter = NULL;
-                       }
+                       m_applyFilter = NXSLCompile(m_applyFilterSource, error, 256);
+                       if (m_applyFilter == NULL)
+                               nxlog_write(MSG_TEMPLATE_SCRIPT_COMPILATION_ERROR, EVENTLOG_WARNING_TYPE, "dss", m_dwId, m_szName, error);
                }
                else
                {
-                       delete_and_null(m_applyFilter);
-                       safe_free_and_null(m_applyFilterSource);
+                       m_applyFilter = NULL;
                }
        }
 
@@ -927,8 +936,8 @@ DWORD *Template::getDCIEventsList(DWORD *pdwCount)
 
 void Template::CreateNXMPRecord(String &str)
 {
-   str.addFormattedString(_T("\t\t<template id=\"%d\">\n\t\t\t<name>%s</name>\n\t\t\t<dataCollection>\n"),
-                              m_dwId, (const TCHAR *)EscapeStringForXML2(m_szName));
+   str.addFormattedString(_T("\t\t<template id=\"%d\">\n\t\t\t<name>%s</name>\n\t\t\t<flags>%s</flags>\n\t\t\t<dataCollection>\n"),
+                              m_dwId, m_flags, (const TCHAR *)EscapeStringForXML2(m_szName));
 
    lockDciAccess();
    for(int i = 0; i < m_dcObjects->size(); i++)
@@ -1114,7 +1123,7 @@ BOOL Template::isApplicable(Node *node)
        BOOL result = FALSE;
 
        LockData();
-       if (m_applyFilter != NULL)
+       if ((m_flags & TF_AUTO_APPLY) && (m_applyFilter != NULL))
        {
                pEnv = new NXSL_ServerEnv;
                m_applyFilter->setGlobalVariable(_T("$node"), new NXSL_Value(new NXSL_Object(&g_nxslNodeClass, node)));
index d46f83f..9f32a56 100644 (file)
@@ -1,6 +1,6 @@
 /* 
 ** NetXMS - Network Management System
-** Copyright (C) 2003-2009 Victor Kirhenshtein
+** Copyright (C) 2003-2012 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
index 2e80a09..a63a01e 100644 (file)
@@ -464,6 +464,7 @@ protected:
        ObjectArray<DCObject> *m_dcObjects;
    DWORD m_dwDCILockStatus;
    DWORD m_dwVersion;
+       DWORD m_flags;
    BOOL m_bDCIListModified;
    TCHAR m_szCurrDCIOwner[MAX_SESSION_NAME];
        TCHAR *m_applyFilterSource;
@@ -484,10 +485,10 @@ public:
        Template(ConfigEntry *config);
    virtual ~Template();
 
-   virtual int Type(void) { return OBJECT_TEMPLATE; }
+   virtual int Type() { return OBJECT_TEMPLATE; }
 
    virtual BOOL SaveToDB(DB_HANDLE hdb);
-   virtual BOOL DeleteFromDB(void);
+   virtual BOOL DeleteFromDB();
    virtual BOOL CreateFromDB(DWORD dwId);
 
    virtual void CreateMessage(CSCPMessage *pMsg);
@@ -1198,6 +1199,7 @@ private:
    DWORD m_dwChildIdListSize;
 
 protected:
+       DWORD m_flags;
    DWORD m_dwCategory;
        NXSL_Program *m_bindFilter;
        TCHAR *m_bindFilterSource;
index a1d92c3..6271b29 100644 (file)
@@ -1,6 +1,6 @@
 /* 
 ** nxdbmgr - NetXMS database manager
-** Copyright (C) 2004-2009 Victor Kirhenshtein
+** Copyright (C) 2004-2012 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
index 859b168..dfc36bd 100644 (file)
@@ -279,6 +279,71 @@ static BOOL CreateEventTemplate(int code, const TCHAR *name, int severity, int f
 
 
 //
+// Upgrade from V252 to V253
+//
+
+static BOOL H_UpgradeFromV252(int currVersion, int newVersion)
+{
+       static TCHAR batch[] = 
+               _T("ALTER TABLE templates ADD flags integer\n")
+               _T("UPDATE templates SET flags=enable_auto_apply\n")
+               _T("ALTER TABLE templates DROP COLUMN enable_auto_apply\n")
+               _T("ALTER TABLE containers ADD flags integer\n")
+               _T("UPDATE containers SET flags=enable_auto_bind\n")
+               _T("ALTER TABLE containers DROP COLUMN enable_auto_bind\n")
+               _T("<END>");
+
+       CHK_EXEC(SQLBatch(batch));
+
+       CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOBIND, _T("SYS_CONTAINER_AUTOBIND"), EVENT_SEVERITY_NORMAL, 1,
+                                    _T("Node %2 automatically bound to container %4"), 
+                                                                                 _T("Generated when node bound to container object by autobind rule.\r\n")
+                                    _T("Parameters:#\r\n")
+                                                                                 _T("   1) Node ID\r\n")
+                                                                                 _T("   2) Node name\r\n")
+                                                                                 _T("   3) Container ID\r\n")
+                                                                                 _T("   4) Container name")
+                                                                                 ));
+
+       CHK_EXEC(CreateEventTemplate(EVENT_CONTAINER_AUTOUNBIND, _T("SYS_CONTAINER_AUTOUNBIND"), EVENT_SEVERITY_NORMAL, 1,
+                                    _T("Node %2 automatically unbound from container %4"), 
+                                                                                 _T("Generated when node unbound from container object by autobind rule.\r\n")
+                                    _T("Parameters:#\r\n")
+                                                                                 _T("   1) Node ID\r\n")
+                                                                                 _T("   2) Node name\r\n")
+                                                                                 _T("   3) Container ID\r\n")
+                                                                                 _T("   4) Container name")
+                                                                                 ));
+
+       CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOAPPLY, _T("SYS_TEMPLATE_AUTOAPPLY"), EVENT_SEVERITY_NORMAL, 1,
+                                    _T("Template %4 automatically applied to node %2"), 
+                                                                                 _T("Generated when template applied to node by autoapply rule.\r\n")
+                                    _T("Parameters:#\r\n")
+                                                                                 _T("   1) Node ID\r\n")
+                                                                                 _T("   2) Node name\r\n")
+                                                                                 _T("   3) Template ID\r\n")
+                                                                                 _T("   4) Template name")
+                                                                                 ));
+
+       CHK_EXEC(CreateEventTemplate(EVENT_TEMPLATE_AUTOREMOVE, _T("SYS_TEMPLATE_AUTOREMOVE"), EVENT_SEVERITY_NORMAL, 1,
+                                    _T("Template %4 automatically removed from node %2"), 
+                                                                                 _T("Generated when template removed from node by autoapply rule.\r\n")
+                                    _T("Parameters:#\r\n")
+                                                                                 _T("   1) Node ID\r\n")
+                                                                                 _T("   2) Node name\r\n")
+                                                                                 _T("   3) Template ID\r\n")
+                                                                                 _T("   4) Template name")
+                                                                                 ));
+
+       CHK_EXEC(ConvertStrings(_T("templates"), _T("id"), _T("apply_filter")));
+       CHK_EXEC(ConvertStrings(_T("containers"), _T("id"), _T("auto_bind_filter")));
+
+       CHK_EXEC(SQLQuery(_T("UPDATE metadata SET var_value='253' WHERE var_name='SchemaVersion'")));
+   return TRUE;
+}
+
+
+//
 // Upgrade from V251 to V252
 //
 
@@ -6241,6 +6306,7 @@ static struct
        { 249, 250, H_UpgradeFromV249 },
        { 250, 251, H_UpgradeFromV250 },
        { 251, 252, H_UpgradeFromV251 },
+       { 252, 253, H_UpgradeFromV252 },
    { 0, 0, NULL }
 };