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