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