9e7dc8ab34e42f28d8a77cdd69e0cef5e8913f02
[public/netxms.git] / src / client / nxevent / nxevent.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Command line event sender
4 ** Copyright (C) 2003-2015 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: nxevent.cpp
21 **
22 **/
23
24 #include "nxevent.h"
25
26 /**
27 * Static data
28 */
29 static BOOL m_bDebug = FALSE;
30 static TCHAR m_szServer[MAX_DB_STRING] = _T("127.0.0.1");
31 static TCHAR m_szLogin[MAX_DB_STRING] = _T("guest");
32 static TCHAR m_szPassword[MAX_DB_STRING] = _T("");
33 static TCHAR m_szUserTag[MAX_USERTAG_LENGTH] = _T("");
34 static DWORD m_dwEventCode = 0;
35 static TCHAR m_eventName[MAX_EVENT_NAME] = _T("");
36 static DWORD m_dwObjectId = 0;
37 static DWORD m_dwTimeOut = 3;
38
39 /**
40 * Callback function for debug printing
41 */
42 static void DebugCallback(const TCHAR *pMsg)
43 {
44 _tprintf(_T("*debug* %s\n"), pMsg);
45 }
46
47 /**
48 * Send event to server
49 */
50 static DWORD SendEvent(int iNumArgs, char **pArgList, BOOL bEncrypt)
51 {
52 DWORD dwResult;
53
54 if (!NXCInitialize())
55 {
56 _tprintf(_T("Failed to initialize NetXMS client library\n"));
57 }
58 else
59 {
60 if (m_bDebug)
61 NXCSetDebugCallback(DebugCallback);
62
63 NXCSession *session = new NXCSession();
64 static UINT32 protocolVersions[] = { CPV_INDEX_TRAP };
65 dwResult = session->connect(m_szServer, m_szLogin, m_szPassword, bEncrypt ? NXCF_ENCRYPT : 0, _T("nxevent/") NETXMS_VERSION_STRING,
66 protocolVersions, sizeof(protocolVersions) / sizeof(UINT32));
67 if (dwResult != RCC_SUCCESS)
68 {
69 _tprintf(_T("Unable to connect to server: %s\n"), NXCGetErrorText(dwResult));
70 }
71 else
72 {
73 session->setCommandTimeout(m_dwTimeOut * 1000);
74 EventController *ctrl = (EventController *)session->getController(CONTROLLER_EVENTS);
75 #ifdef UNICODE
76 WCHAR **argList = (WCHAR **)malloc(sizeof(WCHAR *) * iNumArgs);
77 for(int i = 0; i < iNumArgs; i++)
78 argList[i] = WideStringFromMBString(pArgList[i]);
79 dwResult = ctrl->sendEvent(m_dwEventCode, m_eventName, m_dwObjectId, iNumArgs, argList, m_szUserTag);
80 for(int i = 0; i < iNumArgs; i++)
81 free(argList[i]);
82 free(argList);
83 #else
84 dwResult = ctrl->sendEvent(m_dwEventCode, m_eventName, m_dwObjectId, iNumArgs, pArgList, m_szUserTag);
85 #endif
86 if (dwResult != RCC_SUCCESS)
87 _tprintf(_T("Unable to send event: %s\n"), NXCGetErrorText(dwResult));
88 }
89 delete session;
90 }
91 return dwResult;
92 }
93
94 #ifdef _WIN32
95 #define CMDLINE_OPTIONS "deho:P:T:u:vw:"
96 #else
97 #define CMDLINE_OPTIONS "c:deho:P:T:u:vw:"
98 #endif
99
100 /**
101 * Entry point
102 */
103 int main(int argc, char *argv[])
104 {
105 int ch, rcc = RCC_INVALID_ARGUMENT;
106 BOOL bStart = TRUE, bEncrypt = FALSE;
107
108 // Parse command line
109 opterr = 1;
110 while((ch = getopt(argc, argv, CMDLINE_OPTIONS)) != -1)
111 {
112 switch(ch)
113 {
114 case 'h': // Display help and exit
115 printf("Usage: nxevent [<options>] <server> <event> [<param_1> [... <param_N>]]\n"
116 "Valid options are:\n"
117 #ifndef _WIN32
118 " -c : Codepage (default is " ICONV_DEFAULT_CODEPAGE ")\n"
119 #endif
120 " -d : Turn on debug mode.\n"
121 " -e : Encrypt session.\n"
122 " -h : Display help and exit.\n"
123 " -o <id> : Specify source object ID.\n"
124 " -P <password> : Specify user's password. Default is empty password.\n"
125 " -T <tag> : User tag to be associated with the message. Default is empty.\n"
126 " -u <user> : Login to server as <user>. Default is \"guest\".\n"
127 " -v : Display version and exit.\n"
128 " -w <seconds> : Specify command timeout (default is 3 seconds).\n"
129 "Event could be specified either by code or by name\n"
130 "\n");
131 bStart = FALSE;
132 break;
133 #ifndef _WIN32
134 case 'c':
135 SetDefaultCodepage(optarg);
136 break;
137 #endif
138 case 'd':
139 m_bDebug = TRUE;
140 break;
141 case 'e':
142 bEncrypt = TRUE;
143 break;
144 case 'o':
145 m_dwObjectId = strtoul(optarg, NULL, 0);
146 break;
147 case 'u':
148 #ifdef UNICODE
149 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szLogin, MAX_DB_STRING);
150 m_szLogin[MAX_DB_STRING - 1] = 0;
151 #else
152 nx_strncpy(m_szLogin, optarg, MAX_DB_STRING);
153 #endif
154 break;
155 case 'P':
156 #ifdef UNICODE
157 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szPassword, MAX_DB_STRING);
158 m_szPassword[MAX_DB_STRING - 1] = 0;
159 #else
160 nx_strncpy(m_szPassword, optarg, MAX_DB_STRING);
161 #endif
162 break;
163 case 'T':
164 #ifdef UNICODE
165 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, optarg, -1, m_szUserTag, MAX_USERTAG_LENGTH);
166 m_szUserTag[MAX_USERTAG_LENGTH - 1] = 0;
167 #else
168 nx_strncpy(m_szUserTag, optarg, MAX_USERTAG_LENGTH);
169 #endif
170 break;
171 case 'w':
172 m_dwTimeOut = strtoul(optarg, NULL, 0);
173 if ((m_dwTimeOut < 1) || (m_dwTimeOut > 120))
174 {
175 _tprintf(_T("Invalid timeout %hs\n"), optarg);
176 bStart = FALSE;
177 }
178 break;
179 case 'v':
180 _tprintf(_T("NetXMS Event Sender Version ") NETXMS_VERSION_STRING _T("\n"));
181 bStart = FALSE;
182 break;
183 case '?':
184 bStart = FALSE;
185 break;
186 default:
187 break;
188 }
189 }
190
191 // Do requested action if everything is OK
192 if (bStart)
193 {
194 if (argc - optind < 2)
195 {
196 _tprintf(_T("Required arguments missing. Use nxevent -h for help.\n"));
197 }
198 else
199 {
200 #ifdef _WIN32
201 WSADATA wsaData;
202
203 if (WSAStartup(2, &wsaData) != 0)
204 {
205 _tprintf(_T("Unable to initialize Windows sockets\n"));
206 return RCC_COMM_FAILURE;
207 }
208 #endif
209 #ifdef UNICODE
210 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[optind], -1, m_szServer, 256);
211 m_szServer[255] = 0;
212 #else
213 nx_strncpy(m_szServer, argv[optind], 256);
214 #endif
215
216 char *eptr;
217 m_dwEventCode = strtoul(argv[optind + 1], &eptr, 0);
218 if (*eptr != 0)
219 {
220 // assume that event is specified by name
221 m_dwEventCode = 0;
222 #ifdef UNICODE
223 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, argv[optind + 1], -1, m_eventName, MAX_EVENT_NAME);
224 m_eventName[MAX_EVENT_NAME - 1] = 0;
225 #else
226 nx_strncpy(m_eventName, argv[optind + 1], MAX_EVENT_NAME);
227 #endif
228 }
229
230 rcc = (int)SendEvent(argc - optind - 2, &argv[optind + 2], bEncrypt);
231 }
232 }
233
234 return rcc;
235 }