minor changes
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
CommitLineData
5039dede
AK
1/*
2** nxdbmgr - NetXMS database manager
3** Copyright (C) 2004, 2005, 2006, 2007, 2008 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.cpp
20**
21**/
22
23#include "nxdbmgr.h"
24
25
26//
27// Create table
28//
29
30static BOOL CreateTable(const TCHAR *pszQuery)
31{
32 TCHAR *pszBuffer;
33 BOOL bResult;
34
35 pszBuffer = (TCHAR *)malloc(_tcslen(pszQuery) * sizeof(TCHAR) + 256);
36 _tcscpy(pszBuffer, pszQuery);
37 TranslateStr(pszBuffer, _T("$SQL:TEXT"), g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
38 TranslateStr(pszBuffer, _T("$SQL:INT64"), g_pszSqlType[g_iSyntax][SQL_TYPE_INT64]);
39 if (g_iSyntax == DB_SYNTAX_MYSQL)
40 _tcscat(pszBuffer, g_pszTableSuffix);
41 bResult = SQLQuery(pszBuffer);
42 free(pszBuffer);
43 return bResult;
44}
45
46
47//
48// Create configuration parameter if it doesn't exist (unless bForceUpdate set to true)
49//
50
51static BOOL CreateConfigParam(const TCHAR *pszName, const TCHAR *pszValue,
52 int iVisible, int iNeedRestart, BOOL bForceUpdate = FALSE)
53{
54 TCHAR szQuery[1024], *pszEscValue;
55 DB_RESULT hResult;
56 BOOL bVarExist = FALSE, bResult = TRUE;
57
58 // Check for variable existence
59 _stprintf(szQuery, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
60 hResult = DBSelect(g_hCoreDB, szQuery);
61 if (hResult != 0)
62 {
63 if (DBGetNumRows(hResult) > 0)
64 bVarExist = TRUE;
65 DBFreeResult(hResult);
66 }
67
68 if (!bVarExist)
69 {
70 pszEscValue = EncodeSQLString(pszValue);
71 _stprintf(szQuery, _T("INSERT INTO config (var_name,var_value,is_visible,")
72 _T("need_server_restart) VALUES ('%s','%s',%d,%d)"),
73 pszName, pszEscValue, iVisible, iNeedRestart);
74 free(pszEscValue);
75 bResult = SQLQuery(szQuery);
76 }
77 else if (bForceUpdate)
78 {
79 pszEscValue = EncodeSQLString(pszValue);
80 _stprintf(szQuery, _T("UPDATE config SET var_value='%s' WHERE var_name='%s'"),
81 pszEscValue, pszName);
82 free(pszEscValue);
83 bResult = SQLQuery(szQuery);
84 }
85 return bResult;
86}
87
88
89//
90// Set primary key constraint
91//
92
93static BOOL SetPrimaryKey(const TCHAR *table, const TCHAR *key)
94{
95 TCHAR query[4096];
96
97 if (g_iSyntax == DB_SYNTAX_SQLITE)
98 return TRUE; // SQLite does not support adding constraints
99
100 _sntprintf(query, 4096, _T("ALTER TABLE %s ADD PRIMARY KEY (%s)"), table, key);
101 return SQLQuery(query);
102}
103
104
45d84f8a
VK
105//
106// Upgrade from V92 to V200
107//
108
109static BOOL H_UpgradeFromV92(void)
110{
111 if (!CreateTable(_T("CREATE TABLE ap_common (")
112 _T("id integer not null,")
113 _T("policy_type integer not null,")
114 _T("version integer not null,")
115 _T("description $SQL:TEXT not null,")
116 _T("PRIMARY KEY(id))")))
117 if (!g_bIgnoreErrors)
118 return FALSE;
119
120 if (!CreateTable(_T("CREATE TABLE ap_bindings (")
121 _T("policy_id integer not null,")
122 _T("node_id integer not null,")
123 _T("PRIMARY KEY(policy_id,node_id))")))
124 if (!g_bIgnoreErrors)
125 return FALSE;
126
127 if (!CreateTable(_T("CREATE TABLE ap_config_files (")
128 _T("policy_id integer not null,")
129 _T("file_name varchar(63) not null,")
130 _T("file_content $SQL:TEXT not null,")
131 _T("PRIMARY KEY(policy_id))")))
132 if (!g_bIgnoreErrors)
133 return FALSE;
134
135 if (!SQLQuery(_T("UPDATE metadata SET var_value='200' WHERE var_name='SchemaVersion'")))
136 if (!g_bIgnoreErrors)
137 return FALSE;
138
139 return TRUE;
140}
141
142
6e53f004
VK
143//
144// Upgrade from V91 to V92
145//
146
147static BOOL H_UpgradeFromV91(void)
148{
149 static TCHAR batch[] =
150 _T("DROP TABLE images\n")
151 _T("DROP TABLE default_images\n")
152 _T("<END>");
153
154 if (!SQLBatch(batch))
155 if (!g_bIgnoreErrors)
156 return FALSE;
157
158 if (!SQLQuery(_T("UPDATE metadata SET var_value='92' WHERE var_name='SchemaVersion'")))
159 if (!g_bIgnoreErrors)
160 return FALSE;
161
162 return TRUE;
163}
164
165
c45e0213
VK
166//
167// Upgrade from V90 to V91
168//
169
170static BOOL H_UpgradeFromV90(void)
171{
172 if (!CreateTable(_T("CREATE TABLE userdb_custom_attributes (")
173 _T("object_id integer not null,")
174 _T("attr_name varchar(255) not null,")
175 _T("attr_value $SQL:TEXT not null,")
176 _T("PRIMARY KEY(object_id,attr_name))")))
177 if (!g_bIgnoreErrors)
178 return FALSE;
179
180 if (!SQLQuery(_T("UPDATE metadata SET var_value='91' WHERE var_name='SchemaVersion'")))
181 if (!g_bIgnoreErrors)
182 return FALSE;
183
184 return TRUE;
185}
186
187
4262c0dc
VK
188//
189// Upgrade from V89 to V90
190//
191
192static BOOL H_UpgradeFromV89(void)
193{
194 static TCHAR m_szBatch[] =
195 _T("ALTER TABLE items ADD base_units integer\n")
196 _T("ALTER TABLE items ADD unit_multiplier integer\n")
197 _T("ALTER TABLE items ADD custom_units_name varchar(63)\n")
198 _T("ALTER TABLE items ADD perftab_settings $SQL:TEXT\n")
199 _T("UPDATE items SET base_units=0,unit_multiplier=1,custom_units_name='#00',perftab_settings='#00'\n")
200 _T("<END>");
201
202 if (!SQLBatch(m_szBatch))
203 if (!g_bIgnoreErrors)
204 return FALSE;
205
206 if (!SQLQuery(_T("UPDATE metadata SET var_value='90' WHERE var_name='SchemaVersion'")))
207 if (!g_bIgnoreErrors)
208 return FALSE;
209
210 return TRUE;
211}
212
213
214//
215// Upgrade from V88 to V89
216//
217
218static BOOL H_UpgradeFromV88(void)
219{
220 static TCHAR m_szBatch[] =
221 _T("ALTER TABLE containers ADD enable_auto_bind integer\n")
222 _T("ALTER TABLE containers ADD auto_bind_filter $SQL:TEXT\n")
223 _T("UPDATE containers SET enable_auto_bind=0,auto_bind_filter='#00'\n")
224 _T("ALTER TABLE cluster_resources ADD current_owner integer\n")
225 _T("UPDATE cluster_resources SET current_owner=0\n")
226 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
227 _T("52,'SYS_DB_QUERY_FAILED',4,1,'Database query failed (Query: %1; Error: %2)',")
228 _T("'Generated when SQL query to backend database failed.#0D#0A")
229 _T("Parameters:#0D#0A 1) Query#0D#0A 2) Error message')\n")
230 _T("<END>");
231
232 if (!SQLBatch(m_szBatch))
233 if (!g_bIgnoreErrors)
234 return FALSE;
235
236 if (!SQLQuery(_T("UPDATE metadata SET var_value='89' WHERE var_name='SchemaVersion'")))
237 if (!g_bIgnoreErrors)
238 return FALSE;
239
240 return TRUE;
241}
242
243
244//
245// Upgrade from V87 to V88
246//
247
248static BOOL H_UpgradeFromV87(void)
249{
250 static TCHAR m_szBatch[] =
251 _T("ALTER TABLE templates ADD enable_auto_apply integer\n")
252 _T("ALTER TABLE templates ADD apply_filter $SQL:TEXT\n")
253 _T("UPDATE templates SET enable_auto_apply=0,apply_filter='#00'\n")
254 _T("<END>");
255
256 if (!SQLBatch(m_szBatch))
257 if (!g_bIgnoreErrors)
258 return FALSE;
259
260 if (!SQLQuery(_T("UPDATE metadata SET var_value='88' WHERE var_name='SchemaVersion'")))
261 if (!g_bIgnoreErrors)
262 return FALSE;
263
264 return TRUE;
265}
266
267
28f5b9a4
VK
268//
269// Upgrade from V86 to V87
270//
271
272static BOOL MoveConfigToMetadata(const TCHAR *cfgVar, const TCHAR *mdVar)
273{
274 TCHAR query[1024], buffer[256];
275 DB_RESULT hResult;
276 BOOL success;
277
278 _sntprintf(query, 1024, _T("SELECT var_value FROM config WHERE var_name='%s'"), cfgVar);
279 hResult = SQLSelect(query);
280 if (hResult != NULL)
281 {
282 if (DBGetNumRows(hResult) > 0)
283 {
284 DBGetField(hResult, 0, 0, buffer, 256);
285 DecodeSQLString(buffer);
286 _sntprintf(query, 1024, _T("INSERT INTO metadata (var_name,var_value) VALUES ('%s','%s')"),
287 mdVar, buffer);
288 DBFreeResult(hResult);
289 success = SQLQuery(query);
290 if (success)
291 {
292 _sntprintf(query, 1024, _T("DELETE FROM config WHERE var_name='%s'"), cfgVar);
293 success = SQLQuery(query);
294 }
295 }
296 else
297 {
298 success = TRUE; // Variable missing in 'config' table, nothing to move
299 }
300 }
301 else
302 {
303 success = FALSE;
304 }
305 return success;
306}
307
308static BOOL H_UpgradeFromV86(void)
309{
310 if (!CreateTable(_T("CREATE TABLE metadata (")
311 _T("var_name varchar(63) not null,")
312 _T("var_value varchar(255) not null,")
313 _T("PRIMARY KEY(var_name))")))
314 if (!g_bIgnoreErrors)
315 return FALSE;
316
317 if (!MoveConfigToMetadata(_T("DBFormatVersion"), _T("SchemaVersion")))
318 if (!g_bIgnoreErrors)
319 return FALSE;
320
321 if (!MoveConfigToMetadata(_T("DBSyntax"), _T("Syntax")))
322 if (!g_bIgnoreErrors)
323 return FALSE;
324
325 if (!MoveConfigToMetadata(_T("IDataTableCreationCommand"), _T("IDataTableCreationCommand")))
326 if (!g_bIgnoreErrors)
327 return FALSE;
328
329 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_0"), _T("IDataIndexCreationCommand_0")))
330 if (!g_bIgnoreErrors)
331 return FALSE;
332
333 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_1"), _T("IDataIndexCreationCommand_1")))
334 if (!g_bIgnoreErrors)
335 return FALSE;
336
337 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_2"), _T("IDataIndexCreationCommand_2")))
338 if (!g_bIgnoreErrors)
339 return FALSE;
340
341 if (!MoveConfigToMetadata(_T("IDataIndexCreationCommand_3"), _T("IDataIndexCreationCommand_3")))
342 if (!g_bIgnoreErrors)
343 return FALSE;
344
345 if (!SQLQuery(_T("UPDATE metadata SET var_value='87' WHERE var_name='SchemaVersion'")))
346 if (!g_bIgnoreErrors)
347 return FALSE;
348
349 return TRUE;
350}
351
352
4c4c9b03
VK
353//
354// Upgrade from V85 to V86
355//
356
357static BOOL H_UpgradeFromV85(void)
358{
359 static TCHAR m_szBatch[] =
360 _T("DROP TABLE alarm_grops\n")
361 _T("DROP TABLE alarm_group_map\n")
47912c44
VK
362 _T("DROP TABLE alarm_change_log\n")
363 _T("DROP TABLE lpp\n")
364 _T("DROP TABLE lpp_associations\n")
365 _T("DROP TABLE lpp_rulesets\n")
366 _T("DROP TABLE lpp_rules\n")
367 _T("DROP TABLE lpp_groups\n")
4c4c9b03
VK
368 _T("<END>");
369
370 if (!SQLBatch(m_szBatch))
371 if (!g_bIgnoreErrors)
372 return FALSE;
373
374 if (!SQLQuery(_T("UPDATE config SET var_value='86' WHERE var_name='DBFormatVersion'")))
375 if (!g_bIgnoreErrors)
376 return FALSE;
377
378 return TRUE;
379}
380
381
5039dede
AK
382//
383// Upgrade from V84 to V85
384//
385
386static BOOL H_UpgradeFromV84(void)
387{
388 static TCHAR m_szBatch[] =
389 _T("ALTER TABLE nodes ADD use_ifxtable integer\n")
390 _T("UPDATE nodes SET use_ifxtable=0\n")
391 _T("<END>");
392
393 if (!SQLBatch(m_szBatch))
394 if (!g_bIgnoreErrors)
395 return FALSE;
396
397 if (!CreateConfigParam(_T("UseIfXTable"), _T("1"), 1, 0))
398 if (!g_bIgnoreErrors)
399 return FALSE;
400
401 if (!CreateConfigParam(_T("SMTPRetryCount"), _T("1"), 1, 0))
402 if (!g_bIgnoreErrors)
403 return FALSE;
404
405 if (!SQLQuery(_T("UPDATE config SET var_value='85' WHERE var_name='DBFormatVersion'")))
406 if (!g_bIgnoreErrors)
407 return FALSE;
408
409 return TRUE;
410}
411
412
413//
414// Upgrade from V83 to V84
415//
416
417static BOOL H_UpgradeFromV83(void)
418{
419 if (!CreateConfigParam(_T("EnableAgentRegistration"), _T("1"), 1, 0))
420 if (!g_bIgnoreErrors)
421 return FALSE;
422
423 if (!CreateConfigParam(_T("AnonymousFileAccess"), _T("0"), 1, 0))
424 if (!g_bIgnoreErrors)
425 return FALSE;
426
427 if (!CreateConfigParam(_T("EnableISCListener"), _T("0"), 1, 1))
428 if (!g_bIgnoreErrors)
429 return FALSE;
430
431 if (!CreateConfigParam(_T("ReceiveForwardedEvents"), _T("0"), 1, 0))
432 if (!g_bIgnoreErrors)
433 return FALSE;
434
435 if (!SQLQuery(_T("UPDATE config SET var_value='84' WHERE var_name='DBFormatVersion'")))
436 if (!g_bIgnoreErrors)
437 return FALSE;
438
439 return TRUE;
440}
441
442
443//
444// Upgrade from V82 to V83
445//
446
447static BOOL H_UpgradeFromV82(void)
448{
449 // Fix incorrect alarm timeouts
450 if (!SQLQuery(_T("UPDATE alarms SET timeout=0,timeout_event=43")))
451 if (!g_bIgnoreErrors)
452 return FALSE;
453
454 if (!SQLQuery(_T("UPDATE config SET var_value='83' WHERE var_name='DBFormatVersion'")))
455 if (!g_bIgnoreErrors)
456 return FALSE;
457
458 return TRUE;
459}
460
461
462//
463// Upgrade from V81 to V82
464//
465
466static BOOL H_UpgradeFromV81(void)
467{
468 if (!CreateTable(_T("CREATE TABLE config_clob (")
469 _T("var_name varchar(63) not null,")
470 _T("var_value $SQL:TEXT not null,")
471 _T("PRIMARY KEY(var_name))")))
472 if (!g_bIgnoreErrors)
473 return FALSE;
474
475 if (!SQLQuery(_T("UPDATE config SET var_value='82' WHERE var_name='DBFormatVersion'")))
476 if (!g_bIgnoreErrors)
477 return FALSE;
478
479 return TRUE;
480}
481
482
483//
484// Upgrade from V80 to V81
485//
486
487static BOOL H_UpgradeFromV80(void)
488{
489 DB_RESULT hResult;
490 TCHAR query[1024], buffer[1024];
491 int i;
492
493 // Update dci_schedules table
494 hResult = SQLSelect(_T("SELECT item_id,schedule FROM dci_schedules"));
495 if (hResult != NULL)
496 {
497 if (!SQLQuery(_T("DROP TABLE dci_schedules")))
498 if (!g_bIgnoreErrors)
499 return FALSE;
500
501 if (!CreateTable(_T("CREATE TABLE dci_schedules (")
502 _T("schedule_id integer not null,")
503 _T("item_id integer not null,")
504 _T("schedule varchar(255) not null,")
505 _T("PRIMARY KEY(item_id,schedule_id))")))
506 if (!g_bIgnoreErrors)
507 return FALSE;
508
509 for(i = 0; i < DBGetNumRows(hResult); i++)
510 {
511 _sntprintf(query, 1024, _T("INSERT INTO dci_schedules (item_id,schedule_id,schedule) VALUES(%d,%d,'%s')"),
512 DBGetFieldULong(hResult, i, 0), i + 1, DBGetField(hResult, i, 1, buffer, 1024));
513 if (!SQLQuery(query))
514 if (!g_bIgnoreErrors)
515 return FALSE;
516 }
517 DBFreeResult(hResult);
518 }
519 else
520 {
521 if (!g_bIgnoreErrors)
522 return FALSE;
523 }
524
525 // Update address_lists table
526 hResult = SQLSelect(_T("SELECT list_type,community_id,addr_type,addr1,addr2 FROM address_lists"));
527 if (hResult != NULL)
528 {
529 if (!SQLQuery(_T("DROP TABLE address_lists")))
530 if (!g_bIgnoreErrors)
531 return FALSE;
532
533 if (!CreateTable(_T("CREATE TABLE address_lists (")
534 _T("list_type integer not null,")
535 _T("community_id integer not null,")
536 _T("addr_type integer not null,")
537 _T("addr1 varchar(15) not null,")
538 _T("addr2 varchar(15) not null,")
539 _T("PRIMARY KEY(list_type,community_id,addr_type,addr1,addr2))")))
540 if (!g_bIgnoreErrors)
541 return FALSE;
542
543 for(i = 0; i < DBGetNumRows(hResult); i++)
544 {
545 _sntprintf(query, 1024, _T("INSERT INTO address_lists (list_type,community_id,addr_type,addr1,addr2) VALUES(%d,%d,%d,'%s','%s')"),
546 DBGetFieldULong(hResult, i, 0), DBGetFieldULong(hResult, i, 1),
547 DBGetFieldULong(hResult, i, 2), DBGetField(hResult, i, 3, buffer, 64),
548 DBGetField(hResult, i, 4, &buffer[128], 64));
549 if (!SQLQuery(query))
550 if (!g_bIgnoreErrors)
551 return FALSE;
552 }
553
554 DBFreeResult(hResult);
555 }
556 else
557 {
558 if (!g_bIgnoreErrors)
559 return FALSE;
560 }
561
562 // Create new tables
563 if (!CreateTable(_T("CREATE TABLE object_custom_attributes (")
564 _T("object_id integer not null,")
565 _T("attr_name varchar(127) not null,")
566 _T("attr_value $SQL:TEXT not null,")
567 _T("PRIMARY KEY(object_id,attr_name))")))
568 if (!g_bIgnoreErrors)
569 return FALSE;
570
571 if (!CreateTable(_T("CREATE TABLE web_maps (")
572 _T("id integer not null,")
573 _T("title varchar(63) not null,")
574 _T("properties $SQL:TEXT not null,")
575 _T("data $SQL:TEXT not null,")
576 _T("PRIMARY KEY(id))")))
577 if (!g_bIgnoreErrors)
578 return FALSE;
579
580 if (!SQLQuery(_T("UPDATE config SET var_value='81' WHERE var_name='DBFormatVersion'")))
581 if (!g_bIgnoreErrors)
582 return FALSE;
583
584 return TRUE;
585}
586
587
588//
589// Upgrade from V79 to V80
590//
591
592static BOOL H_UpgradeFromV79(void)
593{
594 static TCHAR m_szBatch[] =
595 _T("ALTER TABLE nodes ADD uname varchar(255)\n")
596 _T("UPDATE nodes SET uname='#00'\n")
597 _T("<END>");
598
599 if (!SQLBatch(m_szBatch))
600 if (!g_bIgnoreErrors)
601 return FALSE;
602
603 if (!SQLQuery(_T("UPDATE config SET var_value='80' WHERE var_name='DBFormatVersion'")))
604 if (!g_bIgnoreErrors)
605 return FALSE;
606
607 return TRUE;
608}
609
610
611//
612// Upgrade from V78 to V79
613//
614
615static BOOL H_UpgradeFromV78(void)
616{
617 static TCHAR m_szBatch[] =
618 _T("DELETE FROM config WHERE var_name='RetainCustomInterfaceNames'\n")
619 _T("DROP TABLE modules\n")
620 _T("<END>");
621 static TCHAR m_szMySQLBatch[] =
622 _T("ALTER TABLE users MODIFY COLUMN cert_mapping_data text not null\n")
623 _T("ALTER TABLE user_profiles MODIFY COLUMN var_value text not null\n")
624 _T("ALTER TABLE object_properties MODIFY COLUMN comments text not null\n")
625 _T("ALTER TABLE network_services MODIFY COLUMN check_request text not null\n")
626 _T("ALTER TABLE network_services MODIFY COLUMN check_responce text not null\n")
627 _T("ALTER TABLE conditions MODIFY COLUMN script text not null\n")
628 _T("ALTER TABLE container_categories MODIFY COLUMN description text not null\n")
629 _T("ALTER TABLE items MODIFY COLUMN transformation text not null\n")
630 _T("ALTER TABLE event_cfg MODIFY COLUMN description text not null\n")
631 _T("ALTER TABLE actions MODIFY COLUMN action_data text not null\n")
632 _T("ALTER TABLE event_policy MODIFY COLUMN comments text not null\n")
633 _T("ALTER TABLE event_policy MODIFY COLUMN script text not null\n")
634 _T("ALTER TABLE alarm_change_log MODIFY COLUMN info_text text not null\n")
635 _T("ALTER TABLE alarm_notes MODIFY COLUMN note_text text not null\n")
636 _T("ALTER TABLE object_tools MODIFY COLUMN tool_data text not null\n")
637 _T("ALTER TABLE syslog MODIFY COLUMN msg_text text not null\n")
638 _T("ALTER TABLE script_library MODIFY COLUMN script_code text not null\n")
639 _T("ALTER TABLE snmp_trap_log MODIFY COLUMN trap_varlist text not null\n")
640 _T("ALTER TABLE maps MODIFY COLUMN description text not null\n")
641 _T("ALTER TABLE agent_configs MODIFY COLUMN config_file text not null\n")
642 _T("ALTER TABLE agent_configs MODIFY COLUMN config_filter text not null\n")
643 _T("ALTER TABLE graphs MODIFY COLUMN config text not null\n")
644 _T("ALTER TABLE certificates MODIFY COLUMN cert_data text not null\n")
645 _T("ALTER TABLE certificates MODIFY COLUMN subject text not null\n")
646 _T("ALTER TABLE certificates MODIFY COLUMN comments text not null\n")
647 _T("ALTER TABLE audit_log MODIFY COLUMN message text not null\n")
648 _T("ALTER TABLE situations MODIFY COLUMN comments text not null\n")
649 _T("<END>");
650
651 if (!SQLBatch(m_szBatch))
652 if (!g_bIgnoreErrors)
653 return FALSE;
654
655 if (g_iSyntax == DB_SYNTAX_MYSQL)
656 {
657 if (!SQLBatch(m_szMySQLBatch))
658 if (!g_bIgnoreErrors)
659 return FALSE;
660 }
661
662 if (!SQLQuery(_T("UPDATE config SET var_value='79' WHERE var_name='DBFormatVersion'")))
663 if (!g_bIgnoreErrors)
664 return FALSE;
665
666 return TRUE;
667}
668
669
670//
671// Upgrade from V77 to V78
672//
673
674static BOOL H_UpgradeFromV77(void)
675{
676 if (!CreateTable(_T("CREATE TABLE trusted_nodes (")
677 _T("source_object_id integer not null,")
678 _T("target_node_id integer not null,")
679 _T("PRIMARY KEY(source_object_id,target_node_id))")))
680 if (!g_bIgnoreErrors)
681 return FALSE;
682
683 if (!CreateConfigParam(_T("CheckTrustedNodes"), _T("1"), 1, 1))
684 if (!g_bIgnoreErrors)
685 return FALSE;
686
687 if (!SQLQuery(_T("UPDATE config SET var_value='78' WHERE var_name='DBFormatVersion'")))
688 if (!g_bIgnoreErrors)
689 return FALSE;
690
691 return TRUE;
692}
693
694
695//
696// Upgrade from V76 to V77
697//
698
699static BOOL H_UpgradeFromV76(void)
700{
701 DB_RESULT hResult;
702 int i, count, seq;
703 DWORD id, lastId;
704 TCHAR query[1024];
705
706 hResult = SQLSelect(_T("SELECT condition_id,dci_id,node_id,dci_func,num_polls FROM cond_dci_map ORDER BY condition_id"));
707 if (hResult == NULL)
708 if (!g_bIgnoreErrors)
709 return FALSE;
710
711 if (!SQLQuery(_T("DROP TABLE cond_dci_map")))
712 if (!g_bIgnoreErrors)
713 goto error;
714
715 if (!CreateTable(_T("CREATE TABLE cond_dci_map (")
716 _T("condition_id integer not null,")
717 _T("sequence_number integer not null,")
718 _T("dci_id integer not null,")
719 _T("node_id integer not null,")
720 _T("dci_func integer not null,")
721 _T("num_polls integer not null,")
722 _T("PRIMARY KEY(condition_id,sequence_number))")))
723 if (!g_bIgnoreErrors)
724 goto error;
725
726 count = DBGetNumRows(hResult);
727 for(i = 0, seq = 0, lastId = 0; i < count; i++, seq++)
728 {
729 id = DBGetFieldULong(hResult, i, 0);
730 if (id != lastId)
731 {
732 seq = 0;
733 lastId = id;
734 }
735 _sntprintf(query, 1024, _T("INSERT INTO cond_dci_map (condition_id,sequence_number,dci_id,node_id,dci_func,num_polls) VALUES (%d,%d,%d,%d,%d,%d)"),
736 id, seq, DBGetFieldULong(hResult, i, 1), DBGetFieldULong(hResult, i, 2),
737 DBGetFieldULong(hResult, i, 3), DBGetFieldULong(hResult, i, 4));
738 if (!SQLQuery(query))
739 if (!g_bIgnoreErrors)
740 goto error;
741 }
742
743 DBFreeResult(hResult);
744
745 if (!SQLQuery(_T("UPDATE config SET var_value='77' WHERE var_name='DBFormatVersion'")))
746 if (!g_bIgnoreErrors)
747 return FALSE;
748
749 return TRUE;
750
751error:
752 DBFreeResult(hResult);
753 return FALSE;
754}
755
756
757//
758// Upgrade from V75 to V76
759//
760
761static BOOL H_UpgradeFromV75(void)
762{
763 static TCHAR m_szBatch[] =
764 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
765 _T("50,'SYS_NETWORK_CONN_LOST',4,1,'NetXMS server network connectivity lost',")
766 _T("'Generated when system detects loss of network connectivity based on beacon ")
767 _T("probing.#0D#0AParameters:#0D#0A 1) Number of beacons')\n")
768 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES (")
769 _T("51,'SYS_NETWORK_CONN_RESTORED',0,1,'NetXMS server network connectivity restored',")
770 _T("'Generated when system detects restoration of network connectivity based on ")
771 _T("beacon probing.#0D#0AParameters:#0D#0A 1) Number of beacons')\n")
772 _T("<END>");
773
774 if (!SQLBatch(m_szBatch))
775 if (!g_bIgnoreErrors)
776 return FALSE;
777
778 if (!CreateConfigParam(_T("AgentCommandTimeout"), _T("2000"), 1, 1))
779 if (!g_bIgnoreErrors)
780 return FALSE;
781
782 if (!CreateConfigParam(_T("BeaconHosts"), _T(""), 1, 1))
783 if (!g_bIgnoreErrors)
784 return FALSE;
785
786 if (!CreateConfigParam(_T("BeaconTimeout"), _T("1000"), 1, 1))
787 if (!g_bIgnoreErrors)
788 return FALSE;
789
790 if (!CreateConfigParam(_T("BeaconPollingInterval"), _T("1000"), 1, 1))
791 if (!g_bIgnoreErrors)
792 return FALSE;
793
794 if (!SQLQuery(_T("UPDATE config SET var_value='76' WHERE var_name='DBFormatVersion'")))
795 if (!g_bIgnoreErrors)
796 return FALSE;
797
798 return TRUE;
799}
800
801
802//
803// Upgrade from V74 to V75
804//
805
806static BOOL H_UpgradeFromV74(void)
807{
808 static TCHAR m_szBatch[] =
809 _T("ALTER TABLE address_lists ADD community_id integer\n")
810 _T("UPDATE address_lists SET community_id=0\n")
811 _T("<END>");
812
813 if (!SQLBatch(m_szBatch))
814 if (!g_bIgnoreErrors)
815 return FALSE;
816
817 if (!CreateTable(_T("CREATE TABLE snmp_communities (")
818 _T("id integer not null,")
819 _T("community varchar(255) not null,")
820 _T("PRIMARY KEY(id))")))
821 if (!g_bIgnoreErrors)
822 return FALSE;
823
824 if (!CreateConfigParam(_T("UseInterfaceAliases"), _T("0"), 1, 0))
825 if (!g_bIgnoreErrors)
826 return FALSE;
827
828 if (!CreateConfigParam(_T("SyncNodeNamesWithDNS"), _T("0"), 1, 0))
829 if (!g_bIgnoreErrors)
830 return FALSE;
831
832 if (!SQLQuery(_T("UPDATE config SET var_value='75' WHERE var_name='DBFormatVersion'")))
833 if (!g_bIgnoreErrors)
834 return FALSE;
835
836 return TRUE;
837}
838
839
840//
841// Upgrade from V73 to V74
842//
843
844static BOOL H_UpgradeFromV73(void)
845{
846 static TCHAR m_szBatch[] =
847 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
848 _T("VALUES (48,'SYS_EVENT_STORM_DETECTED',3,1,'Event storm detected (Events per second: %1)',")
849 _T("'Generated when system detects an event storm.#0D#0AParameters:#0D#0A")
850 _T(" 1) Events per second#0D#0A 2) Duration#0D#0A 3) Threshold')\n")
851 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
852 _T("VALUES (49,'SYS_EVENT_STORM_ENDED',0,1,'Event storm ended',")
853 _T("'Generated when system clears event storm condition.#0D#0AParameters:#0D#0A")
854 _T(" 1) Events per second#0D#0A 2) Duration#0D#0A 3) Threshold')\n")
855 _T("DELETE FROM config WHERE var_name='NumberOfEventProcessors'\n")
856 _T("DELETE FROM config WHERE var_name='EventStormThreshold'\n")
857 _T("<END>");
858
859 if (!SQLBatch(m_szBatch))
860 if (!g_bIgnoreErrors)
861 return FALSE;
862
863 if (!CreateConfigParam(_T("EnableEventStormDetection"), _T("0"), 1, 1))
864 if (!g_bIgnoreErrors)
865 return FALSE;
866
867 if (!CreateConfigParam(_T("EventStormEventsPerSecond"), _T("100"), 1, 1))
868 if (!g_bIgnoreErrors)
869 return FALSE;
870
871 if (!CreateConfigParam(_T("EventStormDuration"), _T("15"), 1, 1))
872 if (!g_bIgnoreErrors)
873 return FALSE;
874
875 if (!SQLQuery(_T("UPDATE config SET var_value='74' WHERE var_name='DBFormatVersion'")))
876 if (!g_bIgnoreErrors)
877 return FALSE;
878
879 return TRUE;
880}
881
882
883//
884// Upgrade from V72 to V73
885//
886
887static BOOL H_UpgradeFromV72(void)
888{
889 static TCHAR m_szBatch[] =
890 _T("ALTER TABLE event_policy ADD situation_id integer\n")
891 _T("ALTER TABLE event_policy ADD situation_instance varchar(255)\n")
892 _T("UPDATE event_policy SET situation_id=0,situation_instance='#00'\n")
893 _T("<END>");
894
895 if (!SQLBatch(m_szBatch))
896 if (!g_bIgnoreErrors)
897 return FALSE;
898
899 if (!CreateTable(_T("CREATE TABLE policy_situation_attr_list (")
900 _T("rule_id integer not null,")
901 _T("situation_id integer not null,")
902 _T("attr_name varchar(255) not null,")
903 _T("attr_value varchar(255) not null,")
904 _T("PRIMARY KEY(rule_id,situation_id,attr_name))")))
905 if (!g_bIgnoreErrors)
906 return FALSE;
907
908 if (!CreateTable(_T("CREATE TABLE situations (")
909 _T("id integer not null,")
910 _T("name varchar(127) not null,")
911 _T("comments $SQL:TEXT not null,")
912 _T("PRIMARY KEY(id))")))
913 if (!g_bIgnoreErrors)
914 return FALSE;
915
916 if (!CreateConfigParam(_T("RetainCustomInterfaceNames"), _T("0"), 1, 0))
917 if (!g_bIgnoreErrors)
918 return FALSE;
919
920 if (!CreateConfigParam(_T("AllowDirectSMS"), _T("0"), 1, 0))
921 if (!g_bIgnoreErrors)
922 return FALSE;
923
924 if (!CreateConfigParam(_T("EventStormThreshold"), _T("0"), 1, 1))
925 if (!g_bIgnoreErrors)
926 return FALSE;
927
928 if (!SQLQuery(_T("UPDATE config SET var_value='73' WHERE var_name='DBFormatVersion'")))
929 if (!g_bIgnoreErrors)
930 return FALSE;
931
932 return TRUE;
933}
934
935
936//
937// Upgrade from V71 to V72
938//
939
940static BOOL H_UpgradeFromV71(void)
941{
942 static TCHAR m_szBatch[] =
943 _T("ALTER TABLE items ADD proxy_node integer\n")
944 _T("UPDATE items SET proxy_node=0\n")
945 _T("<END>");
946
947 if (!SQLBatch(m_szBatch))
948 if (!g_bIgnoreErrors)
949 return FALSE;
950
951 if (!SQLQuery(_T("UPDATE config SET var_value='72' WHERE var_name='DBFormatVersion'")))
952 if (!g_bIgnoreErrors)
953 return FALSE;
954
955 return TRUE;
956}
957
958
959//
960// Upgrade from V70 to V71
961//
962
963static BOOL H_UpgradeFromV70(void)
964{
965 static TCHAR m_szBatch[] =
966 _T("ALTER TABLE nodes ADD required_polls integer\n")
967 _T("UPDATE nodes SET required_polls=0\n")
968 _T("ALTER TABLE interfaces ADD required_polls integer\n")
969 _T("UPDATE interfaces SET required_polls=0\n")
970 _T("ALTER TABLE network_services ADD required_polls integer\n")
971 _T("UPDATE network_services SET required_polls=0\n")
972 _T("<END>");
973
974 if (!SQLBatch(m_szBatch))
975 if (!g_bIgnoreErrors)
976 return FALSE;
977
978 if (!CreateConfigParam(_T("PollCountForStatusChange"), _T("1"), 1, 1))
979 if (!g_bIgnoreErrors)
980 return FALSE;
981
982 if (!SQLQuery(_T("UPDATE config SET var_value='71' WHERE var_name='DBFormatVersion'")))
983 if (!g_bIgnoreErrors)
984 return FALSE;
985
986 return TRUE;
987}
988
989
990//
991// Upgrade from V69 to V70
992//
993
994static BOOL H_UpgradeFromV69(void)
995{
996 static TCHAR m_szBatch[] =
997 _T("ALTER TABLE snmp_trap_cfg ADD user_tag varchar(63)\n")
998 _T("UPDATE snmp_trap_cfg SET user_tag='#00'\n")
999 _T("ALTER TABLE event_log ADD user_tag varchar(63)\n")
1000 _T("UPDATE event_log SET user_tag='#00'\n")
1001 _T("<END>");
1002 int n;
1003 TCHAR buffer[64];
1004
1005 if (!SQLBatch(m_szBatch))
1006 if (!g_bIgnoreErrors)
1007 return FALSE;
1008
1009 // Convert event log retention time from seconds to days
1010 n = ConfigReadInt(_T("EventLogRetentionTime"), 5184000) / 86400;
1011 _stprintf(buffer, _T("%d"), max(n, 1));
1012 if (!CreateConfigParam(_T("EventLogRetentionTime"), buffer, 1, 0, TRUE))
1013 if (!g_bIgnoreErrors)
1014 return FALSE;
1015
1016 // Convert event log retention time from seconds to days
1017 n = ConfigReadInt(_T("SyslogRetentionTime"), 5184000) / 86400;
1018 _stprintf(buffer, _T("%d"), max(n, 1));
1019 if (!CreateConfigParam(_T("SyslogRetentionTime"), buffer, 1, 0, TRUE))
1020 if (!g_bIgnoreErrors)
1021 return FALSE;
1022
1023 if (!SQLQuery(_T("UPDATE config SET var_value='70' WHERE var_name='DBFormatVersion'")))
1024 if (!g_bIgnoreErrors)
1025 return FALSE;
1026
1027 return TRUE;
1028}
1029
1030
1031//
1032// Upgrade from V68 to V69
1033//
1034
1035static BOOL H_UpgradeFromV68(void)
1036{
1037 if (!CreateTable(_T("CREATE TABLE audit_log (")
1038 _T("record_id integer not null,")
1039 _T("timestamp integer not null,")
1040 _T("subsystem varchar(32) not null,")
1041 _T("success integer not null,")
1042 _T("user_id integer not null,")
1043 _T("workstation varchar(63) not null,")
1044 _T("object_id integer not null,")
1045 _T("message $SQL:TEXT not null,")
1046 _T("PRIMARY KEY(record_id))")))
1047 if (!g_bIgnoreErrors)
1048 return FALSE;
1049
1050 if (!CreateConfigParam(_T("EnableAuditLog"), _T("1"), 1, 1))
1051 if (!g_bIgnoreErrors)
1052 return FALSE;
1053
1054 if (!CreateConfigParam(_T("AuditLogRetentionTime"), _T("90"), 1, 0))
1055 if (!g_bIgnoreErrors)
1056 return FALSE;
1057
1058 if (!SQLQuery(_T("UPDATE config SET var_value='69' WHERE var_name='DBFormatVersion'")))
1059 if (!g_bIgnoreErrors)
1060 return FALSE;
1061
1062 return TRUE;
1063}
1064
1065
1066//
1067// Upgrade from V67 to V68
1068//
1069
1070static BOOL H_UpgradeFromV67(void)
1071{
1072 static TCHAR m_szBatch[] =
1073 _T("ALTER TABLE thresholds ADD repeat_interval integer\n")
1074 _T("UPDATE thresholds SET repeat_interval=-1\n")
1075 _T("<END>");
1076
1077 if (!SQLBatch(m_szBatch))
1078 if (!g_bIgnoreErrors)
1079 return FALSE;
1080
1081 if (!CreateConfigParam(_T("ThresholdRepeatInterval"), _T("0"), 1, 1))
1082 if (!g_bIgnoreErrors)
1083 return FALSE;
1084
1085 if (!SQLQuery(_T("UPDATE config SET var_value='68' WHERE var_name='DBFormatVersion'")))
1086 if (!g_bIgnoreErrors)
1087 return FALSE;
1088
1089 return TRUE;
1090}
1091
1092
1093//
1094// Upgrade from V66 to V67
1095//
1096
1097static BOOL H_UpgradeFromV66(void)
1098{
1099 static TCHAR m_szBatch[] =
1100 _T("ALTER TABLE subnets ADD synthetic_mask integer\n")
1101 _T("UPDATE subnets SET synthetic_mask=0\n")
1102 _T("ALTER TABLE interfaces ADD synthetic_mask integer\n")
1103 _T("UPDATE interfaces SET synthetic_mask=0\n")
1104 _T("<END>");
1105
1106 if (!SQLBatch(m_szBatch))
1107 if (!g_bIgnoreErrors)
1108 return FALSE;
1109
1110 if (!SQLQuery(_T("UPDATE config SET var_value='67' WHERE var_name='DBFormatVersion'")))
1111 if (!g_bIgnoreErrors)
1112 return FALSE;
1113
1114 return TRUE;
1115}
1116
1117
1118//
1119// Upgrade from V65 to V66
1120//
1121
1122static BOOL H_UpgradeFromV65(void)
1123{
1124 static TCHAR m_szBatch[] =
1125 _T("ALTER TABLE submap_links ADD port1 varchar(255)\n")
1126 _T("ALTER TABLE submap_links ADD port2 varchar(255)\n")
1127 _T("UPDATE submap_links SET port1='#00',port2='#00'\n")
1128 _T("<END>");
1129
1130 if (!SQLBatch(m_szBatch))
1131 if (!g_bIgnoreErrors)
1132 return FALSE;
1133
1134 if (!SQLQuery(_T("UPDATE config SET var_value='66' WHERE var_name='DBFormatVersion'")))
1135 if (!g_bIgnoreErrors)
1136 return FALSE;
1137
1138 return TRUE;
1139}
1140
1141
1142//
1143// Upgrade from V64 to V65
1144//
1145
1146static BOOL H_UpgradeFromV64(void)
1147{
1148 static TCHAR m_szPGSQLBatch[] =
1149 _T("ALTER TABLE nodes ADD new_community varchar(127)\n")
1150 _T("UPDATE nodes SET new_community=community\n")
1151 _T("ALTER TABLE nodes DROP COLUMN community\n")
1152 _T("ALTER TABLE nodes RENAME COLUMN new_community TO community\n")
1153 _T("ALTER TABLE nodes ALTER COLUMN community SET NOT NULL\n")
1154 _T("<END>");
1155
1156 switch(g_iSyntax)
1157 {
1158 case DB_SYNTAX_MYSQL:
1159 case DB_SYNTAX_ORACLE:
1160 if (!SQLQuery(_T("ALTER TABLE nodes MODIFY community varchar(127)")))
1161 if (!g_bIgnoreErrors)
1162 return FALSE;
1163 break;
1164 case DB_SYNTAX_PGSQL:
1165 if (g_bTrace)
1166 ShowQuery(_T("ALTER TABLE nodes ALTER COLUMN community TYPE varchar(127)"));
1167
1168 if (!DBQuery(g_hCoreDB, _T("ALTER TABLE nodes ALTER COLUMN community TYPE varchar(127)")))
1169 {
1170 // Assume that we are using PostgreSQL oldest than 8.x
1171 if (!SQLBatch(m_szPGSQLBatch))
1172 if (!g_bIgnoreErrors)
1173 return FALSE;
1174 }
1175 break;
1176 case DB_SYNTAX_MSSQL:
1177 if (!SQLQuery(_T("ALTER TABLE nodes ALTER COLUMN community varchar(127)")))
1178 if (!g_bIgnoreErrors)
1179 return FALSE;
1180 break;
1181 case DB_SYNTAX_SQLITE:
1182 _tprintf(_T("WARNING: Due to limitations of SQLite requested operation cannot be completed\nYou system will still be limited to use SNMP commonity strings not longer than 32 characters.\n"));
1183 break;
1184 default:
1185 _tprintf(_T("INTERNAL ERROR: Unknown database syntax %d\n"), g_iSyntax);
1186 break;
1187 }
1188
1189 if (!SQLQuery(_T("UPDATE config SET var_value='65' WHERE var_name='DBFormatVersion'")))
1190 if (!g_bIgnoreErrors)
1191 return FALSE;
1192
1193 return TRUE;
1194}
1195
1196
1197//
1198// Upgrade from V63 to V64
1199//
1200
1201static BOOL H_UpgradeFromV63(void)
1202{
1203 static TCHAR m_szBatch[] =
1204 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (15,'.1.3.6.1.4.1.45.3.29.*',3,0)\n")
1205 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (16,'.1.3.6.1.4.1.45.3.41.*',3,0)\n")
1206 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (17,'.1.3.6.1.4.1.45.3.45.*',3,0)\n")
1207 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (18,'.1.3.6.1.4.1.45.3.43.*',3,0)\n")
1208 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (19,'.1.3.6.1.4.1.45.3.57.*',3,0)\n")
1209 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (20,'.1.3.6.1.4.1.45.3.49.*',3,0)\n")
1210 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (21,'.1.3.6.1.4.1.45.3.54.*',3,0)\n")
1211 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (22,'.1.3.6.1.4.1.45.3.63.*',3,0)\n")
1212 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (23,'.1.3.6.1.4.1.45.3.64.*',3,0)\n")
1213 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (24,'.1.3.6.1.4.1.45.3.53.*',3,0)\n")
1214 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (25,'.1.3.6.1.4.1.45.3.59.*',3,0)\n")
1215 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (26,'.1.3.6.1.4.1.45.3.39.*',3,0)\n")
1216 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (27,'.1.3.6.1.4.1.45.3.65.*',3,0)\n")
1217 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (28,'.1.3.6.1.4.1.45.3.66.*',3,0)\n")
1218 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (29,'.1.3.6.1.4.1.45.3.44.*',4,0)\n")
1219 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (30,'.1.3.6.1.4.1.45.3.47.*',4,0)\n")
1220 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) VALUES (31,'.1.3.6.1.4.1.45.3.48.*',4,0)\n")
1221 _T("<END>");
1222
1223 if (!SQLBatch(m_szBatch))
1224 if (!g_bIgnoreErrors)
1225 return FALSE;
1226
1227 if (!SQLQuery(_T("UPDATE config SET var_value='64' WHERE var_name='DBFormatVersion'")))
1228 if (!g_bIgnoreErrors)
1229 return FALSE;
1230
1231 return TRUE;
1232}
1233
1234
1235//
1236// Upgrade from V62 to V63
1237//
1238
1239static BOOL H_UpgradeFromV62(void)
1240{
1241 static TCHAR m_szBatch[] =
1242 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
1243 _T("VALUES (45,'SYS_IF_UNKNOWN',1,1,")
1244 _T("'Interface \"%2\" changed state to UNKNOWN (IP Addr: %3/%4, IfIndex: %5)',")
1245 _T("'Generated when interface goes to unknown state.#0D#0A")
1246 _T("Please note that source of event is node, not an interface itself.#0D#0A")
1247 _T("Parameters:#0D#0A 1) Interface object ID#0D#0A 2) Interface name#0D#0A")
1248 _T(" 3) Interface IP address#0D#0A 4) Interface netmask#0D#0A")
1249 _T(" 5) Interface index')\n")
1250 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
1251 _T("VALUES (46,'SYS_IF_DISABLED',0,1,")
1252 _T("'Interface \"%2\" disabled (IP Addr: %3/%4, IfIndex: %5)',")
1253 _T("'Generated when interface administratively disabled.#0D#0A")
1254 _T("Please note that source of event is node, not an interface itself.#0D#0A")
1255 _T("Parameters:#0D#0A 1) Interface object ID#0D#0A 2) Interface name#0D#0A")
1256 _T(" 3) Interface IP address#0D#0A 4) Interface netmask#0D#0A")
1257 _T(" 5) Interface index')\n")
1258 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
1259 _T("VALUES (47,'SYS_IF_TESTING',0,1,")
1260 _T("'Interface \"%2\" is testing (IP Addr: %3/%4, IfIndex: %5)',")
1261 _T("'Generated when interface goes to testing state.#0D#0A")
1262 _T("Please note that source of event is node, not an interface itself.#0D#0A")
1263 _T("Parameters:#0D#0A 1) Interface object ID#0D#0A 2) Interface name#0D#0A")
1264 _T(" 3) Interface IP address#0D#0A 4) Interface netmask#0D#0A")
1265 _T(" 5) Interface index')\n")
1266 _T("<END>");
1267
1268 if (!SQLBatch(m_szBatch))
1269 if (!g_bIgnoreErrors)
1270 return FALSE;
1271
1272 if (!SQLQuery(_T("UPDATE config SET var_value='63' WHERE var_name='DBFormatVersion'")))
1273 if (!g_bIgnoreErrors)
1274 return FALSE;
1275
1276 return TRUE;
1277}
1278
1279
1280//
1281// Upgrade from V61 to V62
1282//
1283
1284static BOOL H_UpgradeFromV61(void)
1285{
1286 static TCHAR m_szBatch[] =
1287 _T("UPDATE event_policy SET alarm_key=alarm_ack_key WHERE alarm_severity=6\n")
1288 _T("ALTER TABLE event_policy DROP COLUMN alarm_ack_key\n")
1289 _T("ALTER TABLE event_policy ADD alarm_timeout integer\n")
1290 _T("ALTER TABLE event_policy ADD alarm_timeout_event integer\n")
1291 _T("UPDATE event_policy SET alarm_timeout=0,alarm_timeout_event=43\n")
1292 _T("ALTER TABLE alarms ADD timeout integer\n")
1293 _T("ALTER TABLE alarms ADD timeout_event integer\n")
1294 _T("UPDATE alarms SET timeout=0,timeout_event=43\n")
1295 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
1296 _T("VALUES (43,'SYS_ALARM_TIMEOUT',1,1,'Alarm timeout expired (ID: %1; Text: %2)',")
1297 _T("'Generated when alarm timeout expires.#0D#0AParameters:#0D#0A")
1298 _T(" 1) Alarm ID#0D#0A 2) Alarm message#0D#0A 3) Alarm key#0D#0A 4) Event code')\n")
1299 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) ")
1300 _T("VALUES (44,'SYS_LOG_RECORD_MATCHED',1,1,")
1301 _T("'Log record matched (Policy: %1; File: %2; Record: %4)',")
1302 _T("'Default event for log record match.#0D#0AParameters:#0D#0A")
1303 _T(" 1) Policy name#0D#0A 2) Log file name#0D#0A 3) Matching regular expression#0D#0A")
1304 _T(" 4) Matched record#0D#0A 5 .. 9) Reserved#0D#0A")
1305 _T(" 10 .. 99) Substrings extracted by regular expression')\n")
1306 _T("<END>");
1307
1308 if (!SQLBatch(m_szBatch))
1309 if (!g_bIgnoreErrors)
1310 return FALSE;
1311
1312 if (!CreateTable(_T("CREATE TABLE lpp_groups (")
1313 _T("lpp_group_id integer not null,")
1314 _T("lpp_group_name varchar(63) not null,")
1315 _T("parent_group integer not null,")
1316 _T("PRIMARY KEY(lpp_group_id))")))
1317 if (!g_bIgnoreErrors)
1318 return FALSE;
1319
1320 if (!CreateTable(_T("CREATE TABLE lpp (")
1321 _T("lpp_id integer not null,")
1322 _T("lpp_group_id integer not null,")
1323 _T("lpp_name varchar(63) not null,")
1324 _T("lpp_version integer not null,")
1325 _T("lpp_flags integer not null,")
1326 _T("PRIMARY KEY(lpp_id))")))
1327 if (!g_bIgnoreErrors)
1328 return FALSE;
1329
1330 if (!CreateTable(_T("CREATE TABLE lpp_associations (")
1331 _T("lpp_id integer not null,")
1332 _T("node_id integer not null,")
1333 _T("log_file varchar(255) not null)")))
1334 if (!g_bIgnoreErrors)
1335 return FALSE;
1336
1337 if (!CreateTable(_T("CREATE TABLE lpp_rulesets (")
1338 _T("ruleset_id integer not null,")
1339 _T("ruleset_name varchar(63),")
1340 _T("PRIMARY KEY(ruleset_id))")))
1341 if (!g_bIgnoreErrors)
1342 return FALSE;
1343
1344 if (!CreateTable(_T("CREATE TABLE lpp_rules (")
1345 _T("lpp_id integer not null,")
1346 _T("rule_number integer not null,")
1347 _T("ruleset_id integer not null,")
1348 _T("msg_id_start integer not null,")
1349 _T("msg_id_end integer not null,")
1350 _T("severity integer not null,")
1351 _T("source_name varchar(255) not null,")
1352 _T("msg_text_regexp varchar(255) not null,")
1353 _T("event_code integer not null,")
1354 _T("PRIMARY KEY(lpp_id,rule_number))")))
1355 if (!g_bIgnoreErrors)
1356 return FALSE;
1357
1358 if (!SQLQuery(_T("UPDATE config SET var_value='62' WHERE var_name='DBFormatVersion'")))
1359 if (!g_bIgnoreErrors)
1360 return FALSE;
1361
1362 return TRUE;
1363}
1364
1365
1366//
1367// Upgrade from V60 to V61
1368//
1369
1370static BOOL H_UpgradeFromV60(void)
1371{
1372 static TCHAR m_szBatch[] =
1373 _T("DELETE FROM object_tools WHERE tool_id=14\n")
1374 _T("DELETE FROM object_tools_table_columns WHERE tool_id=14\n")
1375 _T("INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text) ")
1376 _T("VALUES (14,'&Info->Topology table (Nortel)',2,'Topology table',1,' ','Show topology table (Nortel protocol)','#00')\n")
1377 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1378 _T("VALUES (14,0,'Peer IP','.1.3.6.1.4.1.45.1.6.13.2.1.1.3',3,0)\n")
1379 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1380 _T("VALUES (14,1,'Peer MAC','.1.3.6.1.4.1.45.1.6.13.2.1.1.5',4,0)\n")
1381 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1382 _T("VALUES (14,2,'Slot','.1.3.6.1.4.1.45.1.6.13.2.1.1.1',1,0)\n")
1383 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1384 _T("VALUES (14,3,'Port','.1.3.6.1.4.1.45.1.6.13.2.1.1.2',1,0)\n")
1385 _T("INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text) ")
1386 _T("VALUES (17,'&Info->AR&P cache (SNMP)',2,'ARP Cache',1,' ','Show ARP cache','#00')\n")
1387 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1388 _T("VALUES (17,0,'IP Address','.1.3.6.1.2.1.4.22.1.3',3,0)\n")
1389 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1390 _T("VALUES (17,1,'MAC Address','.1.3.6.1.2.1.4.22.1.2',4,0)\n")
1391 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1392 _T("VALUES (17,2,'Interface','.1.3.6.1.2.1.4.22.1.1',5,0)\n")
1393 _T("INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text) ")
1394 _T("VALUES (18,'&Info->AR&P cache (Agent)',3,")
1395 _T("'ARP Cache#7FNet.ArpCache#7F(.*) (.*) (.*)',2,' ','Show ARP cache','#00')\n")
1396 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1397 _T("VALUES (18,0,'IP Address','.1.3.6.1.2.1.4.22.1.3',0,2)\n")
1398 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1399 _T("VALUES (18,1,'MAC Address','.1.3.6.1.2.1.4.22.1.2',0,1)\n")
1400 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1401 _T("VALUES (18,2,'Interface','.1.3.6.1.2.1.4.22.1.1',5,3)\n")
1402 _T("INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text) ")
1403 _T("VALUES (19,'&Info->&Routing table (SNMP)',2,'Routing Table',1,' ','Show IP routing table','#00')\n")
1404 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1405 _T("VALUES (19,0,'Destination','.1.3.6.1.2.1.4.21.1.1',3,0)\n")
1406 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1407 _T("VALUES (19,1,'Mask','.1.3.6.1.2.1.4.21.1.11',3,0)\n")
1408 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1409 _T("VALUES (19,2,'Next hop','.1.3.6.1.2.1.4.21.1.7',3,0)\n")
1410 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1411 _T("VALUES (19,3,'Metric','.1.3.6.1.2.1.4.21.1.3',1,0)\n")
1412 _T("INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) ")
1413 _T("VALUES (19,4,'Interface','.1.3.6.1.2.1.4.21.1.2',5,0)\n")
1414 _T("INSERT INTO object_tools_acl (tool_id,user_id) VALUES (17,-2147483648)\n")
1415 _T("INSERT INTO object_tools_acl (tool_id,user_id) VALUES (18,-2147483648)\n")
1416 _T("INSERT INTO object_tools_acl (tool_id,user_id) VALUES (19,-2147483648)\n")
1417 _T("<END>");
1418
1419 if (!SQLBatch(m_szBatch))
1420 if (!g_bIgnoreErrors)
1421 return FALSE;
1422
1423 if (!CreateConfigParam(_T("TopologyExpirationTime"), _T("900"), 1, 0))
1424 if (!g_bIgnoreErrors)
1425 return FALSE;
1426
1427 if (!CreateConfigParam(_T("TopologyDiscoveryRadius"), _T("3"), 1, 0))
1428 if (!g_bIgnoreErrors)
1429 return FALSE;
1430
1431 if (!SQLQuery(_T("UPDATE config SET var_value='61' WHERE var_name='DBFormatVersion'")))
1432 if (!g_bIgnoreErrors)
1433 return FALSE;
1434
1435 return TRUE;
1436}
1437
1438
1439//
1440// Upgrade from V59 to V60
1441//
1442
1443static BOOL H_UpgradeFromV59(void)
1444{
1445 if (!CreateTable(_T("CREATE TABLE certificates (")
1446 _T("cert_id integer not null,")
1447 _T("cert_type integer not null,")
1448 _T("cert_data $SQL:TEXT not null,")
1449 _T("subject $SQL:TEXT not null,")
1450 _T("comments $SQL:TEXT not null,")
1451 _T("PRIMARY KEY(cert_id))")))
1452 if (!g_bIgnoreErrors)
1453 return FALSE;
1454
1455 if (!CreateConfigParam(_T("SNMPRequestTimeout"), _T("2000"), 1, 1))
1456 if (!g_bIgnoreErrors)
1457 return FALSE;
1458
1459 if (!SQLQuery(_T("UPDATE config SET var_value='60' WHERE var_name='DBFormatVersion'")))
1460 if (!g_bIgnoreErrors)
1461 return FALSE;
1462
1463 return TRUE;
1464}
1465
1466
1467//
1468// Upgrade from V58 to V59
1469//
1470
1471static BOOL H_UpgradeFromV58(void)
1472{
1473 static TCHAR m_szBatch[] =
1474 _T("ALTER TABLE users ADD cert_mapping_method integer\n")
1475 _T("ALTER TABLE users ADD cert_mapping_data $SQL:TEXT\n")
1476 _T("UPDATE users SET cert_mapping_method=0\n")
1477 _T("UPDATE users SET cert_mapping_data='#00'\n")
1478 _T("<END>");
1479
1480 if (!SQLBatch(m_szBatch))
1481 if (!g_bIgnoreErrors)
1482 return FALSE;
1483
1484 if (!CreateConfigParam(_T("InternalCA"), _T("0"), 1, 1))
1485 if (!g_bIgnoreErrors)
1486 return FALSE;
1487
1488 if (!SQLQuery(_T("UPDATE config SET var_value='59' WHERE var_name='DBFormatVersion'")))
1489 if (!g_bIgnoreErrors)
1490 return FALSE;
1491
1492 return TRUE;
1493}
1494
1495
1496//
1497// Upgrade from V57 to V58
1498//
1499
1500static BOOL H_UpgradeFromV57(void)
1501{
1502 static TCHAR m_szBatch[] =
1503 _T("ALTER TABLE object_properties ADD is_system integer\n")
1504 _T("UPDATE object_properties SET is_system=0\n")
1505 _T("<END>");
1506
1507 if (!SQLBatch(m_szBatch))
1508 if (!g_bIgnoreErrors)
1509 return FALSE;
1510
1511 if (!CreateTable(_T("CREATE TABLE graphs (")
1512 _T("graph_id integer not null,")
1513 _T("owner_id integer not null,")
1514 _T("name varchar(255) not null,")
1515 _T("config $SQL:TEXT not null,")
1516 _T("PRIMARY KEY(graph_id))")))
1517 if (!g_bIgnoreErrors)
1518 return FALSE;
1519
1520 if (!CreateTable(_T("CREATE TABLE graph_acl (")
1521 _T("graph_id integer not null,")
1522 _T("user_id integer not null,")
1523 _T("user_rights integer not null,")
1524 _T("PRIMARY KEY(graph_id,user_id))")))
1525 if (!g_bIgnoreErrors)
1526 return FALSE;
1527
1528 if (!SQLQuery(_T("UPDATE config SET var_value='58' WHERE var_name='DBFormatVersion'")))
1529 if (!g_bIgnoreErrors)
1530 return FALSE;
1531
1532 return TRUE;
1533}
1534
1535
1536//
1537// Upgrade from V56 to V57
1538//
1539
1540static BOOL H_UpgradeFromV56(void)
1541{
1542 static TCHAR m_szBatch[] =
1543 _T("ALTER TABLE items ADD resource_id integer\n")
1544 _T("UPDATE items SET resource_id=0\n")
1545 _T("ALTER TABLE nodes ADD snmp_proxy integer\n")
1546 _T("UPDATE nodes SET snmp_proxy=0\n")
1547 _T("<END>");
1548
1549 if (!SQLBatch(m_szBatch))
1550 if (!g_bIgnoreErrors)
1551 return FALSE;
1552
1553 if (!SQLQuery(_T("UPDATE config SET var_value='57' WHERE var_name='DBFormatVersion'")))
1554 if (!g_bIgnoreErrors)
1555 return FALSE;
1556
1557 return TRUE;
1558}
1559
1560
1561//
1562// Upgrade from V55 to V56
1563//
1564
1565static BOOL H_UpgradeFromV55(void)
1566{
1567 static TCHAR m_szBatch[] =
1568 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
1569 _T(" (38,'SYS_CLUSTER_RESOURCE_MOVED',1,1,")
1570 _T("'Cluster resource \"%2\" moved from node %4 to node %6',")
1571 _T("'Generated when cluster resource moved between nodes.#0D#0A")
1572 _T("Parameters:#0D#0A 1) Resource ID#0D#0A")
1573 _T(" 2) Resource name#0D#0A 3) Previous owner node ID#0D#0A")
1574 _T(" 4) Previous owner node name#0D#0A 5) New owner node ID#0D#0A")
1575 _T(" 6) New owner node name')\n")
1576 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
1577 _T(" (39,'SYS_CLUSTER_RESOURCE_DOWN',3,1,")
1578 _T("'Cluster resource \"%2\" is down (last owner was %4)',")
1579 _T("'Generated when cluster resource goes down.#0D#0A")
1580 _T("Parameters:#0D#0A 1) Resource ID#0D#0A 2) Resource name#0D#0A")
1581 _T(" 3) Last owner node ID#0D#0A 4) Last owner node name')\n")
1582 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
1583 _T(" (40,'SYS_CLUSTER_RESOURCE_UP',0,1,")
1584 _T("'Cluster resource \"%2\" is up (new owner is %4)',")
1585 _T("'Generated when cluster resource goes up.#0D#0A")
1586 _T("Parameters:#0D#0A 1) Resource ID#0D#0A 2) Resource name#0D#0A")
1587 _T(" 3) New owner node ID#0D#0A 4) New owner node name')\n")
1588 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
1589 _T(" (41,'SYS_CLUSTER_DOWN',4,1,'Cluster is down',")
1590 _T("'Generated when cluster goes down.#0D#0AParameters:#0D#0A No message-specific parameters')\n")
1591 _T("INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) VALUES")
1592 _T(" (42,'SYS_CLUSTER_UP',0,1,'Cluster is up',")
1593 _T("'Generated when cluster goes up.#0D#0AParameters:#0D#0A No message-specific parameters')\n")
1594 _T("<END>");
1595
1596 if (!SQLBatch(m_szBatch))
1597 if (!g_bIgnoreErrors)
1598 return FALSE;
1599
1600 if (!CreateTable(_T("CREATE TABLE cluster_resources (")
1601 _T("cluster_id integer not null,")
1602 _T("resource_id integer not null,")
1603 _T("resource_name varchar(255) not null,")
1604 _T("ip_addr varchar(15) not null,")
1605 _T("PRIMARY KEY(cluster_id,resource_id))")))
1606 if (!g_bIgnoreErrors)
1607 return FALSE;
1608
1609 if (!SQLQuery(_T("UPDATE config SET var_value='56' WHERE var_name='DBFormatVersion'")))
1610 if (!g_bIgnoreErrors)
1611 return FALSE;
1612
1613 return TRUE;
1614}
1615
1616
1617//
1618// Upgrade from V54 to V55
1619//
1620
1621static BOOL H_UpgradeFromV54(void)
1622{
1623 static TCHAR m_szBatch[] =
1624 _T("ALTER TABLE containers DROP COLUMN description\n")
1625 _T("ALTER TABLE nodes DROP COLUMN description\n")
1626 _T("ALTER TABLE templates DROP COLUMN description\n")
1627 _T("ALTER TABLE zones DROP COLUMN description\n")
1628 _T("INSERT INTO images (image_id,name,file_name_png,file_hash_png,")
1629 _T("file_name_ico,file_hash_ico) VALUES (16,'Obj.Cluster',")
1630 _T("'cluster.png','<invalid_hash>','cluster.ico','<invalid_hash>')\n")
1631 _T("INSERT INTO default_images (object_class,image_id) VALUES (14,16)\n")
1632 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) ")
1633 _T("VALUES (12,'.1.3.6.1.4.1.45.3.46.*',3,0)\n")
1634 _T("INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) ")
1635 _T("VALUES (13,'.1.3.6.1.4.1.45.3.52.*',3,0)\n")
1636 _T("<END>");
1637
1638 if (!SQLBatch(m_szBatch))
1639 if (!g_bIgnoreErrors)
1640 return FALSE;
1641
1642 if (!CreateTable(_T("CREATE TABLE clusters (")
1643 _T("id integer not null,")
1644 _T("cluster_type integer not null,")
1645 _T("PRIMARY KEY(id))")))
1646 if (!g_bIgnoreErrors)
1647 return FALSE;
1648
1649 if (!CreateTable(_T("CREATE TABLE cluster_members (")
1650 _T("cluster_id integer not null,")
1651 _T("node_id integer not null,")
1652 _T("PRIMARY KEY(cluster_id,node_id))")))
1653 if (!g_bIgnoreErrors)
1654 return FALSE;
1655
1656 if (!CreateTable(_T("CREATE TABLE cluster_sync_subnets (")
1657 _T("cluster_id integer not null,")
1658 _T("subnet_addr varchar(15) not null,")
1659 _T("subnet_mask varchar(15) not null,")
1660 _T("PRIMARY KEY(cluster_id,subnet_addr))")))
1661 if (!g_bIgnoreErrors)
1662 return FALSE;
1663
1664 if (!CreateConfigParam(_T("WindowsConsoleUpgradeURL"), _T("http://www.netxms.org/download/netxms-%version%.exe"), 1, 0))
1665 if (!g_bIgnoreErrors)
1666 return FALSE;
1667
1668 if (!SQLQuery(_T("UPDATE config SET var_value='55' WHERE var_name='DBFormatVersion'")))
1669 if (!g_bIgnoreErrors)
1670 return FALSE;
1671
1672 return TRUE;
1673}
1674
1675
1676//
1677// Upgrade from V53 to V54
1678//
1679
1680static BOOL H_UpgradeFromV53(void)
1681{
1682 static TCHAR m_szBatch[] =
1683 _T("CREATE INDEX idx_address_lists_list_type ON address_lists(list_type)\n")
1684 _T("DELETE FROM config WHERE var_name='EnableAccessControl'\n")
1685 _T("DELETE FROM config WHERE var_name='EnableEventAccessControl'\n")
1686 _T("<END>");
1687
1688 if (!CreateTable(_T("CREATE TABLE address_lists (")
1689 _T("list_type integer not null,")
1690 _T("addr_type integer not null,")
1691 _T("addr1 varchar(15) not null,")
1692 _T("addr2 varchar(15) not null)")))
1693 if (!g_bIgnoreErrors)
1694 return FALSE;
1695
1696 if (!SQLBatch(m_szBatch))
1697 if (!g_bIgnoreErrors)
1698 return FALSE;
1699
1700 if (!CreateConfigParam(_T("ActiveNetworkDiscovery"), _T("0"), 1, 1))
1701 if (!g_bIgnoreErrors)
1702 return FALSE;
1703
1704 if (!CreateConfigParam(_T("ActiveDiscoveryInterval"), _T("7200"), 1, 1))
1705 if (!g_bIgnoreErrors)
1706 return FALSE;
1707
1708 if (!CreateConfigParam(_T("DiscoveryFilterFlags"), _T("0"), 1, 0))
1709 if (!g_bIgnoreErrors)
1710 return FALSE;
1711
1712 if (!SQLQuery(_T("UPDATE config SET var_value='54' WHERE var_name='DBFormatVersion'")))
1713 if (!g_bIgnoreErrors)
1714 return FALSE;
1715
1716 return TRUE;
1717}
1718
1719
1720//
1721// Upgrade from V52 to V53
1722//
1723
1724static BOOL H_UpgradeFromV52(void)
1725{
1726 DB_RESULT hResult;
1727 int i, nCount;
1728 DWORD dwId;
1729 TCHAR szQuery[1024];
1730 static const TCHAR *pszNewIdx[] =
1731 {
1732 _T("CREATE INDEX idx_idata_%d_id_timestamp ON idata_%d(item_id,idata_timestamp)"), // MySQL
1733 _T("CREATE INDEX idx_idata_%d_timestamp_id ON idata_%d(idata_timestamp,item_id)"), // POstgreSQL
1734 _T("CREATE CLUSTERED INDEX idx_idata_%d_id_timestamp ON idata_%d(item_id,idata_timestamp)"), // MS SQL
1735 _T("CREATE INDEX idx_idata_%d_timestamp_id ON idata_%d(idata_timestamp,item_id)"), // Oracle
1736 _T("CREATE INDEX idx_idata_%d_timestamp_id ON idata_%d(idata_timestamp,item_id)") // SQLite
1737 };
1738
1739 hResult = SQLSelect(_T("SELECT id FROM nodes"));
1740 if (hResult == NULL)
1741 return FALSE;
1742
1743 _tprintf(_T("Reindexing database:\n"));
1744 nCount = DBGetNumRows(hResult);
1745 for(i = 0; i < nCount; i++)
1746 {
1747 dwId = DBGetFieldULong(hResult, i, 0);
1748 _tprintf(" * idata_%d\n", dwId);
1749
1750 // Drop old indexes
1751 _stprintf(szQuery, _T("DROP INDEX idx_idata_%d_timestamp"), dwId);
1752 DBQuery(g_hCoreDB, szQuery);
1753
1754 // Create new index
1755 _stprintf(szQuery, pszNewIdx[g_iSyntax], dwId, dwId);
1756 SQLQuery(szQuery);
1757 }
1758
1759 DBFreeResult(hResult);
1760
1761 // Update index creation command
1762 DBQuery(g_hCoreDB, _T("DELETE FROM config WHERE var_name='IDataIndexCreationCommand_1'"));
1763 if (!CreateConfigParam(_T("IDataIndexCreationCommand_1"), pszNewIdx[g_iSyntax], 0, 1))
1764 if (!g_bIgnoreErrors)
1765 return FALSE;
1766
1767 if (!SQLQuery(_T("UPDATE config SET var_value='53' WHERE var_name='DBFormatVersion'")))
1768 if (!g_bIgnoreErrors)
1769 return FALSE;
1770
1771 return TRUE;
1772}
1773
1774
1775//
1776// Upgrade from V51 to V52
1777//
1778
1779static BOOL H_UpgradeFromV51(void)
1780{
1781 static TCHAR m_szBatch[] =
1782 "UPDATE object_tools SET tool_data='Configured ICMP targets#7FICMP.TargetList#7F^(.*) (.*) (.*) (.*) (.*)' WHERE tool_id=12\n"
1783 "UPDATE object_tools_table_columns SET col_number=4 WHERE col_number=3 AND tool_id=12\n"
1784 "UPDATE object_tools_table_columns SET col_number=3 WHERE col_number=2 AND tool_id=12\n"
1785 "UPDATE object_tools_table_columns SET col_substr=5 WHERE col_number=1 AND tool_id=12\n"
1786 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
1787 "VALUES (12,2,'Packet size','',0,4)\n"
1788 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description,confirmation_text) "
1789 "VALUES (16,'&Info->Active &user sessions',3,"
1790 "'Active User Sessions#7FSystem.ActiveUserSessions#7F^\"(.*)\" \"(.*)\" \"(.*)\"',"
1791 "2,'','Show list of active user sessions','#00')\n"
1792 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
1793 "VALUES (16,0,'User','',0,1)\n"
1794 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
1795 "VALUES (16,1,'Terminal','',0,2)\n"
1796 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
1797 "VALUES (16,2,'From','',0,3)\n"
1798 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (16,-2147483648)\n"
1799 "<END>";
1800
1801 if (!SQLBatch(m_szBatch))
1802 if (!g_bIgnoreErrors)
1803 return FALSE;
1804
1805 if (!CreateConfigParam(_T("MailEncoding"), _T("iso-8859-1"), 1, 0))
1806 if (!g_bIgnoreErrors)
1807 return FALSE;
1808
1809 if (!SQLQuery(_T("UPDATE config SET var_value='52' WHERE var_name='DBFormatVersion'")))
1810 if (!g_bIgnoreErrors)
1811 return FALSE;
1812
1813 return TRUE;
1814}
1815
1816
1817//
1818// Upgrade from V50 to V51
1819//
1820
1821static BOOL H_UpgradeFromV50(void)
1822{
1823 static TCHAR m_szBatch[] =
1824 "ALTER TABLE event_groups ADD range_start integer\n"
1825 "ALTER TABLE event_groups ADD range_END integer\n"
1826 "UPDATE event_groups SET range_start=0,range_end=0\n"
1827 "<END>";
1828
1829 if (!SQLBatch(m_szBatch))
1830 if (!g_bIgnoreErrors)
1831 return FALSE;
1832
1833 if (!SQLQuery(_T("UPDATE config SET var_value='51' WHERE var_name='DBFormatVersion'")))
1834 if (!g_bIgnoreErrors)
1835 return FALSE;
1836
1837 return TRUE;
1838}
1839
1840
1841//
1842// Upgrade from V49 to V50
1843//
1844
1845static BOOL H_UpgradeFromV49(void)
1846{
1847 static TCHAR m_szBatch[] =
1848 "ALTER TABLE object_tools ADD confirmation_text varchar(255)\n"
1849 "UPDATE object_tools SET confirmation_text='#00'\n"
1850 "UPDATE object_tools SET flags=10 WHERE tool_id=1 OR tool_id=2 OR tool_id=4\n"
1851 "UPDATE object_tools SET confirmation_text='Host #25OBJECT_NAME#25 (#25OBJECT_IP_ADDR#25) will be shut down. Are you sure?' WHERE tool_id=1\n"
1852 "UPDATE object_tools SET confirmation_text='Host #25OBJECT_NAME#25 (#25OBJECT_IP_ADDR#25) will be restarted. Are you sure?' WHERE tool_id=2\n"
1853 "UPDATE object_tools SET confirmation_text='NetXMS agent on host #25OBJECT_NAME#25 (#25OBJECT_IP_ADDR#25) will be restarted. Are you sure?' WHERE tool_id=4\n"
1854 "<END>";
1855
1856 if (!SQLBatch(m_szBatch))
1857 if (!g_bIgnoreErrors)
1858 return FALSE;
1859
1860 if (!SQLQuery(_T("UPDATE config SET var_value='50' WHERE var_name='DBFormatVersion'")))
1861 if (!g_bIgnoreErrors)
1862 return FALSE;
1863
1864 return TRUE;
1865}
1866
1867
1868//
1869// Upgrade from V48 to V49
1870//
1871
1872static BOOL H_UpgradeFromV48(void)
1873{
1874 static TCHAR m_szBatch[] =
1875 "ALTER TABLE items ADD all_thresholds integer\n"
1876 "UPDATE items SET all_thresholds=0\n"
1877 "ALTER TABLE thresholds ADD rearm_event_code integer\n"
1878 "UPDATE thresholds SET rearm_event_code=18\n"
1879 "<END>";
1880
1881 if (!SQLBatch(m_szBatch))
1882 if (!g_bIgnoreErrors)
1883 return FALSE;
1884
1885 if (!SQLQuery(_T("UPDATE config SET var_value='49' WHERE var_name='DBFormatVersion'")))
1886 if (!g_bIgnoreErrors)
1887 return FALSE;
1888
1889 return TRUE;
1890}
1891
1892
1893//
1894// Upgrade from V47 to V48
1895//
1896
1897static BOOL H_UpgradeFromV47(void)
1898{
1899 static TCHAR m_szBatch[] =
1900 "ALTER TABLE event_policy ADD script $SQL:TEXT\n"
1901 "UPDATE event_policy SET script='#00'\n"
1902 "<END>";
1903
1904 if (!SQLBatch(m_szBatch))
1905 if (!g_bIgnoreErrors)
1906 return FALSE;
1907
1908 if (!CreateTable(_T("CREATE TABLE policy_time_range_list (")
1909 _T("rule_id integer not null,")
1910 _T("time_range_id integer not null,")
1911 _T("PRIMARY KEY(rule_id,time_range_id))")))
1912 if (!g_bIgnoreErrors)
1913 return FALSE;
1914
1915 if (!CreateTable(_T("CREATE TABLE time_ranges (")
1916 _T("time_range_id integer not null,")
1917 _T("wday_mask integer not null,")
1918 _T("mday_mask integer not null,")
1919 _T("month_mask integer not null,")
1920 _T("time_range varchar(255) not null,")
1921 _T("PRIMARY KEY(time_range_id))")))
1922 if (!g_bIgnoreErrors)
1923 return FALSE;
1924
1925 if (!SQLQuery(_T("UPDATE config SET var_value='48' WHERE var_name='DBFormatVersion'")))
1926 if (!g_bIgnoreErrors)
1927 return FALSE;
1928
1929 return TRUE;
1930}
1931
1932
1933//
1934// Upgrade from V46 to V47
1935//
1936
1937static BOOL H_UpgradeFromV46(void)
1938{
1939 static TCHAR m_szBatch[] =
1940 "ALTER TABLE object_properties ADD comments $SQL:TEXT\n"
1941 "UPDATE object_properties SET comments='#00'\n"
1942 "ALTER TABLE nodes DROP COLUMN discovery_flags\n"
1943 "DROP TABLE alarm_notes\n"
1944 "ALTER TABLE alarms ADD alarm_state integer\n"
1945 "ALTER TABLE alarms ADD hd_state integer\n"
1946 "ALTER TABLE alarms ADD hd_ref varchar(63)\n"
1947 "ALTER TABLE alarms ADD creation_time integer\n"
1948 "ALTER TABLE alarms ADD last_change_time integer\n"
1949 "ALTER TABLE alarms ADD original_severity integer\n"
1950 "ALTER TABLE alarms ADD current_severity integer\n"
1951 "ALTER TABLE alarms ADD repeat_count integer\n"
1952 "ALTER TABLE alarms ADD term_by integer\n"
1953 "UPDATE alarms SET hd_state=0,hd_ref='#00',creation_time=alarm_timestamp,"
1954 "last_change_time=alarm_timestamp,original_severity=severity,"
1955 "current_severity=severity,repeat_count=1,term_by=ack_by\n"
1956 "UPDATE alarms SET alarm_state=0 WHERE is_ack=0\n"
1957 "UPDATE alarms SET alarm_state=2 WHERE is_ack<>0\n"
1958 "ALTER TABLE alarms DROP COLUMN severity\n"
1959 "ALTER TABLE alarms DROP COLUMN alarm_timestamp\n"
1960 "ALTER TABLE alarms DROP COLUMN is_ack\n"
1961 "ALTER TABLE thresholds ADD current_state integer\n"
1962 "UPDATE thresholds SET current_state=0\n"
1963 "<END>";
1964 static TCHAR m_szBatch2[] =
1965 "CREATE INDEX idx_alarm_notes_alarm_id ON alarm_notes(alarm_id)\n"
1966 "CREATE INDEX idx_alarm_change_log_alarm_id ON alarm_change_log(alarm_id)\n"
1967 "<END>";
1968
1969 if (!SQLBatch(m_szBatch))
1970 if (!g_bIgnoreErrors)
1971 return FALSE;
1972
1973 if (!CreateTable(_T("CREATE TABLE alarm_notes ("
1974 "note_id integer not null,"
1975 "alarm_id integer not null,"
1976 "change_time integer not null,"
1977 "user_id integer not null,"
1978 "note_text $SQL:TEXT not null,"
1979 "PRIMARY KEY(note_id))")))
1980 if (!g_bIgnoreErrors)
1981 return FALSE;
1982
1983 if (!CreateTable(_T("CREATE TABLE alarm_change_log ("
1984 "change_id $SQL:INT64 not null,"
1985 "change_time integer not null,"
1986 "alarm_id integer not null,"
1987 "opcode integer not null,"
1988 "user_id integer not null,"
1989 "info_text $SQL:TEXT not null,"
1990 "PRIMARY KEY(change_id))")))
1991 if (!g_bIgnoreErrors)
1992 return FALSE;
1993
1994 if (!CreateTable(_T("CREATE TABLE alarm_grops ("
1995 "alarm_group_id integer not null,"
1996 "group_name varchar(255) not null,"
1997 "PRIMARY KEY(alarm_group_id))")))
1998 if (!g_bIgnoreErrors)
1999 return FALSE;
2000
2001 if (!CreateTable(_T("CREATE TABLE alarm_group_map ("
2002 "alarm_group_id integer not null,"
2003 "alarm_id integer not null,"
2004 "PRIMARY KEY(alarm_group_id,alarm_id))")))
2005 if (!g_bIgnoreErrors)
2006 return FALSE;
2007
2008 if (!SQLBatch(m_szBatch2))
2009 if (!g_bIgnoreErrors)
2010 return FALSE;
2011
2012 if (!SQLQuery(_T("UPDATE config SET var_value='47' WHERE var_name='DBFormatVersion'")))
2013 if (!g_bIgnoreErrors)
2014 return FALSE;
2015
2016 return TRUE;
2017}
2018
2019
2020//
2021// Upgrade from V45 to V46
2022//
2023
2024static BOOL H_UpgradeFromV45(void)
2025{
2026 static TCHAR m_szBatch[] =
2027 "UPDATE object_tools_table_columns SET col_format=5 WHERE tool_id=5 AND col_number=1\n"
2028 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2029 "VALUES (2,'.1.3.6.1.4.1.45.3.26.*',3,0)\n"
2030 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2031 "VALUES (3,'.1.3.6.1.4.1.45.3.30.*',3,0)\n"
2032 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2033 "VALUES (4,'.1.3.6.1.4.1.45.3.31.*',3,0)\n"
2034 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2035 "VALUES (5,'.1.3.6.1.4.1.45.3.32.*',3,0)\n"
2036 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2037 "VALUES (6,'.1.3.6.1.4.1.45.3.33.*',3,0)\n"
2038 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2039 "VALUES (7,'.1.3.6.1.4.1.45.3.34.*',3,0)\n"
2040 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2041 "VALUES (8,'.1.3.6.1.4.1.45.3.35.*',3,0)\n"
2042 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2043 "VALUES (9,'.1.3.6.1.4.1.45.3.36.*',3,0)\n"
2044 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2045 "VALUES (10,'.1.3.6.1.4.1.45.3.40.*',3,0)\n"
2046 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2047 "VALUES (11,'.1.3.6.1.4.1.45.3.61.*',3,0)\n"
2048 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description) "
2049 "VALUES (14,'&Info->Topology table (Nortel)',2 ,'Topology table',1,'','Show topology table (Nortel protocol)')\n"
2050 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2051 "VALUES (14,0,'Peer IP','.1.3.6.1.4.1.45.1.6.13.2.1.1.3',3 ,0)\n"
2052 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2053 "VALUES (14,1,'Peer MAC','.1.3.6.1.4.1.45.1.6.13.2.1.1.5',4 ,0)\n"
2054 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2055 "VALUES (14,2,'Port','.1.3.6.1.4.1.45.1.6.13.2.1.1.2',5 ,0)\n"
2056 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (14,-2147483648)\n"
2057 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,matching_oid,description) "
2058 "VALUES (15,'&Info->Topology table (CDP)',2 ,'Topology table',1,'','Show topology table (CDP)')\n"
2059 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2060 "VALUES (15,0,'Device ID','.1.3.6.1.4.1.9.9.23.1.2.1.1.6',0 ,0)\n"
2061 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2062 "VALUES (15,1,'IP Address','.1.3.6.1.4.1.9.9.23.1.2.1.1.4',3 ,0)\n"
2063 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2064 "VALUES (15,2,'Platform','.1.3.6.1.4.1.9.9.23.1.2.1.1.8',0 ,0)\n"
2065 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2066 "VALUES (15,3,'Version','.1.3.6.1.4.1.9.9.23.1.2.1.1.5',0 ,0)\n"
2067 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2068 "VALUES (15,4,'Port','.1.3.6.1.4.1.9.9.23.1.2.1.1.7',0 ,0)\n"
2069 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (15,-2147483648)\n"
2070 "<END>";
2071
2072 if (!SQLBatch(m_szBatch))
2073 if (!g_bIgnoreErrors)
2074 return FALSE;
2075
2076 if (!SQLQuery(_T("UPDATE config SET var_value='46' WHERE var_name='DBFormatVersion'")))
2077 if (!g_bIgnoreErrors)
2078 return FALSE;
2079
2080 return TRUE;
2081}
2082
2083
2084//
2085// Upgrade from V44 to V45
2086//
2087
2088static BOOL H_UpgradeFromV44(void)
2089{
2090 static TCHAR m_szBatch[] =
2091 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
2092 "VALUES (36,'SYS_DB_CONN_LOST',4,1,"
2093 "'Lost connection with backend database engine',"
2094 "'Generated if connection with backend database engine is lost.#0D#0A"
2095 "Parameters:#0D#0A No message-specific parameters')\n"
2096 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
2097 "VALUES (37,'SYS_DB_CONN_RESTORED',0,1,"
2098 "'Connection with backend database engine restored',"
2099 "'Generated when connection with backend database engine restored.#0D#0A"
2100 "Parameters:#0D#0A No message-specific parameters')\n"
2101 "<END>";
2102
2103 if (!SQLBatch(m_szBatch))
2104 if (!g_bIgnoreErrors)
2105 return FALSE;
2106
2107 if (!SQLQuery(_T("UPDATE config SET var_value='45' WHERE var_name='DBFormatVersion'")))
2108 if (!g_bIgnoreErrors)
2109 return FALSE;
2110
2111 return TRUE;
2112}
2113
2114
2115//
2116// Upgrade from V43 to V44
2117//
2118
2119static BOOL H_UpgradeFromV43(void)
2120{
2121 static TCHAR m_szBatch[] =
2122 "DELETE FROM object_tools WHERE tool_id=8000\n"
2123 "DELETE FROM object_tools_table_columns WHERE tool_id=8000\n"
2124 "DELETE FROM object_tools_acl WHERE tool_id=8000\n"
2125 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,"
2126 "matching_oid,description) VALUES (13,'&Info->&Process list',3,"
2127 "'Process List#7FSystem.ProcessList#7F^([0-9]+) (.*)',2,'',"
2128 "'Show list of currently running processes')\n"
2129 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,"
2130 "col_oid,col_format,col_substr) VALUES (13,0,'PID','',0,1)\n"
2131 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,"
2132 "col_oid,col_format,col_substr) VALUES (13,1,'Name','',0,2)\n"
2133 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (13,-2147483648)\n"
2134 "<END>";
2135
2136 if (!CreateTable(_T("CREATE TABLE agent_configs ("
2137 "config_id integer not null,"
2138 "config_name varchar(255) not null,"
2139 "config_file $SQL:TEXT not null,"
2140 "config_filter $SQL:TEXT not null,"
2141 "sequence_number integer not null,"
2142 "PRIMARY KEY(config_id))")))
2143 if (!g_bIgnoreErrors)
2144 return FALSE;
2145
2146 if (!SQLBatch(m_szBatch))
2147 if (!g_bIgnoreErrors)
2148 return FALSE;
2149
2150 if (!CreateConfigParam(_T("DBLockPID"), _T("0"), 0, 0))
2151 if (!g_bIgnoreErrors)
2152 return FALSE;
2153
2154 if (!SQLQuery(_T("UPDATE config SET var_value='44' WHERE var_name='DBFormatVersion'")))
2155 if (!g_bIgnoreErrors)
2156 return FALSE;
2157
2158 return TRUE;
2159}
2160
2161
2162//
2163// Upgrade from V42 to V43
2164//
2165
2166static BOOL H_UpgradeFromV42(void)
2167{
2168 if (!CreateConfigParam(_T("RADIUSPort"), _T("1645"), 1, 0))
2169 if (!g_bIgnoreErrors)
2170 return FALSE;
2171
2172 if (!SQLQuery(_T("UPDATE config SET var_value='43' WHERE var_name='DBFormatVersion'")))
2173 if (!g_bIgnoreErrors)
2174 return FALSE;
2175
2176 return TRUE;
2177}
2178
2179
2180//
2181// Upgrade from V41 to V42
2182//
2183
2184static BOOL H_UpgradeFromV41(void)
2185{
2186 static TCHAR m_szBatch[] =
2187 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,"
2188 "file_name_ico,file_hash_ico) VALUES (15,'Obj.Condition',"
2189 "'condition.png','<invalid_hash>','condition.ico','<invalid_hash>')\n"
2190 "INSERT INTO default_images (object_class,image_id) VALUES (13, 15)\n"
2191 "INSERT INTO oid_to_type (pair_id,snmp_oid,node_type,node_flags) "
2192 "VALUES (1,'.1.3.6.1.4.1.3224.1.*',2,0)\n"
2193 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
2194 "VALUES (34,'SYS_CONDITION_ACTIVATED',2,1,'Condition \"%2\" activated',"
2195 "'Default event for condition activation.#0D#0AParameters:#0D#0A"
2196 " 1) Condition object ID#0D#0A 2) Condition object name#0D#0A"
2197 " 3) Previous condition status#0D#0A 4) Current condition status')\n"
2198 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
2199 "VALUES (35,'SYS_CONDITION_DEACTIVATED',0,1,'Condition \"%2\" deactivated',"
2200 "'Default event for condition deactivation.#0D#0AParameters:#0D#0A"
2201 " 1) Condition object ID#0D#0A 2) Condition object name#0D#0A"
2202 " 3) Previous condition status#0D#0A 4) Current condition status')\n"
2203 "<END>";
2204
2205 if (!CreateTable(_T("CREATE TABLE conditions ("
2206 "id integer not null,"
2207 "activation_event integer not null,"
2208 "deactivation_event integer not null,"
2209 "source_object integer not null,"
2210 "active_status integer not null,"
2211 "inactive_status integer not null,"
2212 "script $SQL:TEXT not null,"
2213 "PRIMARY KEY(id))")))
2214 if (!g_bIgnoreErrors)
2215 return FALSE;
2216
2217 if (!CreateTable(_T("CREATE TABLE cond_dci_map ("
2218 "condition_id integer not null,"
2219 "dci_id integer not null,"
2220 "node_id integer not null,"
2221 "dci_func integer not null,"
2222 "num_polls integer not null,"
2223 "PRIMARY KEY(condition_id,dci_id))")))
2224 if (!g_bIgnoreErrors)
2225 return FALSE;
2226
2227 if (!SQLBatch(m_szBatch))
2228 if (!g_bIgnoreErrors)
2229 return FALSE;
2230
2231 if (!CreateConfigParam(_T("NumberOfConditionPollers"), _T("10"), 1, 1))
2232 if (!g_bIgnoreErrors)
2233 return FALSE;
2234
2235 if (!CreateConfigParam(_T("ConditionPollingInterval"), _T("60"), 1, 1))
2236 if (!g_bIgnoreErrors)
2237 return FALSE;
2238
2239 if (!SQLQuery(_T("UPDATE config SET var_value='42' WHERE var_name='DBFormatVersion'")))
2240 if (!g_bIgnoreErrors)
2241 return FALSE;
2242
2243 return TRUE;
2244}
2245
2246
2247//
2248// Upgrade from V40 to V41
2249//
2250
2251static BOOL H_UpgradeFromV40(void)
2252{
2253 static TCHAR m_szBatch[] =
2254 "ALTER TABLE users ADD guid varchar(36)\n"
2255 "ALTER TABLE users ADD auth_method integer\n"
2256 "ALTER TABLE user_groups ADD guid varchar(36)\n"
2257 "UPDATE users SET auth_method=0\n"
2258 "<END>";
2259 DB_RESULT hResult;
2260 int i, nCount;
2261 DWORD dwId;
2262 uuid_t guid;
2263 TCHAR szQuery[256], szGUID[64];
2264
2265 if (!SQLBatch(m_szBatch))
2266 if (!g_bIgnoreErrors)
2267 return FALSE;
2268
2269 // Generate GUIDs for users and groups
2270 printf("Generating GUIDs...\n");
2271
2272 hResult = SQLSelect(_T("SELECT id FROM users"));
2273 if (hResult != NULL)
2274 {
2275 nCount = DBGetNumRows(hResult);
2276 for(i = 0; i < nCount; i++)
2277 {
2278 dwId = DBGetFieldULong(hResult, i, 0);
2279 uuid_generate(guid);
2280 _sntprintf(szQuery, 256, _T("UPDATE users SET guid='%s' WHERE id=%d"),
2281 uuid_to_string(guid, szGUID), dwId);
2282 if (!SQLQuery(szQuery))
2283 if (!g_bIgnoreErrors)
2284 {
2285 DBFreeResult(hResult);
2286 return FALSE;
2287 }
2288 }
2289 DBFreeResult(hResult);
2290 }
2291
2292 hResult = SQLSelect(_T("SELECT id FROM user_groups"));
2293 if (hResult != NULL)
2294 {
2295 nCount = DBGetNumRows(hResult);
2296 for(i = 0; i < nCount; i++)
2297 {
2298 dwId = DBGetFieldULong(hResult, i, 0);
2299 uuid_generate(guid);
2300 _sntprintf(szQuery, 256, _T("UPDATE user_groups SET guid='%s' WHERE id=%d"),
2301 uuid_to_string(guid, szGUID), dwId);
2302 if (!SQLQuery(szQuery))
2303 if (!g_bIgnoreErrors)
2304 {
2305 DBFreeResult(hResult);
2306 return FALSE;
2307 }
2308 }
2309 DBFreeResult(hResult);
2310 }
2311
2312 if (!CreateConfigParam(_T("RADIUSServer"), _T("localhost"), 1, 0))
2313 if (!g_bIgnoreErrors)
2314 return FALSE;
2315
2316 if (!CreateConfigParam(_T("RADIUSSecret"), _T("netxms"), 1, 0))
2317 if (!g_bIgnoreErrors)
2318 return FALSE;
2319
2320 if (!CreateConfigParam(_T("RADIUSNumRetries"), _T("5"), 1, 0))
2321 if (!g_bIgnoreErrors)
2322 return FALSE;
2323
2324 if (!CreateConfigParam(_T("RADIUSTimeout"), _T("3"), 1, 0))
2325 if (!g_bIgnoreErrors)
2326 return FALSE;
2327
2328 if (!SQLQuery(_T("UPDATE config SET var_value='41' WHERE var_name='DBFormatVersion'")))
2329 if (!g_bIgnoreErrors)
2330 return FALSE;
2331
2332 return TRUE;
2333}
2334
2335
2336//
2337// Upgrade from V39 to V40
2338//
2339
2340static BOOL H_UpgradeFromV39(void)
2341{
2342 static TCHAR m_szBatch[] =
2343 "ALTER TABLE users ADD grace_logins integer\n"
2344 "UPDATE users SET grace_logins=5\n"
2345 "<END>";
2346
2347 if (!SQLBatch(m_szBatch))
2348 if (!g_bIgnoreErrors)
2349 return FALSE;
2350
2351 if (!SQLQuery(_T("UPDATE config SET var_value='40' WHERE var_name='DBFormatVersion'")))
2352 if (!g_bIgnoreErrors)
2353 return FALSE;
2354
2355 return TRUE;
2356}
2357
2358
2359//
2360// Upgrade from V38 to V39
2361//
2362
2363static BOOL H_UpgradeFromV38(void)
2364{
2365 static TCHAR m_szBatch[] =
2366 "INSERT INTO maps (map_id,map_name,description,root_object_id) "
2367 "VALUES (1,'Default','Default network map',1)\n"
2368 "INSERT INTO map_access_lists (map_id,user_id,access_rights) VALUES (1,-2147483648,1)\n"
2369 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
2370 "VALUES (33,'SYS_SCRIPT_ERROR',2,1,"
2371 "'Script (%1) execution error: %2',"
2372 "'Generated when server encounters NXSL script execution error.#0D#0A"
2373 "Parameters:#0D#0A"
2374 " 1) Script name#0D#0A"
2375 " 2) Error text#0D#0A"
2376 " 3) DCI ID if script is DCI transformation script, or 0 otherwise')\n"
2377 "<END>";
2378
2379 if (!CreateTable(_T("CREATE TABLE maps ("
2380 "map_id integer not null,"
2381 "map_name varchar(255) not null,"
2382 "description $SQL:TEXT not null,"
2383 "root_object_id integer not null,"
2384 "PRIMARY KEY(map_id))")))
2385 if (!g_bIgnoreErrors)
2386 return FALSE;
2387
2388 if (!CreateTable(_T("CREATE TABLE map_access_lists ("
2389 "map_id integer not null,"
2390 "user_id integer not null,"
2391 "access_rights integer not null,"
2392 "PRIMARY KEY(map_id,user_id))")))
2393 if (!g_bIgnoreErrors)
2394 return FALSE;
2395
2396 if (!CreateTable(_T("CREATE TABLE submaps ("
2397 "map_id integer not null,"
2398 "submap_id integer not null,"
2399 "attributes integer not null,"
2400 "PRIMARY KEY(map_id,submap_id))")))
2401 if (!g_bIgnoreErrors)
2402 return FALSE;
2403
2404 if (!CreateTable(_T("CREATE TABLE submap_object_positions ("
2405 "map_id integer not null,"
2406 "submap_id integer not null,"
2407 "object_id integer not null,"
2408 "x integer not null,"
2409 "y integer not null,"
2410 "PRIMARY KEY(map_id,submap_id,object_id))")))
2411 if (!g_bIgnoreErrors)
2412 return FALSE;
2413
2414 if (!CreateTable(_T("CREATE TABLE submap_links ("
2415 "map_id integer not null,"
2416 "submap_id integer not null,"
2417 "object_id1 integer not null,"
2418 "object_id2 integer not null,"
2419 "link_type integer not null,"
2420 "PRIMARY KEY(map_id,submap_id,object_id1,object_id2))")))
2421 if (!g_bIgnoreErrors)
2422 return FALSE;
2423
2424 if (!SQLBatch(m_szBatch))
2425 if (!g_bIgnoreErrors)
2426 return FALSE;
2427
2428 if (!CreateConfigParam(_T("LockTimeout"), _T("60000"), 1, 1))
2429 if (!g_bIgnoreErrors)
2430 return FALSE;
2431
2432 if (!CreateConfigParam(_T("DisableVacuum"), _T("0"), 1, 0))
2433 if (!g_bIgnoreErrors)
2434 return FALSE;
2435
2436 if (!SQLQuery(_T("UPDATE config SET var_value='39' WHERE var_name='DBFormatVersion'")))
2437 if (!g_bIgnoreErrors)
2438 return FALSE;
2439
2440 return TRUE;
2441}
2442
2443
2444//
2445// Upgrade from V37 to V38
2446//
2447
2448static BOOL H_UpgradeFromV37(void)
2449{
2450 static TCHAR m_szBatch[] =
2451 "CREATE INDEX idx_event_log_event_timestamp ON event_log(event_timestamp)\n"
2452 "CREATE INDEX idx_syslog_msg_timestamp ON syslog(msg_timestamp)\n"
2453 "CREATE INDEX idx_snmp_trap_log_trap_timestamp ON snmp_trap_log(trap_timestamp)\n"
2454 "<END>";
2455
2456 if (!CreateTable(_T("CREATE TABLE snmp_trap_log ("
2457 "trap_id $SQL:INT64 not null,"
2458 "trap_timestamp integer not null,"
2459 "ip_addr varchar(15) not null,"
2460 "object_id integer not null,"
2461 "trap_oid varchar(255) not null,"
2462 "trap_varlist $SQL:TEXT not null,"
2463 "PRIMARY KEY(trap_id))")))
2464
2465 if (!g_bIgnoreErrors)
2466 return FALSE;
2467
2468 if (!SQLBatch(m_szBatch))
2469 if (!g_bIgnoreErrors)
2470 return FALSE;
2471
2472 if (!CreateConfigParam(_T("LogAllSNMPTraps"), _T("0"), 1, 1))
2473 if (!g_bIgnoreErrors)
2474 return FALSE;
2475
2476 if (!SQLQuery(_T("UPDATE config SET var_value='38' WHERE var_name='DBFormatVersion'")))
2477 if (!g_bIgnoreErrors)
2478 return FALSE;
2479
2480 return TRUE;
2481}
2482
2483
2484//
2485// Upgrade from V36 to V37
2486//
2487
2488static BOOL H_UpgradeFromV36(void)
2489{
2490 static TCHAR m_szBatch[] =
2491 "DROP TABLE new_nodes\n"
2492 "DELETE FROM config WHERE var_name='NewNodePollingInterval'\n"
2493 "INSERT INTO script_library (script_id,script_name,script_code) "
2494 "VALUES (1,'Filter::SNMP','sub main()#0D#0A{#0D#0A return $1->isSNMP;#0D#0A}#0D#0A')\n"
2495 "INSERT INTO script_library (script_id,script_name,script_code) "
2496 "VALUES (2,'Filter::Agent','sub main()#0D#0A{#0D#0A return $1->isAgent;#0D#0A}#0D#0A')\n"
2497 "INSERT INTO script_library (script_id,script_name,script_code) "
2498 "VALUES (3,'Filter::AgentOrSNMP','sub main()#0D#0A{#0D#0A return $1->isAgent || $1->isSNMP;#0D#0A}#0D#0A')\n"
2499 "INSERT INTO script_library (script_id,script_name,script_code) "
2500 "VALUES (4,'DCI::SampleTransform','sub dci_transform()#0D#0A{#0D#0A return $1 + 1;#0D#0A}#0D#0A')\n"
2501 "<END>";
2502
2503 if (!CreateTable(_T("CREATE TABLE script_library ("
2504 "script_id integer not null,"
2505 "script_name varchar(63) not null,"
2506 "script_code $SQL:TEXT not null,"
2507 "PRIMARY KEY(script_id))")))
2508 if (!g_bIgnoreErrors)
2509 return FALSE;
2510
2511 if (!SQLBatch(m_szBatch))
2512 if (!g_bIgnoreErrors)
2513 return FALSE;
2514
2515 if (!CreateConfigParam(_T("DefaultCommunityString"), _T("public"), 1, 0))
2516 if (!g_bIgnoreErrors)
2517 return FALSE;
2518
2519 if (!CreateConfigParam(_T("DiscoveryFilter"), _T("none"), 1, 0))
2520 if (!g_bIgnoreErrors)
2521 return FALSE;
2522
2523 if (!SQLQuery(_T("UPDATE config SET var_value='37' WHERE var_name='DBFormatVersion'")))
2524 if (!g_bIgnoreErrors)
2525 return FALSE;
2526
2527 return TRUE;
2528}
2529
2530
2531//
2532// Upgrade from V35 to V36
2533//
2534
2535static BOOL H_UpgradeFromV35(void)
2536{
2537 static TCHAR m_szBatch[] =
2538 "ALTER TABLE nodes ADD proxy_node integer\n"
2539 "UPDATE nodes SET proxy_node=0\n"
2540 "ALTER TABLE object_tools ADD matching_oid varchar(255)\n"
2541 "UPDATE object_tools SET matching_oid='#00'\n"
2542 "<END>";
2543
2544 if (!SQLBatch(m_szBatch))
2545 if (!g_bIgnoreErrors)
2546 return FALSE;
2547
2548 if (!CreateConfigParam(_T("CapabilityExpirationTime"), _T("604800"), 1, 0))
2549 if (!g_bIgnoreErrors)
2550 return FALSE;
2551
2552 if (!SQLQuery(_T("UPDATE config SET var_value='36' WHERE var_name='DBFormatVersion'")))
2553 if (!g_bIgnoreErrors)
2554 return FALSE;
2555
2556 return TRUE;
2557}
2558
2559
2560//
2561// Upgrade from V34 to V35
2562//
2563
2564static BOOL H_UpgradeFromV34(void)
2565{
2566 static TCHAR m_szBatch[] =
2567 "ALTER TABLE object_properties DROP COLUMN status_alg\n"
2568 "ALTER TABLE object_properties ADD status_calc_alg integer\n"
2569 "ALTER TABLE object_properties ADD status_prop_alg integer\n"
2570 "ALTER TABLE object_properties ADD status_fixed_val integer\n"
2571 "ALTER TABLE object_properties ADD status_shift integer\n"
2572 "ALTER TABLE object_properties ADD status_translation varchar(8)\n"
2573 "ALTER TABLE object_properties ADD status_single_threshold integer\n"
2574 "ALTER TABLE object_properties ADD status_thresholds varchar(8)\n"
2575 "UPDATE object_properties SET status_calc_alg=0,status_prop_alg=0,"
2576 "status_fixed_val=0,status_shift=0,status_translation='01020304',"
2577 "status_single_threshold=75,status_thresholds='503C2814'\n"
2578 "DELETE FROM config WHERE var_name='StatusCalculationAlgorithm'\n"
2579 "<END>";
2580
2581 if (!SQLBatch(m_szBatch))
2582 if (!g_bIgnoreErrors)
2583 return FALSE;
2584
2585 if (!CreateConfigParam(_T("StatusCalculationAlgorithm"), _T("1"), 1, 1))
2586 if (!g_bIgnoreErrors)
2587 return FALSE;
2588
2589 if (!CreateConfigParam(_T("StatusPropagationAlgorithm"), _T("1"), 1, 1))
2590 if (!g_bIgnoreErrors)
2591 return FALSE;
2592
2593 if (!CreateConfigParam(_T("FixedStatusValue"), _T("0"), 1, 1))
2594 if (!g_bIgnoreErrors)
2595 return FALSE;
2596
2597 if (!CreateConfigParam(_T("StatusShift"), _T("0"), 1, 1))
2598 if (!g_bIgnoreErrors)
2599 return FALSE;
2600
2601 if (!CreateConfigParam(_T("StatusTranslation"), _T("01020304"), 1, 1))
2602 if (!g_bIgnoreErrors)
2603 return FALSE;
2604
2605 if (!CreateConfigParam(_T("StatusSingleThreshold"), _T("75"), 1, 1))
2606 if (!g_bIgnoreErrors)
2607 return FALSE;
2608
2609 if (!CreateConfigParam(_T("StatusThresholds"), _T("503C2814"), 1, 1))
2610 if (!g_bIgnoreErrors)
2611 return FALSE;
2612
2613 if (!SQLQuery(_T("UPDATE config SET var_value='35' WHERE var_name='DBFormatVersion'")))
2614 if (!g_bIgnoreErrors)
2615 return FALSE;
2616
2617 return TRUE;
2618}
2619
2620
2621//
2622// Upgrade from V33 to V34
2623//
2624
2625static BOOL H_UpgradeFromV33(void)
2626{
2627 static TCHAR m_szBatch[] =
2628 "ALTER TABLE items ADD adv_schedule integer\n"
2629 "UPDATE items SET adv_schedule=0\n"
2630 "<END>";
2631
2632 if (!SQLBatch(m_szBatch))
2633 if (!g_bIgnoreErrors)
2634 return FALSE;
2635
2636 if (!CreateTable(_T("CREATE TABLE dci_schedules ("
2637 "item_id integer not null,"
2638 "schedule varchar(255) not null)")))
2639 if (!g_bIgnoreErrors)
2640 return FALSE;
2641
2642 if (!CreateTable(_T("CREATE TABLE syslog ("
2643 "msg_id $SQL:INT64 not null,"
2644 "msg_timestamp integer not null,"
2645 "facility integer not null,"
2646 "severity integer not null,"
2647 "source_object_id integer not null,"
2648 "hostname varchar(127) not null,"
2649 "msg_tag varchar(32) not null,"
2650 "msg_text $SQL:TEXT not null,"
2651 "PRIMARY KEY(msg_id))")))
2652 if (!g_bIgnoreErrors)
2653 return FALSE;
2654
2655 if (!CreateConfigParam(_T("IcmpPingSize"), _T("46"), 1, 1))
2656 if (!g_bIgnoreErrors)
2657 return FALSE;
2658
2659 if (!CreateConfigParam(_T("SMSDrvConfig"), _T(""), 1, 1))
2660 if (!g_bIgnoreErrors)
2661 return FALSE;
2662
2663 if (!CreateConfigParam(_T("EnableSyslogDaemon"), _T("0"), 1, 1))
2664 if (!g_bIgnoreErrors)
2665 return FALSE;
2666
2667 if (!CreateConfigParam(_T("SyslogListenPort"), _T("514"), 1, 1))
2668 if (!g_bIgnoreErrors)
2669 return FALSE;
2670
2671 if (!CreateConfigParam(_T("SyslogRetentionTime"), _T("5184000"), 1, 0))
2672 if (!g_bIgnoreErrors)
2673 return FALSE;
2674
2675 if (!SQLQuery(_T("UPDATE config SET var_value='34' WHERE var_name='DBFormatVersion'")))
2676 if (!g_bIgnoreErrors)
2677 return FALSE;
2678
2679 return TRUE;
2680}
2681
2682
2683//
2684// Upgrade from V32 to V33
2685//
2686
2687static BOOL H_UpgradeFromV32(void)
2688{
2689 static TCHAR m_szBatch[] =
2690 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2691 "VALUES (5,'&Info->&Switch forwarding database (FDB)',2 ,'Forwarding database',0,'Show switch forwarding database')\n"
2692 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2693 "VALUES (5,0,'MAC Address','.1.3.6.1.2.1.17.4.3.1.1',4 ,0)\n"
2694 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2695 "VALUES (5,1,'Port','.1.3.6.1.2.1.17.4.3.1.2',1 ,0)\n"
2696 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2697 "VALUES (6,'&Connect->Open &web browser',4 ,'http://%OBJECT_IP_ADDR%',0,'Open embedded web browser to node')\n"
2698 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2699 "VALUES (7,'&Connect->Open &web browser (HTTPS)',4 ,'https://%OBJECT_IP_ADDR%',0,'Open embedded web browser to node using HTTPS')\n"
2700 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2701 "VALUES (8,'&Info->&Agent->&Subagent list',3 ,'Subagent List#7FAgent.SubAgentList#7F^(.*) (.*) (.*) (.*)',0,'Show list of loaded subagents')\n"
2702 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2703 "VALUES (8,0,'Name','',0 ,1)\n"
2704 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2705 "VALUES (8,1,'Version','',0 ,2)\n"
2706 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2707 "VALUES (8,2,'File','',0 ,4)\n"
2708 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2709 "VALUES (8,3,'Module handle','',0 ,3)\n"
2710 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2711 "VALUES (9,'&Info->&Agent->Supported &parameters',3 ,'Supported parameters#7FAgent.SupportedParameters#7F^(.*)',0,'Show list of parameters supported by agent')\n"
2712 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2713 "VALUES (9,0,'Parameter','',0 ,1)\n"
2714 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2715 "VALUES (10,'&Info->&Agent->Supported &enums',3 ,'Supported enums#7FAgent.SupportedEnums#7F^(.*)',0,'Show list of enums supported by agent')\n"
2716 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2717 "VALUES (10,0,'Parameter','',0 ,1)\n"
2718 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2719 "VALUES (11,'&Info->&Agent->Supported &actions',3 ,'Supported actions#7FAgent.ActionList#7F^(.*) (.*) #22(.*)#22.*',0,'Show list of actions supported by agent')\n"
2720 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2721 "VALUES (11,0,'Name','',0 ,1)\n"
2722 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2723 "VALUES (11,1,'Type','',0 ,2)\n"
2724 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2725 "VALUES (11,2,'Data','',0 ,3)\n"
2726 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2727 "VALUES (12,'&Info->&Agent->Configured &ICMP targets',3 ,'Configured ICMP targets#7FICMP.TargetList#7F^(.*) (.*) (.*) (.*)',0,'Show list of actions supported by agent')\n"
2728 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2729 "VALUES (12,0,'IP Address','',0 ,1)\n"
2730 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2731 "VALUES (12,1,'Name','',0 ,4)\n"
2732 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2733 "VALUES (12,2,'Last RTT','',0 ,2)\n"
2734 "INSERT INTO object_tools_table_columns (tool_id,col_number,col_name,col_oid,col_format,col_substr) "
2735 "VALUES (12,4,'Average RTT','',0 ,3)\n"
2736 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (5,-2147483648)\n"
2737 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (6,-2147483648)\n"
2738 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (7,-2147483648)\n"
2739 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (8,-2147483648)\n"
2740 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (9,-2147483648)\n"
2741 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (10,-2147483648)\n"
2742 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (11,-2147483648)\n"
2743 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (12,-2147483648)\n"
2744 "<END>";
2745
2746 if (!CreateTable(_T("CREATE TABLE object_tools_table_columns ("
2747 "tool_id integer not null,"
2748 "col_number integer not null,"
2749 "col_name varchar(255),"
2750 "col_oid varchar(255),"
2751 "col_format integer,"
2752 "col_substr integer,"
2753 "PRIMARY KEY(tool_id,col_number))")))
2754 if (!g_bIgnoreErrors)
2755 return FALSE;
2756
2757 if (!SQLBatch(m_szBatch))
2758 if (!g_bIgnoreErrors)
2759 return FALSE;
2760
2761 if (!SQLQuery(_T("UPDATE config SET var_value='33' WHERE var_name='DBFormatVersion'")))
2762 if (!g_bIgnoreErrors)
2763 return FALSE;
2764
2765 return TRUE;
2766}
2767
2768
2769//
2770// Upgrade from V31 to V32
2771//
2772
2773static BOOL H_UpgradeFromV31(void)
2774{
2775 static TCHAR m_szBatch[] =
2776 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2777 "VALUES (1,'&Shutdown system',1 ,'System.Shutdown',0,'Shutdown target node via NetXMS agent')\n"
2778 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2779 "VALUES (2,'&Restart system',1 ,'System.Restart',0,'Restart target node via NetXMS agent')\n"
2780 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2781 "VALUES (3,'&Wakeup node',0 ,'wakeup',0,'Wakeup node using Wake-On-LAN magic packet')\n"
2782 "INSERT INTO object_tools (tool_id,tool_name,tool_type,tool_data,flags,description) "
2783 "VALUES (4,'Restart &agent',1 ,'Agent.Restart',0,'Restart NetXMS agent on target node')\n"
2784 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (1,-2147483648)\n"
2785 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (2,-2147483648)\n"
2786 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (3,-2147483648)\n"
2787 "INSERT INTO object_tools_acl (tool_id,user_id) VALUES (4,-2147483648)\n"
2788 "<END>";
2789
2790 if (!CreateTable(_T("CREATE TABLE object_tools ("
2791 "tool_id integer not null,"
2792 "tool_name varchar(255) not null,"
2793 "tool_type integer not null,"
2794 "tool_data $SQL:TEXT,"
2795 "description varchar(255),"
2796 "flags integer not null,"
2797 "PRIMARY KEY(tool_id))")))
2798 if (!g_bIgnoreErrors)
2799 return FALSE;
2800
2801 if (!CreateTable(_T("CREATE TABLE object_tools_acl ("
2802 "tool_id integer not null,"
2803 "user_id integer not null,"
2804 "PRIMARY KEY(tool_id,user_id))")))
2805 if (!g_bIgnoreErrors)
2806 return FALSE;
2807
2808 if (!SQLBatch(m_szBatch))
2809 if (!g_bIgnoreErrors)
2810 return FALSE;
2811
2812 if (!SQLQuery(_T("UPDATE config SET var_value='32' WHERE var_name='DBFormatVersion'")))
2813 if (!g_bIgnoreErrors)
2814 return FALSE;
2815
2816 return TRUE;
2817}
2818
2819
2820//
2821// Upgrade from V30 to V31
2822//
2823
2824static BOOL H_UpgradeFromV30(void)
2825{
2826 static TCHAR m_szBatch[] =
2827 "INSERT INTO default_images (object_class,image_id) "
2828 "VALUES (12, 14)\n"
2829 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,"
2830 "file_name_ico,file_hash_ico) VALUES (14,'Obj.VPNConnector',"
2831 "'vpnc.png','<invalid_hash>','vpnc.ico','<invalid_hash>')\n"
2832 "<END>";
2833
2834 if (!SQLBatch(m_szBatch))
2835 if (!g_bIgnoreErrors)
2836 return FALSE;
2837
2838 if (!CreateTable(_T("CREATE TABLE vpn_connectors ("
2839 "id integer not null,"
2840 "node_id integer not null,"
2841 "peer_gateway integer not null,"
2842 "PRIMARY KEY(id))")))
2843 if (!g_bIgnoreErrors)
2844 return FALSE;
2845
2846 if (!CreateTable(_T("CREATE TABLE vpn_connector_networks ("
2847 "vpn_id integer not null,"
2848 "network_type integer not null,"
2849 "ip_addr varchar(15) not null,"
2850 "ip_netmask varchar(15) not null,"
2851 "PRIMARY KEY(vpn_id,ip_addr))")))
2852 if (!g_bIgnoreErrors)
2853 return FALSE;
2854
2855 if (!CreateConfigParam(_T("NumberOfRoutingTablePollers"), _T("5"), 1, 1))
2856 if (!g_bIgnoreErrors)
2857 return FALSE;
2858
2859 if (!CreateConfigParam(_T("RoutingTableUpdateInterval"), _T("300"), 1, 1))
2860 if (!g_bIgnoreErrors)
2861 return FALSE;
2862
2863 if (!SQLQuery(_T("UPDATE config SET var_value='31' WHERE var_name='DBFormatVersion'")))
2864 if (!g_bIgnoreErrors)
2865 return FALSE;
2866
2867 return TRUE;
2868}
2869
2870
2871//
2872// Upgrade from V29 to V30
2873//
2874
2875static BOOL H_UpgradeFromV29(void)
2876{
2877 static TCHAR m_szBatch[] =
2878 "ALTER TABLE object_properties ADD status_alg integer\n"
2879 "UPDATE object_properties SET status_alg=-1\n"
2880 "<END>";
2881
2882 if (!SQLBatch(m_szBatch))
2883 if (!g_bIgnoreErrors)
2884 return FALSE;
2885
2886 if (!CreateConfigParam(_T("StatusCalculationAlgorithm"), _T("0"), 1, 1))
2887 if (!g_bIgnoreErrors)
2888 return FALSE;
2889
2890 if (!CreateConfigParam(_T("EnableMultipleDBConnections"), _T("1"), 1, 1))
2891 if (!g_bIgnoreErrors)
2892 return FALSE;
2893
2894 if (!CreateConfigParam(_T("NumberOfDatabaseWriters"), _T("1"), 1, 1))
2895 if (!g_bIgnoreErrors)
2896 return FALSE;
2897
2898 if (!CreateConfigParam(_T("DefaultEncryptionPolicy"), _T("1"), 1, 1))
2899 if (!g_bIgnoreErrors)
2900 return FALSE;
2901
2902 if (!CreateConfigParam(_T("AllowedCiphers"), _T("15"), 1, 1))
2903 if (!g_bIgnoreErrors)
2904 return FALSE;
2905
2906 if (!CreateConfigParam(_T("KeepAliveInterval"), _T("60"), 1, 1))
2907 if (!g_bIgnoreErrors)
2908 return FALSE;
2909
2910 if (!SQLQuery(_T("UPDATE config SET var_value='30' WHERE var_name='DBFormatVersion'")))
2911 if (!g_bIgnoreErrors)
2912 return FALSE;
2913
2914 return TRUE;
2915}
2916
2917
2918//
2919// Upgrade from V28 to V29
2920//
2921
2922static BOOL H_UpgradeFromV28(void)
2923{
2924 static TCHAR m_szBatch[] =
2925 "ALTER TABLE nodes ADD zone_guid integer\n"
2926 "ALTER TABLE subnets ADD zone_guid integer\n"
2927 "UPDATE nodes SET zone_guid=0\n"
2928 "UPDATE subnets SET zone_guid=0\n"
2929 "INSERT INTO default_images (object_class,image_id) VALUES (6,13)\n"
2930 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,"
2931 "file_name_ico,file_hash_ico) VALUES (13,'Obj.Zone','zone.png',"
2932 "'<invalid_hash>','zone.ico','<invalid_hash>')\n"
2933 "<END>";
2934
2935 if (!CreateTable(_T("CREATE TABLE zones ("
2936 "id integer not null,"
2937 "zone_guid integer not null,"
2938 "zone_type integer not null,"
2939 "controller_ip varchar(15) not null,"
2940 "description $SQL:TEXT,"
2941 "PRIMARY KEY(id))")))
2942 if (!g_bIgnoreErrors)
2943 return FALSE;
2944
2945 if (!CreateTable(_T("CREATE TABLE zone_ip_addr_list ("
2946 "zone_id integer not null,"
2947 "ip_addr varchar(15) not null,"
2948 "PRIMARY KEY(zone_id,ip_addr))")))
2949 if (!g_bIgnoreErrors)
2950 return FALSE;
2951
2952 if (!SQLBatch(m_szBatch))
2953 if (!g_bIgnoreErrors)
2954 return FALSE;
2955
2956 if (!CreateConfigParam(_T("EnableZoning"), _T("0"), 1, 1))
2957 if (!g_bIgnoreErrors)
2958 return FALSE;
2959
2960 if (!SQLQuery(_T("UPDATE config SET var_value='29' WHERE var_name='DBFormatVersion'")))
2961 if (!g_bIgnoreErrors)
2962 return FALSE;
2963
2964 return TRUE;
2965}
2966
2967
2968//
2969// Upgrade from V27 to V28
2970//
2971
2972static BOOL H_UpgradeFromV27(void)
2973{
2974 static TCHAR m_szBatch[] =
2975 "ALTER TABLE users ADD system_access integer\n"
2976 "UPDATE users SET system_access=access\n"
2977 "ALTER TABLE users DROP COLUMN access\n"
2978 "ALTER TABLE user_groups ADD system_access integer\n"
2979 "UPDATE user_groups SET system_access=access\n"
2980 "ALTER TABLE user_groups DROP COLUMN access\n"
2981 "<END>";
2982
2983 if (!SQLBatch(m_szBatch))
2984 if (!g_bIgnoreErrors)
2985 return FALSE;
2986
2987 if (!SQLQuery(_T("UPDATE config SET var_value='28' WHERE var_name='DBFormatVersion'")))
2988 if (!g_bIgnoreErrors)
2989 return FALSE;
2990
2991 return TRUE;
2992}
2993
2994
2995//
2996// Move object data from class-specific tables to object_prioperties table
2997//
2998
2999static BOOL MoveObjectData(DWORD dwId, BOOL bInheritRights)
3000{
3001 DB_RESULT hResult;
3002 TCHAR szQuery[1024] ,szName[MAX_OBJECT_NAME];
3003 BOOL bRead = FALSE, bIsDeleted, bIsTemplate;
3004 DWORD i, dwStatus, dwImageId;
3005 static const TCHAR *m_pszTableNames[] = { _T("nodes"), _T("interfaces"), _T("subnets"),
3006 _T("templates"), _T("network_services"),
3007 _T("containers"), NULL };
3008
3009 // Try to read information from nodes table
3010 for(i = 0; (!bRead) && (m_pszTableNames[i] != NULL); i++)
3011 {
3012 bIsTemplate = !_tcscmp(m_pszTableNames[i], _T("templates"));
3013 _sntprintf(szQuery, 1024, _T("SELECT name,is_deleted,image_id%s FROM %s WHERE id=%d"),
3014 bIsTemplate ? _T("") : _T(",status"),
3015 m_pszTableNames[i], dwId);
3016 hResult = SQLSelect(szQuery);
3017 if (hResult != NULL)
3018 {
3019 if (DBGetNumRows(hResult) > 0)
3020 {
3021 DBGetField(hResult, 0, 0, szName, MAX_OBJECT_NAME);
3022 bIsDeleted = DBGetFieldLong(hResult, 0, 1) ? TRUE : FALSE;
3023 dwImageId = DBGetFieldULong(hResult, 0, 2);
3024 dwStatus = bIsTemplate ? STATUS_UNKNOWN : DBGetFieldULong(hResult, 0, 3);
3025 bRead = TRUE;
3026 }
3027 DBFreeResult(hResult);
3028 }
3029 else
3030 {
3031 if (!g_bIgnoreErrors)
3032 return FALSE;
3033 }
3034 }
3035
3036 if (bRead)
3037 {
3038 _sntprintf(szQuery, 1024, _T("INSERT INTO object_properties (object_id,name,"
3039 "status,is_deleted,image_id,inherit_access_rights,"
3040 "last_modified) VALUES (%d,'%s',%d,%d,%d,%d,%ld)"),
3041 dwId, szName, dwStatus, bIsDeleted, dwImageId, bInheritRights, time(NULL));
3042
3043 if (!SQLQuery(szQuery))
3044 if (!g_bIgnoreErrors)
3045 return FALSE;
3046 }
3047 else
3048 {
3049 _tprintf(_T("WARNING: object with ID %d presented in access control tables but cannot be found in data tables\n"), dwId);
3050 }
3051
3052 return TRUE;
3053}
3054
3055
3056//
3057// Upgrade from V26 to V27
3058//
3059
3060static BOOL H_UpgradeFromV26(void)
3061{
3062 DB_RESULT hResult;
3063 DWORD i, dwNumObjects, dwId;
3064 static TCHAR m_szBatch[] =
3065 "ALTER TABLE nodes DROP COLUMN name\n"
3066 "ALTER TABLE nodes DROP COLUMN status\n"
3067 "ALTER TABLE nodes DROP COLUMN is_deleted\n"
3068 "ALTER TABLE nodes DROP COLUMN image_id\n"
3069 "ALTER TABLE interfaces DROP COLUMN name\n"
3070 "ALTER TABLE interfaces DROP COLUMN status\n"
3071 "ALTER TABLE interfaces DROP COLUMN is_deleted\n"
3072 "ALTER TABLE interfaces DROP COLUMN image_id\n"
3073 "ALTER TABLE subnets DROP COLUMN name\n"
3074 "ALTER TABLE subnets DROP COLUMN status\n"
3075 "ALTER TABLE subnets DROP COLUMN is_deleted\n"
3076 "ALTER TABLE subnets DROP COLUMN image_id\n"
3077 "ALTER TABLE network_services DROP COLUMN name\n"
3078 "ALTER TABLE network_services DROP COLUMN status\n"
3079 "ALTER TABLE network_services DROP COLUMN is_deleted\n"
3080 "ALTER TABLE network_services DROP COLUMN image_id\n"
3081 "ALTER TABLE containers DROP COLUMN name\n"
3082 "ALTER TABLE containers DROP COLUMN status\n"
3083 "ALTER TABLE containers DROP COLUMN is_deleted\n"
3084 "ALTER TABLE containers DROP COLUMN image_id\n"
3085 "ALTER TABLE templates DROP COLUMN name\n"
3086 "ALTER TABLE templates DROP COLUMN is_deleted\n"
3087 "ALTER TABLE templates DROP COLUMN image_id\n"
3088 "DROP TABLE access_options\n"
3089 "DELETE FROM config WHERE var_name='TopologyRootObjectName'\n"
3090 "DELETE FROM config WHERE var_name='TopologyRootImageId'\n"
3091 "DELETE FROM config WHERE var_name='ServiceRootObjectName'\n"
3092 "DELETE FROM config WHERE var_name='ServiceRootImageId'\n"
3093 "DELETE FROM config WHERE var_name='TemplateRootObjectName'\n"
3094 "DELETE FROM config WHERE var_name='TemplateRootImageId'\n"
3095 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
3096 "VALUES (31,'SYS_SNMP_OK',0,1,'Connectivity with SNMP agent restored',"
3097 "'Generated when connectivity with node#27s SNMP agent restored.#0D#0A"
3098 "Parameters:#0D#0A No message-specific parameters')\n"
3099 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
3100 "VALUES (32,'SYS_AGENT_OK',0,1,'Connectivity with native agent restored',"
3101 "'Generated when connectivity with node#27s native agent restored.#0D#0A"
3102 "Parameters:#0D#0A No message-specific parameters')\n"
3103 "<END>";
3104
3105 if (!CreateTable(_T("CREATE TABLE object_properties ("
3106 "object_id integer not null,"
3107 "name varchar(63) not null,"
3108 "status integer not null,"
3109 "is_deleted integer not null,"
3110 "image_id integer,"
3111 "last_modified integer not null,"
3112 "inherit_access_rights integer not null,"
3113 "PRIMARY KEY(object_id))")))
3114 if (!g_bIgnoreErrors)
3115 return FALSE;
3116
3117 if (!CreateTable(_T("CREATE TABLE user_profiles ("
3118 "user_id integer not null,"
3119 "var_name varchar(255) not null,"
3120 "var_value $SQL:TEXT,"
3121 "PRIMARY KEY(user_id,var_name))")))
3122 if (!g_bIgnoreErrors)
3123 return FALSE;
3124
3125 // Move data from access_options and class-specific tables to object_properties
3126 hResult = SQLSelect(_T("SELECT object_id,inherit_rights FROM access_options"));
3127 if (hResult != NULL)
3128 {
3129 dwNumObjects = DBGetNumRows(hResult);
3130 for(i = 0; i < dwNumObjects; i++)
3131 {
3132 dwId = DBGetFieldULong(hResult, i, 0);
3133 if (dwId >= 10) // Id below 10 reserved for built-in objects
3134 {
3135 if (!MoveObjectData(dwId, DBGetFieldLong(hResult, i, 1) ? TRUE : FALSE))
3136 {
3137 DBFreeResult(hResult);
3138 return FALSE;
3139 }
3140 }
3141 else
3142 {
3143 TCHAR szName[MAX_OBJECT_NAME], szQuery[1024];
3144 DWORD dwImageId;
3145 BOOL bValidObject = TRUE;
3146
3147 switch(dwId)
3148 {
3149 case 1: // Topology Root
3150 ConfigReadStr(_T("TopologyRootObjectName"), szName,
3151 MAX_OBJECT_NAME, _T("Entire Network"));
3152 dwImageId = ConfigReadULong(_T("TopologyRootImageId"), 0);
3153 break;
3154 case 2: // Service Root
3155 ConfigReadStr(_T("ServiceRootObjectName"), szName,
3156 MAX_OBJECT_NAME, _T("All Services"));
3157 dwImageId = ConfigReadULong(_T("ServiceRootImageId"), 0);
3158 break;
3159 case 3: // Template Root
3160 ConfigReadStr(_T("TemplateRootObjectName"), szName,
3161 MAX_OBJECT_NAME, _T("All Services"));
3162 dwImageId = ConfigReadULong(_T("TemplateRootImageId"), 0);
3163 break;
3164 default:
3165 bValidObject = FALSE;
3166 break;
3167 }
3168
3169 if (bValidObject)
3170 {
3171 _sntprintf(szQuery, 1024, _T("INSERT INTO object_properties (object_id,name,"
3172 "status,is_deleted,image_id,inherit_access_rights,"
3173 "last_modified) VALUES (%d,'%s',5,0,%d,%d,%ld)"),
3174 dwId, szName, dwImageId,
3175 DBGetFieldLong(hResult, i, 1) ? TRUE : FALSE,
3176 time(NULL));
3177
3178 if (!SQLQuery(szQuery))
3179 if (!g_bIgnoreErrors)
3180 return FALSE;
3181 }
3182 else
3183 {
3184 _tprintf(_T("WARNING: Invalid built-in object ID %d\n"), dwId);
3185 }
3186 }
3187 }
3188 DBFreeResult(hResult);
3189 }
3190 else
3191 {
3192 if (!g_bIgnoreErrors)
3193 return FALSE;
3194 }
3195
3196 if (!SQLBatch(m_szBatch))
3197 if (!g_bIgnoreErrors)
3198 return FALSE;
3199
3200 if (!SQLQuery(_T("UPDATE config SET var_value='27' WHERE var_name='DBFormatVersion'")))
3201 if (!g_bIgnoreErrors)
3202 return FALSE;
3203
3204 return TRUE;
3205}
3206
3207
3208//
3209// Upgrade from V25 to V26
3210//
3211
3212static BOOL H_UpgradeFromV25(void)
3213{
3214 DB_RESULT hResult;
3215 TCHAR szTemp[512];
3216
3217 hResult = SQLSelect(_T("SELECT var_value FROM config WHERE var_name='IDataIndexCreationCommand'"));
3218 if (hResult != NULL)
3219 {
3220 if (DBGetNumRows(hResult) > 0)
3221 {
3222 if (!CreateConfigParam(_T("IDataIndexCreationCommand_0"),
3223 DBGetField(hResult, 0, 0, szTemp, 512), 0, 1))
3224 {
3225 if (!g_bIgnoreErrors)
3226 {
3227 DBFreeResult(hResult);
3228 return FALSE;
3229 }
3230 }
3231 }
3232 DBFreeResult(hResult);
3233
3234 if (!SQLQuery(_T("DELETE FROM config WHERE var_name='IDataIndexCreationCommand'")))
3235 if (!g_bIgnoreErrors)
3236 return FALSE;
3237 }
3238
3239 if (!CreateConfigParam(_T("IDataIndexCreationCommand_1"),
3240 _T("CREATE INDEX idx_timestamp ON idata_%d(idata_timestamp)"), 0, 1))
3241 if (!g_bIgnoreErrors)
3242 return FALSE;
3243
3244 if (!SQLQuery(_T("UPDATE config SET var_value='26' WHERE var_name='DBFormatVersion'")))
3245 if (!g_bIgnoreErrors)
3246 return FALSE;
3247
3248 return TRUE;
3249}
3250
3251
3252//
3253// Upgrade from V24 to V25
3254//
3255
3256static BOOL H_UpgradeFromV24(void)
3257{
3258 DB_RESULT hResult;
3259 int i, iNumRows;
3260 DWORD dwNodeId;
3261 TCHAR szQuery[256];
3262
3263 _tprintf("Create indexes on existing IDATA tables? (Y/N) ");
3264 if (GetYesNo())
3265 {
3266 hResult = SQLSelect(_T("SELECT id FROM nodes WHERE is_deleted=0"));
3267 if (hResult != NULL)
3268 {
3269 iNumRows = DBGetNumRows(hResult);
3270 for(i = 0; i < iNumRows; i++)
3271 {
3272 dwNodeId = DBGetFieldULong(hResult, i, 0);
3273 _tprintf(_T("Creating indexes for table \"idata_%d\"...\n"), dwNodeId);
3274 _sntprintf(szQuery, 256, _T("CREATE INDEX idx_timestamp ON idata_%d(idata_timestamp)"), dwNodeId);
3275 if (!SQLQuery(szQuery))
3276 if (!g_bIgnoreErrors)
3277 {
3278 DBFreeResult(hResult);
3279 return FALSE;
3280 }
3281 }
3282 DBFreeResult(hResult);
3283 }
3284 else
3285 {
3286 if (!g_bIgnoreErrors)
3287 return FALSE;
3288 }
3289 }
3290
3291 if (!SQLQuery(_T("UPDATE config SET var_value='25' WHERE var_name='DBFormatVersion'")))
3292 if (!g_bIgnoreErrors)
3293 return FALSE;
3294
3295 return TRUE;
3296}
3297
3298
3299//
3300// Upgrade from V23 to V24
3301//
3302
3303static BOOL H_UpgradeFromV23(void)
3304{
3305 DB_RESULT hResult;
3306 TCHAR szQuery[256];
3307 int i, iNumRows;
3308
3309 if (!CreateTable(_T("CREATE TABLE raw_dci_values ("
3310 " item_id integer not null,"
3311 " raw_value varchar(255),"
3312 " last_poll_time integer,"
3313 " PRIMARY KEY(item_id))")))
3314 if (!g_bIgnoreErrors)
3315 return FALSE;
3316
3317 if (!SQLQuery(_T("CREATE INDEX idx_item_id ON raw_dci_values(item_id)")))
3318 if (!g_bIgnoreErrors)
3319 return FALSE;
3320
3321
3322 // Create empty records in raw_dci_values for all existing DCIs
3323 hResult = SQLSelect(_T("SELECT item_id FROM items"));
3324 if (hResult != NULL)
3325 {
3326 iNumRows = DBGetNumRows(hResult);
3327 for(i = 0; i < iNumRows; i++)
3328 {
3329 _stprintf(szQuery, _T("INSERT INTO raw_dci_values (item_id,"
3330 "raw_value,last_poll_time) VALUES (%d,'#00',1)"),
3331 DBGetFieldULong(hResult, i, 0));
3332 if (!SQLQuery(szQuery))
3333 if (!g_bIgnoreErrors)
3334 {
3335 DBFreeResult(hResult);
3336 return FALSE;
3337 }
3338 }
3339 DBFreeResult(hResult);
3340 }
3341 else
3342 {
3343 if (!g_bIgnoreErrors)
3344 return FALSE;
3345 }
3346
3347 if (!SQLQuery(_T("UPDATE config SET var_value='24' WHERE var_name='DBFormatVersion'")))
3348 if (!g_bIgnoreErrors)
3349 return FALSE;
3350
3351 return TRUE;
3352}
3353
3354
3355//
3356// Upgrade from V22 to V23
3357//
3358
3359static BOOL H_UpgradeFromV22(void)
3360{
3361 static TCHAR m_szBatch[] =
3362 "ALTER TABLE items ADD template_item_id integer\n"
3363 "UPDATE items SET template_item_id=0\n"
3364 "CREATE INDEX idx_sequence ON thresholds(sequence_number)\n"
3365 "<END>";
3366
3367 if (!SQLBatch(m_szBatch))
3368 if (!g_bIgnoreErrors)
3369 return FALSE;
3370
3371 if (!SQLQuery(_T("UPDATE config SET var_value='23' WHERE var_name='DBFormatVersion'")))
3372 if (!g_bIgnoreErrors)
3373 return FALSE;
3374
3375 return TRUE;
3376}
3377
3378
3379//
3380// Upgrade from V21 to V22
3381//
3382
3383static BOOL H_UpgradeFromV21(void)
3384{
3385 static TCHAR m_szBatch[] =
3386 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
3387 "VALUES (4009,'DC_HDD_TEMP_WARNING',1,1,"
3388 "'Temperature of hard disk %6 is above warning level of %3 (current: %4)',"
3389 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
3390 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
3391 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
3392 " 5) Data collection item ID#0D#0A 6) Instance')\n"
3393 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
3394 "VALUES (4010,'DC_HDD_TEMP_MAJOR',3,1,"
3395 "'Temperature of hard disk %6 is above %3 (current: %4)',"
3396 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
3397 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
3398 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
3399 " 5) Data collection item ID#0D#0A 6) Instance')\n"
3400 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description) "
3401 "VALUES (4011,'DC_HDD_TEMP_CRITICAL',4,1,"
3402 "'Temperature of hard disk %6 is above critical level of %3 (current: %4)',"
3403 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
3404 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
3405 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
3406 " 5) Data collection item ID#0D#0A 6) Instance')\n"
3407 "<END>";
3408
3409 if (!SQLBatch(m_szBatch))
3410 if (!g_bIgnoreErrors)
3411 return FALSE;
3412
3413 if (!CreateConfigParam(_T("SMSDriver"), _T("<none>"), 1, 1))
3414 if (!g_bIgnoreErrors)
3415 return FALSE;
3416
3417 if (!CreateConfigParam(_T("AgentUpgradeWaitTime"), _T("600"), 1, 0))
3418 if (!g_bIgnoreErrors)
3419 return FALSE;
3420
3421 if (!CreateConfigParam(_T("NumberOfUpgradeThreads"), _T("10"), 1, 0))
3422 if (!g_bIgnoreErrors)
3423 return FALSE;
3424
3425 if (!SQLQuery(_T("UPDATE config SET var_value='22' WHERE var_name='DBFormatVersion'")))
3426 if (!g_bIgnoreErrors)
3427 return FALSE;
3428
3429 return TRUE;
3430}
3431
3432
3433//
3434// Upgrade from V20 to V21
3435//
3436
3437static BOOL H_UpgradeFromV20(void)
3438{
3439 static TCHAR m_szBatch[] =
3440 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3441 " VALUES (30,'SYS_SMS_FAILURE',1,1,'Unable to send SMS to phone %1',"
3442 "'Generated when server is unable to send SMS.#0D#0A"
3443 " Parameters:#0D#0A 1) Phone number')\n"
3444 "ALTER TABLE nodes ADD node_flags integer\n"
3445 "<END>";
3446 static TCHAR m_szBatch2[] =
3447 "ALTER TABLE nodes DROP COLUMN is_snmp\n"
3448 "ALTER TABLE nodes DROP COLUMN is_agent\n"
3449 "ALTER TABLE nodes DROP COLUMN is_bridge\n"
3450 "ALTER TABLE nodes DROP COLUMN is_router\n"
3451 "ALTER TABLE nodes DROP COLUMN is_local_mgmt\n"
3452 "ALTER TABLE nodes DROP COLUMN is_ospf\n"
3453 "CREATE INDEX idx_item_id ON thresholds(item_id)\n"
3454 "<END>";
3455 static DWORD m_dwFlag[] = { NF_IS_SNMP, NF_IS_NATIVE_AGENT, NF_IS_BRIDGE,
3456 NF_IS_ROUTER, NF_IS_LOCAL_MGMT, NF_IS_OSPF };
3457 DB_RESULT hResult;
3458 int i, j, iNumRows;
3459 DWORD dwFlags, dwNodeId;
3460 TCHAR szQuery[256];
3461
3462 if (!SQLBatch(m_szBatch))
3463 if (!g_bIgnoreErrors)
3464 return FALSE;
3465
3466 // Convert "is_xxx" fields into one "node_flags" field
3467 hResult = SQLSelect(_T("SELECT id,is_snmp,is_agent,is_bridge,is_router,"
3468 "is_local_mgmt,is_ospf FROM nodes"));
3469 if (hResult != NULL)
3470 {
3471 iNumRows = DBGetNumRows(hResult);
3472 for(i = 0; i < iNumRows; i++)
3473 {
3474 dwFlags = 0;
3475 for(j = 1; j <= 6; j++)
3476 if (DBGetFieldLong(hResult, i, j))
3477 dwFlags |= m_dwFlag[j - 1];
3478 _sntprintf(szQuery, 256, _T("UPDATE nodes SET node_flags=%d WHERE id=%d"),
3479 dwFlags, DBGetFieldULong(hResult, i, 0));
3480 if (!SQLQuery(szQuery))
3481 if (!g_bIgnoreErrors)
3482 {
3483 DBFreeResult(hResult);
3484 return FALSE;
3485 }
3486 }
3487 DBFreeResult(hResult);
3488 }
3489 else
3490 {
3491 if (!g_bIgnoreErrors)
3492 return FALSE;
3493 }
3494
3495 if (!SQLBatch(m_szBatch2))
3496 if (!g_bIgnoreErrors)
3497 return FALSE;
3498
3499 _tprintf("Create indexes on existing IDATA tables? (Y/N) ");
3500 if (GetYesNo())
3501 {
3502 hResult = SQLSelect(_T("SELECT id FROM nodes WHERE is_deleted=0"));
3503 if (hResult != NULL)
3504 {
3505 iNumRows = DBGetNumRows(hResult);
3506 for(i = 0; i < iNumRows; i++)
3507 {
3508 dwNodeId = DBGetFieldULong(hResult, i, 0);
3509 _tprintf(_T("Creating indexes for table \"idata_%d\"...\n"), dwNodeId);
3510 _sntprintf(szQuery, 256, _T("CREATE INDEX idx_item_id ON idata_%d(item_id)"), dwNodeId);
3511 if (!SQLQuery(szQuery))
3512 if (!g_bIgnoreErrors)
3513 {
3514 DBFreeResult(hResult);
3515 return FALSE;
3516 }
3517 }
3518 DBFreeResult(hResult);
3519 }
3520 else
3521 {
3522 if (!g_bIgnoreErrors)
3523 return FALSE;
3524 }
3525 }
3526
3527 if (!CreateConfigParam(_T("NumberOfStatusPollers"), _T("10"), 1, 1))
3528 if (!g_bIgnoreErrors)
3529 return FALSE;
3530 if (!CreateConfigParam(_T("NumberOfConfigurationPollers"), _T("4"), 1, 1))
3531 if (!g_bIgnoreErrors)
3532 return FALSE;
3533 if (!CreateConfigParam(_T("IDataIndexCreationCommand"), _T("CREATE INDEX idx_item_id ON idata_%d(item_id)"), 0, 1))
3534 if (!g_bIgnoreErrors)
3535 return FALSE;
3536
3537 if (!SQLQuery(_T("UPDATE config SET var_value='21' WHERE var_name='DBFormatVersion'")))
3538 if (!g_bIgnoreErrors)
3539 return FALSE;
3540
3541 return TRUE;
3542}
3543
3544
3545//
3546// Upgrade from V19 to V20
3547//
3548
3549static BOOL H_UpgradeFromV19(void)
3550{
3551 static TCHAR m_szBatch[] =
3552 "ALTER TABLE nodes ADD poller_node_id integer\n"
3553 "ALTER TABLE nodes ADD is_ospf integer\n"
3554 "UPDATE nodes SET poller_node_id=0,is_ospf=0\n"
3555 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3556 " VALUES (28,'SYS_NODE_DOWN',4,1,'Node down',"
3557 "'Generated when node is not responding to management server.#0D#0A"
3558 "Parameters:#0D#0A No event-specific parameters')\n"
3559 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3560 " VALUES (29,'SYS_NODE_UP',0,1,'Node up',"
3561 "'Generated when communication with the node re-established.#0D#0A"
3562 "Parameters:#0D#0A No event-specific parameters')\n"
3563 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3564 " VALUES (25,'SYS_SERVICE_DOWN',3,1,'Network service #22%1#22 is not responding',"
3565 "'Generated when network service is not responding to management server as "
3566 "expected.#0D#0AParameters:#0D#0A 1) Service name#0D0A"
3567 " 2) Service object ID#0D0A 3) Service type')\n"
3568 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3569 " VALUES (26,'SYS_SERVICE_UP',0,1,"
3570 "'Network service #22%1#22 returned to operational state',"
3571 "'Generated when network service responds as expected after failure.#0D#0A"
3572 "Parameters:#0D#0A 1) Service name#0D0A"
3573 " 2) Service object ID#0D0A 3) Service type')\n"
3574 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
3575 " VALUES (27,'SYS_SERVICE_UNKNOWN',1,1,"
3576 "'Status of network service #22%1#22 is unknown',"
3577 "'Generated when management server is unable to determine state of the network "
3578 "service due to agent or server-to-agent communication failure.#0D#0A"
3579 "Parameters:#0D#0A 1) Service name#0D0A"
3580 " 2) Service object ID#0D0A 3) Service type')\n"
3581 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,file_name_ico,"
3582 "file_hash_ico) VALUES (12,'Obj.NetworkService','network_service.png',"
3583 "'<invalid_hash>','network_service.ico','<invalid_hash>')\n"
3584 "INSERT INTO default_images (object_class,image_id) VALUES (11,12)\n"
3585 "<END>";
3586
3587 if (!SQLBatch(m_szBatch))
3588 if (!g_bIgnoreErrors)
3589 return FALSE;
3590
3591 if (!CreateTable(_T("CREATE TABLE network_services ("
3592 "id integer not null,"
3593 "name varchar(63),"
3594 "status integer,"
3595 "is_deleted integer,"
3596 "node_id integer not null,"
3597 "service_type integer,"
3598 "ip_bind_addr varchar(15),"
3599 "ip_proto integer,"
3600 "ip_port integer,"
3601 "check_request $SQL:TEXT,"
3602 "check_responce $SQL:TEXT,"
3603 "poller_node_id integer not null,"
3604 "image_id integer not null,"
3605 "PRIMARY KEY(id))")))
3606 if (!g_bIgnoreErrors)
3607 return FALSE;
3608
3609 if (!SQLQuery(_T("UPDATE config SET var_value='20' WHERE var_name='DBFormatVersion'")))
3610 if (!g_bIgnoreErrors)
3611 return FALSE;
3612
3613 return TRUE;
3614}
3615
3616
3617//
3618// Upgrade from V18 to V19
3619//
3620
3621static BOOL H_UpgradeFromV18(void)
3622{
3623 static TCHAR m_szBatch[] =
3624 "ALTER TABLE nodes ADD platform_name varchar(63)\n"
3625 "UPDATE nodes SET platform_name=''\n"
3626 "<END>";
3627
3628 if (!SQLBatch(m_szBatch))
3629 if (!g_bIgnoreErrors)
3630 return FALSE;
3631
3632 if (!CreateTable(_T("CREATE TABLE agent_pkg ("
3633 "pkg_id integer not null,"
3634 "pkg_name varchar(63),"
3635 "version varchar(31),"
3636 "platform varchar(63),"
3637 "pkg_file varchar(255),"
3638 "description varchar(255),"
3639 "PRIMARY KEY(pkg_id))")))
3640 if (!g_bIgnoreErrors)
3641 return FALSE;
3642
3643 if (!SQLQuery(_T("UPDATE config SET var_value='19' WHERE var_name='DBFormatVersion'")))
3644 if (!g_bIgnoreErrors)
3645 return FALSE;
3646
3647 return TRUE;
3648}
3649
3650
3651//
3652// Upgrade from V17 to V18
3653//
3654
3655static BOOL H_UpgradeFromV17(void)
3656{
3657 static TCHAR m_szBatch[] =
3658 "ALTER TABLE nodes DROP COLUMN inherit_access_rights\n"
3659 "ALTER TABLE nodes ADD agent_version varchar(63)\n"
3660 "UPDATE nodes SET agent_version='' WHERE is_agent=0\n"
3661 "UPDATE nodes SET agent_version='<unknown>' WHERE is_agent=1\n"
3662 "INSERT INTO event_cfg (event_code,event_name,severity,flags,"
3663 "message,description) SELECT event_id,name,severity,flags,"
3664 "message,description FROM events\n"
3665 "DROP TABLE events\n"
3666 "DROP TABLE event_group_members\n"
3667 "CREATE TABLE event_group_members (group_id integer not null,"
3668 "event_code integer not null, PRIMARY KEY(group_id,event_code))\n"
3669 "ALTER TABLE alarms ADD source_event_code integer\n"
3670 "UPDATE alarms SET source_event_code=source_event_id\n"
3671 "ALTER TABLE alarms DROP COLUMN source_event_id\n"
3672 "ALTER TABLE alarms ADD source_event_id bigint\n"
3673 "UPDATE alarms SET source_event_id=0\n"
3674 "ALTER TABLE snmp_trap_cfg ADD event_code integer not null default 0\n"
3675 "UPDATE snmp_trap_cfg SET event_code=event_id\n"
3676 "ALTER TABLE snmp_trap_cfg DROP COLUMN event_id\n"
3677 "DROP TABLE event_log\n"
3678 "CREATE TABLE event_log (event_id bigint not null,event_code integer,"
3679 "event_timestamp integer,event_source integer,event_severity integer,"
3680 "event_message varchar(255),root_event_id bigint default 0,"
3681 "PRIMARY KEY(event_id))\n"
3682 "<END>";
3683 TCHAR szQuery[4096];
3684 DB_RESULT hResult;
3685
3686 hResult = SQLSelect(_T("SELECT rule_id,event_id FROM policy_event_list"));
3687 if (hResult != NULL)
3688 {
3689 DWORD i, dwNumRows;
3690
3691 if (!SQLQuery(_T("DROP TABLE policy_event_list")))
3692 {
3693 if (!g_bIgnoreErrors)
3694 {
3695 DBFreeResult(hResult);
3696 return FALSE;
3697 }
3698 }
3699
3700 if (!SQLQuery(_T("CREATE TABLE policy_event_list ("
3701 "rule_id integer not null,"
3702 "event_code integer not null,"
3703 "PRIMARY KEY(rule_id,event_code))")))
3704 {
3705 if (!g_bIgnoreErrors)
3706 {
3707 DBFreeResult(hResult);
3708 return FALSE;
3709 }
3710 }
3711
3712 dwNumRows = DBGetNumRows(hResult);
3713 for(i = 0; i < dwNumRows; i++)
3714 {
3715 _sntprintf(szQuery, 4096, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%d,%d)"),
3716 DBGetFieldULong(hResult, i, 0), DBGetFieldULong(hResult, i, 1));
3717 if (!SQLQuery(szQuery))
3718 if (!g_bIgnoreErrors)
3719 {
3720 DBFreeResult(hResult);
3721 return FALSE;
3722 }
3723 }
3724
3725 DBFreeResult(hResult);
3726 }
3727 else
3728 {
3729 if (!g_bIgnoreErrors)
3730 return FALSE;
3731 }
3732
3733 _sntprintf(szQuery, 4096,
3734 _T("CREATE TABLE event_cfg (event_code integer not null,"
3735 "event_name varchar(63) not null,severity integer,flags integer,"
3736 "message varchar(255),description %s,PRIMARY KEY(event_code))"),
3737 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
3738 if (!SQLQuery(szQuery))
3739 if (!g_bIgnoreErrors)
3740 return FALSE;
3741
3742 if (!SQLBatch(m_szBatch))
3743 if (!g_bIgnoreErrors)
3744 return FALSE;
3745
3746 _sntprintf(szQuery, 4096,
3747 _T("CREATE TABLE modules (module_id integer not null,"
3748 "module_name varchar(63),exec_name varchar(255),"
3749 "module_flags integer not null default 0,description %s,"
3750 "license_key varchar(255),PRIMARY KEY(module_id))"),
3751 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
3752 if (!SQLQuery(szQuery))
3753 if (!g_bIgnoreErrors)
3754 return FALSE;
3755
3756 if (!SQLQuery(_T("UPDATE config SET var_value='18' WHERE var_name='DBFormatVersion'")))
3757 if (!g_bIgnoreErrors)
3758 return FALSE;
3759
3760 return TRUE;
3761}
3762
3763
3764//
3765// Upgrade from V16 to V17
3766//
3767
3768static BOOL H_UpgradeFromV16(void)
3769{
3770 static TCHAR m_szBatch[] =
3771 "DROP TABLE locks\n"
3772 "CREATE TABLE snmp_trap_cfg (trap_id integer not null,snmp_oid varchar(255) not null,"
3773 "event_id integer not null,description varchar(255),PRIMARY KEY(trap_id))\n"
3774 "CREATE TABLE snmp_trap_pmap (trap_id integer not null,parameter integer not null,"
3775 "snmp_oid varchar(255),description varchar(255),PRIMARY KEY(trap_id,parameter))\n"
3776 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3777 "(500, 'SNMP_UNMATCHED_TRAP', 0, 1, 'SNMP trap received: %1 (Parameters: %2)',"
3778 "'Generated when system receives an SNMP trap without match in trap "
3779 "configuration table#0D#0AParameters:#0D#0A 1) SNMP trap OID#0D#0A"
3780 " 2) Trap parameters')\n"
3781 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3782 "(501, 'SNMP_COLD_START', 0, 1, 'System was cold-started',"
3783 "'Generated when system receives a coldStart SNMP trap#0D#0AParameters:#0D#0A"
3784 " 1) SNMP trap OID')\n"
3785 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3786 "(502, 'SNMP_WARM_START', 0, 1, 'System was warm-started',"
3787 "'Generated when system receives a warmStart SNMP trap#0D#0A"
3788 "Parameters:#0D#0A 1) SNMP trap OID')\n"
3789 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3790 "(503, 'SNMP_LINK_DOWN', 3, 1, 'Link is down',"
3791 "'Generated when system receives a linkDown SNMP trap#0D#0A"
3792 "Parameters:#0D#0A 1) SNMP trap OID#0D#0A 2) Interface index')\n"
3793 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3794 "(504, 'SNMP_LINK_UP', 0, 1, 'Link is up',"
3795 "'Generated when system receives a linkUp SNMP trap#0D#0AParameters:#0D#0A"
3796 " 1) SNMP trap OID#0D#0A 2) Interface index')\n"
3797 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3798 "(505, 'SNMP_AUTH_FAILURE', 1, 1, 'SNMP authentication failure',"
3799 "'Generated when system receives an authenticationFailure SNMP trap#0D#0A"
3800 "Parameters:#0D#0A 1) SNMP trap OID')\n"
3801 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
3802 "(506, 'SNMP_EGP_NEIGHBOR_LOSS', 1, 1, 'EGP neighbor loss',"
3803 "'Generated when system receives an egpNeighborLoss SNMP trap#0D#0A"
3804 "Parameters:#0D#0A 1) SNMP trap OID')\n"
3805 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
3806 "VALUES (1,'.1.3.6.1.6.3.1.1.5.1',501,'Generic coldStart trap')\n"
3807 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "