89f58151201771320005ba3cfd00ff1817bca480
[public/netxms.git] / src / agent / subagents / logscan / logscan.cpp
1 /* $Id: logscan.cpp,v 1.6 2007-06-08 00:02:36 alk Exp $ */
2 /*
3 ** NetXMS LogScan subagent
4 ** Copyright (C) 2006 Alex Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: logscan.cpp
21 **
22 **/
23
24 #include "logscan.h"
25
26 using namespace std;
27
28
29 //
30 // Typedefs
31 //
32 typedef map<string, long> tLocations;
33
34 //
35 // static vars
36 //
37
38 static tLocations m_locations;
39 static MUTEX m_locationsMutex;
40
41 //
42 // Forward declarations
43 //
44
45 LONG H_GetString(char *, char *, char *);
46
47
48 //
49 // initialization callback
50 //
51
52 static BOOL SubAgentInit(TCHAR *pszConfigFile)
53 {
54 m_locationsMutex = MutexCreate();
55 return TRUE;
56 }
57
58
59 //
60 // Cleanup callback
61 //
62
63 static void SubAgentShutdown(void)
64 {
65 MutexDestroy(m_locationsMutex);
66 }
67
68
69 //
70 // Subagent information
71 //
72
73 static NETXMS_SUBAGENT_PARAM m_parameters[] =
74 {
75 { "LogScan.FindString(*)", H_GetString, NULL, DCI_DT_STRING, ""},
76 };
77
78 static NETXMS_SUBAGENT_INFO m_info =
79 {
80 NETXMS_SUBAGENT_INFO_MAGIC,
81 "LOGSCAN", NETXMS_VERSION_STRING,
82 SubAgentInit, SubAgentShutdown, NULL, // Handlers
83 sizeof(m_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),
84 m_parameters,
85 0, //sizeof(m_enums) / sizeof(NETXMS_SUBAGENT_ENUM),
86 NULL, //m_enums,
87 0, NULL // actions
88 };
89
90 //
91 // Entry point for NetXMS agent
92 //
93
94 DECLARE_SUBAGENT_ENTRY_POINT(LOGSCAN)
95 {
96 *ppInfo = &m_info;
97 return TRUE;
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////
101 //
102 // main code
103 //
104
105 LONG H_GetString(char *pszParam, char *pArg, char *pValue)
106 {
107 int ret = SYSINFO_RC_ERROR;
108 char fileName[1024];
109 char subString[1024];
110
111 NxGetParameterArg(pszParam, 1, fileName, sizeof(fileName));
112 NxGetParameterArg(pszParam, 2, subString, sizeof(subString));
113
114 if (fileName[0] != 0 && subString[0] != 0)
115 {
116 char tmp[10240];
117 struct stat s;
118
119 if (stat(fileName, &s) == 0)
120 {
121 long location = 0;
122 MutexLock(m_locationsMutex, INFINITE);
123 tLocations::iterator it = m_locations.find(string(fileName) + string(":::") + subString);
124 if (it != m_locations.end())
125 {
126 // TODO: add more strict rotation checking
127 if (it->second <= s.st_size) // last location is beyond end of file
128 {
129 location = it->second;
130 }
131 }
132 MutexUnlock(m_locationsMutex);
133
134 FILE *f = fopen(fileName, "r");
135 if (f != NULL)
136 {
137 fseek(f, location, SEEK_SET);
138
139 tmp[0] = 0;
140 while (fgets(tmp,sizeof(tmp), f) != NULL)
141 {
142 for (int i = 0; i < strlen(tmp); i++)
143 {
144 if (tmp[i] == 0x0D || tmp[i] == 0x0A)
145 {
146 tmp[i] = 0;
147 break;
148 }
149 }
150 // TODO: change to strcasestr?
151 if (strstr(tmp, subString) != NULL)
152 {
153 ret_string(pValue, tmp);
154 ret = SYSINFO_RC_SUCCESS;
155 break;
156 }
157 tmp[0] = 0;
158 }
159
160 if (ret != SYSINFO_RC_SUCCESS)
161 {
162 ret = SYSINFO_RC_SUCCESS;
163 ret_string(pValue, "");
164 }
165
166 MutexLock(m_locationsMutex, INFINITE);
167 //m_locations[string(fileName) + string(":::") + subString] = ftell(f);
168 m_locations[string(fileName) + string(":::") + subString] = s.st_size;
169 MutexUnlock(m_locationsMutex);
170
171 fclose(f);
172 }
173 }
174 }
175
176 return ret;
177 }
178
179
180 ///////////////////////////////////////////////////////////////////////////////
181 /*
182
183 $Log: not supported by cvs2svn $
184 Revision 1.5 2007/04/24 18:22:11 victor
185 Subagent API changed
186
187 Revision 1.4 2006/03/09 15:18:49 alk
188 lineends are removed before sending responce
189
190 Revision 1.3 2006/03/09 12:25:26 victor
191 Windows port
192
193 Revision 1.2 2006/03/09 11:44:42 alk
194 *** empty log message ***
195
196 Revision 1.1 2006/03/09 11:14:55 alk
197 simple log scanner added
198
199
200 */