b45e67d4e9fcbc4615050579cc93af859ded117f
[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 const 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 AddOneTimeScheduledTask(_T("Policy.Deploy"), time(NULL) + getNextJobExecutionTime(), serializeParameters(), 0, getRemoteNode(), SYSTEM_ACCESS_FULL, SCHEDULED_TASK_SYSTEM);//TODO: change to correct user
181 }
182
183
184 /**
185 * Scheduled file upload
186 */
187 void ScheduleUninstallPolicy(const ScheduledTaskParameters *params)
188 {
189 Node *object = (Node *)FindObjectById(params->m_objectId, OBJECT_NODE);
190 if (object != NULL)
191 {
192 if (object->checkAccessRights(params->m_userId, OBJECT_ACCESS_CONTROL))
193 {
194 ServerJob *job = new PolicyUninstallJob(params->m_params, params->m_objectId, params->m_userId);
195 if (!AddJob(job))
196 {
197 delete job;
198 DbgPrintf(4, _T("ScheduleUninstallPolicy: Failed to add job(incorrect parameters or no such object)."));
199 }
200 }
201 else
202 DbgPrintf(4, _T("ScheduleUninstallPolicy: Access to node %s denied"), object->getName());
203 }
204 else
205 DbgPrintf(4, _T("ScheduleUninstallPolicy: Node with id=\'%d\' not found"), params->m_userId);
206 }
207
208 /**
209 * Constructor
210 */
211 PolicyUninstallJob::PolicyUninstallJob(Node *node, AgentPolicy *policy, UINT32 userId)
212 : ServerJob(_T("UNINSTALL_AGENT_POLICY"), _T("Uninstall agent policy"), node->getId(), userId, false)
213 {
214 m_node = node;
215 m_policy = policy;
216 node->incRefCount();
217 policy->incRefCount();
218
219 TCHAR buffer[1024];
220 _sntprintf(buffer, 1024, _T("Uninstall policy %s"), policy->getName());
221 setDescription(buffer);
222 }
223
224 /**
225 * Constructor
226 */
227 PolicyUninstallJob::PolicyUninstallJob(const TCHAR* params, UINT32 node, UINT32 userId)
228 : ServerJob(_T("DEPLOY_AGENT_POLICY"), _T("Deploy agent policy"), node, userId, false)
229 {
230 StringList paramList(params, _T(","));
231 if(paramList.size() < 1)
232 {
233 setIsValid(false);
234 return;
235 }
236
237 m_node = (Node *)FindObjectById(node, OBJECT_NODE);
238 NetObj *obj = FindObjectById(_tcstol(paramList.get(0), NULL, 0));
239 if(obj != NULL && (obj->getObjectClass() == OBJECT_AGENTPOLICY || obj->getObjectClass() == OBJECT_AGENTPOLICY_CONFIG
240 || obj->getObjectClass() == OBJECT_AGENTPOLICY_LOGPARSER))
241 {
242 m_policy = (AgentPolicy *)obj;
243 }
244
245 if(m_node != NULL && m_policy != NULL)
246 {
247 m_node->incRefCount();
248 m_policy->incRefCount();
249 }
250 else
251 {
252 setIsValid(false);
253 return;
254 }
255
256 if(paramList.size() >= 2)
257 {
258 m_retryCount = _tcstol(paramList.get(1), NULL, 0);
259 }
260 else
261 m_retryCount = 0;
262
263 TCHAR buffer[1024];
264 _sntprintf(buffer, 1024, _T("Uninstall policy %s"), m_policy->getName());
265 setDescription(buffer);
266 }
267
268 /**
269 * Destructor
270 */
271 PolicyUninstallJob::~PolicyUninstallJob()
272 {
273 if(isValid())
274 {
275 m_node->decRefCount();
276 m_policy->decRefCount();
277 }
278 }
279
280 /**
281 * Run job
282 */
283 ServerJobResult PolicyUninstallJob::run()
284 {
285 ServerJobResult success = JOB_RESULT_FAILED;
286
287 AgentConnectionEx *conn = m_node->createAgentConnection();
288 if (conn != NULL)
289 {
290 UINT32 rcc = conn->uninstallPolicy(m_policy);
291 conn->decRefCount();
292 if (rcc == ERR_SUCCESS)
293 {
294 m_policy->unlinkNode(m_node);
295 success = JOB_RESULT_SUCCESS;
296 }
297 else
298 {
299 setFailureMessage(AgentErrorCodeToText(rcc));
300 }
301 }
302 else
303 {
304 setFailureMessage(_T("Agent connection not available"));
305 }
306
307 if(success == JOB_RESULT_FAILED && m_retryCount-- > 0)
308 {
309 TCHAR description[256];
310 _sntprintf(description, 256, _T("Policy uninstall failed. Wainting %d minutes to restart job."), getNextJobExecutionTime()/60);
311 setDescription(description);
312 success = JOB_RESULT_RESCHEDULE;
313 }
314
315 return success;
316 }
317
318 /**
319 * Serializes job parameters into TCHAR line separated by ';'
320 */
321 const TCHAR *PolicyUninstallJob::serializeParameters()
322 {
323 String params;
324 params.append(m_policy->getId());
325 params.append(_T(','));
326 params.append(m_retryCount);
327 return _tcsdup(params.getBuffer());
328 }
329
330 /**
331 * Schedules execution in 10 minutes
332 */
333 void PolicyUninstallJob::rescheduleExecution()
334 {
335 AddOneTimeScheduledTask(_T("Policy.Uninstall"), time(NULL) + getNextJobExecutionTime(), serializeParameters(), 0, getRemoteNode(), SYSTEM_ACCESS_FULL, SCHEDULED_TASK_SYSTEM);//TODO: change to correct user
336 }