outdated object image stuff removed
[public/netxms.git] / src / server / core / id.cpp
1 /* $Id$ */
2 /*
3 ** NetXMS - Network Management System
4 ** Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: id.cpp
21 **
22 **/
23
24 #include "nxcore.h"
25
26
27 //
28 // Constants
29 //
30
31 #define NUMBER_OF_GROUPS 22
32
33
34 //
35 // Static data
36 //
37
38 static MUTEX m_mutexTableAccess;
39 static DWORD m_dwFreeIdTable[NUMBER_OF_GROUPS] = { 10, 1, FIRST_USER_EVENT_ID, 1, 1,
40 1000, 1, 0x80000000,
41 1, 1, 0x80000001, 1, 1, 1, 1,
42 10000, 10000, 1, 1, 1, 1, 1
43 };
44 static DWORD m_dwIdLimits[NUMBER_OF_GROUPS] = { 0xFFFFFFFE, 0xFFFFFFFE, 0x7FFFFFFF, 0x7FFFFFFF,
45 0x7FFFFFFF, 0xFFFFFFFE, 0x7FFFFFFF, 0xFFFFFFFF,
46 0x7FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFE, 0xFFFFFFFE,
47 0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFE,
48 0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFE, 0xFFFFFFFE,
49 0xFFFFFFFE, 0xFFFFFFFE
50 };
51 static QWORD m_qwFreeEventId = 1;
52 static const char *m_pszGroupNames[NUMBER_OF_GROUPS] =
53 {
54 "Network Objects",
55 "Container Categories",
56 "Events",
57 "Data Collection Items",
58 "SNMP Trap",
59 "--Images",
60 "Actions",
61 "Event Groups",
62 "Data Collection Thresholds",
63 "Users",
64 "User Groups",
65 "Alarms",
66 "Alarm Notes",
67 "Packages",
68 "--Log Processing Policies",
69 "Object Tools",
70 "Scripts",
71 "Agent Configs",
72 "Graphs",
73 "Certificates",
74 "Situations",
75 "Maps"
76 };
77
78
79 //
80 // Initialize ID table
81 //
82
83 BOOL InitIdTable(void)
84 {
85 DB_RESULT hResult;
86
87 m_mutexTableAccess = MutexCreate();
88
89 // Get first available network object ID
90 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM nodes");
91 if (hResult != NULL)
92 {
93 if (DBGetNumRows(hResult) > 0)
94 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
95 DBGetFieldULong(hResult, 0, 0) + 1);
96 DBFreeResult(hResult);
97 }
98 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM subnets");
99 if (hResult != NULL)
100 {
101 if (DBGetNumRows(hResult) > 0)
102 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
103 DBGetFieldULong(hResult, 0, 0) + 1);
104 DBFreeResult(hResult);
105 }
106 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM interfaces");
107 if (hResult != NULL)
108 {
109 if (DBGetNumRows(hResult) > 0)
110 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
111 DBGetFieldULong(hResult, 0, 0) + 1);
112 DBFreeResult(hResult);
113 }
114 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM containers");
115 if (hResult != NULL)
116 {
117 if (DBGetNumRows(hResult) > 0)
118 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
119 DBGetFieldULong(hResult, 0, 0) + 1);
120 DBFreeResult(hResult);
121 }
122 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM templates");
123 if (hResult != NULL)
124 {
125 if (DBGetNumRows(hResult) > 0)
126 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
127 DBGetFieldULong(hResult, 0, 0) + 1);
128 DBFreeResult(hResult);
129 }
130 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM network_services");
131 if (hResult != NULL)
132 {
133 if (DBGetNumRows(hResult) > 0)
134 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
135 DBGetFieldULong(hResult, 0, 0) + 1);
136 DBFreeResult(hResult);
137 }
138 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM conditions");
139 if (hResult != NULL)
140 {
141 if (DBGetNumRows(hResult) > 0)
142 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
143 DBGetFieldULong(hResult, 0, 0) + 1);
144 DBFreeResult(hResult);
145 }
146 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM clusters");
147 if (hResult != NULL)
148 {
149 if (DBGetNumRows(hResult) > 0)
150 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
151 DBGetFieldULong(hResult, 0, 0) + 1);
152 DBFreeResult(hResult);
153 }
154 hResult = DBSelect(g_hCoreDB, "SELECT max(object_id) FROM deleted_objects");
155 if (hResult != NULL)
156 {
157 if (DBGetNumRows(hResult) > 0)
158 m_dwFreeIdTable[IDG_NETWORK_OBJECT] = max(m_dwFreeIdTable[IDG_NETWORK_OBJECT],
159 DBGetFieldULong(hResult, 0, 0) + 1);
160 DBFreeResult(hResult);
161 }
162
163 // Get first available container category id
164 hResult = DBSelect(g_hCoreDB, "SELECT max(category) FROM container_categories");
165 if (hResult != NULL)
166 {
167 if (DBGetNumRows(hResult) > 0)
168 m_dwFreeIdTable[IDG_CONTAINER_CAT] = max(1, DBGetFieldULong(hResult, 0, 0) + 1);
169 DBFreeResult(hResult);
170 }
171
172 // Get first available event code
173 hResult = DBSelect(g_hCoreDB, "SELECT max(event_code) FROM event_cfg");
174 if (hResult != NULL)
175 {
176 if (DBGetNumRows(hResult) > 0)
177 m_dwFreeIdTable[IDG_EVENT] = max(m_dwFreeIdTable[IDG_EVENT], DBGetFieldULong(hResult, 0, 0) + 1);
178 DBFreeResult(hResult);
179 }
180
181 // Get first available data collection item id
182 hResult = DBSelect(g_hCoreDB, "SELECT max(item_id) FROM items");
183 if (hResult != NULL)
184 {
185 if (DBGetNumRows(hResult) > 0)
186 m_dwFreeIdTable[IDG_ITEM] = max(1, DBGetFieldULong(hResult, 0, 0) + 1);
187 DBFreeResult(hResult);
188 }
189
190 // Get first available SNMP trap configuration record id
191 hResult = DBSelect(g_hCoreDB, "SELECT max(trap_id) FROM snmp_trap_cfg");
192 if (hResult != NULL)
193 {
194 if (DBGetNumRows(hResult) > 0)
195 m_dwFreeIdTable[IDG_SNMP_TRAP] = max(m_dwFreeIdTable[IDG_SNMP_TRAP], DBGetFieldULong(hResult, 0, 0) + 1);
196 DBFreeResult(hResult);
197 }
198
199 // Get first available action id
200 hResult = DBSelect(g_hCoreDB, "SELECT max(action_id) FROM actions");
201 if (hResult != NULL)
202 {
203 if (DBGetNumRows(hResult) > 0)
204 m_dwFreeIdTable[IDG_ACTION] = max(1, DBGetFieldULong(hResult, 0, 0) + 1);
205 DBFreeResult(hResult);
206 }
207
208 // Get first available event group id
209 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM event_groups");
210 if (hResult != NULL)
211 {
212 if (DBGetNumRows(hResult) > 0)
213 m_dwFreeIdTable[IDG_EVENT_GROUP] = max(0x80000000, DBGetFieldULong(hResult, 0, 0) + 1);
214 DBFreeResult(hResult);
215 }
216
217 // Get first available threshold id
218 hResult = DBSelect(g_hCoreDB, "SELECT max(threshold_id) FROM thresholds");
219 if (hResult != NULL)
220 {
221 if (DBGetNumRows(hResult) > 0)
222 m_dwFreeIdTable[IDG_THRESHOLD] = max(m_dwFreeIdTable[IDG_THRESHOLD],
223 DBGetFieldULong(hResult, 0, 0) + 1);
224 DBFreeResult(hResult);
225 }
226
227 // Get first available user id
228 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM users");
229 if (hResult != NULL)
230 {
231 if (DBGetNumRows(hResult) > 0)
232 m_dwFreeIdTable[IDG_USER] = max(m_dwFreeIdTable[IDG_USER],
233 DBGetFieldULong(hResult, 0, 0) + 1);
234 DBFreeResult(hResult);
235 }
236
237 // Get first available user group id
238 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM user_groups");
239 if (hResult != NULL)
240 {
241 if (DBGetNumRows(hResult) > 0)
242 m_dwFreeIdTable[IDG_USER_GROUP] = max(m_dwFreeIdTable[IDG_USER_GROUP],
243 DBGetFieldULong(hResult, 0, 0) + 1);
244 DBFreeResult(hResult);
245 }
246
247 // Get first available alarm id
248 hResult = DBSelect(g_hCoreDB, "SELECT max(alarm_id) FROM alarms");
249 if (hResult != NULL)
250 {
251 if (DBGetNumRows(hResult) > 0)
252 m_dwFreeIdTable[IDG_ALARM] = max(m_dwFreeIdTable[IDG_ALARM],
253 DBGetFieldULong(hResult, 0, 0) + 1);
254 DBFreeResult(hResult);
255 }
256
257 // Get first available alarm note id
258 hResult = DBSelect(g_hCoreDB, "SELECT max(note_id) FROM alarm_notes");
259 if (hResult != NULL)
260 {
261 if (DBGetNumRows(hResult) > 0)
262 m_dwFreeIdTable[IDG_ALARM_NOTE] = max(m_dwFreeIdTable[IDG_ALARM_NOTE],
263 DBGetFieldULong(hResult, 0, 0) + 1);
264 DBFreeResult(hResult);
265 }
266
267 // Get first available event identifier
268 hResult = DBSelect(g_hCoreDB, "SELECT max(event_id) FROM event_log");
269 if (hResult != NULL)
270 {
271 if (DBGetNumRows(hResult) > 0)
272 m_qwFreeEventId = max(m_qwFreeEventId, DBGetFieldUInt64(hResult, 0, 0) + 1);
273 DBFreeResult(hResult);
274 }
275
276 // Get first available package id
277 hResult = DBSelect(g_hCoreDB, "SELECT max(pkg_id) FROM agent_pkg");
278 if (hResult != NULL)
279 {
280 if (DBGetNumRows(hResult) > 0)
281 m_dwFreeIdTable[IDG_PACKAGE] = max(m_dwFreeIdTable[IDG_PACKAGE],
282 DBGetFieldULong(hResult, 0, 0) + 1);
283 DBFreeResult(hResult);
284 }
285
286 // Get first available object tool id
287 hResult = DBSelect(g_hCoreDB, "SELECT max(tool_id) FROM object_tools");
288 if (hResult != NULL)
289 {
290 if (DBGetNumRows(hResult) > 0)
291 m_dwFreeIdTable[IDG_OBJECT_TOOL] = max(m_dwFreeIdTable[IDG_OBJECT_TOOL],
292 DBGetFieldULong(hResult, 0, 0) + 1);
293 DBFreeResult(hResult);
294 }
295
296 // Get first available script id
297 hResult = DBSelect(g_hCoreDB, "SELECT max(script_id) FROM script_library");
298 if (hResult != NULL)
299 {
300 if (DBGetNumRows(hResult) > 0)
301 m_dwFreeIdTable[IDG_SCRIPT] = max(m_dwFreeIdTable[IDG_SCRIPT],
302 DBGetFieldULong(hResult, 0, 0) + 1);
303 DBFreeResult(hResult);
304 }
305
306 // Get first available agent config id
307 hResult = DBSelect(g_hCoreDB, "SELECT max(config_id) FROM agent_configs");
308 if (hResult != NULL)
309 {
310 if (DBGetNumRows(hResult) > 0)
311 m_dwFreeIdTable[IDG_AGENT_CONFIG] = max(m_dwFreeIdTable[IDG_AGENT_CONFIG],
312 DBGetFieldULong(hResult, 0, 0) + 1);
313 DBFreeResult(hResult);
314 }
315
316 // Get first available graph id
317 hResult = DBSelect(g_hCoreDB, "SELECT max(graph_id) FROM graphs");
318 if (hResult != NULL)
319 {
320 if (DBGetNumRows(hResult) > 0)
321 m_dwFreeIdTable[IDG_GRAPH] = max(m_dwFreeIdTable[IDG_GRAPH],
322 DBGetFieldULong(hResult, 0, 0) + 1);
323 DBFreeResult(hResult);
324 }
325
326 // Get first available certificate id
327 hResult = DBSelect(g_hCoreDB, "SELECT max(cert_id) FROM certificates");
328 if (hResult != NULL)
329 {
330 if (DBGetNumRows(hResult) > 0)
331 m_dwFreeIdTable[IDG_CERTIFICATE] = max(m_dwFreeIdTable[IDG_CERTIFICATE],
332 DBGetFieldULong(hResult, 0, 0) + 1);
333 DBFreeResult(hResult);
334 }
335
336 // Get first available situation id
337 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM situations");
338 if (hResult != NULL)
339 {
340 if (DBGetNumRows(hResult) > 0)
341 m_dwFreeIdTable[IDG_SITUATION] = max(m_dwFreeIdTable[IDG_SITUATION],
342 DBGetFieldULong(hResult, 0, 0) + 1);
343 DBFreeResult(hResult);
344 }
345
346 // Get first available map id
347 hResult = DBSelect(g_hCoreDB, "SELECT max(map_id) FROM maps");
348 if (hResult != NULL)
349 {
350 if (DBGetNumRows(hResult) > 0)
351 m_dwFreeIdTable[IDG_MAP] = max(m_dwFreeIdTable[IDG_MAP],
352 DBGetFieldULong(hResult, 0, 0) + 1);
353 DBFreeResult(hResult);
354 }
355 hResult = DBSelect(g_hCoreDB, "SELECT max(id) FROM web_maps");
356 if (hResult != NULL)
357 {
358 if (DBGetNumRows(hResult) > 0)
359 m_dwFreeIdTable[IDG_MAP] = max(m_dwFreeIdTable[IDG_MAP],
360 DBGetFieldULong(hResult, 0, 0) + 1);
361 DBFreeResult(hResult);
362 }
363
364 return TRUE;
365 }
366
367
368 //
369 // Create unique ID
370 //
371
372 DWORD CreateUniqueId(int iGroup)
373 {
374 DWORD dwId;
375
376 MutexLock(m_mutexTableAccess, INFINITE);
377 if (m_dwFreeIdTable[iGroup] == m_dwIdLimits[iGroup])
378 {
379 dwId = 0; // ID zero means "no unique ID available"
380 nxlog_write(MSG_NO_UNIQUE_ID, EVENTLOG_ERROR_TYPE, "s", m_pszGroupNames[iGroup]);
381 }
382 else
383 {
384 dwId = m_dwFreeIdTable[iGroup];
385 m_dwFreeIdTable[iGroup]++;
386 }
387 MutexUnlock(m_mutexTableAccess);
388 return dwId;
389 }
390
391
392 //
393 // Create unique ID for event log record
394 //
395
396 QWORD CreateUniqueEventId(void)
397 {
398 QWORD qwId;
399
400 MutexLock(m_mutexTableAccess, INFINITE);
401 qwId = m_qwFreeEventId++;
402 MutexUnlock(m_mutexTableAccess);
403 return qwId;
404 }