- Added support for SNMP trap logging (server side)
[public/netxms.git] / src / server / tools / nxconfig / nxconfig.cpp
1 // nxconfig.cpp : Defines the class behaviors for the application.
2 //
3
4 #include "stdafx.h"
5 #include "nxconfig.h"
6
7 #include "MainFrm.h"
8 #include "ConfigWizard.h"
9 #include "IntroPage.h"
10 #include "DBSelectPage.h"
11 #include "ODBCPage.h"
12 #include "PollCfgPage.h"
13 #include "SMTPPage.h"
14 #include "SummaryPage.h"
15 #include "ProcessingPage.h"
16 #include "ConfigFilePage.h"
17 #include "WinSrvPage.h"
18 #include "LoggingPage.h"
19 #include "FinishPage.h"
20
21 #include <iphlpapi.h>
22 #include <iprtrmib.h>
23 #include <rtinfo.h>
24
25 #ifdef _DEBUG
26 #define new DEBUG_NEW
27 #undef THIS_FILE
28 static char THIS_FILE[] = __FILE__;
29 #endif
30
31 /////////////////////////////////////////////////////////////////////////////
32 // CNxconfigApp
33
34 BEGIN_MESSAGE_MAP(CNxconfigApp, CWinApp)
35 //{{AFX_MSG_MAP(CNxconfigApp)
36 ON_COMMAND(ID_FILE_CFG_WIZARD, OnFileCfgWizard)
37 //}}AFX_MSG_MAP
38 END_MESSAGE_MAP()
39
40 /////////////////////////////////////////////////////////////////////////////
41 // CNxconfigApp construction
42
43 CNxconfigApp::CNxconfigApp()
44 {
45 // TODO: add construction code here,
46 // Place all significant initialization in InitInstance
47 }
48
49 /////////////////////////////////////////////////////////////////////////////
50 // The one and only CNxconfigApp object
51
52 CNxconfigApp theApp;
53
54 /////////////////////////////////////////////////////////////////////////////
55 // CNxconfigApp initialization
56
57 BOOL CNxconfigApp::InitInstance()
58 {
59 HKEY hKey;
60 DWORD dwSize, dwData = 0;
61
62 if (!AfxSocketInit())
63 {
64 AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
65 return FALSE;
66 }
67
68 // Read installation data from registry
69 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
70 KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
71 {
72 dwSize = sizeof(DWORD);
73 RegQueryValueEx(hKey, _T("ServerIsConfigured"), NULL, NULL, (BYTE *)&dwData, &dwSize);
74
75 dwSize = (MAX_PATH - 16) * sizeof(TCHAR);
76 if (RegQueryValueEx(hKey, _T("InstallPath"), NULL, NULL,
77 (BYTE *)m_szInstallDir, &dwSize) != ERROR_SUCCESS)
78 {
79 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
80 m_szInstallDir[0] = 0;
81 }
82
83 RegCloseKey(hKey);
84 }
85 else
86 {
87 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
88 m_szInstallDir[0] = 0;
89 }
90
91 // Parse command line
92 if (!_tcsicmp(m_lpCmdLine, _T("--create-agent-config")))
93 {
94 CreateAgentConfig();
95 return FALSE;
96 }
97
98 // Check if server is already configured or we cannot determine
99 // installation directory
100 if ((dwData != 0) || (m_szInstallDir[0] == 0))
101 {
102 if ((dwData != 0) && (_tcsicmp(m_lpCmdLine, _T("--configure-if-needed"))))
103 AfxMessageBox(_T("Server already configured"));
104 return FALSE;
105 }
106
107 AfxEnableControlContainer();
108
109 // Standard initialization
110 // If you are not using these features and wish to reduce the size
111 // of your final executable, you should remove from the following
112 // the specific initialization routines you do not need.
113
114 #ifdef _AFXDLL
115 Enable3dControls(); // Call this when using MFC in a shared DLL
116 #else
117 Enable3dControlsStatic(); // Call this when linking to MFC statically
118 #endif
119
120 // Change the registry key under which our settings are stored.
121 // TODO: You should modify this string to be something appropriate
122 // such as the name of your company or organization.
123 SetRegistryKey(_T("Local AppWizard-Generated Applications"));
124
125
126 // To create the main window, this code creates a new frame window
127 // object and then sets it as the application's main window object.
128
129 CMainFrame* pFrame = new CMainFrame;
130 m_pMainWnd = pFrame;
131
132 // create and load the frame with its resources
133
134 pFrame->LoadFrame(IDR_MAINFRAME,
135 WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
136 NULL);
137
138 // The one and only window has been initialized, so show and update it.
139 //pFrame->ShowWindow(SW_SHOW);
140 //pFrame->UpdateWindow();
141
142 pFrame->PostMessage(WM_COMMAND, ID_FILE_CFG_WIZARD);
143
144 return TRUE;
145 }
146
147 /////////////////////////////////////////////////////////////////////////////
148 // CNxconfigApp message handlers
149
150 void CNxconfigApp::OnFileCfgWizard()
151 {
152 CConfigWizard dlg(_T("Configure NetXMS Server"), m_pMainWnd, 0);
153 CIntroPage pgIntro;
154 CConfigFilePage pgConfigFile;
155 CDBSelectPage pgSelectDB;
156 CODBCPage pgCheckODBC;
157 CPollCfgPage pgPollConfig;
158 CSMTPPage pgSMTP;
159 CLoggingPage pgLogging;
160 CWinSrvPage pgWinSrv;
161 CSummaryPage pgSummary;
162 CProcessingPage pgProcessing;
163 CFinishPage pgFinish;
164
165 dlg.m_psh.dwFlags |= PSH_WIZARD;
166 dlg.AddPage(&pgIntro);
167 dlg.AddPage(&pgConfigFile);
168 dlg.AddPage(&pgSelectDB);
169 dlg.AddPage(&pgCheckODBC);
170 dlg.AddPage(&pgPollConfig);
171 dlg.AddPage(&pgSMTP);
172 dlg.AddPage(&pgLogging);
173 dlg.AddPage(&pgWinSrv);
174 dlg.AddPage(&pgSummary);
175 dlg.AddPage(&pgProcessing);
176 dlg.AddPage(&pgFinish);
177
178 if (dlg.DoModal() == ID_WIZFINISH)
179 {
180 HKEY hKey;
181 DWORD dwData = 1;
182
183 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Software\\NetXMS\\Server"), 0,
184 KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
185 {
186 RegSetValueEx(hKey, _T("ServerIsConfigured"), 0, REG_DWORD, (BYTE *)&dwData, sizeof(DWORD));
187 RegCloseKey(hKey);
188 }
189 }
190
191 m_pMainWnd->PostMessage(WM_COMMAND, ID_APP_EXIT);
192 }
193
194
195 //
196 // Create configuration file for local agent
197 //
198
199 void CNxconfigApp::CreateAgentConfig()
200 {
201 FILE *fp;
202 time_t currTime;
203 DWORD dwSize;
204 IP_ADAPTER_INFO *pBuffer, *pInfo;
205 IP_ADDR_STRING *pAddr;
206 TCHAR szAddrList[4096], szFile[MAX_PATH];
207
208 if (_taccess(_T("nxagentd.conf"), 0) == 0)
209 return; // File already exist, we shouldn't overwrite it
210
211 // Get local interface list
212 szAddrList[0] = 0;
213 if (GetAdaptersInfo(NULL, &dwSize) == ERROR_BUFFER_OVERFLOW)
214 {
215 pBuffer = (IP_ADAPTER_INFO *)malloc(dwSize);
216 if (GetAdaptersInfo(pBuffer, &dwSize) == ERROR_SUCCESS)
217 {
218 for(pInfo = pBuffer; pInfo != NULL; pInfo = pInfo->Next)
219 {
220 // Read all IP addresses for adapter
221 for(pAddr = &pInfo->IpAddressList; pAddr != NULL; pAddr = pAddr->Next)
222 {
223 if (_tcscmp(pAddr->IpAddress.String, _T("0.0.0.0")))
224 {
225 if (szAddrList[0] != 0)
226 _tcscat(szAddrList, _T(", "));
227 _tcscat(szAddrList, pAddr->IpAddress.String);
228 }
229 }
230 }
231 }
232 free(pBuffer);
233 }
234
235 _sntprintf(szFile, MAX_PATH, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir);
236 fp = _tfopen(szFile, _T("w"));
237 if (fp != NULL)
238 {
239 currTime = time(NULL);
240 _ftprintf(fp, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"),
241 _tctime(&currTime));
242 _ftprintf(fp, _T("LogFile = {syslog}\nServers = 127.0.0.1\n"));
243 if (szAddrList[0] != 0)
244 _ftprintf(fp, _T("InstallationServers = %s\n"), szAddrList);
245 _ftprintf(fp, _T("FileStore = %s\\var\n"), m_szInstallDir);
246 _ftprintf(fp, _T("RequireAuthentication = no\n"));
247 _ftprintf(fp, _T("SubAgent = winperf.nsm\nSubAgent = portcheck.nsm\n"));
248 fclose(fp);
249 }
250 }