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