fixed issue #26 (Config file with CRLF loaded incorrectly on UNIX)
[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);
71 iNumArgs = (int)pMsg->GetVariableShort(VID_NUM_ARGS);
72 if (iNumArgs > 32)
73 iNumArgs = 32;
74 for(i = 0; i < iNumArgs; i++)
75 pszArgList[i] = pMsg->GetVariableStr(VID_EVENT_ARG_BASE + i);
76 DbgPrintf(3, _T("Event from trap: %d"), dwEventCode);
5039dede 77
fed33789
VK
78 // Following call is not very good, but I'm too lazy now
79 // to change PostEvent()
80 szFormat[iNumArgs] = 0;
81 PostEvent(dwEventCode, pNode->Id(), (iNumArgs > 0) ? szFormat : NULL,
82 pszArgList[0], pszArgList[1], pszArgList[2], pszArgList[3],
83 pszArgList[4], pszArgList[5], pszArgList[6], pszArgList[7],
84 pszArgList[8], pszArgList[9], pszArgList[10], pszArgList[11],
85 pszArgList[12], pszArgList[13], pszArgList[14], pszArgList[15],
86 pszArgList[16], pszArgList[17], pszArgList[18], pszArgList[19],
87 pszArgList[20], pszArgList[21], pszArgList[22], pszArgList[23],
88 pszArgList[24], pszArgList[25], pszArgList[26], pszArgList[27],
89 pszArgList[28], pszArgList[29], pszArgList[30], pszArgList[31]);
90
91 // Cleanup
92 for(i = 0; i < iNumArgs; i++)
93 free(pszArgList[i]);
94 }
5039dede
AK
95 }
96 else
97 {
7c521895 98 DbgPrintf(3, _T("Cannot find node for IP address %s"), IpToStr(getIpAddr(), szBuffer));
5039dede
AK
99 }
100}
45d84f8a
VK
101
102
f480bdd4
VK
103//
104// Handler for data push
105//
106
107void AgentConnectionEx::onDataPush(CSCPMessage *msg)
108{
109 TCHAR name[MAX_PARAM_NAME], value[MAX_RESULT_LENGTH];
110
111 msg->GetVariableStr(VID_NAME, name, MAX_PARAM_NAME);
112 msg->GetVariableStr(VID_VALUE, value, MAX_RESULT_LENGTH);
113
89135050 114 Node *node = FindNodeByIP(0, getIpAddr()); /* FIXME: is it possible to receive push data form other zones? */
f480bdd4
VK
115 if (node != NULL)
116 {
70d317d0 117 DbgPrintf(5, _T("%s: agent data push: %s=%s"), node->Name(), name, value);
16d6f798
VK
118 DCObject *dci = node->getDCObjectByName(name);
119 if ((dci != NULL) && (dci->getType() == DCO_TYPE_ITEM) && (dci->getDataSource() == DS_PUSH_AGENT) && (dci->getStatus() == ITEM_STATUS_ACTIVE))
f480bdd4 120 {
70d317d0 121 DbgPrintf(5, _T("%s: agent data push: found DCI %d"), node->Name(), dci->getId());
ced80327 122 time_t t = time(NULL);
df94e0ce 123 node->processNewDCValue(dci, t, value);
ced80327 124 dci->setLastPollTime(t);
f480bdd4 125 }
70d317d0
VK
126 else
127 {
128 DbgPrintf(5, _T("%s: agent data push: DCI not found for %s"), node->Name(), name);
129 }
f480bdd4
VK
130 }
131}
132
133
45d84f8a
VK
134//
135// Deploy policy to agent
136//
137
138DWORD AgentConnectionEx::deployPolicy(AgentPolicy *policy)
139{
140 DWORD rqId, rcc;
141 CSCPMessage msg(getProtocolVersion());
142
143 rqId = generateRequestId();
144 msg.SetId(rqId);
145 msg.SetCode(CMD_DEPLOY_AGENT_POLICY);
8051849b
VK
146 if (policy->createDeploymentMessage(&msg))
147 {
7c521895 148 if (sendMessage(&msg))
8051849b 149 {
7c521895 150 rcc = waitForRCC(rqId, getCommandTimeout());
8051849b
VK
151 }
152 else
153 {
154 rcc = ERR_CONNECTION_BROKEN;
155 }
156 }
157 else
158 {
159 rcc = ERR_INTERNAL_ERROR;
160 }
45d84f8a
VK
161 return rcc;
162}
93599cfd
VK
163
164
165//
166// Uninstall policy from agent
167//
168
169DWORD AgentConnectionEx::uninstallPolicy(AgentPolicy *policy)
170{
171 DWORD rqId, rcc;
172 CSCPMessage msg(getProtocolVersion());
173
174 rqId = generateRequestId();
175 msg.SetId(rqId);
176 msg.SetCode(CMD_UNINSTALL_AGENT_POLICY);
177 if (policy->createUninstallMessage(&msg))
178 {
7c521895 179 if (sendMessage(&msg))
93599cfd 180 {
7c521895 181 rcc = waitForRCC(rqId, getCommandTimeout());
93599cfd
VK
182 }
183 else
184 {
185 rcc = ERR_CONNECTION_BROKEN;
186 }
187 }
188 else
189 {
190 rcc = ERR_INTERNAL_ERROR;
191 }
192 return rcc;
193}