"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/server/tools/nxdbmgr/upgrade_v22.cpp" (23 Feb 2021, 48729 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "upgrade_v22.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** nxdbmgr - NetXMS database manager
    3 ** Copyright (C) 2004-2019 Victor Kirhenshtein
    4 **
    5 ** This program is free software; you can redistribute it and/or modify
    6 ** it under the terms of the GNU General Public License as published by
    7 ** the Free Software Foundation; either version 2 of the License, or
    8 ** (at your option) any later version.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13 ** GNU General Public License for more details.
   14 **
   15 ** You should have received a copy of the GNU General Public License
   16 ** along with this program; if not, write to the Free Software
   17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18 **
   19 ** File: upgrade_v22.cpp
   20 **
   21 **/
   22 
   23 #include "nxdbmgr.h"
   24 #include <nxevent.h>
   25 
   26 /**
   27  * Upgrade from 22.59 to 30.0
   28  */
   29 static bool H_UpgradeFromV59()
   30 {
   31    CHK_EXEC(SetMajorSchemaVersion(30, 0));
   32    return true;
   33 }
   34 
   35 /**
   36  * Upgrade 22.58 to 22.59
   37  */
   38 static bool H_UpgradeFromV58()
   39 {
   40    CHK_EXEC(SQLQuery(_T("UPDATE config_values SET var_description='AUTO' WHERE var_name='DefaultInterfaceExpectedState' AND var_value='1'")));
   41    CHK_EXEC(SetMinorSchemaVersion(59));
   42    return true;
   43 }
   44 
   45 /**
   46  * Upgrade 22.57 to 22.58
   47  */
   48 static bool H_UpgradeFromV57()
   49 {
   50    CHK_EXEC(DBResizeColumn(g_dbHandle, _T("items"), _T("instance"), 1023, true));
   51    CHK_EXEC(DBResizeColumn(g_dbHandle, _T("items"), _T("instd_data"), 1023, true));
   52    CHK_EXEC(DBResizeColumn(g_dbHandle, _T("dc_tables"), _T("instance"), 1023, true));
   53    CHK_EXEC(DBResizeColumn(g_dbHandle, _T("dc_tables"), _T("instd_data"), 1023, true));
   54    CHK_EXEC(SetMinorSchemaVersion(58));
   55    return true;
   56 }
   57 
   58 /**
   59  * Upgrade 22.56 to 22.57
   60  */
   61 static bool H_UpgradeFromV56()
   62 {
   63    CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='AlarmListDisplayLimit'")));
   64    CHK_EXEC(SetMinorSchemaVersion(57));
   65    return true;
   66 }
   67 
   68 /**
   69  * Upgrade 22.55 to 22.56
   70  */
   71 static bool H_UpgradeFromV55()
   72 {
   73    CHK_EXEC(CreateTable(
   74       _T("CREATE TABLE zone_proxies (")
   75       _T("   object_id integer not null,")
   76       _T("   proxy_node integer not null,")
   77       _T("PRIMARY KEY(object_id,proxy_node))")));
   78 
   79    DB_RESULT hResult = SQLSelect(_T("SELECT id,proxy_node FROM zones"));
   80    if (hResult != NULL)
   81    {
   82       int count = DBGetNumRows(hResult);
   83       if (count > 0)
   84       {
   85          DB_STATEMENT hStmt = DBPrepare(g_dbHandle, _T("INSERT INTO zone_proxies (object_id,proxy_node) VALUES (?,?)"));
   86          if (hStmt != NULL)
   87          {
   88             for(int i = 0; i < count; i++)
   89             {
   90                DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 0));
   91                DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, DBGetFieldULong(hResult, i, 1));
   92                if (!SQLExecute(hStmt) && !g_ignoreErrors)
   93                {
   94                   DBFreeStatement(hStmt);
   95                   DBFreeResult(hResult);
   96                   return false;
   97                }
   98             }
   99             DBFreeStatement(hStmt);
  100          }
  101          else if (!g_ignoreErrors)
  102          {
  103             DBFreeResult(hResult);
  104             return false;
  105          }
  106       }
  107       DBFreeResult(hResult);
  108    }
  109    else if (!g_ignoreErrors)
  110    {
  111       return false;
  112    }
  113 
  114    CHK_EXEC(DBDropColumn(g_dbHandle, _T("zones"), _T("proxy_node")));
  115    CHK_EXEC(SetMinorSchemaVersion(56));
  116    return true;
  117 }
  118 
  119 /**
  120  * Upgrade 22.54 to 22.55
  121  */
  122 static bool H_UpgradeFromV54()
  123 {
  124    CHK_EXEC(SQLQuery(
  125             _T("INSERT INTO script_library (guid,script_id,script_name,script_code) ")
  126             _T("VALUES ('4ec1a7bc-d46f-4df3-b846-e9dfd66571dc',19,'Hook::CreateSubnet',")
  127             _T("'/* Available global variables:\r\n *  $node - current node, object of ''Node'' class\r\n")
  128             _T(" *  $1 - current subnet, object of ''Subnet'' class\r\n")
  129             _T(" *\r\n * Expected return value:\r\n")
  130             _T(" *  true/false - boolean - whether subnet should be created\r\n")
  131             _T(" */\r\nreturn true;\r\n')")
  132          ));
  133 
  134    CHK_EXEC(SetMinorSchemaVersion(55));
  135    return true;
  136 }
  137 
  138 /**
  139  * Upgrade from 22.53 to 22.54
  140  */
  141 static bool H_UpgradeFromV53()
  142 {
  143    CHK_EXEC(SQLQuery(_T("ALTER TABLE dct_threshold_instances ADD instance_id integer")));
  144 
  145    DB_RESULT hResult = SQLSelect(_T("SELECT threshold_id,instance,maint_copy FROM dct_threshold_instances"));
  146    if (hResult != NULL)
  147    {
  148       int count = DBGetNumRows(hResult);
  149       if (count > 0)
  150       {
  151          DB_STATEMENT hStmt = DBPrepare(g_dbHandle, _T("UPDATE dct_threshold_instances SET instance_id=? WHERE threshold_id=? AND instance=? AND maint_copy=?"));
  152          if (hStmt != NULL)
  153          {
  154             TCHAR instance[256];
  155             UINT32 instanceId = 1;
  156             for(int i = 0; i < count; i++)
  157             {
  158                UINT32 thresholdId = DBGetFieldULong(hResult, i, 0);
  159                DBGetField(hResult, i, 1, instance, 256);
  160                int maintCopy = DBGetFieldLong(hResult, i, 2);
  161 
  162                DBBind(hStmt, 1, DB_SQLTYPE_INTEGER, instanceId++);
  163                DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, thresholdId);
  164                DBBind(hStmt, 3, DB_SQLTYPE_VARCHAR, instance, DB_BIND_STATIC);
  165                DBBind(hStmt, 4, DB_SQLTYPE_VARCHAR, maintCopy ? _T("1") : _T("0"), DB_BIND_STATIC);
  166                if (!SQLExecute(hStmt) && !g_ignoreErrors)
  167                {
  168                   DBFreeStatement(hStmt);
  169                   DBFreeResult(hResult);
  170                   return false;
  171                }
  172             }
  173             DBFreeStatement(hStmt);
  174          }
  175          else if (!g_ignoreErrors)
  176          {
  177             DBFreeResult(hResult);
  178             return false;
  179          }
  180       }
  181       DBFreeResult(hResult);
  182    }
  183    else if (!g_ignoreErrors)
  184    {
  185       return false;
  186    }
  187 
  188    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("dct_threshold_instances"), _T("instance_id")));
  189    CHK_EXEC(DBDropPrimaryKey(g_dbHandle, _T("dct_threshold_instances")));
  190    CHK_EXEC(DBAddPrimaryKey(g_dbHandle, _T("dct_threshold_instances"), _T("threshold_id,instance_id")));
  191 
  192    CHK_EXEC(SetMinorSchemaVersion(54));
  193    return true;
  194 }
  195 
  196 /**
  197  * Upgrade from 22.52 to 22.53
  198  */
  199 static bool H_UpgradeFromV52()
  200 {
  201    CHK_EXEC(DBDropColumn(g_dbHandle, _T("config"), _T("possible_values")));
  202    CHK_EXEC(SetMinorSchemaVersion(53));
  203    return true;
  204 }
  205 
  206 /**
  207  * Upgrade from 22.51 to 22.52
  208  */
  209 static bool H_UpgradeFromV51()
  210 {
  211    CHK_EXEC(CreateTable(
  212       _T("CREATE TABLE node_components (")
  213       _T("   node_id integer not null,")
  214       _T("   component_index integer not null,")
  215       _T("   parent_index integer not null,")
  216       _T("   position integer not null,")
  217       _T("   component_class integer not null,")
  218       _T("   if_index integer not null,")
  219       _T("   name varchar(255) null,")
  220       _T("   description varchar(255) null,")
  221       _T("   model varchar(255) null,")
  222       _T("   serial_number varchar(63) null,")
  223       _T("   vendor varchar(63) null,")
  224       _T("   firmware varchar(127) null,")
  225       _T("PRIMARY KEY(node_id,component_index))")));
  226 
  227    CHK_EXEC(SetMinorSchemaVersion(52));
  228    return true;
  229 }
  230 
  231 /**
  232  * Upgrade from 22.50 to 22.51
  233  */
  234 static bool H_UpgradeFromV50()
  235 {
  236    CHK_EXEC(SQLQuery(_T("ALTER TABLE thresholds ADD last_checked_value varchar(255)")));
  237    CHK_EXEC(SetMinorSchemaVersion(51));
  238    return true;
  239 }
  240 
  241 /**
  242  * Upgrade from 22.49 to 22.50
  243  */
  244 static bool H_UpgradeFromV49()
  245 {
  246    CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description='Generated when node, cluster, or mobile device entered maintenance mode.\r\nParameters:\r\n   1) Comments' WHERE guid='5f6c8b1c-f162-413e-8028-80e7ad2c362d'")));
  247    CHK_EXEC(SetMinorSchemaVersion(50));
  248    return true;
  249 }
  250 
  251 /**
  252  * Upgrade from 22.48 to 22.49
  253  */
  254 static bool H_UpgradeFromV48()
  255 {
  256    DB_RESULT hResult = SQLSelect(_T("SELECT system_access FROM user_groups WHERE id=-2147483647"));
  257    if (hResult != NULL)
  258    {
  259       if (DBGetNumRows(hResult) != 0)
  260       {
  261          UINT64 accessRights = DBGetFieldUInt64(hResult, 0, 0);
  262          accessRights |= SYSTEM_ACCESS_IMPORT_CONFIGURATION;
  263 
  264          TCHAR query[256];
  265          _sntprintf(query, 256, _T("UPDATE user_groups SET system_access=") UINT64_FMT _T(" WHERE id=-2147483647"), accessRights);
  266          CHK_EXEC(SQLQuery(query));
  267       }
  268       DBFreeResult(hResult);
  269    }
  270 
  271    CHK_EXEC(SetMinorSchemaVersion(49));
  272    return true;
  273 }
  274 
  275 /**
  276  * Upgrade from 22.47 to 22.48
  277  */
  278 static bool H_UpgradeFromV47()
  279 {
  280    static const TCHAR *batch =
  281       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('ImportConfigurationOnStartup','0','Never')\n")
  282       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('ImportConfigurationOnStartup','1','Only missing elements')\n")
  283       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('ImportConfigurationOnStartup','2','Always')\n")
  284       _T("UPDATE config SET data_type='C' WHERE var_name='ImportConfigurationOnStartup'\n")
  285       _T("<END>");
  286    CHK_EXEC(SQLBatch(batch));
  287    CHK_EXEC(SetMinorSchemaVersion(48));
  288    return true;
  289 }
  290 
  291 /**
  292  * Upgrade from 22.46 to 22.47
  293  */
  294 static bool H_UpgradeFromV46()
  295 {
  296    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='Topology.AdHocRequest.ExpirationTime',description='Ad-hoc network topology request expiration time. Server will use cached result of previous request if it is newer than given interval.' WHERE var_name='TopologyExpirationTime'")));
  297    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='Topology.DefaultDiscoveryRadius',default_value='5',description='Default number of hops from seed node to be added to topology map.' WHERE var_name='TopologyDiscoveryRadius'")));
  298    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='Topology.PollingInterval' WHERE var_name='TopologyPollingInterval'")));
  299    CHK_EXEC(SetMinorSchemaVersion(47));
  300    return true;
  301 }
  302 
  303 /**
  304  * Upgrade from 22.45 to 22.46
  305  */
  306 static bool H_UpgradeFromV45()
  307 {
  308    CHK_EXEC(CreateTable(
  309       _T("CREATE TABLE idata (")
  310       _T("   node_id integer not null,")
  311       _T("   item_id integer not null,")
  312       _T("   idata_timestamp integer not null,")
  313       _T("   idata_value varchar(255) null,")
  314       _T("   raw_value varchar(255) null,")
  315       _T("PRIMARY KEY(node_id,item_id,idata_timestamp))")));
  316 
  317    CHK_EXEC(CreateTable(
  318       _T("CREATE TABLE tdata (")
  319       _T("   node_id integer not null,")
  320       _T("   item_id integer not null,")
  321       _T("   tdata_timestamp integer not null,")
  322       _T("   tdata_value $SQL:TEXT null,")
  323       _T("PRIMARY KEY(node_id,item_id,tdata_timestamp))")));
  324 
  325    if (g_dbSyntax == DB_SYNTAX_TSDB)
  326    {
  327       CHK_EXEC(SQLQuery(_T("SELECT create_hypertable('idata', 'idata_timestamp', 'node_id', chunk_time_interval => 604800, number_partitions => 100, migrate_data => true)")));
  328       CHK_EXEC(SQLQuery(_T("SELECT create_hypertable('tdata', 'tdata_timestamp', 'node_id', chunk_time_interval => 604800, number_partitions => 100, migrate_data => true)")));
  329    }
  330 
  331    CHK_EXEC(SQLQuery(_T("INSERT INTO metadata (var_name,var_value) VALUES ('SingeTablePerfData','0')")));
  332 
  333    CHK_EXEC(SetMinorSchemaVersion(46));
  334    return true;
  335 }
  336 
  337 /**
  338  * Upgrade from 22.44 to 22.45
  339  */
  340 static bool H_UpgradeFromV44()
  341 {
  342    CHK_EXEC(CreateConfigParam(_T("Alarms.ResolveExpirationTime"), _T("0"),
  343             _T("Expiration time (in seconds) for resolved alarms. If set to non-zero, resolved and untouched alarms will be terminated automatically after given timeout."),
  344             _T("seconds"), 'I', true, false, false, false));
  345    CHK_EXEC(SetMinorSchemaVersion(45));
  346    return true;
  347 }
  348 
  349 /**
  350  * Upgrade from 22.43 to 22.44
  351  */
  352 static bool H_UpgradeFromV43()
  353 {
  354    CHK_EXEC(CreateConfigParam(_T("NetworkDiscovery.MergeDuplicateNodes"), _T("0"),
  355             _T("Enable/disable merge of duplicate nodes. When enabled, configuration of duplicate node(s) will be merged into original node and duplicate(s) will be deleted."),
  356             NULL, 'B', true, false, false, false));
  357    CHK_EXEC(SetMinorSchemaVersion(44));
  358    return true;
  359 }
  360 
  361 /**
  362  * Upgrade from 22.42 to 22.43
  363  */
  364 static bool H_UpgradeFromV42()
  365 {
  366    CHK_EXEC(CreateConfigParam(_T("NetworkDiscovery.EnableParallelProcessing"), _T("0"), _T("Enable/disable parallel processing of discovered addresses."), NULL, 'B', true, false, false, false));
  367    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Discovery.BaseSize"), _T("1"), _T("Base size for network discovery thread pool."), NULL, 'I', true, true, false, false));
  368    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Discovery.MaxSize"), _T("16"), _T("Maximum size for network discovery thread pool."), NULL, 'I', true, true, false, false));
  369    CHK_EXEC(SetMinorSchemaVersion(43));
  370    return true;
  371 }
  372 
  373 /**
  374  * Upgrade from 22.41 to 22.42
  375  */
  376 static bool H_UpgradeFromV41()
  377 {
  378    CHK_EXEC(DBDropPrimaryKey(g_dbHandle, _T("address_lists")));
  379 
  380    static const TCHAR *batch =
  381       _T("ALTER TABLE address_lists ADD zone_uin integer\n")
  382       _T("ALTER TABLE address_lists ADD proxy_id integer\n")
  383       _T("UPDATE address_lists SET zone_uin=0,proxy_id=0\n")
  384       _T("<END>");
  385    CHK_EXEC(SQLBatch(batch));
  386 
  387    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("address_lists"), _T("proxy_id")));
  388    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("address_lists"), _T("zone_uin")));
  389    CHK_EXEC(DBAddPrimaryKey(g_dbHandle, _T("address_lists"), _T("list_type,community_id,zone_uin,addr_type,addr1,addr2")));
  390 
  391    CHK_EXEC(SetMinorSchemaVersion(42));
  392    return true;
  393 }
  394 
  395 /**
  396  * Upgrade from 22.40 to 22.41
  397  */
  398 static bool H_UpgradeFromV40()
  399 {
  400    CHK_EXEC(DBRenameColumn(g_dbHandle, _T("dct_threshold_instances"), _T("row_number"), _T("tt_row_number")));
  401    CHK_EXEC(SetMinorSchemaVersion(41));
  402    return true;
  403 }
  404 
  405 /**
  406  * Upgrade from 22.39 to 22.40
  407  */
  408 static bool H_UpgradeFromV39()
  409 {
  410    static const TCHAR *batch =
  411       _T("ALTER TABLE items ADD grace_period_start integer\n")
  412       _T("ALTER TABLE dc_tables ADD grace_period_start integer\n")
  413       _T("UPDATE items SET grace_period_start=0\n")
  414       _T("UPDATE dc_tables SET grace_period_start=0\n")
  415       _T("<END>");
  416    CHK_EXEC(SQLBatch(batch));
  417 
  418    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("items"), _T("grace_period_start")));
  419    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("dc_tables"), _T("grace_period_start")));
  420 
  421    CHK_EXEC(SetMinorSchemaVersion(40));
  422    return true;
  423 }
  424 
  425 /**
  426  * Upgrade from 22.38 to 22.39
  427  */
  428 static bool H_UpgradeFromV38()
  429 {
  430    TCHAR tmp[MAX_CONFIG_VALUE] = _T("");
  431    DB_RESULT hResult = DBSelect(g_dbHandle, _T("SELECT var_value from config WHERE var_name='LdapMappingName'"));
  432    if (hResult != NULL)
  433    {
  434       if(DBGetNumRows(hResult) > 0)
  435          DBGetField(hResult, 0, 0, tmp, MAX_CONFIG_VALUE);
  436    }
  437 
  438    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='LdapUserMappingName' WHERE var_name='LdapMappingName'")));
  439    CHK_EXEC(CreateConfigParam(_T("LdapGroupMappingName"), _T(""), _T("The name of an attribute whose value will be used as a group''s login name."), NULL, 'S', true, false, false, false));
  440 
  441    DB_STATEMENT hStmt = DBPrepare(g_dbHandle, _T("UPDATE config SET var_value=? WHERE var_name='LdapGroupMappingName'"));
  442    if (hStmt != NULL)
  443    {
  444       DBBind(hStmt, 1, DB_SQLTYPE_TEXT, tmp, DB_BIND_STATIC);
  445       CHK_EXEC(SQLExecute(hStmt));
  446       DBFreeStatement(hStmt);
  447    }
  448    else
  449    {
  450       if (!g_ignoreErrors)
  451          return false;
  452    }
  453 
  454    CHK_EXEC(SetMinorSchemaVersion(39));
  455    return true;
  456 }
  457 
  458 /**
  459  * Upgrade from 22.37 to 22.38
  460  */
  461 static bool H_UpgradeFromV37()
  462 {
  463    CHK_EXEC(CreateEventTemplate(EVENT_SERVER_STARTED, _T("SYS_SERVER_STARTED"), SEVERITY_NORMAL, EF_LOG, _T("32f3305b-1c1b-4597-9eb5-b74eca54330d"),
  464             _T("Server started"),
  465             _T("Generated when server initialization is completed.")
  466             ));
  467    CHK_EXEC(SetMinorSchemaVersion(38));
  468    return true;
  469 }
  470 
  471 /**
  472  * Upgrade from 22.36 to 22.37
  473  */
  474 static bool H_UpgradeFromV36()
  475 {
  476    // This schema upgrade only needed to indicate change of node flag meaning
  477    // NF_IS_SMCLP replaced by NF_SNMP_SETTINGS_LOCKED
  478    CHK_EXEC(SetMinorSchemaVersion(37));
  479    return true;
  480 }
  481 
  482 /**
  483  * Upgrade from 22.35 to 22.36
  484  */
  485 static bool H_UpgradeFromV35()
  486 {
  487    static const TCHAR *batch =
  488       _T("UPDATE object_properties SET state_before_maint=0 WHERE state_before_maint IS NULL\n")
  489       _T("UPDATE nodes SET port_rows=0 WHERE port_rows IS NULL\n")
  490       _T("UPDATE nodes SET port_numbering_scheme=0 WHERE port_numbering_scheme IS NULL\n")
  491       _T("UPDATE dct_threshold_instances SET row_number=0 WHERE row_number IS NULL\n")
  492       _T("<END>");
  493    CHK_EXEC(SQLBatch(batch));
  494 
  495    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("object_properties"), _T("state_before_maint")));
  496    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("nodes"), _T("port_rows")));
  497    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("nodes"), _T("port_numbering_scheme")));
  498    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("dct_threshold_instances"), _T("row_number")));
  499 
  500    CHK_EXEC(SetMinorSchemaVersion(36));
  501    return true;
  502 }
  503 
  504 /**
  505  * Upgrade from 22.34 to 22.35
  506  */
  507 static bool H_UpgradeFromV34()
  508 {
  509    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='AgentTunnels.ListenPort',default_value='4703',description='TCP port number to listen on for incoming agent tunnel connections.' WHERE var_name='AgentTunnelListenPort'")));
  510    CHK_EXEC(CreateConfigParam(_T("AgentTunnels.ListenPort"), _T("4703"), _T("TCP port number to listen on for incoming agent tunnel connections."), NULL, 'I', true, true, false, false));
  511    CHK_EXEC(CreateConfigParam(_T("Events.Correlation.TopologyBased"), _T("1"), _T("Enable/disable topology based event correlation."), NULL, 'B', true, false, false, false));
  512    CHK_EXEC(SetMinorSchemaVersion(35));
  513    return true;
  514 }
  515 
  516 /**
  517  * Upgrade from 22.33 to 22.34
  518  */
  519 static bool H_UpgradeFromV33()
  520 {
  521    static const TCHAR *batch =
  522       _T("ALTER TABLE event_log ADD raw_data $SQL:TEXT\n")
  523       _T("ALTER TABLE scheduled_tasks ADD task_key varchar(255)\n")
  524       _T("<END>");
  525    CHK_EXEC(SQLBatch(batch));
  526 
  527    CHK_EXEC(SetMinorSchemaVersion(34));
  528    return true;
  529 }
  530 
  531 /**
  532  * Upgrade from 22.32 to 22.33
  533  */
  534 static bool H_UpgradeFromV32()
  535 {
  536    static const TCHAR *batch =
  537       _T("ALTER TABLE policy_action_list ADD timer_delay integer\n")
  538       _T("ALTER TABLE policy_action_list ADD timer_key varchar(127)\n")
  539       _T("UPDATE policy_action_list SET timer_delay=0\n")
  540       _T("<END>");
  541    CHK_EXEC(SQLBatch(batch));
  542    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("policy_action_list"), _T("timer_delay")));
  543 
  544    CHK_EXEC(CreateTable(
  545       _T("CREATE TABLE policy_timer_cancellation_list (")
  546       _T("   rule_id integer not null,")
  547       _T("   timer_key varchar(127) not null,")
  548       _T("   PRIMARY KEY(rule_id,timer_key))")));
  549 
  550    CHK_EXEC(SetMinorSchemaVersion(33));
  551    return true;
  552 }
  553 
  554 /**
  555  * Upgrade from 22.31 to 22.32
  556  */
  557 static bool H_UpgradeFromV31()
  558 {
  559    CHK_EXEC(CreateLibraryScript(17, _T("ee6dd107-982b-4ad1-980b-fc0cc7a03911"), _T("Hook::DiscoveryPoll"),
  560             _T("/* Available global variables:\r\n *  $node - current node, object of 'Node' type\r\n *\r\n * Expected return value:\r\n *  none - returned value is ignored\r\n */\r\n")));
  561    CHK_EXEC(CreateLibraryScript(18, _T("a02ea666-e1e9-4f98-a746-1c3ce19428e9"), _T("Hook::PostObjectCreate"),
  562             _T("/* Available global variables:\r\n *  $object - current object, one of 'NetObj' subclasses\r\n *  $node - current object if it is 'Node' class\r\n *\r\n * Expected return value:\r\n *  none - returned value is ignored\r\n */\r\n")));
  563    CHK_EXEC(SetMinorSchemaVersion(32));
  564    return true;
  565 }
  566 
  567 /**
  568  * Upgrade from 22.30 to 22.31
  569  */
  570 static bool H_UpgradeFromV30()
  571 {
  572    CHK_EXEC(CreateTable(
  573       _T("CREATE TABLE interface_vlan_list (")
  574       _T("   iface_id integer not null,")
  575       _T("   vlan_id integer not null,")
  576       _T("   PRIMARY KEY(iface_id,vlan_id))")));
  577    CHK_EXEC(SetMinorSchemaVersion(31));
  578    return true;
  579 }
  580 
  581 /**
  582  * Upgrade from 22.29 to 22.30
  583  */
  584 static bool H_UpgradeFromV29()
  585 {
  586    CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD hypervisor_type varchar(31)")));
  587    CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD hypervisor_info varchar(255)")));
  588    CHK_EXEC(SetMinorSchemaVersion(30));
  589    return true;
  590 }
  591 
  592 /**
  593  * Upgrade from 22.28 to 22.29
  594  */
  595 static bool H_UpgradeFromV28()
  596 {
  597    DB_STATEMENT hStmt = DBPrepare(g_dbHandle, _T("UPDATE event_cfg SET description=? WHERE event_code=?"));
  598    if (hStmt != NULL)
  599    {
  600       DBBind(hStmt, 1, DB_SQLTYPE_TEXT,
  601                _T("Generated when agent tunnel is open and bound.\r\n")
  602                _T("Parameters:\r\n")
  603                _T("   1) Tunnel ID (tunnelId)\r\n")
  604                _T("   2) Remote system IP address (ipAddress)\r\n")
  605                _T("   3) Remote system name (systemName)\r\n")
  606                _T("   4) Remote system FQDN (hostName)\r\n")
  607                _T("   5) Remote system platform (platformName)\r\n")
  608                _T("   6) Remote system information (systemInfo)\r\n")
  609                _T("   7) Agent version (agentVersion)\r\n")
  610                _T("   8) Agent ID (agentId)"),
  611                DB_BIND_STATIC);
  612       DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, EVENT_TUNNEL_OPEN);
  613       bool success = DBExecute(hStmt);
  614       if (!success && !g_ignoreErrors)
  615       {
  616          DBFreeStatement(hStmt);
  617          return false;
  618       }
  619 
  620       DBBind(hStmt, 1, DB_SQLTYPE_TEXT,
  621                _T("Generated when agent tunnel is closed.\r\n")
  622                _T("Parameters:\r\n")
  623                _T("   1) Tunnel ID (tunnelId)\r\n")
  624                _T("   2) Remote system IP address (ipAddress)\r\n")
  625                _T("   3) Remote system name (systemName)\r\n")
  626                _T("   4) Remote system FQDN (hostName)\r\n")
  627                _T("   5) Remote system platform (platformName)\r\n")
  628                _T("   6) Remote system information (systemInfo)\r\n")
  629                _T("   7) Agent version (agentVersion)\r\n")
  630                _T("   8) Agent ID (agentId)"),
  631                DB_BIND_STATIC);
  632       DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, EVENT_TUNNEL_CLOSED);
  633       success = DBExecute(hStmt);
  634       if (!success && !g_ignoreErrors)
  635       {
  636          DBFreeStatement(hStmt);
  637          return false;
  638       }
  639 
  640       DBBind(hStmt, 1, DB_SQLTYPE_TEXT,
  641                _T("Generated when unbound agent tunnel is not bound or closed for more than configured threshold.\r\n")
  642                _T("Parameters:\r\n")
  643                _T("   1) Tunnel ID (tunnelId)\r\n")
  644                _T("   2) Remote system IP address (ipAddress)\r\n")
  645                _T("   3) Remote system name (systemName)\r\n")
  646                _T("   4) Remote system FQDN (hostName)\r\n")
  647                _T("   5) Remote system platform (platformName)\r\n")
  648                _T("   6) Remote system information (systemInfo)\r\n")
  649                _T("   7) Agent version (agentVersion)\r\n")
  650                _T("   8) Agent ID (agentId)\r\n")
  651                _T("   9) Configured idle timeout (idleTimeout)"),
  652                DB_BIND_STATIC);
  653       DBBind(hStmt, 2, DB_SQLTYPE_INTEGER, EVENT_UNBOUND_TUNNEL);
  654       success = DBExecute(hStmt);
  655       if (!success && !g_ignoreErrors)
  656       {
  657          DBFreeStatement(hStmt);
  658          return false;
  659       }
  660 
  661       DBFreeStatement(hStmt);
  662    }
  663    else
  664    {
  665       if (!g_ignoreErrors)
  666          return false;
  667    }
  668 
  669    CHK_EXEC(CreateEventTemplate(EVENT_TUNNEL_AGENT_ID_MISMATCH, _T("SYS_TUNNEL_AGENT_ID_MISMATCH"), SEVERITY_WARNING, EF_LOG, _T("30792e3d-f94a-4866-9616-457ba3ac276a"),
  670             _T("Agent ID %<nodeAgentId> on node do not match agent ID %<tunnelAgentId> on tunnel from %<systemName> (%<ipAddress>) at bind"),
  671             _T("Generated when agent ID mismatch detected during tunnel bind.\r\n")
  672             _T("Parameters:\r\n")
  673             _T("   1) Tunnel ID (tunnelId)\r\n")
  674             _T("   2) Remote system IP address (ipAddress)\r\n")
  675             _T("   3) Remote system name (systemName)\r\n")
  676             _T("   4) Remote system FQDN (hostName)\r\n")
  677             _T("   5) Remote system platform (platformName)\r\n")
  678             _T("   6) Remote system information (systemInfo)\r\n")
  679             _T("   7) Agent version (agentVersion)\r\n")
  680             _T("   8) Tunnel agent ID (tunnelAgentId)\r\n")
  681             _T("   9) Node agent ID (nodeAgentId)")
  682             ));
  683 
  684    CHK_EXEC(SetMinorSchemaVersion(29));
  685    return true;
  686 }
  687 
  688 /**
  689  * Upgrade from 22.27 to 22.28
  690  */
  691 static bool H_UpgradeFromV27()
  692 {
  693    CHK_EXEC(CreateEventTemplate(EVENT_TUNNEL_OPEN, _T("SYS_TUNNEL_OPEN"), SEVERITY_NORMAL, EF_LOG, _T("2569c729-1f8c-4a13-9e75-a1d0c1995bc2"),
  694             _T("Agent tunnel from %<systemName> (%<ipAddress>) is open"),
  695             _T("Generated when agent tunnel is open and bound.\r\n")
  696             _T("Parameters:\r\n")
  697             _T("   1) Tunnel ID (tunnelId)\r\n")
  698             _T("   2) Remote system IP address (ipAddress)\r\n")
  699             _T("   3) Remote system name (systemName)\r\n")
  700             _T("   4) Remote system FQDN (hostName)\r\n")
  701             _T("   5) Remote system platform (platformName)\r\n")
  702             _T("   6) Remote system information (systemInfo)\r\n")
  703             _T("   7) Agent version (agentVersion)")
  704             ));
  705    CHK_EXEC(CreateEventTemplate(EVENT_TUNNEL_CLOSED, _T("SYS_TUNNEL_CLOSED"), SEVERITY_WARNING, EF_LOG, _T("50a61266-710d-48d7-b620-9eaa0f85a94f"),
  706             _T("Agent tunnel from %<systemName> (%<ipAddress>) is closed"),
  707             _T("Generated when agent tunnel is closed.\r\n")
  708             _T("Parameters:\r\n")
  709             _T("   1) Tunnel ID (tunnelId)\r\n")
  710             _T("   2) Remote system IP address (ipAddress)\r\n")
  711             _T("   3) Remote system name (systemName)\r\n")
  712             _T("   4) Remote system FQDN (hostName)\r\n")
  713             _T("   5) Remote system platform (platformName)\r\n")
  714             _T("   6) Remote system information (systemInfo)\r\n")
  715             _T("   7) Agent version (agentVersion)")
  716             ));
  717    CHK_EXEC(SetMinorSchemaVersion(28));
  718    return true;
  719 }
  720 
  721 /**
  722  * Upgrade from 22.26 to 22.27
  723  */
  724 static bool H_UpgradeFromV26()
  725 {
  726    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='Client.AlarmList.DisplayLimit' WHERE var_name='AlarmListDisplayLimit'")));
  727    CHK_EXEC(CreateConfigParam(_T("Client.ObjectBrowser.AutoApplyFilter"), _T("1"), _T("Enable or disable object browser's filter applying as user types (if disabled, user has to press ENTER to apply filter)."), NULL, 'B', true, false, false, false));
  728    CHK_EXEC(CreateConfigParam(_T("Client.ObjectBrowser.FilterDelay"), _T("300"), _T("Delay between typing in object browser''s filter and applying it to object tree."), _T("milliseconds"), 'I', true, false, false, false));
  729    CHK_EXEC(CreateConfigParam(_T("Client.ObjectBrowser.MinFilterStringLength"), _T("1"), _T("Minimal length of filter string in object browser required for automatic apply."), _T("characters"), 'I', true, false, false, false));
  730    CHK_EXEC(SetMinorSchemaVersion(27));
  731    return true;
  732 }
  733 
  734 /**
  735  * Upgrade from 22.25 to 22.26
  736  */
  737 static bool H_UpgradeFromV25()
  738 {
  739    CHK_EXEC(CreateTable(
  740       _T("CREATE TABLE certificate_action_log (")
  741       _T("   record_id integer not null,")
  742       _T("   timestamp integer not null,")
  743       _T("   operation integer not null,")
  744       _T("   user_id integer not null,")
  745       _T("   node_id integer not null,")
  746       _T("   node_guid varchar(36) null,")
  747       _T("   cert_type integer not null,")
  748       _T("   subject varchar(255) null,")
  749       _T("   serial integer null,")
  750       _T("   PRIMARY KEY(record_id))")));
  751 
  752    CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD agent_id varchar(36)")));
  753    CHK_EXEC(SQLQuery(_T("ALTER TABLE nodes ADD agent_cert_subject varchar(500)")));
  754 
  755    CHK_EXEC(CreateEventTemplate(EVENT_AGENT_ID_CHANGED, _T("SYS_AGENT_ID_CHANGED"), SEVERITY_WARNING, EF_LOG,
  756             _T("741f0abc-1e69-46e4-adbc-bf1c4ed8549a"),
  757             _T("Agent ID changed from %1 to %2"),
  758             _T("Generated when agent ID change detected.\r\n")
  759             _T("Parameters:\r\n")
  760             _T("   1) Old agent ID\r\n")
  761             _T("   2) New agent ID")
  762             ));
  763 
  764    CHK_EXEC(SetMinorSchemaVersion(26));
  765    return true;
  766 }
  767 
  768 /**
  769  * Upgrade from 22.24 to 22.25
  770  */
  771 static bool H_UpgradeFromV24()
  772 {
  773    CHK_EXEC(SQLQuery(_T("ALTER TABLE actions ADD guid varchar(36)")));
  774    CHK_EXEC(GenerateGUID(_T("actions"), _T("action_id"), _T("guid"), NULL));
  775    DBSetNotNullConstraint(g_dbHandle, _T("actions"), _T("guid"));
  776    CHK_EXEC(SetMinorSchemaVersion(25));
  777    return true;
  778 }
  779 
  780 /**
  781  * Upgrade from 22.23 to 22.24
  782  */
  783 static bool H_UpgradeFromV23()
  784 {
  785    CHK_EXEC(DBRenameColumn(g_dbHandle, _T("dct_threshold_instances"), _T("row"), _T("row_number")));
  786    CHK_EXEC(SetMinorSchemaVersion(24));
  787    return true;
  788 }
  789 
  790 /**
  791  * Upgrade from 22.22 to 22.24
  792  */
  793 static bool H_UpgradeFromV22()
  794 {
  795    static const TCHAR *batch =
  796       _T("ALTER TABLE object_properties ADD state_before_maint integer\n")
  797       _T("ALTER TABLE dct_threshold_instances ADD row_number integer\n")
  798       _T("ALTER TABLE dct_threshold_instances ADD maint_copy char(1)\n")
  799       _T("ALTER TABLE thresholds ADD state_before_maint char(1)\n")
  800       _T("<END>");
  801    CHK_EXEC(SQLBatch(batch));
  802 
  803    CHK_EXEC(DBDropColumn(g_dbHandle, _T("object_properties"), _T("maint_mode")));
  804    CHK_EXEC(SQLQuery(_T("UPDATE dct_threshold_instances SET maint_copy='0'")));
  805    CHK_EXEC(DBDropPrimaryKey(g_dbHandle, _T("dct_threshold_instances")));
  806    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("dct_threshold_instances"), _T("maint_copy")));
  807    CHK_EXEC(DBAddPrimaryKey(g_dbHandle, _T("dct_threshold_instances"), _T("threshold_id,instance,maint_copy")));
  808 
  809    CHK_EXEC(SetMinorSchemaVersion(24));
  810    return true;
  811 }
  812 
  813 /**
  814  * Upgrade from 22.21 to 22.22
  815  */
  816 static bool H_UpgradeFromV21()
  817 {
  818    CHK_EXEC(CreateConfigParam(_T("Alarms.IgnoreHelpdeskState"), _T("0"), _T("If set alarm helpdesk state will be ignored when resolving or terminating."), NULL, 'B', true, false, false, false));
  819    CHK_EXEC(SetMinorSchemaVersion(22));
  820    return true;
  821 }
  822 
  823 /**
  824  * Upgrade from 22.20 to 22.21
  825  */
  826 static bool H_UpgradeFromV20()
  827 {
  828    static const TCHAR *batch =
  829       _T("ALTER TABLE alarms ADD zone_uin integer\n")
  830       _T("ALTER TABLE event_log ADD zone_uin integer\n")
  831       _T("ALTER TABLE snmp_trap_log ADD zone_uin integer\n")
  832       _T("ALTER TABLE syslog ADD zone_uin integer\n")
  833       _T("<END>");
  834    CHK_EXEC(SQLBatch(batch));
  835 
  836    RegisterOnlineUpgrade(22, 21);
  837 
  838    CHK_EXEC(SetMinorSchemaVersion(21));
  839    return true;
  840 }
  841 
  842 /**
  843  * Upgrade from 22.19 to 22.20
  844  */
  845 static bool H_UpgradeFromV19()
  846 {
  847    CHK_EXEC(SQLQuery(_T("UPDATE config_values SET var_description='Don''t use aliases' WHERE var_name='UseInterfaceAliases' AND var_value='0'")));
  848    CHK_EXEC(SQLQuery(_T("UPDATE config SET description='Resolve node name using DNS, SNMP system name, or host name if current node name is it''s IP address.' WHERE var_name='ResolveNodeNames'")));
  849    CHK_EXEC(SQLQuery(_T("UPDATE config SET description='Number of hops from seed node to be added to topology map.' WHERE var_name='TopologyDiscoveryRadius'")));
  850    CHK_EXEC(SQLQuery(_T("UPDATE config SET description='Network topology information expiration time. Server will use cached topology information if it is newer than given interval.' WHERE var_name='TopologyExpirationTime'")));
  851    CHK_EXEC(SetMinorSchemaVersion(20));
  852    return true;
  853 }
  854 
  855 /**
  856  * Upgrade from 22.18 to 22.19
  857  */
  858 static bool H_UpgradeFromV18()
  859 {
  860    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Syncer.BaseSize"), _T("1"), _T("Base size for syncer thread pool."), NULL, 'I', true, true, false, false));
  861    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Syncer.MaxSize"), _T("1"), _T("Maximum size for syncer thread pool (value of 1 will disable pool creation)."), NULL, 'I', true, true, false, false));
  862    CHK_EXEC(SetMinorSchemaVersion(19));
  863    return true;
  864 }
  865 
  866 /**
  867  * Upgrade from 22.17 to 22.18
  868  */
  869 static bool H_UpgradeFromV17()
  870 {
  871    CHK_EXEC(CreateConfigParam(_T("DBWriter.RawDataFlushInterval"), _T("30"), _T("Interval between writes of accumulated raw DCI data to database."), NULL, 'I', true, true, false, false));
  872    CHK_EXEC(SetMinorSchemaVersion(18));
  873    return true;
  874 }
  875 
  876 /**
  877  * Upgrade from 22.16 to 22.17
  878  */
  879 static bool H_UpgradeFromV16()
  880 {
  881    CHK_EXEC(CreateConfigParam(_T("DBWriter.DataQueues"), _T("1"), _T("Number of queues for DCI data writer."), NULL, 'I', true, true, false, false));
  882    CHK_EXEC(SetMinorSchemaVersion(17));
  883    return true;
  884 }
  885 
  886 /**
  887  * Upgrade from 22.15 to 22.16
  888  */
  889 static bool H_UpgradeFromV15()
  890 {
  891    CHK_EXEC(CreateConfigParam(_T("DataCollection.ScriptErrorReportInterval"), _T("86400"), _T("Minimal interval between reporting errors in data collection related script."), NULL, 'I', true, false, false, false));
  892    CHK_EXEC(SetMinorSchemaVersion(16));
  893    return true;
  894 }
  895 
  896 /**
  897  * Upgrade from 22.14 to 22.15
  898  */
  899 static bool H_UpgradeFromV14()
  900 {
  901    CHK_EXEC(CreateConfigParam(_T("NXSL.EnableFileIOFunctions"), _T("0"), _T("Enable/disable server-side NXSL functions for file I/O (such as OpenFile, DeleteFile, etc.)."), NULL, 'B', true, true, false, false));
  902    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='NXSL.EnableContainerFunctions',description='Enable/disable server-side NXSL functions for container management (such as CreateContainer, RemoveContainer, BindObject, UnbindObject).' WHERE var_name='EnableNXSLContainerFunctions'")));
  903    CHK_EXEC(SetMinorSchemaVersion(15));
  904    return true;
  905 }
  906 
  907 /**
  908  * Upgrade from 22.13 to 22.14
  909  */
  910 static bool H_UpgradeFromV13()
  911 {
  912    CHK_EXEC(CreateConfigParam(_T("DataCollection.OnDCIDelete.TerminateRelatedAlarms"), _T("1"), _T("Enable/disable automatic termination of related alarms when data collection item is deleted."), NULL, 'B', true, false, false, false));
  913    CHK_EXEC(SQLQuery(_T("UPDATE config SET data_type='I',description='Inactivity time after which user account will be blocked (0 to disable blocking).' WHERE var_name='BlockInactiveUserAccounts'")));
  914    CHK_EXEC(SetMinorSchemaVersion(14));
  915    return true;
  916 }
  917 
  918 /**
  919  * Upgrade from 22.12 to 22.13
  920  */
  921 static bool H_UpgradeFromV12()
  922 {
  923    CHK_EXEC(CreateEventTemplate(EVENT_UNBOUND_TUNNEL, _T("SYS_UNBOUND_TUNNEL"), SEVERITY_NORMAL, EF_LOG, _T("7f781ec2-a8f5-4c02-ad7f-9e5b0a223b87"),
  924             _T("Unbound agent tunnel from %<systemName> (%<ipAddress>) is idle for more than %<idleTimeout> seconds"),
  925             _T("Generated when unbound agent tunnel is not bound or closed for more than configured threshold.\r\n")
  926             _T("Parameters:\r\n")
  927             _T("   1) Tunnel ID (tunnelId)\r\n")
  928             _T("   2) Remote system IP address (ipAddress)\r\n")
  929             _T("   3) Remote system name (systemName)\r\n")
  930             _T("   4) Remote system FQDN (hostName)\r\n")
  931             _T("   5) Remote system platform (platformName)\r\n")
  932             _T("   6) Remote system information (systemInfo)\r\n")
  933             _T("   7) Agent version (agentVersion)\r\n")
  934             _T("   8) Configured idle timeout (idleTimeout)")
  935             ));
  936 
  937    CHK_EXEC(CreateConfigParam(_T("AgentTunnels.NewNodesContainer"), _T(""), _T("Name of the container where nodes created automatically for unbound tunnels will be placed. If empty or missing, such nodes will be created in infrastructure services root."), NULL, 'S', true, false, false, false));
  938    CHK_EXEC(CreateConfigParam(_T("AgentTunnels.UnboundTunnelTimeout"), _T("3600"), _T("Unbound agent tunnels inactivity timeout. If tunnel is not bound or closed after timeout, action defined by AgentTunnels.UnboundTunnelTimeoutAction parameter will be taken."), _T("seconds"), 'I', true, false, false, false));
  939    CHK_EXEC(CreateConfigParam(_T("AgentTunnels.UnboundTunnelTimeoutAction"), _T("0"), _T("Action to be taken when unbound agent tunnel idle timeout expires."), NULL, 'C', true, false, false, false));
  940 
  941    static TCHAR batch[] =
  942       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('AgentTunnels.UnboundTunnelTimeoutAction','0','Reset tunnel')\n")
  943       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('AgentTunnels.UnboundTunnelTimeoutAction','1','Generate event')\n")
  944       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('AgentTunnels.UnboundTunnelTimeoutAction','2','Bind tunnel to existing node')\n")
  945       _T("INSERT INTO config_values (var_name,var_value,var_description) VALUES ('AgentTunnels.UnboundTunnelTimeoutAction','3','Bind tunnel to existing node or create new node')\n")
  946       _T("<END>");
  947    CHK_EXEC(SQLBatch(batch));
  948 
  949    CHK_EXEC(SQLQuery(_T("UPDATE event_cfg SET description='Generated when new node object added to the database.\r\nParameters:\r\n   1) Node origin (0 = created manually, 1 = created by network discovery, 2 = created by tunnel auto bind)' WHERE event_code=1")));
  950 
  951    CHK_EXEC(SetMinorSchemaVersion(13));
  952    return true;
  953 }
  954 
  955 /**
  956  * Upgrade from 22.11 to 22.12
  957  */
  958 static bool H_UpgradeFromV11()
  959 {
  960    static const TCHAR *batch =
  961       _T("ALTER TABLE ap_common ADD flags integer\n")
  962       _T("UPDATE ap_common SET flags=0\n")
  963       _T("ALTER TABLE ap_common ADD deploy_filter $SQL:TEXT\n")
  964       _T("<END>");
  965    CHK_EXEC(SQLBatch(batch));
  966    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("ap_common"), _T("flags")));
  967 
  968    CHK_EXEC(CreateEventTemplate(EVENT_POLICY_AUTODEPLOY, _T("SYS_POLICY_AUTODEPLOY"), SEVERITY_NORMAL, EF_LOG, _T("f26d70b3-d48d-472c-b2ec-bfa7c20958ea"),
  969             _T("Agent policy %4 automatically deployed to node %2"),
  970             _T("Generated when agent policy deployed to node by autodeploy rule.\r\n")
  971             _T("Parameters:\r\n")
  972             _T("   1) Node ID\r\n")
  973             _T("   2) Node name\r\n")
  974             _T("   3) Policy ID\r\n")
  975             _T("   4) Policy name")
  976             ));
  977 
  978    CHK_EXEC(CreateEventTemplate(EVENT_POLICY_AUTOUNINSTALL, _T("SYS_POLICY_AUTOUNINSTALL"), SEVERITY_NORMAL, EF_LOG, _T("2fbac886-2cfa-489f-bef1-364a38fa7062"),
  979             _T("Agent policy %4 automatically uninstalled from node %2"),
  980             _T("Generated when agent policy uninstalled from node by autodeploy rule.\r\n")
  981             _T("Parameters:\r\n")
  982             _T("   1) Node ID\r\n")
  983             _T("   2) Node name\r\n")
  984             _T("   3) Policy ID\r\n")
  985             _T("   4) Policy name")
  986             ));
  987 
  988    CHK_EXEC(SetMinorSchemaVersion(12));
  989    return true;
  990 }
  991 
  992 /**
  993  * Upgrade from 22.10 to 22.11
  994  */
  995 static bool H_UpgradeFromV10()
  996 {
  997    CHK_EXEC(SQLQuery(_T("UPDATE config SET var_name='Housekeeper.StartTime' WHERE var_name='HousekeeperStartTime'")));
  998    CHK_EXEC(CreateConfigParam(_T("Housekeeper.Throttle.HighWatermark"), _T("250000"), _T("High watermark for housekeeper throttling"), NULL, 'I', true, false, false, false));
  999    CHK_EXEC(CreateConfigParam(_T("Housekeeper.Throttle.LowWatermark"), _T("50000"), _T("Low watermark for housekeeper throttling"), NULL, 'I', true, false, false, false));
 1000    CHK_EXEC(SetMinorSchemaVersion(11));
 1001    return true;
 1002 }
 1003 
 1004 /**
 1005  * Upgrade from 22.9 to 22.10
 1006  */
 1007 static bool H_UpgradeFromV9()
 1008 {
 1009    static TCHAR batch[] =
 1010       _T("UPDATE config SET var_name='ThreadPool.DataCollector.BaseSize' WHERE var_name='DataCollector.ThreadPool.BaseSize'\n")
 1011       _T("UPDATE config SET var_name='ThreadPool.DataCollector.MaxSize' WHERE var_name='DataCollector.ThreadPool.MaxSize'\n")
 1012       _T("UPDATE config SET var_name='ThreadPool.Poller.BaseSize',description='Base size for poller thread pool' WHERE var_name='PollerThreadPoolBaseSize'\n")
 1013       _T("UPDATE config SET var_name='ThreadPool.Poller.MaxSize',description='Maximum size for poller thread pool' WHERE var_name='PollerThreadPoolMaxSize'\n")
 1014       _T("<END>");
 1015    CHK_EXEC(SQLBatch(batch));
 1016    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Agent.BaseSize"), _T("4"), _T("Base size for agent connector thread pool"), NULL, 'I', true, true, false, false));
 1017    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Agent.MaxSize"), _T("256"), _T("Maximum size for agent connector thread pool"), NULL, 'I', true, true, false, false));
 1018    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Main.BaseSize"), _T("8"), _T("Base size for main server thread pool"), NULL, 'I', true, true, false, false));
 1019    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Main.MaxSize"), _T("256"), _T("Maximum size for main server thread pool"), NULL, 'I', true, true, false, false));
 1020    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Scheduler.BaseSize"), _T("1"), _T("Base size for scheduler thread pool"), NULL, 'I', true, true, false, false));
 1021    CHK_EXEC(CreateConfigParam(_T("ThreadPool.Scheduler.MaxSize"), _T("64"), _T("Maximum size for scheduler thread pool"), NULL, 'I', true, true, false, false));
 1022    CHK_EXEC(SetMinorSchemaVersion(10));
 1023    return true;
 1024 }
 1025 
 1026 /**
 1027  * Upgrade from 22.8 to 22.9
 1028  */
 1029 static bool H_UpgradeFromV8()
 1030 {
 1031    CHK_EXEC(DBResizeColumn(g_dbHandle, _T("nodes"), _T("lldp_id"), 255, true));
 1032    CHK_EXEC(SetMinorSchemaVersion(9));
 1033    return true;
 1034 }
 1035 
 1036 /**
 1037  * Upgrade from 22.7 to 22.8
 1038  */
 1039 static bool H_UpgradeFromV7()
 1040 {
 1041    static TCHAR batch[] =
 1042       _T("ALTER TABLE nodes ADD rack_image_rear varchar(36)\n")
 1043       _T("ALTER TABLE chassis ADD rack_image_rear varchar(36)\n")
 1044       _T("UPDATE nodes SET rack_image_rear='00000000-0000-0000-0000-000000000000'\n")
 1045       _T("UPDATE chassis SET rack_image_rear='00000000-0000-0000-0000-000000000000'\n")
 1046       _T("<END>");
 1047    CHK_EXEC(SQLBatch(batch));
 1048    CHK_EXEC(DBRenameColumn(g_dbHandle, _T("nodes"), _T("rack_image"), _T("rack_image_front")));
 1049    CHK_EXEC(DBRenameColumn(g_dbHandle, _T("chassis"), _T("rack_image"), _T("rack_image_front")));
 1050    CHK_EXEC(SetMinorSchemaVersion(8));
 1051    return true;
 1052 }
 1053 
 1054 /**
 1055  * Upgrade from 22.6 to 22.7
 1056  */
 1057 static bool H_UpgradeFromV6()
 1058 {
 1059    static TCHAR batch[] =
 1060       _T("UPDATE config SET default_value='2' WHERE var_name='DefaultEncryptionPolicy'\n")
 1061       _T("UPDATE config SET var_value='2' WHERE var_name='DefaultEncryptionPolicy' AND var_value!='3'\n")
 1062       _T("<END>");
 1063    CHK_EXEC(SQLBatch(batch));
 1064    CHK_EXEC(SetMinorSchemaVersion(7));
 1065    return true;
 1066 }
 1067 
 1068 /**
 1069  * Upgrade from 22.5 to 22.6
 1070  */
 1071 static bool H_UpgradeFromV5()
 1072 {
 1073    static TCHAR batch[] =
 1074       _T("ALTER TABLE racks ADD passive_elements $SQL:TEXT\n")
 1075       _T("<END>");
 1076    CHK_EXEC(SQLBatch(batch));
 1077    CHK_EXEC(SetMinorSchemaVersion(6));
 1078    return true;
 1079 }
 1080 
 1081 /**
 1082  * Upgrade from 22.4 to 22.5
 1083  */
 1084 static bool H_UpgradeFromV4()
 1085 {
 1086    static const TCHAR *batch =
 1087             _T("ALTER TABLE items ADD instance_retention_time integer\n")
 1088             _T("ALTER TABLE dc_tables ADD instance_retention_time integer\n")
 1089             _T("UPDATE items SET instance_retention_time=-1\n")
 1090             _T("UPDATE dc_tables SET instance_retention_time=-1\n")
 1091             _T("INSERT INTO config (var_name,var_value,default_value,is_visible,need_server_restart,is_public,data_type,description) ")
 1092             _T("VALUES ('InstanceRetentionTime','0','0',1,1,'Y','I','Default retention time (in days) for missing DCI instances')\n")
 1093             _T("<END>");
 1094    CHK_EXEC(SQLBatch(batch));
 1095 
 1096    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("items"), _T("instance_retention_time")));
 1097    CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("dc_tables"), _T("instance_retention_time")));
 1098 
 1099    CHK_EXEC(SetMinorSchemaVersion(5));
 1100    return true;
 1101 }
 1102 
 1103 /**
 1104  * Upgrade from 22.3 to 22.4
 1105  */
 1106 static bool H_UpgradeFromV3()
 1107 {
 1108    if (GetSchemaLevelForMajorVersion(21) < 5)
 1109    {
 1110       static const TCHAR *batch =
 1111                _T("ALTER TABLE nodes ADD rack_orientation integer\n")
 1112                _T("ALTER TABLE chassis ADD rack_orientation integer\n")
 1113                _T("UPDATE nodes SET rack_orientation=0\n")
 1114                _T("UPDATE chassis SET rack_orientation=0\n")
 1115                _T("<END>");
 1116       CHK_EXEC(SQLBatch(batch));
 1117 
 1118       CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("nodes"), _T("rack_orientation")));
 1119       CHK_EXEC(DBSetNotNullConstraint(g_dbHandle, _T("chassis"), _T("rack_orientation")));
 1120 
 1121       CHK_EXEC(SetSchemaLevelForMajorVersion(21, 5));
 1122    }
 1123    CHK_EXEC(SetMinorSchemaVersion(4));
 1124    return true;
 1125 }
 1126 
 1127 /**
 1128  * Upgrade from 22.2 to 22.3
 1129  */
 1130 static bool H_UpgradeFromV2()
 1131 {
 1132    CHK_EXEC(CreateTable(
 1133          _T("CREATE TABLE dci_access (")
 1134          _T("   dci_id integer not null,")
 1135          _T("   user_id integer not null,")
 1136          _T("   PRIMARY KEY(dci_id,user_id))")));
 1137    CHK_EXEC(SetMinorSchemaVersion(3));
 1138    return true;
 1139 }
 1140 
 1141 /**
 1142  * Upgrade from 22.1 to 22.2
 1143  */
 1144 static bool H_UpgradeFromV1()
 1145 {
 1146    CHK_EXEC(CreateConfigParam(_T("DBWriter.MaxRecordsPerTransaction"), _T("1000"), _T("Maximum number of records per one transaction for delayed database writes."), NULL, 'I', true, true, false, false));
 1147    CHK_EXEC(SetMinorSchemaVersion(2));
 1148    return true;
 1149 }
 1150 
 1151 /**
 1152  * Upgrade from 22.0 to 22.1
 1153  */
 1154 static bool H_UpgradeFromV0()
 1155 {
 1156    int count = DBMgrConfigReadInt32(_T("NumberOfDataCollectors"), 250);
 1157    TCHAR value[64];
 1158    _sntprintf(value, 64,_T("%d"), std::max(250, count));
 1159    CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.BaseSize"), _T("10"), _T("Base size for data collector thread pool."), NULL, 'I', true, true, false, false));
 1160    CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.MaxSize"), value, _T("Maximum size for data collector thread pool."), NULL, 'I', true, true, false, false));
 1161    CHK_EXEC(SQLQuery(_T("UPDATE config SET default_value='250' WHERE var_name='DataCollector.ThreadPool.MaxSize'")));
 1162    CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfDataCollectors'")));
 1163    CHK_EXEC(SetMinorSchemaVersion(1));
 1164    return true;
 1165 }
 1166 
 1167 /**
 1168  * Upgrade map
 1169  */
 1170 static struct
 1171 {
 1172    int version;
 1173    int nextMajor;
 1174    int nextMinor;
 1175    bool (* upgradeProc)();
 1176 } s_dbUpgradeMap[] =
 1177 {
 1178    { 59, 30, 0,  H_UpgradeFromV59 },
 1179    { 58, 22, 59, H_UpgradeFromV58 },
 1180    { 57, 22, 58, H_UpgradeFromV57 },
 1181    { 56, 22, 57, H_UpgradeFromV56 },
 1182    { 55, 22, 56, H_UpgradeFromV55 },
 1183    { 54, 22, 55, H_UpgradeFromV54 },
 1184    { 53, 22, 54, H_UpgradeFromV53 },
 1185    { 52, 22, 53, H_UpgradeFromV52 },
 1186    { 51, 22, 52, H_UpgradeFromV51 },
 1187    { 50, 22, 51, H_UpgradeFromV50 },
 1188    { 49, 22, 50, H_UpgradeFromV49 },
 1189    { 48, 22, 49, H_UpgradeFromV48 },
 1190    { 47, 22, 48, H_UpgradeFromV47 },
 1191    { 46, 22, 47, H_UpgradeFromV46 },
 1192    { 45, 22, 46, H_UpgradeFromV45 },
 1193    { 44, 22, 45, H_UpgradeFromV44 },
 1194    { 43, 22, 44, H_UpgradeFromV43 },
 1195    { 42, 22, 43, H_UpgradeFromV42 },
 1196    { 41, 22, 42, H_UpgradeFromV41 },
 1197    { 40, 22, 41, H_UpgradeFromV40 },
 1198    { 39, 22, 40, H_UpgradeFromV39 },
 1199    { 38, 22, 39, H_UpgradeFromV38 },
 1200    { 37, 22, 38, H_UpgradeFromV37 },
 1201    { 36, 22, 37, H_UpgradeFromV36 },
 1202    { 35, 22, 36, H_UpgradeFromV35 },
 1203    { 34, 22, 35, H_UpgradeFromV34 },
 1204    { 33, 22, 34, H_UpgradeFromV33 },
 1205    { 32, 22, 33, H_UpgradeFromV32 },
 1206    { 31, 22, 32, H_UpgradeFromV31 },
 1207    { 30, 22, 31, H_UpgradeFromV30 },
 1208    { 29, 22, 30, H_UpgradeFromV29 },
 1209    { 28, 22, 29, H_UpgradeFromV28 },
 1210    { 27, 22, 28, H_UpgradeFromV27 },
 1211    { 26, 22, 27, H_UpgradeFromV26 },
 1212    { 25, 22, 26, H_UpgradeFromV25 },
 1213    { 24, 22, 25, H_UpgradeFromV24 },
 1214    { 23, 22, 24, H_UpgradeFromV23 },
 1215    { 22, 22, 24, H_UpgradeFromV22 },
 1216    { 21, 22, 22, H_UpgradeFromV21 },
 1217    { 20, 22, 21, H_UpgradeFromV20 },
 1218    { 19, 22, 20, H_UpgradeFromV19 },
 1219    { 18, 22, 19, H_UpgradeFromV18 },
 1220    { 17, 22, 18, H_UpgradeFromV17 },
 1221    { 16, 22, 17, H_UpgradeFromV16 },
 1222    { 15, 22, 16, H_UpgradeFromV15 },
 1223    { 14, 22, 15, H_UpgradeFromV14 },
 1224    { 13, 22, 14, H_UpgradeFromV13 },
 1225    { 12, 22, 13, H_UpgradeFromV12 },
 1226    { 11, 22, 12, H_UpgradeFromV11 },
 1227    { 10, 22, 11, H_UpgradeFromV10 },
 1228    { 9,  22, 10, H_UpgradeFromV9 },
 1229    { 8,  22, 9,  H_UpgradeFromV8 },
 1230    { 7,  22, 8,  H_UpgradeFromV7 },
 1231    { 6,  22, 7,  H_UpgradeFromV6 },
 1232    { 5,  22, 6,  H_UpgradeFromV5 },
 1233    { 4,  22, 5,  H_UpgradeFromV4 },
 1234    { 3,  22, 4,  H_UpgradeFromV3 },
 1235    { 2,  22, 3,  H_UpgradeFromV2 },
 1236    { 1,  22, 2,  H_UpgradeFromV1 },
 1237    { 0,  22, 1,  H_UpgradeFromV0 },
 1238    { 0,  0,  0,  NULL }
 1239 };
 1240 
 1241 /**
 1242  * Upgrade database to new version
 1243  */
 1244 bool MajorSchemaUpgrade_V22()
 1245 {
 1246    INT32 major, minor;
 1247    if (!DBGetSchemaVersion(g_dbHandle, &major, &minor))
 1248       return false;
 1249 
 1250    while(major == 22)
 1251    {
 1252       // Find upgrade procedure
 1253       int i;
 1254       for(i = 0; s_dbUpgradeMap[i].upgradeProc != NULL; i++)
 1255          if (s_dbUpgradeMap[i].version == minor)
 1256             break;
 1257       if (s_dbUpgradeMap[i].upgradeProc == NULL)
 1258       {
 1259          _tprintf(_T("Unable to find upgrade procedure for version 22.%d\n"), minor);
 1260          return false;
 1261       }
 1262       _tprintf(_T("Upgrading from version 22.%d to %d.%d\n"), minor, s_dbUpgradeMap[i].nextMajor, s_dbUpgradeMap[i].nextMinor);
 1263       DBBegin(g_dbHandle);
 1264       if (s_dbUpgradeMap[i].upgradeProc())
 1265       {
 1266          DBCommit(g_dbHandle);
 1267          if (!DBGetSchemaVersion(g_dbHandle, &major, &minor))
 1268             return false;
 1269       }
 1270       else
 1271       {
 1272          _tprintf(_T("Rolling back last stage due to upgrade errors...\n"));
 1273          DBRollback(g_dbHandle);
 1274          return false;
 1275       }
 1276    }
 1277    return true;
 1278 }