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