-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog_parser.cpp
131 lines (110 loc) · 4.24 KB
/
log_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
#include "log_parser.h"
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <regex>
#include <jsoncpp/json/json.h>
std::unordered_map<std::string, std::vector<std::string>> LogParser::ParsePacketLogFile(const std::string& logFilePath, const std::string& date) {
std::unordered_map<std::string, std::vector<std::string>> logData;
std::ifstream logFile(logFilePath);
if (!logFile.is_open()) {
std::cerr << "Could not open log file: " << logFilePath << std::endl;
return logData;
}
std::string line;
bool capture = false;
while (std::getline(logFile, line)) {
// 날짜가 포함된 라인을 찾으면 capture 시작
if (line.find("[" + date) != std::string::npos) {
capture = true;
}
// capture 상태에서는 로그를 기록
if (capture) {
logData[date].push_back(line);
}
// 다음 날짜 라인이 나오면 capture 종료
if (capture && line.find("[2024-") != std::string::npos && line.find("[" + date) == std::string::npos) {
capture = false;
}
}
logFile.close();
return logData;
}
std::vector<std::unordered_map<std::string, std::string>> LogParser::ParseJsonLogFile(const std::string& logFilePath) {
std::vector<std::unordered_map<std::string, std::string>> logEntries;
std::ifstream logFile(logFilePath, std::ifstream::binary);
if (!logFile.is_open()) {
std::cerr << "Could not open log file: " << logFilePath << std::endl;
return logEntries;
}
Json::Value root;
Json::CharReaderBuilder builder;
std::string errs;
if (!Json::parseFromStream(builder, logFile, &root, &errs)) {
std::cerr << "Failed to parse JSON: " << errs << std::endl;
return logEntries;
}
if (root.isArray()) {
for (const auto& entry : root) {
std::unordered_map<std::string, std::string> logData;
logData["event_type"] = entry["event_type"].asString();
logData["file_size"] = std::to_string(entry["file_size"].asInt());
logData["new_hash"] = entry["new_hash"].asString();
logData["old_hash"] = entry["old_hash"].asString();
logData["pid"] = std::to_string(entry["pid"].asInt());
logData["target_file"] = entry["target_file"].asString();
logData["timestamp"] = entry["timestamp"].asString();
logData["user"] = entry["user"].asString();
logEntries.push_back(logData);
}
}
logFile.close();
return logEntries;
}
std::unordered_map<std::string, std::string> LogParser::ParseFirewallLog(const std::string& logFilePath) {
std::unordered_map<std::string, std::string> logData;
std::ifstream logFile(logFilePath);
if (!logFile.is_open()) {
std::cerr << "Could not open log file: " << logFilePath << std::endl;
return logData;
}
std::string line;
int totalEvents = 0;
int allowedTraffic = 0;
int blockedTraffic = 0;
std::string date;
std::stringstream logEntries;
while (std::getline(logFile, line)) {
std::istringstream iss(line);
std::string month, day, time, hostname, kernel, timestamp, action;
std::string restOfLine;
iss >> month >> day >> time >> hostname >> kernel;
std::getline(iss, timestamp, ']');
timestamp += "]";
iss >> action;
std::getline(iss, restOfLine);
if (date.empty()) {
date = month + " " + day;
}
logEntries << "<tr>"
<< "<td>" << time << "</td>"
<< "<td>" << hostname << "</td>"
<< "<td>" << action << "</td>"
<< "<td>" << restOfLine << "</td>"
<< "</tr>";
totalEvents++;
if (action == "ALLOW") {
allowedTraffic++;
} else if (action == "BLOCK") {
blockedTraffic++;
}
}
logFile.close();
logData["날짜"] = date;
logData["총 이벤트 수"] = std::to_string(totalEvents);
logData["허용된 트래픽"] = std::to_string(allowedTraffic);
logData["차단된 트래픽"] = std::to_string(blockedTraffic);
logData["entries"] = logEntries.str();
return logData;
}