-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNginxParser.cpp
239 lines (179 loc) · 5.74 KB
/
NginxParser.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "NginxParser.hpp"
NginxParser::NginxParser(string input, string output){
input_file = input;
output_file = output;
}
inline string NginxParser::findIP(char *line){
int number=0;
int point_count = 0;
string result;
for( int i=0 ; line[i] != ' ' && line[i] != '-' && line[i] != '\0' ; i++ ){
if( isdigit(line[i]) )
number = number*10 + (line[i]-'0');
else if( number > 255 )
return "-1";
else if( line[i] == '.' ){
number = 0;
point_count++;
}
else
return "-1";
result += line[i];
}
if( point_count == 3 )
return result;
return "-1";
}
inline int NginxParser::characterCount(char *line, int length, char character){
int result = 0;
for( int i=0 ; i<length ; i++ )
result += (line[i] == character);
return result;
}
inline int NginxParser::findCharacter(char *line, int begin, int length, char character){
for( int i=begin ; i<length ; i++ )
if( line[i] == character )
return i;
return length;
}
void NginxParser::parse(){
ifstream input(input_file);
int length, left, right, begin, end, bitrate, ten, http_status, sent_bytes;
char line[MAX_LINE], type;
string ip, id, user_agent;
bool flag = true, cs_hit, cs_flag;
while( input.getline(line,MAX_LINE,'\n') ){
lines++;
length = strlen(line);
if( (ip = findIP(line)) == "-1" )
continue;
if( characterCount(line,length,'"') != 10 )
continue;
right = 0;
flag = true;
for( int i=0 ; i<5 ; i++ ){
left = findCharacter(line,right,length,'"');
right = findCharacter(line,left+1,length,'"');
if( right-left == 2 ) // Empty parts
continue;
else if( strncmp(line+left+1,"GET",3)==0 ||
strncmp(line+left+1,"POST",4)==0 ||
strncmp(line+left+1,"HEAD",4)==0 ||
strncmp(line+left+1,"PUT",3)==0 ||
strncmp(line+left+1,"DELETE",6)==0 ||
strncmp(line+left+1,"OPTIONS",7)==0 ||
strncmp(line+left+1,"CONNECT",7)==0 ){ // Url part
end = left;
for( int j=0 ; j<2; j++ ){ // Finding ID
begin = findCharacter(line,end+1,right,'/');
end = findCharacter(line,begin+1,right,'/');
}
id = ""; // Saving ID
for( int j=begin+1 ; j<end ; j++ )
id += line[j];
begin = findCharacter(line,end+1,right,'('); // Finding bitrate
if( begin == right ){
flag = false;
break;
}
bitrate = (line[begin+1]-'0')*10+(line[begin+2]-'0');
for( int j=begin+3 ; line[j]!=')' ; j++ ) // Saving bitrate
bitrate = bitrate*10;
begin = findCharacter(line,begin+1,right,'('); // Finding type
if( begin == right ){
flag = false;
break;
}
type = line[begin+1]; // Saving type
}
else if( isdigit(line[left+2]) ){ // HTTP Status and sent bytes
http_status = line[left+2]-'0'; // Saving HTTP Status
sent_bytes = 0;
ten = 1;
for( int j=right-2 ; line[j]!=' ' ; j--,ten*=10 ) // Saving sent bytes
sent_bytes += (line[j]-'0')*ten;
}
else{
user_agent = "";
for( int j=left+1 ; j < right && line[j] != ' ' ; j++ ) // Finding User Agent
user_agent += line[j];
}
}
cs_flag = false;
for( int j=length-1 ; j>right ; j-- ) // Finding cs hit
if( line[j] == 'c' && line[j+1] == 's' && line[j+2] == '=' ){
cs_flag = true;
cs_hit = (line[j+3] == 'H');
break;
}
if( !flag || !cs_flag )
continue;
valid_lines++;
total_data += sent_bytes;
cs_hits += cs_hit;
if( type == 'a' )
audios++;
else
videos++;
http_statuses[http_status-1]++;
/* // This part is for comparing users by amount of their data transfers. If used, type of the second parameter of map should be 'long long int'
* if( users.find(ip) == users.end() )
users[ip] = sent_bytes;
else
users[ip] += sent_bytes;
*/
// This part is for comparing users by amount of their data requests
if( users.find(ip) == users.end() )
users[ip] = 1;
else
users[ip] += 1;
if( contents.find(id) == contents.end() )
contents[id] = 1;
else
contents[id] ++;
if( bitrates.find(bitrate) == bitrates.end() )
bitrates[bitrate] = 1;
else
bitrates[bitrate] ++;
if( user_agents.find(user_agent) == user_agents.end() )
user_agents[user_agent] = 1;
else
user_agents[user_agent] ++;
}
input.close();
}
void NginxParser::statistics(){
int current_max,w=35;
string ip,id;
ofstream output(output_file);
output << setw(w) << left << "Number of unique IPs: " << users.size() << "\n\n";
output << setw(w) << "Number of unique contents: " << contents.size() << "\n\n";
output << setw(w) << "Total data sent: " << (double)total_data/1e9 << "GB\n\n";
output << setw(w) << "Number of video requests: " << videos << "\n\n";
output << setw(w) << "Number of audio requests: " << audios << "\n\n";
output << setw(w) << "Number of cs hits requests: " << cs_hits << "\n\n";
current_max = 0;
for( auto &user: users )
if( user.second > current_max ){
current_max = user.second;
ip = user.first;
}
output << setw(w) << "The IP that watched contents most: " << ip << " (" << current_max << " times)\n\n";
current_max = 0;
for( auto &content: contents )
if( content.second > current_max ){
current_max = content.second;
id = content.first;
}
output << setw(w) << "The content that watched most: " << id << " (" << current_max << " times)\n\n";
output << "Browsers:\n";
for( auto &browser: user_agents )
output << browser.first << ": %" << ((double)browser.second/valid_lines)*100.0 << "\n";
output << "\nHTTP Statuses:\n";
for( int i=0 ; i<5 ; i++ )
output << i+1 << "xx: %" << ((double)http_statuses[i]/valid_lines)*100.0 << "\n";
output << "\nBitrates:\n";
for( auto &bitrate: bitrates )
output << bitrate.first << ": %" << ((double)bitrate.second/valid_lines)*100.0 << "\n";
output.close();
}