NLS data sync
[public/netxms.git] / src / server / core / agent.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
df94e0ce 3** Copyright (C) 2003-2012 Victor Kirhenshtein
5039dede
AK
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: agent.cpp
20**
21**/
22
23#include "nxcore.h"
24
d1c1c522
VK
25/**
26 * Destructor for extended agent connection class
27 */
5039dede
AK
28AgentConnectionEx::~AgentConnectionEx()
29{
30}
31
d1c1c522
VK
32/**
33 * Trap processor
34 */
f480bdd4 35void AgentConnectionEx::onTrap(CSCPMessage *pMsg)
5039dede 36{
967893bb 37 UINT32 dwEventCode;
5039dede 38 int i, iNumArgs;
d1c1c522 39 Node *pNode = NULL;
5039dede 40 TCHAR *pszArgList[32], szBuffer[32];
35f836fe 41 char szFormat[] = "ssssssssssssssssssssssssssssssss";
5039dede 42
d1c1c522
VK
43 DbgPrintf(3, _T("AgentConnectionEx::onTrap(): Received trap message from agent at %s, node ID %d"), IpToStr(getIpAddr(), szBuffer), m_nodeId);
44 if (m_nodeId != 0)
45 pNode = (Node *)FindObjectById(m_nodeId, OBJECT_NODE);
46 if (pNode == NULL)
47 pNode = FindNodeByIP(0, getIpAddr());
5039dede
AK
48 if (pNode != NULL)
49 {
fed33789
VK
50 // Check for duplicate traps - only accept traps with ID
51 // higher than last received
52 // agents prior to 1.1.6 will not send trap id
53 // we should accept trap in that case to maintain compatibility
54 bool acceptTrap;
55 QWORD trapId = pMsg->GetVariableInt64(VID_TRAP_ID);
56 if (trapId != 0)
57 {
58 acceptTrap = pNode->checkAgentTrapId(trapId);
59 DbgPrintf(5, _T("AgentConnectionEx::onTrap(): trapID is%s valid"), acceptTrap ? _T("") : _T(" not"));
60 }
61 else
62 {
63 acceptTrap = true;
64 DbgPrintf(5, _T("AgentConnectionEx::onTrap(): trap ID not provided"));
65 }
66
67 if (acceptTrap)
68 {
69 dwEventCode = pMsg->GetVariableLong(VID_EVENT_CODE);
2dd24569
VK
70 if ((dwEventCode == 0) && pMsg->IsVariableExist(VID_EVENT_NAME))
71 {
72 TCHAR eventName[256];
73 pMsg->GetVariableStr(VID_EVENT_NAME, eventName, 256);
74 dwEventCode = EventCodeFromName(eventName, 0);
75 }
fed33789
VK
76 iNumArgs = (int)pMsg->GetVariableShort(VID_NUM_ARGS);
77 if (iNumArgs > 32)
78 iNumArgs = 32;
79 for(i = 0; i < iNumArgs; i++)
80 pszArgList[i] = pMsg->GetVariableStr(VID_EVENT_ARG_BASE + i);
81 DbgPrintf(3, _T("Event from trap: %d"), dwEventCode);
5039dede 82
fed33789
VK
83 // Following call is not very good, but I'm too lazy now
84 // to change PostEvent()
85 szFormat[iNumArgs] = 0;
86 PostEvent(dwEventCode, pNode->Id(), (iNumArgs > 0) ? szFormat : NULL,
87 pszArgList[0], pszArgList[1], pszArgList[2], pszArgList[3],
88 pszArgList[4], pszArgList[5], pszArgList[6], pszArgList[7],
89 pszArgList[8], pszArgList[9], pszArgList[10], pszArgList[11],
90 pszArgList[12], pszArgList[13], pszArgList[14], pszArgList[15],
91 pszArgList[16], pszArgList[17], pszArgList[18], pszArgList[19],
92 pszArgList[20], pszArgList[21], pszArgList[22], pszArgList[23],
93 pszArgList[24], pszArgList[25], pszArgList[26], pszArgList[27],
94 pszArgList[28], pszArgList[29], pszArgList[30], pszArgList[31]);
95
96 // Cleanup
97 for(i = 0; i < iNumArgs; i++)
98 free(pszArgList[i]);
99 }
5039dede
AK
100 }
101 else
102 {
7c521895 103 DbgPrintf(3, _T("Cannot find node for IP address %s"), IpToStr(getIpAddr(), szBuffer));
5039dede
AK
104 }
105}
45d84f8a 106
d1c1c522
VK
107/**
108 * Handler for data push
109 */
f480bdd4
VK
110void AgentConnectionEx::onDataPush(CSCPMessage *msg)
111{
112 TCHAR name[MAX_PARAM_NAME], value[MAX_RESULT_LENGTH];
113
114 msg->GetVariableStr(VID_NAME, name, MAX_PARAM_NAME);
115 msg->GetVariableStr(VID_VALUE, value, MAX_RESULT_LENGTH);
116
89135050 117 Node *node = FindNodeByIP(0, getIpAddr()); /* FIXME: is it possible to receive push data form other zones? */
f480bdd4
VK
118 if (node != NULL)
119 {
70d317d0 120 DbgPrintf(5, _T("%s: agent data push: %s=%s"), node->Name(), name, value);
16d6f798
VK
121 DCObject *dci = node->getDCObjectByName(name);
122 if ((dci != NULL) && (dci->getType() == DCO_TYPE_ITEM) && (dci->getDataSource() == DS_PUSH_AGENT) && (dci->getStatus() == ITEM_STATUS_ACTIVE))
f480bdd4 123 {
70d317d0 124 DbgPrintf(5, _T("%s: agent data push: found DCI %d"), node->Name(), dci->getId());
ced80327 125 time_t t = time(NULL);
df94e0ce 126 node->processNewDCValue(dci, t, value);
ced80327 127 dci->setLastPollTime(t);
f480bdd4 128 }
70d317d0
VK
129 else
130 {
131 DbgPrintf(5, _T("%s: agent data push: DCI not found for %s"), node->Name(), name);
132 }
f480bdd4
VK
133 }
134}
135
d1c1c522
VK
136/**
137 * Deploy policy to agent
138 */
967893bb 139UINT32 AgentConnectionEx::deployPolicy(AgentPolicy *policy)
45d84f8a 140{
967893bb 141 UINT32 rqId, rcc;
45d84f8a
VK
142 CSCPMessage msg(getProtocolVersion());
143
144 rqId = generateRequestId();
145 msg.SetId(rqId);
146 msg.SetCode(CMD_DEPLOY_AGENT_POLICY);
8051849b
VK
147 if (policy->createDeploymentMessage(&msg))
148 {
7c521895 149 if (sendMessage(&msg))
8051849b 150 {
7c521895 151 rcc = waitForRCC(rqId, getCommandTimeout());
8051849b
VK
152 }
153 else
154 {
155 rcc = ERR_CONNECTION_BROKEN;
156 }
157 }
158 else
159 {
160 rcc = ERR_INTERNAL_ERROR;
161 }
45d84f8a
VK
162 return rcc;
163}
93599cfd 164
d1c1c522
VK
165/**
166 * Uninstall policy from agent
167 */
967893bb 168UINT32 AgentConnectionEx::uninstallPolicy(AgentPolicy *policy)
93599cfd 169{
967893bb 170 UINT32 rqId, rcc;
93599cfd
VK
171 CSCPMessage msg(getProtocolVersion());
172
173 rqId = generateRequestId();
174 msg.SetId(rqId);
175 msg.SetCode(CMD_UNINSTALL_AGENT_POLICY);
176 if (policy->createUninstallMessage(&msg))
177 {
7c521895 178 if (sendMessage(&msg))
93599cfd 179 {
7c521895 180 rcc = waitForRCC(rqId, getCommandTimeout());
93599cfd
VK
181 }
182 else
183 {
184 rcc = ERR_CONNECTION_BROKEN;
185 }
186 }
187 else
188 {
189 rcc = ERR_INTERNAL_ERROR;
190 }
191 return rcc;
192}