2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2016 Victor Kirhenshtein
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.
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.
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.
19 ** File: agent_policy.cpp
26 * Redefined status calculation for policy group
28 void PolicyGroup
::calculateCompoundStatus(BOOL bForcedRecalc
)
30 m_status
= STATUS_NORMAL
;
34 * Called by client session handler to check if threshold summary should be shown for this object.
36 bool PolicyGroup
::showThresholdSummary()
42 * Agent policy default constructor
44 AgentPolicy
::AgentPolicy(int type
) : NetObj()
46 m_version
= 0x00010000;
51 * Constructor for user-initiated object creation
53 AgentPolicy
::AgentPolicy(const TCHAR
*name
, int type
) : NetObj()
55 nx_strncpy(m_name
, name
, MAX_OBJECT_NAME
);
56 m_version
= 0x00010000;
61 * Save common policy properties to database
63 bool AgentPolicy
::savePolicyCommonProperties(DB_HANDLE hdb
)
65 if (!saveCommonProperties(hdb
))
69 if (!IsDatabaseRecordExist(hdb
, _T("ap_common"), _T("id"), m_id
))
70 hStmt
= DBPrepare(hdb
, _T("INSERT INTO ap_common (policy_type,version,id) VALUES (?,?,?)"));
72 hStmt
= DBPrepare(hdb
, _T("UPDATE ap_common SET policy_type=?,version=? WHERE id=?"));
76 DBBind(hStmt
, 1, DB_SQLTYPE_INTEGER
, m_policyType
);
77 DBBind(hStmt
, 2, DB_SQLTYPE_INTEGER
, m_version
);
78 DBBind(hStmt
, 3, DB_SQLTYPE_INTEGER
, m_id
);
79 bool success
= DBExecute(hStmt
);
80 DBFreeStatement(hStmt
);
84 success
= saveACLToDB(hdb
);
86 // Update node bindings
88 success
= executeQueryOnObject(hdb
, _T("DELETE FROM ap_bindings WHERE policy_id=?"));
91 if (success
&& (m_childList
->size() > 0))
93 hStmt
= DBPrepare(hdb
, _T("INSERT INTO ap_bindings (policy_id,node_id) VALUES (?,?)"));
96 DBBind(hStmt
, 1, DB_SQLTYPE_INTEGER
, m_id
);
97 for(int i
= 0; (i
< m_childList
->size()) && success
; i
++)
99 DBBind(hStmt
, 2, DB_SQLTYPE_INTEGER
, m_childList
->get(i
)->getId());
100 success
= DBExecute(hStmt
);
102 DBFreeStatement(hStmt
);
116 bool AgentPolicy
::saveToDatabase(DB_HANDLE hdb
)
120 bool success
= savePolicyCommonProperties(hdb
);
122 // Clear modifications flag and unlock object
131 * Delete from database
133 bool AgentPolicy
::deleteFromDatabase(DB_HANDLE hdb
)
135 bool success
= NetObj
::deleteFromDatabase(hdb
);
138 success
= executeQueryOnObject(hdb
, _T("DELETE FROM ap_common WHERE id=?"));
140 success
= executeQueryOnObject(hdb
, _T("DELETE FROM ap_bindings WHERE policy_id=?"));
148 bool AgentPolicy
::loadFromDatabase(DB_HANDLE hdb
, UINT32 dwId
)
152 if (!loadCommonProperties(hdb
))
154 DbgPrintf(2, _T("Cannot load common properties for agent policy object %d"), dwId
);
164 _sntprintf(query
, 256, _T("SELECT version FROM ap_common WHERE id=%d"), dwId
);
165 DB_RESULT hResult
= DBSelect(hdb
, query
);
169 m_version
= DBGetFieldULong(hResult
, 0, 0);
170 DBFreeResult(hResult
);
172 // Load related nodes list
173 _sntprintf(query
, 256, _T("SELECT node_id FROM ap_bindings WHERE policy_id=%d"), m_id
);
174 hResult
= DBSelect(hdb
, query
);
177 int numNodes
= DBGetNumRows(hResult
);
178 for(int i
= 0; i
< numNodes
; i
++)
180 UINT32 nodeId
= DBGetFieldULong(hResult
, i
, 0);
181 NetObj
*object
= FindObjectById(nodeId
);
184 if (object
->getObjectClass() == OBJECT_NODE
)
187 object
->addParent(this);
191 nxlog_write(MSG_AP_BINDING_NOT_NODE
, EVENTLOG_ERROR_TYPE
, "dd", m_id
, nodeId
);
196 nxlog_write(MSG_INVALID_AP_BINDING
, EVENTLOG_ERROR_TYPE
, "dd", m_id
, nodeId
);
199 DBFreeResult(hResult
);
207 * Create NXCP message with policy data
209 void AgentPolicy
::fillMessageInternal(NXCPMessage
*msg
)
211 NetObj
::fillMessageInternal(msg
);
212 msg
->setField(VID_POLICY_TYPE
, (WORD
)m_policyType
);
213 msg
->setField(VID_VERSION
, m_version
);
217 * Modify policy from message
219 UINT32 AgentPolicy
::modifyFromMessageInternal(NXCPMessage
*pRequest
)
221 return NetObj
::modifyFromMessageInternal(pRequest
);
225 * Create deployment message
227 bool AgentPolicy
::createDeploymentMessage(NXCPMessage
*msg
)
229 msg
->setField(VID_POLICY_TYPE
, (WORD
)m_policyType
);
230 msg
->setField(VID_GUID
, m_guid
);
235 * Create uninstall message
237 bool AgentPolicy
::createUninstallMessage(NXCPMessage
*msg
)
239 msg
->setField(VID_POLICY_TYPE
, (WORD
)m_policyType
);
240 msg
->setField(VID_GUID
, m_guid
);
245 * Serialize object to JSON
247 json_t
*AgentPolicy
::toJson()
249 json_t
*root
= NetObj
::toJson();
250 json_object_set_new(root
, "version", json_integer(m_version
));
251 json_object_set_new(root
, "policyType", json_integer(m_policyType
));