nxalarm tool improved
[public/netxms.git] / src / server / core / agent.cpp
CommitLineData
5039dede
AK
1/*
2** NetXMS - Network Management System
3** Copyright (C) 2006 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: 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
7c521895 47 DbgPrintf(3, _T("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 {
51 dwEventCode = pMsg->GetVariableLong(VID_EVENT_CODE);
52 iNumArgs = (int)pMsg->GetVariableShort(VID_NUM_ARGS);
53 if (iNumArgs > 32)
54 iNumArgs = 32;
55 for(i = 0; i < iNumArgs; i++)
56 pszArgList[i] = pMsg->GetVariableStr(VID_EVENT_ARG_BASE + i);
57 DbgPrintf(3, _T("Event from trap: %d"), dwEventCode);
58
59 // Following call is not very good, but I'm too lazy now
60 // to change PostEvent()
61 szFormat[iNumArgs] = 0;
62 PostEvent(dwEventCode, pNode->Id(), (iNumArgs > 0) ? szFormat : NULL,
63 pszArgList[0], pszArgList[1], pszArgList[2], pszArgList[3],
64 pszArgList[4], pszArgList[5], pszArgList[6], pszArgList[7],
65 pszArgList[8], pszArgList[9], pszArgList[10], pszArgList[11],
66 pszArgList[12], pszArgList[13], pszArgList[14], pszArgList[15],
67 pszArgList[16], pszArgList[17], pszArgList[18], pszArgList[19],
68 pszArgList[20], pszArgList[21], pszArgList[22], pszArgList[23],
69 pszArgList[24], pszArgList[25], pszArgList[26], pszArgList[27],
70 pszArgList[28], pszArgList[29], pszArgList[30], pszArgList[31]);
71
72 // Cleanup
73 for(i = 0; i < iNumArgs; i++)
74 free(pszArgList[i]);
75 }
76 else
77 {
7c521895 78 DbgPrintf(3, _T("Cannot find node for IP address %s"), IpToStr(getIpAddr(), szBuffer));
5039dede
AK
79 }
80}
45d84f8a
VK
81
82
f480bdd4
VK
83//
84// Handler for data push
85//
86
87void AgentConnectionEx::onDataPush(CSCPMessage *msg)
88{
89 TCHAR name[MAX_PARAM_NAME], value[MAX_RESULT_LENGTH];
90
91 msg->GetVariableStr(VID_NAME, name, MAX_PARAM_NAME);
92 msg->GetVariableStr(VID_VALUE, value, MAX_RESULT_LENGTH);
93
89135050 94 Node *node = FindNodeByIP(0, getIpAddr()); /* FIXME: is it possible to receive push data form other zones? */
f480bdd4
VK
95 if (node != NULL)
96 {
70d317d0 97 DbgPrintf(5, _T("%s: agent data push: %s=%s"), node->Name(), name, value);
7c521895 98 DCItem *dci = node->getItemByName(name);
ced80327 99 if ((dci != NULL) && (dci->getDataSource() == DS_PUSH_AGENT) && (dci->getStatus() == ITEM_STATUS_ACTIVE))
f480bdd4 100 {
70d317d0 101 DbgPrintf(5, _T("%s: agent data push: found DCI %d"), node->Name(), dci->getId());
ced80327
VK
102 time_t t = time(NULL);
103 node->processNewDciValue(dci, t, value);
104 dci->setLastPollTime(t);
f480bdd4 105 }
70d317d0
VK
106 else
107 {
108 DbgPrintf(5, _T("%s: agent data push: DCI not found for %s"), node->Name(), name);
109 }
f480bdd4
VK
110 }
111}
112
113
45d84f8a
VK
114//
115// Deploy policy to agent
116//
117
118DWORD AgentConnectionEx::deployPolicy(AgentPolicy *policy)
119{
120 DWORD rqId, rcc;
121 CSCPMessage msg(getProtocolVersion());
122
123 rqId = generateRequestId();
124 msg.SetId(rqId);
125 msg.SetCode(CMD_DEPLOY_AGENT_POLICY);
8051849b
VK
126 if (policy->createDeploymentMessage(&msg))
127 {
7c521895 128 if (sendMessage(&msg))
8051849b 129 {
7c521895 130 rcc = waitForRCC(rqId, getCommandTimeout());
8051849b
VK
131 }
132 else
133 {
134 rcc = ERR_CONNECTION_BROKEN;
135 }
136 }
137 else
138 {
139 rcc = ERR_INTERNAL_ERROR;
140 }
45d84f8a
VK
141 return rcc;
142}
93599cfd
VK
143
144
145//
146// Uninstall policy from agent
147//
148
149DWORD AgentConnectionEx::uninstallPolicy(AgentPolicy *policy)
150{
151 DWORD rqId, rcc;
152 CSCPMessage msg(getProtocolVersion());
153
154 rqId = generateRequestId();
155 msg.SetId(rqId);
156 msg.SetCode(CMD_UNINSTALL_AGENT_POLICY);
157 if (policy->createUninstallMessage(&msg))
158 {
7c521895 159 if (sendMessage(&msg))
93599cfd 160 {
7c521895 161 rcc = waitForRCC(rqId, getCommandTimeout());
93599cfd
VK
162 }
163 else
164 {
165 rcc = ERR_CONNECTION_BROKEN;
166 }
167 }
168 else
169 {
170 rcc = ERR_INTERNAL_ERROR;
171 }
172 return rcc;
173}