Log and database functions moved from server core to libnxsrv
[public/netxms.git] / src / server / core / config.cpp
1 /*
2 ** Project X - Network Management System
3 ** Copyright (C) 2003 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: config.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25
26 //
27 // Help text
28 //
29
30 static char help_text[]="NetXMS Server Version " NETXMS_VERSION_STRING "\n"
31 "Copyright (c) 2003 SecurityProjects.org\n\n"
32 "Usage: netxmsd [<options>] <command>\n\n"
33 "Valid options are:\n"
34 " --config <file> : Set non-default configuration file\n"
35 " : Default is " DEFAULT_CONFIG_FILE "\n"
36 " --debug-all : Turn on all possible debug output\n"
37 " --debug-actions : Print debug information for event actions.\n"
38 " --debug-cscp : Print client-server communication protocol debug\n"
39 " : information to console.\n"
40 " --debug-dc : Print data collection debug information to console.\n"
41 " --debug-discovery : Print network discovery debug information to console.\n"
42 " --debug-events : Print events to console.\n"
43 " --debug-housekeeper : Print debug information for housekeeping thread.\n"
44 " --debug-locks : Print debug information about component locking.\n"
45 " --dump-sql : Dump all SQL queries to log.\n"
46 #ifndef _WIN32
47 " --pid-file <file> : Specify pid file.\n"
48 #endif
49 "\n"
50 "Valid commands are:\n"
51 " check-config : Check configuration file syntax\n"
52 #ifdef _WIN32
53 " install : Install Win32 service\n"
54 " install-events : Install Win32 event source\n"
55 #endif
56 " help : Display help and exit\n"
57 #ifdef _WIN32
58 " remove : Remove Win32 service\n"
59 " remove-events : Remove Win32 event source\n"
60 #endif
61 " standalone : Run in standalone mode (not as service)\n"
62 " version : Display version and exit\n"
63 "\n"
64 "NOTE: All debug options will work only in standalone mode.\n\n";
65
66
67 //
68 // Load and parse configuration file
69 // Returns TRUE on success and FALSE on failure
70 //
71
72 static NX_CFG_TEMPLATE m_cfgTemplate[] =
73 {
74 { "DBDriver", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDriver },
75 { "DBDrvParams", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbDrvParams },
76 { "DBLogin", CT_STRING, 0, 0, MAX_DB_LOGIN, 0, g_szDbLogin },
77 { "DBName", CT_STRING, 0, 0, MAX_DB_NAME, 0, g_szDbName },
78 { "DBPassword", CT_STRING, 0, 0, MAX_DB_PASSWORD, 0, g_szDbPassword },
79 { "DBServer", CT_STRING, 0, 0, MAX_PATH, 0, g_szDbServer },
80 { "LogFailedSQLQueries", CT_BOOLEAN, 0, 0, AF_LOG_SQL_ERRORS, 0, &g_dwFlags },
81 { "LogFile", CT_STRING, 0, 0, MAX_PATH, 0, g_szLogFile },
82 { "", CT_END_OF_LIST, 0, 0, 0, 0, NULL }
83 };
84
85 BOOL LoadConfig(void)
86 {
87 BOOL bSuccess = FALSE;
88
89 if (IsStandalone())
90 printf("Using configuration file \"%s\"\n", g_szConfigFile);
91
92 if (NxLoadConfig(g_szConfigFile, "", m_cfgTemplate, IsStandalone()) == NXCFG_ERR_OK)
93 {
94 if ((!stricmp(g_szLogFile,"{EventLog}")) ||
95 (!stricmp(g_szLogFile,"{syslog}")))
96 {
97 g_dwFlags |= AF_USE_EVENT_LOG;
98 }
99 else
100 {
101 g_dwFlags &= ~AF_USE_EVENT_LOG;
102 }
103 bSuccess = TRUE;
104 }
105 return bSuccess;
106 }
107
108
109 //
110 // Parse command line
111 // Returns TRUE on success and FALSE on failure
112 //
113
114 BOOL ParseCommandLine(int argc, char *argv[])
115 {
116 int i;
117
118 for(i = 1; i < argc; i++)
119 {
120 if (!strcmp(argv[i], "help")) // Display help and exit
121 {
122 printf(help_text);
123 return FALSE;
124 }
125 else if (!strcmp(argv[i], "version")) // Display version and exit
126 {
127 printf("NMS Version " NETXMS_VERSION_STRING " Build of " __DATE__ "\n");
128 return FALSE;
129 }
130 else if (!strcmp(argv[i], "--config")) // Config file
131 {
132 i++;
133 strcpy(g_szConfigFile, argv[i]); // Next word should contain name of the config file
134 }
135 #ifndef _WIN32
136 else if (!strcmp(argv[i], "--pid-file")) // PID file
137 {
138 i++;
139 strcpy(g_szPIDFile, argv[i]); // Next word should contain name of the PID file
140 }
141 #endif
142 else if (!strcmp(argv[i], "--debug-all"))
143 {
144 g_dwFlags |= AF_DEBUG_ALL;
145 }
146 else if (!strcmp(argv[i], "--debug-events"))
147 {
148 g_dwFlags |= AF_DEBUG_EVENTS;
149 }
150 else if (!strcmp(argv[i], "--debug-cscp"))
151 {
152 g_dwFlags |= AF_DEBUG_CSCP;
153 }
154 else if (!strcmp(argv[i], "--debug-discovery"))
155 {
156 g_dwFlags |= AF_DEBUG_DISCOVERY;
157 }
158 else if (!strcmp(argv[i], "--debug-dc"))
159 {
160 g_dwFlags |= AF_DEBUG_DC;
161 }
162 else if (!strcmp(argv[i], "--debug-locks"))
163 {
164 g_dwFlags |= AF_DEBUG_LOCKS;
165 }
166 else if (!strcmp(argv[i], "--debug-housekeeper"))
167 {
168 g_dwFlags |= AF_DEBUG_HOUSEKEEPER;
169 }
170 else if (!strcmp(argv[i], "--debug-actions"))
171 {
172 g_dwFlags |= AF_DEBUG_ACTIONS;
173 }
174 else if (!strcmp(argv[i], "--dump-sql"))
175 {
176 g_dwFlags |= AF_DEBUG_SQL;
177 }
178 else if (!strcmp(argv[i], "check-config"))
179 {
180 g_dwFlags |= AF_STANDALONE;
181 printf("Checking configuration file (%s):\n\n", g_szConfigFile);
182 LoadConfig();
183 return FALSE;
184 }
185 else if (!strcmp(argv[i], "standalone")) // Run in standalone mode
186 {
187 g_dwFlags |= AF_STANDALONE;
188 return TRUE;
189 }
190 #ifdef _WIN32
191 else if ((!strcmp(argv[i], "install"))||
192 (!strcmp(argv[i], "install-events")))
193 {
194 char exePath[MAX_PATH], dllPath[MAX_PATH], *ptr;
195
196 ptr = strrchr(argv[0], '\\');
197 if (ptr != NULL)
198 ptr++;
199 else
200 ptr = argv[0];
201
202 _fullpath(exePath, ptr, 255);
203
204 if (stricmp(&exePath[strlen(exePath)-4], ".exe"))
205 strcat(exePath, ".exe");
206 strcpy(dllPath, exePath);
207 ptr = strrchr(dllPath, '\\');
208 if (ptr != NULL) // Shouldn't be NULL
209 {
210 ptr++;
211 strcpy(ptr, "libnxsrv.dll");
212 }
213
214 if (!strcmp(argv[i], "install"))
215 InstallService(exePath, dllPath);
216 else
217 InstallEventSource(dllPath);
218 return FALSE;
219 }
220 else if (!strcmp(argv[i], "remove"))
221 {
222 RemoveService();
223 return FALSE;
224 }
225 else if (!strcmp(argv[i], "remove-events"))
226 {
227 RemoveEventSource();
228 return FALSE;
229 }
230 else if (!strcmp(argv[i], "start"))
231 {
232 StartCoreService();
233 return FALSE;
234 }
235 else if (!strcmp(argv[i], "stop"))
236 {
237 StopCoreService();
238 return FALSE;
239 }
240 #endif /* _WIN32 */
241 else
242 {
243 printf("ERROR: Invalid command line argument\n\n");
244 return FALSE;
245 }
246 }
247
248 return TRUE;
249 }
250
251
252 //
253 // Read string value from configuration table
254 //
255
256 BOOL ConfigReadStr(char *szVar, char *szBuffer, int iBufSize, const char *szDefault)
257 {
258 DB_RESULT hResult;
259 char szQuery[256];
260 BOOL bSuccess = FALSE;
261
262 strncpy(szBuffer, szDefault, iBufSize - 1);
263 szBuffer[iBufSize - 1] = 0;
264 if (strlen(szVar) > 127)
265 return FALSE;
266
267 sprintf(szQuery, "SELECT var_value FROM config WHERE var_name='%s'", szVar);
268 hResult = DBSelect(g_hCoreDB, szQuery);
269 if (hResult == 0)
270 return FALSE;
271
272 if (DBGetNumRows(hResult) > 0)
273 {
274 strncpy(szBuffer, DBGetField(hResult, 0, 0), iBufSize - 1);
275 bSuccess = TRUE;
276 }
277
278 DBFreeResult(hResult);
279 return bSuccess;
280 }
281
282
283 //
284 // Read integer value from configuration table
285 //
286
287 int ConfigReadInt(char *szVar, int iDefault)
288 {
289 char szBuffer[64];
290
291 if (ConfigReadStr(szVar, szBuffer, 64, ""))
292 return strtol(szBuffer, NULL, 0);
293 else
294 return iDefault;
295 }
296
297
298 //
299 // Read unsigned long value from configuration table
300 //
301
302 DWORD ConfigReadULong(char *szVar, DWORD dwDefault)
303 {
304 char szBuffer[64];
305
306 if (ConfigReadStr(szVar, szBuffer, 64, ""))
307 return strtoul(szBuffer, NULL, 0);
308 else
309 return dwDefault;
310 }
311
312
313 //
314 // Write string value to configuration table
315 //
316
317 BOOL ConfigWriteStr(char *szVar, char *szValue, BOOL bCreate)
318 {
319 DB_RESULT hResult;
320 char szQuery[1024];
321 BOOL bVarExist = FALSE;
322
323 if (strlen(szVar) > 127)
324 return FALSE;
325
326 // Check for variable existence
327 sprintf(szQuery, "SELECT var_value FROM config WHERE var_name='%s'", szVar);
328 hResult = DBSelect(g_hCoreDB, szQuery);
329 if (hResult != 0)
330 {
331 if (DBGetNumRows(hResult) > 0)
332 bVarExist = TRUE;
333 DBFreeResult(hResult);
334 }
335
336 // Don't create non-existing variable if creation flag not set
337 if (!bCreate && !bVarExist)
338 return FALSE;
339
340 // Create or update variable value
341 if (bVarExist)
342 sprintf(szQuery, "UPDATE config SET var_value='%s' WHERE var_name='%s'", szValue, szVar);
343 else
344 sprintf(szQuery, "INSERT INTO config (var_name,var_value,is_visible,"
345 "need_server_restart) VALUES ('%s','%s',0,0)", szVar, szValue);
346 return DBQuery(g_hCoreDB, szQuery);
347 }
348
349
350 //
351 // Write integer value to configuration table
352 //
353
354 BOOL ConfigWriteInt(char *szVar, int iValue, BOOL bCreate)
355 {
356 char szBuffer[64];
357
358 sprintf(szBuffer, "%ld", iValue);
359 return ConfigWriteStr(szVar, szBuffer, bCreate);
360 }
361
362
363 //
364 // Write unsigned long value to configuration table
365 //
366
367 BOOL ConfigWriteULong(char *szVar, DWORD dwValue, BOOL bCreate)
368 {
369 char szBuffer[64];
370
371 sprintf(szBuffer, "%lu", dwValue);
372 return ConfigWriteStr(szVar, szBuffer, bCreate);
373 }