Fixed problem with file sheduled file upload. Fixed some memory leaks Fixes #1187
[public/netxms.git] / src / server / core / ap_jobs.cpp
CommitLineData
a2871ffc 1/*
45d84f8a 2** NetXMS - Network Management System
21c9acce 3** Copyright (C) 2003-2013 Victor Kirhenshtein
45d84f8a
VK
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
82deb2d7
TD
25/**
26 * Scheduled file upload
27 */
28void 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
21c9acce
VK
49/**
50 * Constructor
51 */
967893bb 52PolicyDeploymentJob::PolicyDeploymentJob(Node *node, AgentPolicy *policy, UINT32 userId)
c42b4551 53 : ServerJob(_T("DEPLOY_AGENT_POLICY"), _T("Deploy agent policy"), node->getId(), userId, false)
45d84f8a
VK
54{
55 m_node = node;
56 m_policy = policy;
21c9acce
VK
57 node->incRefCount();
58 policy->incRefCount();
45d84f8a
VK
59
60 TCHAR buffer[1024];
c42b4551 61 _sntprintf(buffer, 1024, _T("Deploy policy %s"), policy->getName());
45d84f8a
VK
62 setDescription(buffer);
63}
64
82deb2d7
TD
65
66/**
67 * Constructor
68 */
69PolicyDeploymentJob::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);
13438df0 95 return;
82deb2d7
TD
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
21c9acce
VK
110/**
111 * Destructor
112 */
45d84f8a
VK
113PolicyDeploymentJob::~PolicyDeploymentJob()
114{
82deb2d7
TD
115 if(isValid())
116 {
117 m_node->decRefCount();
118 m_policy->decRefCount();
119 }
45d84f8a
VK
120}
121
21c9acce
VK
122/**
123 * Run job
124 */
82deb2d7 125ServerJobResult PolicyDeploymentJob::run()
45d84f8a 126{
82deb2d7 127 ServerJobResult success = JOB_RESULT_FAILED;
45d84f8a 128
693ac985 129 TCHAR jobName[1024];
c42b4551 130 _sntprintf(jobName, 1024, _T("Deploy policy %s"), m_policy->getName());
693ac985 131
82deb2d7 132 setDescription(jobName);
7f6ecb6d 133 AgentConnectionEx *conn = m_node->createAgentConnection(true);
82deb2d7 134 if (conn != NULL)
693ac985 135 {
82deb2d7
TD
136 UINT32 rcc = conn->deployPolicy(m_policy);
137 conn->decRefCount();
138 if (rcc == ERR_SUCCESS)
693ac985 139 {
82deb2d7
TD
140 m_policy->linkNode(m_node);
141 success = JOB_RESULT_SUCCESS;
693ac985
AK
142 }
143 else
144 {
82deb2d7 145 setFailureMessage(AgentErrorCodeToText(rcc));
693ac985 146 }
82deb2d7
TD
147 }
148 else
149 {
150 setFailureMessage(_T("Agent connection not available"));
151 }
693ac985 152
82deb2d7
TD
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 }
93599cfd
VK
160 return success;
161}
162
82deb2d7
TD
163/**
164 * Serializes job parameters into TCHAR line separated by ';'
165 */
c9b9393b 166TCHAR *PolicyDeploymentJob::serializeParameters()
82deb2d7
TD
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 */
178void PolicyDeploymentJob::rescheduleExecution()
179{
c9b9393b 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);
82deb2d7
TD
183}
184
185
186/**
187 * Scheduled file upload
188 */
189void 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
21c9acce
VK
210/**
211 * Constructor
212 */
967893bb 213PolicyUninstallJob::PolicyUninstallJob(Node *node, AgentPolicy *policy, UINT32 userId)
c42b4551 214 : ServerJob(_T("UNINSTALL_AGENT_POLICY"), _T("Uninstall agent policy"), node->getId(), userId, false)
93599cfd
VK
215{
216 m_node = node;
217 m_policy = policy;
21c9acce
VK
218 node->incRefCount();
219 policy->incRefCount();
93599cfd
VK
220
221 TCHAR buffer[1024];
c42b4551 222 _sntprintf(buffer, 1024, _T("Uninstall policy %s"), policy->getName());
93599cfd
VK
223 setDescription(buffer);
224}
225
82deb2d7
TD
226/**
227 * Constructor
228 */
229PolicyUninstallJob::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);
13438df0 255 return;
82deb2d7
TD
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
21c9acce
VK
270/**
271 * Destructor
272 */
93599cfd
VK
273PolicyUninstallJob::~PolicyUninstallJob()
274{
82deb2d7
TD
275 if(isValid())
276 {
277 m_node->decRefCount();
278 m_policy->decRefCount();
279 }
93599cfd
VK
280}
281
21c9acce
VK
282/**
283 * Run job
284 */
82deb2d7 285ServerJobResult PolicyUninstallJob::run()
93599cfd 286{
82deb2d7 287 ServerJobResult success = JOB_RESULT_FAILED;
93599cfd 288
cd9f247e 289 AgentConnectionEx *conn = m_node->createAgentConnection();
93599cfd
VK
290 if (conn != NULL)
291 {
967893bb 292 UINT32 rcc = conn->uninstallPolicy(m_policy);
1693f955 293 conn->decRefCount();
93599cfd
VK
294 if (rcc == ERR_SUCCESS)
295 {
296 m_policy->unlinkNode(m_node);
82deb2d7 297 success = JOB_RESULT_SUCCESS;
45d84f8a
VK
298 }
299 else
300 {
301 setFailureMessage(AgentErrorCodeToText(rcc));
302 }
303 }
304 else
305 {
306 setFailureMessage(_T("Agent connection not available"));
307 }
82deb2d7
TD
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
45d84f8a
VK
317 return success;
318}
82deb2d7
TD
319
320/**
321 * Serializes job parameters into TCHAR line separated by ';'
322 */
c9b9393b 323TCHAR *PolicyUninstallJob::serializeParameters()
82deb2d7
TD
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 */
335void PolicyUninstallJob::rescheduleExecution()
336{
c9b9393b 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);
82deb2d7 340}