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;
67 AfxMessageBox(IDP_SOCKETS_INIT_FAILED
);
71 // Read installation data from registry
72 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
73 KEY_QUERY_VALUE
, &hKey
) == ERROR_SUCCESS
)
75 dwSize
= sizeof(DWORD
);
76 RegQueryValueEx(hKey
, _T("ServerIsConfigured"), NULL
, NULL
, (BYTE
*)&dwData
, &dwSize
);
78 dwSize
= (MAX_PATH
- 16) * sizeof(TCHAR
);
79 if (RegQueryValueEx(hKey
, _T("InstallPath"), NULL
, NULL
,
80 (BYTE
*)m_szInstallDir
, &dwSize
) != ERROR_SUCCESS
)
82 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
83 m_szInstallDir
[0] = 0;
90 AfxMessageBox(_T("Unable to determine NetXMS installation directory"));
91 m_szInstallDir
[0] = 0;
95 if (!_tcsicmp(m_lpCmdLine
, _T("--create-agent-config")))
101 // Check if server is already configured or we cannot determine
102 // installation directory
103 if ((dwData
!= 0) || (m_szInstallDir
[0] == 0))
105 if ((dwData
!= 0) && (_tcsicmp(m_lpCmdLine
, _T("--configure-if-needed"))))
106 AfxMessageBox(_T("Server already configured"));
110 AfxEnableControlContainer();
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.
117 // Change the registry key under which our settings are stored.
118 SetRegistryKey(_T("NetXMS"));
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
;
125 // create and load the frame with its resources
126 pFrame
->LoadFrame(IDR_MAINFRAME
,
127 WS_OVERLAPPEDWINDOW
| FWS_ADDTOTITLE
, NULL
,
130 // The one and only window has been initialized, so show and update it.
131 //pFrame->ShowWindow(SW_SHOW);
132 //pFrame->UpdateWindow();
134 pFrame
->PostMessage(WM_COMMAND
, ID_FILE_CFG_WIZARD
);
139 /////////////////////////////////////////////////////////////////////////////
140 // CNxconfigApp message handlers
142 void CNxconfigApp::OnFileCfgWizard()
144 CConfigWizard
dlg(_T("Configure NetXMS Server"), m_pMainWnd
, 0);
146 CConfigFilePage pgConfigFile
;
147 CDBSelectPage pgSelectDB
;
148 CODBCPage pgCheckODBC
;
149 CPollCfgPage pgPollConfig
;
151 CLoggingPage pgLogging
;
152 CWinSrvPage pgWinSrv
;
153 CSrvDepsPage pgSrvDeps
;
154 CSummaryPage pgSummary
;
155 CProcessingPage pgProcessing
;
156 CFinishPage pgFinish
;
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
);
172 if (dlg
.DoModal() == ID_WIZFINISH
)
177 if (RegOpenKeyEx(HKEY_LOCAL_MACHINE
, _T("Software\\NetXMS\\Server"), 0,
178 KEY_ALL_ACCESS
, &hKey
) == ERROR_SUCCESS
)
180 RegSetValueEx(hKey
, _T("ServerIsConfigured"), 0, REG_DWORD
, (BYTE
*)&dwData
, sizeof(DWORD
));
185 m_pMainWnd
->PostMessage(WM_COMMAND
, ID_APP_EXIT
);
190 // Create configuration file for local agent
193 void CNxconfigApp::CreateAgentConfig()
198 IP_ADAPTER_INFO
*pBuffer
, *pInfo
;
199 IP_ADDR_STRING
*pAddr
;
200 TCHAR szAddrList
[4096], szFile
[MAX_PATH
];
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
206 // Get local interface list
208 if (GetAdaptersInfo(NULL
, &dwSize
) == ERROR_BUFFER_OVERFLOW
)
210 pBuffer
= (IP_ADAPTER_INFO
*)malloc(dwSize
);
211 if (GetAdaptersInfo(pBuffer
, &dwSize
) == ERROR_SUCCESS
)
213 for(pInfo
= pBuffer
; pInfo
!= NULL
; pInfo
= pInfo
->Next
)
215 // Read all IP addresses for adapter
216 for(pAddr
= &pInfo
->IpAddressList
; pAddr
!= NULL
; pAddr
= pAddr
->Next
)
218 if (strcmp(pAddr
->IpAddress
.String
, "0.0.0.0"))
220 if (szAddrList
[0] != 0)
221 _tcscat(szAddrList
, _T(", "));
224 MultiByteToWideChar(CP_ACP
, MB_PRECOMPOSED
, pAddr
->IpAddress
.String
, -1, ipaddr
, 32);
225 wcscat(szAddrList
, ipaddr
);
227 strcat(szAddrList
, pAddr
->IpAddress
.String
);
236 fp
= _tfopen(szFile
, _T("w"));
239 currTime
= time(NULL
);
240 _ftprintf(fp
, _T("#\n# NetXMS agent configuration file\n# Created by server installer at %s#\n\n"), _tctime(&currTime
));
241 if (szAddrList
[0] != 0)
243 _ftprintf(fp
, _T("LogFile = {syslog}\nMasterServers = 127.0.0.1, %s\n"), szAddrList
);
247 _ftprintf(fp
, _T("LogFile = {syslog}\nMasterServers = 127.0.0.1\n"));
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\nSubAgent = ping.nsm\nSubAgent = ssh.nsm\n"));