forked from flrnull/http-logs-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.cpp
executable file
·201 lines (190 loc) · 7.28 KB
/
Parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "Parser.h"
#include "Debug.h"
#include "Config.h"
#include <iostream>
#include "regex_functions.h"
#include "Result.h"
#include <sstream>
#include "common_functions.h"
Parser::Parser(Config *config)
: config(config)
{
if (config->debugMode == 1) {
Debug::print("<Parser constructor>");
}
}
Parser::~Parser() {
if (config->debugMode == 1) {
Debug::print("</Parser destructor>");
}
}
bool Parser::parse(std::string logLine, LogRegexCompiled *logRegExpsCompiled) {
const char * logLineChars = logLine.c_str();
if (config->debugMode == 1) {
Debug::print("Parser::parse: logLine = " + logLine);
}
// For each line
Result::lines++;
// Parse Agent
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found agent");
}
std::string agentRes = matchRegex(&(*logRegExpsCompiled).agent, logLineChars, config);
if (agentRes.length() < 5) {
Result::fails++;
return false;
} else {
// Parse search engines scan
const char * agentChars = agentRes.c_str();
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found " + std::string(GOOGLE_PATTERN) + " in agent" + agentRes);
}
std::string googleRes = matchRegex(&(*logRegExpsCompiled).google, agentChars, config);
if (googleRes.length() > 3) {
Result::google++;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found " + std::string(BING_PATTERN) + " in agent" + agentRes);
}
std::string bingRes = matchRegex(&(*logRegExpsCompiled).bing, agentChars, config);
if (bingRes.length() > 3) {
Result::bing++;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found " + std::string(BAIDU_PATTERN) + " in agent" + agentRes);
}
std::string baiduRes = matchRegex(&(*logRegExpsCompiled).baidu, agentChars, config);
if (baiduRes.length() > 3) {
Result::baidu++;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found " + std::string(YANDEX_PATTERN) + " in agent" + agentRes);
}
std::string yandexRes = matchRegex(&(*logRegExpsCompiled).yandex, agentChars, config);
if (yandexRes.length() > 3) {
Result::yandex++;
}
}
}
}
}
// Parse IP
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found ip");
}
std::string ipRes = matchRegex(&(*logRegExpsCompiled).ip, logLineChars, config);
if (ipRes.length() < 5) {
Result::fails++;
return false;
} else {
// Save to IPs map for calc uniq visitors
std::ostringstream ss;
ss << agentRes.length();
std::string agentResLength = ss.str();
std::string mapKey = ipRes + agentResLength;
std::map<std::string,int>::iterator it;
it = Result::ipAgentMap.find(mapKey);
if (it == Result::ipAgentMap.end()) {
Result::ipAgentMap[mapKey] = 1;
} else {
it->second++;
}
}
// Parse url
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found url");
}
std::string urlRes = matchRegex(&(*logRegExpsCompiled).url, logLineChars, config);
if (urlRes.length() < 1) {
Result::fails++;
return false;
} else {
// Save to URLs map for calc uniq URLs
std::string urlMapKey = urlRes;
std::map<std::string,int>::iterator itUrl;
itUrl = Result::urlMap.find(urlMapKey);
if (itUrl == Result::urlMap.end()) {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + urlMapKey + " is not in our reesult list, added");
}
Result::urlMap[urlMapKey] = 1;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + urlMapKey + " is already in our reesult list, increment");
}
itUrl->second++;
}
// Add URL to top URLs
int urlCount = (itUrl == Result::urlMap.end()) ? 1 : itUrl->second;
Result::topUrlTryToAdd(urlMapKey, urlCount, config);
}
// Parse referer
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found referer");
}
std::string refRes = matchRegex(&(*logRegExpsCompiled).referer, logLineChars, config);
if (refRes.length() < 1) {
Result::fails++;
return false;
} else {
// Save to Refs map for calc uniq Refs
std::string refMapKey = refRes;
std::map<std::string,int>::iterator itRef;
itRef = Result::refsMap.find(refMapKey);
if (itRef == Result::refsMap.end()) {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + refMapKey + " is not in our reesult list, added");
}
Result::refsMap[refMapKey] = 1;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + refMapKey + " is already in our reesult list, increment");
}
itRef->second++;
}
// Add Ref to top Refs
int refCount = (itRef == Result::refsMap.end()) ? 1 : itRef->second;
Result::topRefsTryToAdd(refMapKey, refCount, config);
}
// Parse status code
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found status code");
}
std::string codeRes = matchRegex(&(*logRegExpsCompiled).code, logLineChars, config);
if (codeRes.length() < 1) {
Result::fails++;
return false;
} else {
// Save to Codes map
std::string codeMapKey = codeRes;
std::map<std::string,int>::iterator itCode;
itCode = Result::codesMap.find(codeMapKey);
if (itCode == Result::codesMap.end()) {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + codeMapKey + " is not in our reesult list, added");
}
Result::codesMap[codeMapKey] = 1;
} else {
if (config->debugMode == 1) {
Debug::print("Parser::parse: url " + codeMapKey + " is already in our reesult list, increment");
}
itCode->second++;
}
}
// Parse traffic
if (config->debugMode == 1) {
Debug::print("Parser::parse: trying to found traffic");
}
std::string trafficRes = matchRegex(&(*logRegExpsCompiled).traffic, logLineChars, config);
if (trafficRes.length()) {
if (config->debugMode == 1) {
Debug::print("Parser::parse: trafficRes: " + trafficRes);
}
int traffic = 0;
traffic = StringToNumber(trafficRes, 0);
Result::traffic += traffic;
}
// For each success line
Result::views++;
return true;
}