d235078ad8eb98af60d02c73f6b178813493cf95
[public/netxms.git] / src / server / core / ap_logparser.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003-2010 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: ap_config.cpp
20 **
21 **/
22
23 #include "nxcore.h"
24
25 /**
26 * Agent policy default constructor
27 */
28 AgentPolicyLogParser::AgentPolicyLogParser()
29 : AgentPolicy(AGENT_POLICY_LOG_PARSER)
30 {
31 m_fileContent = NULL;
32 }
33
34
35 /**
36 * Constructor for user-initiated object creation
37 */
38 AgentPolicyLogParser::AgentPolicyLogParser(const TCHAR *name)
39 : AgentPolicy(name, AGENT_POLICY_LOG_PARSER)
40 {
41 m_fileContent = NULL;
42 }
43
44
45 /**
46 * Destructor
47 */
48 AgentPolicyLogParser::~AgentPolicyLogParser()
49 {
50 safe_free(m_fileContent);
51 }
52
53
54 /**
55 * Save to database
56 */
57 bool AgentPolicyLogParser::saveToDatabase(DB_HANDLE hdb)
58 {
59 lockProperties();
60
61 bool success = savePolicyCommonProperties(hdb);
62 if (success)
63 {
64 String data = DBPrepareString(hdb, m_fileContent);
65 size_t len = data.length() + 256;
66 TCHAR *query = (TCHAR *)malloc(len * sizeof(TCHAR));
67
68 _sntprintf(query, len, _T("SELECT policy_id FROM ap_log_parser WHERE policy_id=%d"), m_id);
69 DB_RESULT hResult = DBSelect(hdb, query);
70 if (hResult != NULL)
71 {
72 bool isNew = (DBGetNumRows(hResult) == 0);
73 DBFreeResult(hResult);
74
75 if (isNew)
76 _sntprintf(query, len, _T("INSERT INTO ap_log_parser (policy_id,file_content) VALUES (%d,%s)"),
77 m_id, (const TCHAR *)data);
78 else
79 _sntprintf(query, len, _T("UPDATE ap_log_parser SET file_content=%s WHERE policy_id=%d"),
80 (const TCHAR *)data, m_id);
81 success = DBQuery(hdb, query);
82 }
83 free(query);
84 }
85
86 // Clear modifications flag and unlock object
87 if (success)
88 m_isModified = false;
89 unlockProperties();
90
91 return success;
92 }
93
94 /**
95 * Delete from database
96 */
97 bool AgentPolicyLogParser::deleteFromDatabase(DB_HANDLE hdb)
98 {
99 bool success = AgentPolicy::deleteFromDatabase(hdb);
100 if (success)
101 {
102 success = executeQueryOnObject(hdb, _T("DELETE FROM ap_log_parser WHERE policy_id=?"));
103 }
104 return success;
105 }
106
107 /**
108 * Load from database
109 */
110 bool AgentPolicyLogParser::loadFromDatabase(DB_HANDLE hdb, UINT32 dwId)
111 {
112 bool success = false;
113
114 if (AgentPolicy::loadFromDatabase(hdb, dwId))
115 {
116 TCHAR query[256];
117
118 _sntprintf(query, 256, _T("SELECT file_content FROM ap_log_parser WHERE policy_id=%d"), dwId);
119 DB_RESULT hResult = DBSelect(hdb, query);
120 if (hResult != NULL)
121 {
122 if (DBGetNumRows(hResult) > 0)
123 {
124 m_fileContent = DBGetField(hResult, 0, 0, NULL, 0);
125 success = true;
126 }
127 DBFreeResult(hResult);
128 }
129 }
130 return success;
131 }
132
133 /**
134 * Create NXCP message with policy data
135 */
136 void AgentPolicyLogParser::fillMessageInternal(NXCPMessage *msg)
137 {
138 AgentPolicy::fillMessageInternal(msg);
139 msg->setField(VID_CONFIG_FILE_DATA, CHECK_NULL_EX(m_fileContent));
140 }
141
142 /**
143 * Modify policy from message
144 */
145 UINT32 AgentPolicyLogParser::modifyFromMessageInternal(NXCPMessage *pRequest)
146 {
147 if (pRequest->isFieldExist(VID_CONFIG_FILE_DATA))
148 {
149 safe_free(m_fileContent);
150 m_fileContent = pRequest->getFieldAsString(VID_CONFIG_FILE_DATA);
151 }
152
153 return AgentPolicy::modifyFromMessageInternal(pRequest);
154 }
155
156 /**
157 * Create deployment message
158 */
159 bool AgentPolicyLogParser::createDeploymentMessage(NXCPMessage *msg)
160 {
161 if (!AgentPolicy::createDeploymentMessage(msg))
162 return false;
163
164 if (m_fileContent == NULL)
165 return false; // Policy cannot be deployed
166
167 #ifdef UNICODE
168 char *fd = MBStringFromWideStringSysLocale(m_fileContent);
169 msg->setField(VID_CONFIG_FILE_DATA, (BYTE *)fd, (UINT32)strlen(fd));
170 free(fd);
171 #else
172 msg->setField(VID_CONFIG_FILE_DATA, (BYTE *)m_fileContent, (UINT32)strlen(m_fileContent));
173 #endif
174 return true;
175 }
176
177
178 /**
179 * Create uninstall message
180 */
181 bool AgentPolicyLogParser::createUninstallMessage(NXCPMessage *msg)
182 {
183 return AgentPolicy::createUninstallMessage(msg);
184 }
185
186 /**
187 * Serialize object to JSON
188 */
189 json_t *AgentPolicyLogParser::toJson()
190 {
191 json_t *root = AgentPolicy::toJson();
192 json_object_set_new(root, "fileContent", json_string_t(m_fileContent));
193 return root;
194 }