- Implemented interface for SMS drivers
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004 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 ** $module: upgrade.cpp
20 **
21 **/
22
23 #include "nxdbmgr.h"
24
25
26 //
27 // Create table
28 //
29
30 static BOOL CreateTable(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 if (g_iSyntax == DB_SYNTAX_MYSQL)
39 _tcscat(pszBuffer, _T(" type=InnoDB"));
40 bResult = SQLQuery(pszBuffer);
41 free(pszBuffer);
42 return bResult;
43 }
44
45
46 //
47 // Create configuration parameter if it doesn't exist
48 //
49
50 static BOOL CreateConfigParam(TCHAR *pszName, TCHAR *pszValue, int iVisible, int iNeedRestart)
51 {
52 TCHAR szQuery[1024];
53 DB_RESULT hResult;
54 BOOL bVarExist = FALSE, bResult = TRUE;
55
56 // Check for variable existence
57 _stprintf(szQuery, _T("SELECT var_value FROM config WHERE var_name='%s'"), pszName);
58 hResult = DBSelect(g_hCoreDB, szQuery);
59 if (hResult != 0)
60 {
61 if (DBGetNumRows(hResult) > 0)
62 bVarExist = TRUE;
63 DBFreeResult(hResult);
64 }
65
66 if (!bVarExist)
67 {
68 _stprintf(szQuery, _T("INSERT INTO config (var_name,var_value,is_visible,"
69 "need_server_restart) VALUES ('%s','%s',%d,%d)"),
70 pszName, pszValue, iVisible, iNeedRestart);
71 bResult = SQLQuery(szQuery);
72 }
73 return bResult;
74 }
75
76
77 //
78 // Upgrade from V20 to V21
79 //
80
81 static BOOL H_UpgradeFromV20(void)
82 {
83 static TCHAR m_szBatch[] =
84 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
85 " VALUES (30,'SYS_SMS_FAILURE',1,1,'Unable to send SMS to phone %1',"
86 "'Generated when server is unable to send SMS.#0D#0A"
87 " Parameters:#0D#0A 1) Phone number')\n"
88 "ALTER TABLE nodes ADD node_flags integer\n"
89 "<END>";
90 static TCHAR m_szBatch2[] =
91 "ALTER TABLE nodes DROP COLUMN is_snmp\n"
92 "ALTER TABLE nodes DROP COLUMN is_agent\n"
93 "ALTER TABLE nodes DROP COLUMN is_bridge\n"
94 "ALTER TABLE nodes DROP COLUMN is_router\n"
95 "ALTER TABLE nodes DROP COLUMN is_local_mgmt\n"
96 "ALTER TABLE nodes DROP COLUMN is_ospf\n"
97 "<END>";
98 static DWORD m_dwFlag[] = { NF_IS_SNMP, NF_IS_NATIVE_AGENT, NF_IS_BRIDGE,
99 NF_IS_ROUTER, NF_IS_LOCAL_MGMT, NF_IS_OSPF };
100 DB_RESULT hResult;
101 int i, j, iNumRows;
102 DWORD dwFlags;
103 TCHAR szQuery[256];
104
105 if (!SQLBatch(m_szBatch))
106 if (!g_bIgnoreErrors)
107 return FALSE;
108
109 // Convert "is_xxx" fields into one "node_flags" field
110 hResult = SQLSelect(_T("SELECT id,is_snmp,is_agent,is_bridge,is_router,"
111 "is_local_mgmt,is_ospf FROM nodes"));
112 if (hResult != NULL)
113 {
114 iNumRows = DBGetNumRows(hResult);
115 for(i = 0; i < iNumRows; i++)
116 {
117 dwFlags = 0;
118 for(j = 1; j <= 6; j++)
119 if (DBGetFieldLong(hResult, i, j))
120 dwFlags |= m_dwFlag[j];
121 _sntprintf(szQuery, 256, _T("UPDATE nodes SET node_flags=%ld WHERE id=%ld"),
122 dwFlags, DBGetFieldULong(hResult, i, 0));
123 if (!SQLQuery(m_szBatch))
124 if (!g_bIgnoreErrors)
125 {
126 DBFreeResult(hResult);
127 return FALSE;
128 }
129 }
130 DBFreeResult(hResult);
131 }
132 else
133 {
134 if (!g_bIgnoreErrors)
135 return FALSE;
136 }
137
138 if (!SQLBatch(m_szBatch2))
139 if (!g_bIgnoreErrors)
140 return FALSE;
141
142 if (!SQLQuery(_T("UPDATE config SET var_value='21' WHERE var_name='DBFormatVersion'")))
143 if (!g_bIgnoreErrors)
144 return FALSE;
145
146 return TRUE;
147 }
148
149
150 //
151 // Upgrade from V19 to V20
152 //
153
154 static BOOL H_UpgradeFromV19(void)
155 {
156 static TCHAR m_szBatch[] =
157 "ALTER TABLE nodes ADD poller_node_id integer\n"
158 "ALTER TABLE nodes ADD is_ospf integer\n"
159 "UPDATE nodes SET poller_node_id=0,is_ospf=0\n"
160 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
161 " VALUES (28,'SYS_NODE_DOWN',4,1,'Node down',"
162 "'Generated when node is not responding to management server.#0D#0A"
163 "Parameters:#0D#0A No event-specific parameters')\n"
164 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
165 " VALUES (29,'SYS_NODE_UP',0,1,'Node up',"
166 "'Generated when communication with the node re-established.#0D#0A"
167 "Parameters:#0D#0A No event-specific parameters')\n"
168 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
169 " VALUES (25,'SYS_SERVICE_DOWN',3,1,'Network service #22%1#22 is not responding',"
170 "'Generated when network service is not responding to management server as "
171 "expected.#0D#0AParameters:#0D#0A 1) Service name#0D0A"
172 " 2) Service object ID#0D0A 3) Service type')\n"
173 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
174 " VALUES (26,'SYS_SERVICE_UP',0,1,"
175 "'Network service #22%1#22 returned to operational state',"
176 "'Generated when network service responds as expected after failure.#0D#0A"
177 "Parameters:#0D#0A 1) Service name#0D0A"
178 " 2) Service object ID#0D0A 3) Service type')\n"
179 "INSERT INTO event_cfg (event_code,event_name,severity,flags,message,description)"
180 " VALUES (27,'SYS_SERVICE_UNKNOWN',1,1,"
181 "'Status of network service #22%1#22 is unknown',"
182 "'Generated when management server is unable to determine state of the network "
183 "service due to agent or server-to-agent communication failure.#0D#0A"
184 "Parameters:#0D#0A 1) Service name#0D0A"
185 " 2) Service object ID#0D0A 3) Service type')\n"
186 "INSERT INTO images (image_id,name,file_name_png,file_hash_png,file_name_ico,"
187 "file_hash_ico) VALUES (12,'Obj.NetworkService','network_service.png',"
188 "'<invalid_hash>','network_service.ico','<invalid_hash>')\n"
189 "INSERT INTO default_images (object_class,image_id) VALUES (11,12)\n"
190 "<END>";
191
192 if (!SQLBatch(m_szBatch))
193 if (!g_bIgnoreErrors)
194 return FALSE;
195
196 if (!CreateTable(_T("CREATE TABLE network_services ("
197 "id integer not null,"
198 "name varchar(63),"
199 "status integer,"
200 "is_deleted integer,"
201 "node_id integer not null,"
202 "service_type integer,"
203 "ip_bind_addr varchar(15),"
204 "ip_proto integer,"
205 "ip_port integer,"
206 "check_request $SQL:TEXT,"
207 "check_responce $SQL:TEXT,"
208 "poller_node_id integer not null,"
209 "image_id integer not null,"
210 "PRIMARY KEY(id))")))
211 if (!g_bIgnoreErrors)
212 return FALSE;
213
214 if (!SQLQuery(_T("UPDATE config SET var_value='20' WHERE var_name='DBFormatVersion'")))
215 if (!g_bIgnoreErrors)
216 return FALSE;
217
218 return TRUE;
219 }
220
221
222 //
223 // Upgrade from V18 to V19
224 //
225
226 static BOOL H_UpgradeFromV18(void)
227 {
228 static TCHAR m_szBatch[] =
229 "ALTER TABLE nodes ADD platform_name varchar(63)\n"
230 "UPDATE nodes SET platform_name=''\n"
231 "<END>";
232
233 if (!SQLBatch(m_szBatch))
234 if (!g_bIgnoreErrors)
235 return FALSE;
236
237 if (!CreateTable(_T("CREATE TABLE agent_pkg ("
238 "pkg_id integer not null,"
239 "pkg_name varchar(63),"
240 "version varchar(31),"
241 "platform varchar(63),"
242 "pkg_file varchar(255),"
243 "description varchar(255),"
244 "PRIMARY KEY(pkg_id))")))
245 if (!g_bIgnoreErrors)
246 return FALSE;
247
248 if (!SQLQuery(_T("UPDATE config SET var_value='19' WHERE var_name='DBFormatVersion'")))
249 if (!g_bIgnoreErrors)
250 return FALSE;
251
252 return TRUE;
253 }
254
255
256 //
257 // Upgrade from V17 to V18
258 //
259
260 static BOOL H_UpgradeFromV17(void)
261 {
262 static TCHAR m_szBatch[] =
263 "ALTER TABLE nodes DROP COLUMN inherit_access_rights\n"
264 "ALTER TABLE nodes ADD agent_version varchar(63)\n"
265 "UPDATE nodes SET agent_version='' WHERE is_agent=0\n"
266 "UPDATE nodes SET agent_version='<unknown>' WHERE is_agent=1\n"
267 "INSERT INTO event_cfg (event_code,event_name,severity,flags,"
268 "message,description) SELECT event_id,name,severity,flags,"
269 "message,description FROM events\n"
270 "DROP TABLE events\n"
271 "DROP TABLE event_group_members\n"
272 "CREATE TABLE event_group_members (group_id integer not null,"
273 "event_code integer not null, PRIMARY KEY(group_id,event_code))\n"
274 "ALTER TABLE alarms ADD source_event_code integer\n"
275 "UPDATE alarms SET source_event_code=source_event_id\n"
276 "ALTER TABLE alarms DROP COLUMN source_event_id\n"
277 "ALTER TABLE alarms ADD source_event_id bigint\n"
278 "UPDATE alarms SET source_event_id=0\n"
279 "ALTER TABLE snmp_trap_cfg ADD event_code integer not null default 0\n"
280 "UPDATE snmp_trap_cfg SET event_code=event_id\n"
281 "ALTER TABLE snmp_trap_cfg DROP COLUMN event_id\n"
282 "DROP TABLE event_log\n"
283 "CREATE TABLE event_log (event_id bigint not null,event_code integer,"
284 "event_timestamp integer,event_source integer,event_severity integer,"
285 "event_message varchar(255),root_event_id bigint default 0,"
286 "PRIMARY KEY(event_id))\n"
287 "<END>";
288 TCHAR szQuery[4096];
289 DB_RESULT hResult;
290
291 hResult = SQLSelect(_T("SELECT rule_id,event_id FROM policy_event_list"));
292 if (hResult != NULL)
293 {
294 DWORD i, dwNumRows;
295
296 if (!SQLQuery(_T("DROP TABLE policy_event_list")))
297 {
298 if (!g_bIgnoreErrors)
299 {
300 DBFreeResult(hResult);
301 return FALSE;
302 }
303 }
304
305 if (!SQLQuery(_T("CREATE TABLE policy_event_list ("
306 "rule_id integer not null,"
307 "event_code integer not null,"
308 "PRIMARY KEY(rule_id,event_code))")))
309 {
310 if (!g_bIgnoreErrors)
311 {
312 DBFreeResult(hResult);
313 return FALSE;
314 }
315 }
316
317 dwNumRows = DBGetNumRows(hResult);
318 for(i = 0; i < dwNumRows; i++)
319 {
320 _sntprintf(szQuery, 4096, _T("INSERT INTO policy_event_list (rule_id,event_code) VALUES (%ld,%ld)"),
321 DBGetFieldULong(hResult, i, 0), DBGetFieldULong(hResult, i, 1));
322 if (!SQLQuery(szQuery))
323 if (!g_bIgnoreErrors)
324 {
325 DBFreeResult(hResult);
326 return FALSE;
327 }
328 }
329
330 DBFreeResult(hResult);
331 }
332 else
333 {
334 if (!g_bIgnoreErrors)
335 return FALSE;
336 }
337
338 _sntprintf(szQuery, 4096,
339 _T("CREATE TABLE event_cfg (event_code integer not null,"
340 "event_name varchar(63) not null,severity integer,flags integer,"
341 "message varchar(255),description %s,PRIMARY KEY(event_code))"),
342 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
343 if (!SQLQuery(szQuery))
344 if (!g_bIgnoreErrors)
345 return FALSE;
346
347 if (!SQLBatch(m_szBatch))
348 if (!g_bIgnoreErrors)
349 return FALSE;
350
351 _sntprintf(szQuery, 4096,
352 _T("CREATE TABLE modules (module_id integer not null,"
353 "module_name varchar(63),exec_name varchar(255),"
354 "module_flags integer not null default 0,description %s,"
355 "license_key varchar(255),PRIMARY KEY(module_id))"),
356 g_pszSqlType[g_iSyntax][SQL_TYPE_TEXT]);
357 if (!SQLQuery(szQuery))
358 if (!g_bIgnoreErrors)
359 return FALSE;
360
361 if (!SQLQuery(_T("UPDATE config SET var_value='18' WHERE var_name='DBFormatVersion'")))
362 if (!g_bIgnoreErrors)
363 return FALSE;
364
365 return TRUE;
366 }
367
368
369 //
370 // Upgrade from V16 to V17
371 //
372
373 static BOOL H_UpgradeFromV16(void)
374 {
375 static TCHAR m_szBatch[] =
376 "DROP TABLE locks\n"
377 "CREATE TABLE snmp_trap_cfg (trap_id integer not null,snmp_oid varchar(255) not null,"
378 "event_id integer not null,description varchar(255),PRIMARY KEY(trap_id))\n"
379 "CREATE TABLE snmp_trap_pmap (trap_id integer not null,parameter integer not null,"
380 "snmp_oid varchar(255),description varchar(255),PRIMARY KEY(trap_id,parameter))\n"
381 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
382 "(500, 'SNMP_UNMATCHED_TRAP', 0, 1, 'SNMP trap received: %1 (Parameters: %2)',"
383 "'Generated when system receives an SNMP trap without match in trap "
384 "configuration table#0D#0AParameters:#0D#0A 1) SNMP trap OID#0D#0A"
385 " 2) Trap parameters')\n"
386 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
387 "(501, 'SNMP_COLD_START', 0, 1, 'System was cold-started',"
388 "'Generated when system receives a coldStart SNMP trap#0D#0AParameters:#0D#0A"
389 " 1) SNMP trap OID')\n"
390 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
391 "(502, 'SNMP_WARM_START', 0, 1, 'System was warm-started',"
392 "'Generated when system receives a warmStart SNMP trap#0D#0A"
393 "Parameters:#0D#0A 1) SNMP trap OID')\n"
394 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
395 "(503, 'SNMP_LINK_DOWN', 3, 1, 'Link is down',"
396 "'Generated when system receives a linkDown SNMP trap#0D#0A"
397 "Parameters:#0D#0A 1) SNMP trap OID#0D#0A 2) Interface index')\n"
398 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
399 "(504, 'SNMP_LINK_UP', 0, 1, 'Link is up',"
400 "'Generated when system receives a linkUp SNMP trap#0D#0AParameters:#0D#0A"
401 " 1) SNMP trap OID#0D#0A 2) Interface index')\n"
402 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
403 "(505, 'SNMP_AUTH_FAILURE', 1, 1, 'SNMP authentication failure',"
404 "'Generated when system receives an authenticationFailure SNMP trap#0D#0A"
405 "Parameters:#0D#0A 1) SNMP trap OID')\n"
406 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
407 "(506, 'SNMP_EGP_NEIGHBOR_LOSS', 1, 1, 'EGP neighbor loss',"
408 "'Generated when system receives an egpNeighborLoss SNMP trap#0D#0A"
409 "Parameters:#0D#0A 1) SNMP trap OID')\n"
410 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
411 "VALUES (1,'.1.3.6.1.6.3.1.1.5.1',501,'Generic coldStart trap')\n"
412 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
413 "VALUES (2,'.1.3.6.1.6.3.1.1.5.2',502,'Generic warmStart trap')\n"
414 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
415 "VALUES (3,'.1.3.6.1.6.3.1.1.5.3',503,'Generic linkDown trap')\n"
416 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
417 "VALUES (4,'.1.3.6.1.6.3.1.1.5.4',504,'Generic linkUp trap')\n"
418 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
419 "VALUES (5,'.1.3.6.1.6.3.1.1.5.5',505,'Generic authenticationFailure trap')\n"
420 "INSERT INTO snmp_trap_cfg (trap_id,snmp_oid,event_id,description) "
421 "VALUES (6,'.1.3.6.1.6.3.1.1.5.6',506,'Generic egpNeighborLoss trap')\n"
422 "INSERT INTO snmp_trap_pmap (trap_id,parameter,snmp_oid,description) "
423 "VALUES (3,1,'.1.3.6.1.2.1.2.2.1.1','Interface index')\n"
424 "INSERT INTO snmp_trap_pmap (trap_id,parameter,snmp_oid,description) "
425 "VALUES (4,1,'.1.3.6.1.2.1.2.2.1.1','Interface index')\n"
426 "<END>";
427
428 if (!SQLBatch(m_szBatch))
429 if (!g_bIgnoreErrors)
430 return FALSE;
431
432 if (!CreateConfigParam(_T("DBLockStatus"), _T("UNLOCKED"), 0, 1))
433 if (!g_bIgnoreErrors)
434 return FALSE;
435
436 if (!CreateConfigParam(_T("DBLockInfo"), _T(""), 0, 0))
437 if (!g_bIgnoreErrors)
438 return FALSE;
439
440 if (!CreateConfigParam(_T("EnableSNMPTraps"), _T("1"), 1, 1))
441 if (!g_bIgnoreErrors)
442 return FALSE;
443
444 if (!CreateConfigParam(_T("SMSDriver"), _T("<none>"), 1, 1))
445 if (!g_bIgnoreErrors)
446 return FALSE;
447
448 if (!CreateConfigParam(_T("SMTPServer"), _T("localhost"), 1, 0))
449 if (!g_bIgnoreErrors)
450 return FALSE;
451
452 if (!CreateConfigParam(_T("SMTPFromAddr"), _T("netxms@localhost"), 1, 0))
453 if (!g_bIgnoreErrors)
454 return FALSE;
455
456 if (!SQLQuery(_T("UPDATE config SET var_value='17' WHERE var_name='DBFormatVersion'")))
457 if (!g_bIgnoreErrors)
458 return FALSE;
459
460 return TRUE;
461 }
462
463
464 //
465 // Upgrade from V15 to V16
466 //
467
468 static BOOL H_UpgradeFromV15(void)
469 {
470 static TCHAR m_szBatch[] =
471 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
472 "(4005, 'DC_MAILBOX_TOO_LARGE', 1, 1,"
473 "'Mailbox #22%6#22 exceeds size limit (allowed size: %3; actual size: %4)',"
474 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
475 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
476 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
477 " 5) Data collection item ID#0D#0A 6) Instance')\n"
478 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
479 "(4006, 'DC_AGENT_VERSION_CHANGE', 0, 1,"
480 "'NetXMS agent version was changed from %3 to %4',"
481 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
482 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
483 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
484 " 5) Data collection item ID#0D#0A 6) Instance')\n"
485 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
486 "(4007, 'DC_HOSTNAME_CHANGE', 1, 1,"
487 "'Host name was changed from %3 to %4',"
488 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
489 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
490 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
491 " 5) Data collection item ID#0D#0A 6) Instance')\n"
492 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
493 "(4008, 'DC_FILE_CHANGE', 1, 1,"
494 "'File #22%6#22 was changed',"
495 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
496 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
497 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
498 " 5) Data collection item ID#0D#0A 6) Instance')\n"
499 "<END>";
500
501 if (!SQLBatch(m_szBatch))
502 if (!g_bIgnoreErrors)
503 return FALSE;
504 if (!SQLQuery(_T("UPDATE config SET var_value='16' WHERE var_name='DBFormatVersion'")))
505 if (!g_bIgnoreErrors)
506 return FALSE;
507 return TRUE;
508 }
509
510
511 //
512 // Upgrade from V14 to V15
513 //
514
515 static BOOL H_UpgradeFromV14(void)
516 {
517 static TCHAR m_szBatch[] =
518 "ALTER TABLE items ADD instance varchar(255)\n"
519 "UPDATE items SET instance=''\n"
520 "INSERT INTO config (var_name,var_value,is_visible,need_server_restart) VALUES "
521 "('SMTPServer','localhost',1,0)\n"
522 "INSERT INTO config (var_name,var_value,is_visible,need_server_restart) VALUES "
523 "('SMTPFromAddr','netxms@localhost',1,0)\n"
524 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
525 "(4003, 'DC_AGENT_RESTARTED', 0, 1,"
526 "'NetXMS agent was restarted within last 5 minutes',"
527 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
528 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
529 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
530 " 5) Data collection item ID#0D#0A 6) Instance')\n"
531 "INSERT INTO events (event_id,name,severity,flags,message,description) VALUES "
532 "(4004, 'DC_SERVICE_NOT_RUNNING', 3, 1,"
533 "'Service #22%6#22 is not running',"
534 "'Custom data collection threshold event.#0D#0AParameters:#0D#0A"
535 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
536 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
537 " 5) Data collection item ID#0D#0A 6) Instance')\n"
538 "UPDATE events SET "
539 "description='Generated when threshold value reached "
540 "for specific data collection item.#0D#0A"
541 " 1) Parameter name#0D#0A 2) Item description#0D#0A"
542 " 3) Threshold value#0D#0A 4) Actual value#0D#0A"
543 " 5) Data collection item ID#0D#0A 6) Instance' WHERE "
544 "event_id=17 OR (event_id>=4000 AND event_id<5000)\n"
545 "UPDATE events SET "
546 "description='Generated when threshold check is rearmed "
547 "for specific data collection item.#0D#0A"
548 "Parameters:#0D#0A 1) Parameter name#0D#0A"
549 " 2) Item description#0D#0A 3) Data collection item ID' "
550 "WHERE event_id=18\n"
551 "<END>";
552
553 if (!SQLBatch(m_szBatch))
554 if (!g_bIgnoreErrors)
555 return FALSE;
556 if (!SQLQuery(_T("UPDATE config SET var_value='15' WHERE var_name='DBFormatVersion'")))
557 if (!g_bIgnoreErrors)
558 return FALSE;
559 return TRUE;
560 }
561
562
563 //
564 // Upgrade map
565 //
566
567 static struct
568 {
569 int iVersion;
570 BOOL (* fpProc)(void);
571 } m_dbUpgradeMap[] =
572 {
573 { 14, H_UpgradeFromV14 },
574 { 15, H_UpgradeFromV15 },
575 { 16, H_UpgradeFromV16 },
576 { 17, H_UpgradeFromV17 },
577 { 18, H_UpgradeFromV18 },
578 { 19, H_UpgradeFromV19 },
579 { 20, H_UpgradeFromV20 },
580 { 0, NULL }
581 };
582
583
584 //
585 // Upgrade database to new version
586 //
587
588 void UpgradeDatabase(void)
589 {
590 DB_RESULT hResult;
591 long i, iVersion = 0;
592 BOOL bLocked = FALSE;
593
594 _tprintf(_T("Upgrading database...\n"));
595
596 // Get database format version
597 hResult = DBSelect(g_hCoreDB, _T("SELECT var_value FROM config WHERE var_name='DBFormatVersion'"));
598 if (hResult != NULL)
599 {
600 if (DBGetNumRows(hResult) > 0)
601 iVersion = DBGetFieldLong(hResult, 0, 0);
602 DBFreeResult(hResult);
603 }
604 if (iVersion == DB_FORMAT_VERSION)
605 {
606 _tprintf(_T("Your database format is up to date\n"));
607 }
608 else if (iVersion > DB_FORMAT_VERSION)
609 {
610 _tprintf(_T("Your database has format version %d, this tool is compiled for version %d.\n"
611 "You need to upgrade your server before using this database.\n"),
612 iVersion, DB_FORMAT_VERSION);
613 }
614 else
615 {
616 // Check if database is locked
617 hResult = DBSelect(g_hCoreDB, _T("SELECT var_value FROM config WHERE var_name='DBLockStatus'"));
618 if (hResult != NULL)
619 {
620 if (DBGetNumRows(hResult) > 0)
621 bLocked = _tcscmp(DBGetField(hResult, 0, 0), _T("UNLOCKED"));
622 DBFreeResult(hResult);
623 }
624 if (!bLocked)
625 {
626 // Upgrade database
627 while(iVersion < DB_FORMAT_VERSION)
628 {
629 // Find upgrade procedure
630 for(i = 0; m_dbUpgradeMap[i].fpProc != NULL; i++)
631 if (m_dbUpgradeMap[i].iVersion == iVersion)
632 break;
633 if (m_dbUpgradeMap[i].fpProc == NULL)
634 {
635 _tprintf(_T("Unable to find upgrade procedure for version %d\n"), iVersion);
636 break;
637 }
638 printf("Upgrading from version %d to %d\n", iVersion, iVersion + 1);
639 if (m_dbUpgradeMap[i].fpProc())
640 iVersion++;
641 else
642 break;
643 }
644
645 _tprintf(_T("Database upgrade %s\n"), (iVersion == DB_FORMAT_VERSION) ? _T("succeeded") : _T("failed"));
646 }
647 else
648 {
649 _tprintf(_T("Database is locked\n"));
650 }
651 }
652 }