Fixed problem with file sheduled file upload. Fixed some memory leaks Fixes #1187
[public/netxms.git] / src / server / core / ap_jobs.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2013 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: ap_jobs.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Scheduled file upload
27 */
28 void ScheduleDeployPolicy(const ScheduledTaskParameters *params)
29 {
30 Node *object = (Node *)FindObjectById(params->m_objectId, OBJECT_NODE);
31 if (object != NULL)
32 {
33 if (object->checkAccessRights(params->m_userId, OBJECT_ACCESS_CONTROL))
34 {
35 ServerJob *job = new PolicyDeploymentJob(params->m_params, params->m_objectId, params->m_userId);
36 if (!AddJob(job))
37 {
38 delete job;
39 DbgPrintf(4, _T("ScheduleDeployPolicy: Failed to add job(incorrect parameters or no such object)."));
40 }
41 }
42 else
43 DbgPrintf(4, _T("ScheduleDeployPolicy: Access to node %s denied"), object->getName());
44 }
45 else
46 DbgPrintf(4, _T("ScheduleDeployPolicy: Node with id=\'%d\' not found"), params->m_userId);
47 }
48
49 /**
50 * Constructor
51 */
52 PolicyDeploymentJob::PolicyDeploymentJob(Node *node, AgentPolicy *policy, UINT32 userId)
53 : ServerJob(_T("DEPLOY_AGENT_POLICY"), _T("Deploy agent policy"), node->getId(), userId, false)
54 {
55 m_node = node;
56 m_policy = policy;
57 node->incRefCount();
58 policy->incRefCount();
59
60 TCHAR buffer[1024];
61 _sntprintf(buffer, 1024, _T("Deploy policy %s"), policy->getName());
62 setDescription(buffer);
63 }
64
65
66 /**
67 * Constructor
68 */
69 PolicyDeploymentJob::PolicyDeploymentJob(const TCHAR* params, UINT32 node, UINT32 userId)
70 : ServerJob(_T("DEPLOY_AGENT_POLICY"), _T("Deploy agent policy"), node, userId, false)
71 {
72 StringList paramList(params, _T(","));
73 if(paramList.size() < 1)
74 {
75 setIsValid(false);
76 return;
77 }
78
79 m_node = (Node *)FindObjectById(node, OBJECT_NODE);
80 NetObj *obj = FindObjectById(_tcstol(paramList.get(0), NULL, 0));
81 if(obj != NULL && (obj->getObjectClass() == OBJECT_AGENTPOLICY || obj->getObjectClass() == OBJECT_AGENTPOLICY_CONFIG
82 || obj->getObjectClass() == OBJECT_AGENTPOLICY_LOGPARSER))
83 {
84 m_policy = (AgentPolicy *)obj;
85 }
86
87 if(m_node != NULL && m_policy != NULL)
88 {
89 m_node->incRefCount();
90 m_policy->incRefCount();
91 }
92 else
93 {
94 setIsValid(false);
95 return;
96 }
97
98 if(paramList.size() >= 2)
99 {
100 m_retryCount = _tcstol(paramList.get(1), NULL, 0);
101 }
102 else
103 m_retryCount = 0;
104
105 TCHAR buffer[1024];
106 _sntprintf(buffer, 1024, _T("Deploy policy %s"), m_policy->getName());
107 setDescription(buffer);
108 }
109
110 /**
111 * Destructor
112 */
113 PolicyDeploymentJob::~PolicyDeploymentJob()
114 {
115 if(isValid())
116 {
117 m_node->decRefCount();
118 m_policy->decRefCount();
119 }
120 }
121
122 /**
123 * Run job
124 */
125 ServerJobResult PolicyDeploymentJob::run()
126 {
127 ServerJobResult success = JOB_RESULT_FAILED;
128
129 TCHAR jobName[1024];
130 _sntprintf(jobName, 1024, _T("Deploy policy %s"), m_policy->getName());
131
132 setDescription(jobName);
133 AgentConnectionEx *conn = m_node->createAgentConnection(true);
134 if (conn != NULL)
135 {
136 UINT32 rcc = conn->deployPolicy(m_policy);
137 conn->decRefCount();
138 if (rcc == ERR_SUCCESS)
139 {
140 m_policy->linkNode(m_node);
141 success = JOB_RESULT_SUCCESS;
142 }
143 else
144 {
145 setFailureMessage(AgentErrorCodeToText(rcc));
146 }
147 }
148 else
149 {
150 setFailureMessage(_T("Agent connection not available"));
151 }
152
153 if(success == JOB_RESULT_FAILED && m_retryCount-- > 0)
154 {
155 TCHAR description[256];
156 _sntprintf(description, 256, _T("Policy deploy failed. Wainting %d minutes to restart job."), getNextJobExecutionTime()/60);
157 setDescription(description);
158 success = JOB_RESULT_RESCHEDULE;
159 }
160 return success;
161 }
162
163 /**
164 * Serializes job parameters into TCHAR line separated by ';'
165 */
166 TCHAR *PolicyDeploymentJob::serializeParameters()
167 {
168 String params;
169 params.append(m_policy->getId());
170 params.append(_T(','));
171 params.append(m_retryCount);
172 return _tcsdup(params.getBuffer());
173 }
174
175 /**
176 * Schedules execution in 10 minutes
177 */
178 void PolicyDeploymentJob::rescheduleExecution()
179 {
180 TCHAR *params = serializeParameters();
181 AddOneTimeScheduledTask(_T("Policy.Deploy"), time(NULL) + getNextJobExecutionTime(), params, 0, getRemoteNode(), SYSTEM_ACCESS_FULL, SCHEDULED_TASK_SYSTEM);//TODO: change to correct user
182 free(params);
183 }
184
185
186 /**
187 * Scheduled file upload
188 */
189 void ScheduleUninstallPolicy(const ScheduledTaskParameters *params)
190 {
191 Node *object = (Node *)FindObjectById(params->m_objectId, OBJECT_NODE);
192 if (object != NULL)
193 {
194 if (object->checkAccessRights(params->m_userId, OBJECT_ACCESS_CONTROL))
195 {
196 ServerJob *job = new PolicyUninstallJob(params->m_params, params->m_objectId, params->m_userId);
197 if (!AddJob(job))
198 {
199 delete job;
200 DbgPrintf(4, _T("ScheduleUninstallPolicy: Failed to add job(incorrect parameters or no such object)."));
201 }
202 }
203 else
204 DbgPrintf(4, _T("ScheduleUninstallPolicy: Access to node %s denied"), object->getName());
205 }
206 else
207 DbgPrintf(4, _T("ScheduleUninstallPolicy: Node with id=\'%d\' not found"), params->m_userId);
208 }
209
210 /**
211 * Constructor
212 */
213 PolicyUninstallJob::PolicyUninstallJob(Node *node, AgentPolicy *policy, UINT32 userId)
214 : ServerJob(_T("UNINSTALL_AGENT_POLICY"), _T("Uninstall agent policy"), node->getId(), userId, false)
215 {
216 m_node = node;
217 m_policy = policy;
218 node->incRefCount();
219 policy->incRefCount();
220
221 TCHAR buffer[1024];
222 _sntprintf(buffer, 1024, _T("Uninstall policy %s"), policy->getName());
223 setDescription(buffer);
224 }
225
226 /**
227 * Constructor
228 */
229 PolicyUninstallJob::PolicyUninstallJob(const TCHAR* params, UINT32 node, UINT32 userId)
230 : ServerJob(_T("DEPLOY_AGENT_POLICY"), _T("Deploy agent policy"), node, userId, false)
231 {
232 StringList paramList(params, _T(","));
233 if(paramList.size() < 1)
234 {
235 setIsValid(false);
236 return;
237 }
238
239 m_node = (Node *)FindObjectById(node, OBJECT_NODE);
240 NetObj *obj = FindObjectById(_tcstol(paramList.get(0), NULL, 0));
241 if(obj != NULL && (obj->getObjectClass() == OBJECT_AGENTPOLICY || obj->getObjectClass() == OBJECT_AGENTPOLICY_CONFIG
242 || obj->getObjectClass() == OBJECT_AGENTPOLICY_LOGPARSER))
243 {
244 m_policy = (AgentPolicy *)obj;
245 }
246
247 if(m_node != NULL && m_policy != NULL)
248 {
249 m_node->incRefCount();
250 m_policy->incRefCount();
251 }
252 else
253 {
254 setIsValid(false);
255 return;
256 }
257
258 if(paramList.size() >= 2)
259 {
260 m_retryCount = _tcstol(paramList.get(1), NULL, 0);
261 }
262 else
263 m_retryCount = 0;
264
265 TCHAR buffer[1024];
266 _sntprintf(buffer, 1024, _T("Uninstall policy %s"), m_policy->getName());
267 setDescription(buffer);
268 }
269
270 /**
271 * Destructor
272 */
273 PolicyUninstallJob::~PolicyUninstallJob()
274 {
275 if(isValid())
276 {
277 m_node->decRefCount();
278 m_policy->decRefCount();
279 }
280 }
281
282 /**
283 * Run job
284 */
285 ServerJobResult PolicyUninstallJob::run()
286 {
287 ServerJobResult success = JOB_RESULT_FAILED;
288
289 AgentConnectionEx *conn = m_node->createAgentConnection();
290 if (conn != NULL)
291 {
292 UINT32 rcc = conn->uninstallPolicy(m_policy);
293 conn->decRefCount();
294 if (rcc == ERR_SUCCESS)
295 {
296 m_policy->unlinkNode(m_node);
297 success = JOB_RESULT_SUCCESS;
298 }
299 else
300 {
301 setFailureMessage(AgentErrorCodeToText(rcc));
302 }
303 }
304 else
305 {
306 setFailureMessage(_T("Agent connection not available"));
307 }
308
309 if(success == JOB_RESULT_FAILED && m_retryCount-- > 0)
310 {
311 TCHAR description[256];
312 _sntprintf(description, 256, _T("Policy uninstall failed. Wainting %d minutes to restart job."), getNextJobExecutionTime()/60);
313 setDescription(description);
314 success = JOB_RESULT_RESCHEDULE;
315 }
316
317 return success;
318 }
319
320 /**
321 * Serializes job parameters into TCHAR line separated by ';'
322 */
323 TCHAR *PolicyUninstallJob::serializeParameters()
324 {
325 String params;
326 params.append(m_policy->getId());
327 params.append(_T(','));
328 params.append(m_retryCount);
329 return _tcsdup(params.getBuffer());
330 }
331
332 /**
333 * Schedules execution in 10 minutes
334 */
335 void PolicyUninstallJob::rescheduleExecution()
336 {
337 TCHAR *params = serializeParameters();
338 AddOneTimeScheduledTask(_T("Policy.Uninstall"), time(NULL) + getNextJobExecutionTime(), params, 0, getRemoteNode(), SYSTEM_ACCESS_FULL, SCHEDULED_TASK_SYSTEM);//TODO: change to correct user
339 free(params);
340 }