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"
23 #include <afxsock.h> // MFC socket extensions
31 static char THIS_FILE
[] = __FILE__
;
34 /////////////////////////////////////////////////////////////////////////////
37 BEGIN_MESSAGE_MAP(CNxconfigApp
, CWinApp
)
38 //{{AFX_MSG_MAP(CNxconfigApp)
39 ON_COMMAND(ID_FILE_CFG_WIZARD
, OnFileCfgWizard
)
43 /////////////////////////////////////////////////////////////////////////////
44 // CNxconfigApp construction
46 CNxconfigApp::CNxconfigApp()
48 // TODO: add construction code here,
49 // Place all significant initialization in InitInstance
52 /////////////////////////////////////////////////////////////////////////////
53 // The one and only CNxconfigApp object
55 CNxconfigApp appNxConfig
;
57 /////////////////////////////////////////////////////////////////////////////
58 // CNxconfigApp initialization
60 BOOL
CNxconfigApp::InitInstance()
63 DWORD dwSize
, dwData
= 0;
64 TCHAR
*pszArg
, szCmd
[1024];
68 AfxMessageBox(IDP_SOCKETS_INIT_FAILED
);
72 // Read installation data from registry
73 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
74 KEY_QUERY_VALUE
, &hKey
) == ERROR_SUCCESS
)
76 dwSize
= sizeof(DWORD
);
77 RegQueryValueEx(hKey
, _T("ServerIsConfigured"), NULL
, NULL
, (BYTE
*)&dwData
, &dwSize
);
79 dwSize
= (MAX_PATH
- 16) * sizeof(TCHAR
);
80 if (RegQueryValueEx(hKey
, _T("InstallPath"), NULL
, NULL
,
81 (BYTE
*)m_szInstallDir
, &dwSize
) != ERROR_SUCCESS
)
83 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
84 m_szInstallDir
[0] = 0;
91 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
92 m_szInstallDir
[0] = 0;
96 if (!_tcsicmp(m_lpCmdLine
, _T("--create-agent-config")))
101 pszArg
= ExtractWord(m_lpCmdLine
, szCmd
);
102 if (!_tcsicmp(szCmd
, _T("--create-nxhttpd-config")))
104 CreateWebConfig(pszArg
);
108 // Check if server is already configured or we cannot determine
109 // installation directory
110 if ((dwData
!= 0) || (m_szInstallDir
[0] == 0))
112 if ((dwData
!= 0) && (_tcsicmp(m_lpCmdLine
, _T("--configure-if-needed"))))
113 AfxMessageBox(_T("Server already configured"));
117 AfxEnableControlContainer();
119 // Standard initialization
120 // If you are not using these features and wish to reduce the size
121 // of your final executable, you should remove from the following
122 // the specific initialization routines you do not need.
124 // Change the registry key under which our settings are stored.
125 SetRegistryKey(_T("NetXMS"));
127 // To create the main window, this code creates a new frame window
128 // object and then sets it as the application's main window object.
129 CMainFrame
* pFrame
= new CMainFrame
;
132 // create and load the frame with its resources
133 pFrame
->LoadFrame(IDR_MAINFRAME
,
134 WS_OVERLAPPEDWINDOW
| FWS_ADDTOTITLE
, NULL
,
137 // The one and only window has been initialized, so show and update it.
138 //pFrame->ShowWindow(SW_SHOW);
139 //pFrame->UpdateWindow();
141 pFrame
->PostMessage(WM_COMMAND
, ID_FILE_CFG_WIZARD
);
146 /////////////////////////////////////////////////////////////////////////////
147 // CNxconfigApp message handlers
149 void CNxconfigApp::OnFileCfgWizard()
151 CConfigWizard
dlg(_T("Configure NetXMS Server"), m_pMainWnd
, 0);
153 CConfigFilePage pgConfigFile
;
154 CDBSelectPage pgSelectDB
;
155 CODBCPage pgCheckODBC
;
156 CPollCfgPage pgPollConfig
;
158 CLoggingPage pgLogging
;
159 CWinSrvPage pgWinSrv
;
160 CSrvDepsPage pgSrvDeps
;
161 CSummaryPage pgSummary
;
162 CProcessingPage pgProcessing
;
163 CFinishPage pgFinish
;
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(&pgSrvDeps
);
175 dlg
.AddPage(&pgSummary
);
176 dlg
.AddPage(&pgProcessing
);
177 dlg
.AddPage(&pgFinish
);
179 if (dlg
.DoModal() == ID_WIZFINISH
)
184 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
185 KEY_ALL_ACCESS
, &hKey
) == ERROR_SUCCESS
)
187 RegSetValueEx(hKey
, _T("ServerIsConfigured"), 0, REG_DWORD
, (BYTE
*)&dwData
, sizeof(DWORD
));
192 m_pMainWnd
->PostMessage(WM_COMMAND
, ID_APP_EXIT
);
197 // Create configuration file for local agent
200 void CNxconfigApp::CreateAgentConfig()
205 IP_ADAPTER_INFO
*pBuffer
, *pInfo
;
206 IP_ADDR_STRING
*pAddr
;
207 TCHAR szAddrList
[4096], szFile
[MAX_PATH
];
209 _sntprintf(szFile
, MAX_PATH
, _T("%s\\etc\\nxagentd.conf"), m_szInstallDir
);
210 if (_taccess(szFile
, 0) == 0)
211 return; // File already exist, we shouldn't overwrite it
213 // Get local interface list
215 if (GetAdaptersInfo(NULL
, &dwSize
) == ERROR_BUFFER_OVERFLOW
)
217 pBuffer
= (IP_ADAPTER_INFO
*)malloc(dwSize
);
218 if (GetAdaptersInfo(pBuffer
, &dwSize
) == ERROR_SUCCESS
)
220 for(pInfo
= pBuffer
; pInfo
!= NULL
; pInfo
= pInfo
->Next
)
222 // Read all IP addresses for adapter
223 for(pAddr
= &pInfo
->IpAddressList
; pAddr
!= NULL
; pAddr
= pAddr
->Next
)
225 if (_tcscmp(pAddr
->IpAddress
.String
, _T("0.0.0.0")))
227 if (szAddrList
[0] != 0)
228 _tcscat(szAddrList
, _T(", "));
229 _tcscat(szAddrList
, pAddr
->IpAddress
.String
);
237 fp
= _tfopen(szFile
, _T("w"));
240 currTime
= time(NULL
);
241 _ftprintf(fp
, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"),
243 _ftprintf(fp
, _T("LogFile = {syslog}\nServers = 127.0.0.1\n"));
244 if (szAddrList
[0] != 0)
245 _ftprintf(fp
, _T("InstallationServers = %s\n"), szAddrList
);
246 _ftprintf(fp
, _T("FileStore = %s\\var\n"), m_szInstallDir
);
247 _ftprintf(fp
, _T("RequireAuthentication = no\n"));
248 _ftprintf(fp
, _T("SubAgent = winperf.nsm\nSubAgent = portcheck.nsm\n"));
255 // Create configuration file for nxhttpd
258 void CNxconfigApp::CreateWebConfig(TCHAR
*pszServer
)
262 TCHAR szFile
[MAX_PATH
];
264 _sntprintf(szFile
, MAX_PATH
, _T("%s\\etc\\nxhttpd.conf"), m_szInstallDir
);
265 if (_taccess(szFile
, 0) == 0)
266 return; // File already exist, we shouldn't overwrite it
268 fp
= _tfopen(szFile
, _T("w"));
271 currTime
= time(NULL
);
272 _ftprintf(fp
, _T("#\n# NetXMS web server configuration file\n# Created by server installer at %s#\n\n"),
274 _ftprintf(fp
, _T("LogFile = {syslog}\nMasterServer = %s\n"), pszServer
);
275 _ftprintf(fp
, _T("DocumentRoot = %s\\var\\www\n"), m_szInstallDir
);