1 // nxconfig.cpp : Defines the class behaviors for the application.
8 #include "ConfigWizard.h"
10 #include "DBSelectPage.h"
12 #include "PollCfgPage.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"
29 static char THIS_FILE
[] = __FILE__
;
32 /////////////////////////////////////////////////////////////////////////////
35 BEGIN_MESSAGE_MAP(CNxconfigApp
, CWinApp
)
36 //{{AFX_MSG_MAP(CNxconfigApp)
37 ON_COMMAND(ID_FILE_CFG_WIZARD
, OnFileCfgWizard
)
41 /////////////////////////////////////////////////////////////////////////////
42 // CNxconfigApp construction
44 CNxconfigApp::CNxconfigApp()
46 // TODO: add construction code here,
47 // Place all significant initialization in InitInstance
50 /////////////////////////////////////////////////////////////////////////////
51 // The one and only CNxconfigApp object
53 CNxconfigApp appNxConfig
;
55 /////////////////////////////////////////////////////////////////////////////
56 // CNxconfigApp initialization
58 BOOL
CNxconfigApp::InitInstance()
61 DWORD dwSize
, dwData
= 0;
62 TCHAR
*pszArg
, szCmd
[1024];
66 AfxMessageBox(IDP_SOCKETS_INIT_FAILED
);
70 // Read installation data from registry
71 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
72 KEY_QUERY_VALUE
, &hKey
) == ERROR_SUCCESS
)
74 dwSize
= sizeof(DWORD
);
75 RegQueryValueEx(hKey
, _T("ServerIsConfigured"), NULL
, NULL
, (BYTE
*)&dwData
, &dwSize
);
77 dwSize
= (MAX_PATH
- 16) * sizeof(TCHAR
);
78 if (RegQueryValueEx(hKey
, _T("InstallPath"), NULL
, NULL
,
79 (BYTE
*)m_szInstallDir
, &dwSize
) != ERROR_SUCCESS
)
81 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
82 m_szInstallDir
[0] = 0;
89 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
90 m_szInstallDir
[0] = 0;
94 if (!_tcsicmp(m_lpCmdLine
, _T("--create-agent-config")))
99 pszArg
= ExtractWord(m_lpCmdLine
, szCmd
);
100 if (!_tcsicmp(szCmd
, _T("--create-nxhttpd-config")))
102 CreateWebConfig(pszArg
);
106 // Check if server is already configured or we cannot determine
107 // installation directory
108 if ((dwData
!= 0) || (m_szInstallDir
[0] == 0))
110 if ((dwData
!= 0) && (_tcsicmp(m_lpCmdLine
, _T("--configure-if-needed"))))
111 AfxMessageBox(_T("Server already configured"));
115 AfxEnableControlContainer();
117 // Standard initialization
118 // If you are not using these features and wish to reduce the size
119 // of your final executable, you should remove from the following
120 // the specific initialization routines you do not need.
123 Enable3dControls(); // Call this when using MFC in a shared DLL
125 Enable3dControlsStatic(); // Call this when linking to MFC statically
128 // Change the registry key under which our settings are stored.
129 SetRegistryKey(_T("NetXMS"));
131 // To create the main window, this code creates a new frame window
132 // object and then sets it as the application's main window object.
133 CMainFrame
* pFrame
= new CMainFrame
;
136 // create and load the frame with its resources
137 pFrame
->LoadFrame(IDR_MAINFRAME
,
138 WS_OVERLAPPEDWINDOW
| FWS_ADDTOTITLE
, NULL
,
141 // The one and only window has been initialized, so show and update it.
142 //pFrame->ShowWindow(SW_SHOW);
143 //pFrame->UpdateWindow();
145 pFrame
->PostMessage(WM_COMMAND
, ID_FILE_CFG_WIZARD
);
150 /////////////////////////////////////////////////////////////////////////////
151 // CNxconfigApp message handlers
153 void CNxconfigApp::OnFileCfgWizard()
155 CConfigWizard
dlg(_T("Configure NetXMS Server"), m_pMainWnd
, 0);
157 CConfigFilePage pgConfigFile
;
158 CDBSelectPage pgSelectDB
;
159 CODBCPage pgCheckODBC
;
160 CPollCfgPage pgPollConfig
;
162 CLoggingPage pgLogging
;
163 CWinSrvPage pgWinSrv
;
164 CSrvDepsPage pgSrvDeps
;
165 CSummaryPage pgSummary
;
166 CProcessingPage pgProcessing
;
167 CFinishPage pgFinish
;
169 dlg
.m_psh
.dwFlags
|= PSH_WIZARD
;
170 dlg
.AddPage(&pgIntro
);
171 dlg
.AddPage(&pgConfigFile
);
172 dlg
.AddPage(&pgSelectDB
);
173 dlg
.AddPage(&pgCheckODBC
);
174 dlg
.AddPage(&pgPollConfig
);
175 dlg
.AddPage(&pgSMTP
);
176 dlg
.AddPage(&pgLogging
);
177 dlg
.AddPage(&pgWinSrv
);
178 dlg
.AddPage(&pgSrvDeps
);
179 dlg
.AddPage(&pgSummary
);
180 dlg
.AddPage(&pgProcessing
);
181 dlg
.AddPage(&pgFinish
);
183 if (dlg
.DoModal() == ID_WIZFINISH
)
188 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
189 KEY_ALL_ACCESS
, &hKey
) == ERROR_SUCCESS
)
191 RegSetValueEx(hKey
, _T("ServerIsConfigured"), 0, REG_DWORD
, (BYTE
*)&dwData
, sizeof(DWORD
));
196 m_pMainWnd
->PostMessage(WM_COMMAND
, ID_APP_EXIT
);
201 // Create configuration file for local agent
204 void CNxconfigApp::CreateAgentConfig()
209 IP_ADAPTER_INFO
*pBuffer
, *pInfo
;
210 IP_ADDR_STRING
*pAddr
;
211 TCHAR szAddrList
[4096], szFile
[MAX_PATH
];
213 _sntprintf(szFile
, MAX_PATH
, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir
);
214 if (_taccess(szFile
, 0) == 0)
215 return; // File already exist, we shouldn't overwrite it
217 // Get local interface list
219 if (GetAdaptersInfo(NULL
, &dwSize
) == ERROR_BUFFER_OVERFLOW
)
221 pBuffer
= (IP_ADAPTER_INFO
*)malloc(dwSize
);
222 if (GetAdaptersInfo(pBuffer
, &dwSize
) == ERROR_SUCCESS
)
224 for(pInfo
= pBuffer
; pInfo
!= NULL
; pInfo
= pInfo
->Next
)
226 // Read all IP addresses for adapter
227 for(pAddr
= &pInfo
->IpAddressList
; pAddr
!= NULL
; pAddr
= pAddr
->Next
)
229 if (_tcscmp(pAddr
->IpAddress
.String
, _T("0.0.0.0")))
231 if (szAddrList
[0] != 0)
232 _tcscat(szAddrList
, _T(", "));
233 _tcscat(szAddrList
, pAddr
->IpAddress
.String
);
241 fp
= _tfopen(szFile
, _T("w"));
244 currTime
= time(NULL
);
245 _ftprintf(fp
, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"),
247 _ftprintf(fp
, _T("LogFile = {syslog}\nServers = 127.0.0.1\n"));
248 if (szAddrList
[0] != 0)
249 _ftprintf(fp
, _T("InstallationServers = %s\n"), szAddrList
);
250 _ftprintf(fp
, _T("FileStore = %s\\var\n"), m_szInstallDir
);
251 _ftprintf(fp
, _T("RequireAuthentication = no\n"));
252 _ftprintf(fp
, _T("SubAgent = winperf.nsm\nSubAgent = portcheck.nsm\n"));
259 // Create configuration file for nxhttpd
262 void CNxconfigApp::CreateWebConfig(TCHAR
*pszServer
)
266 TCHAR szFile
[MAX_PATH
];
268 _sntprintf(szFile
, MAX_PATH
, _T("%s\\etc\\nxhttpd.conf"), m_szInstallDir
);
269 if (_taccess(szFile
, 0) == 0)
270 return; // File already exist, we shouldn't overwrite it
272 fp
= _tfopen(szFile
, _T("w"));
275 currTime
= time(NULL
);
276 _ftprintf(fp
, _T("#\n# NetXMS web server configuration file\n# Created by server installer at %s#\n\n"),
278 _ftprintf(fp
, _T("LogFile = {syslog}\nMasterServer = %s\n"), pszServer
);
279 _ftprintf(fp
, _T("DocumentRoot = %s\\var\\www\n"), m_szInstallDir
);