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