use your favorite programming/scripting language to write a program to analyze w
ID: 2247671 • Letter: U
Question
use your favorite programming/scripting language to write a program to analyze web server log, and print out the remote client IP, and the number of successful requests (status code 200) made by each remote client. The link below defines the log format for this exercise: http://en.wikipedia.org/wiki/Common_Log_Format For example, if the log file contains the following 5 log entries: 192.168.1.2 - - [17/Sep/2013:22:18:19 -0700] "GET /abc HTTP/1.1" 404 201 192.168.1.2 - - [17/Sep/2013:22:18:19 -0700] "GET /favicon.ico HTTP/1.1" 200 1406 192.168.1.2 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/ HTTP/1.1" 200 5325 192.168.1.2 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/wp-content/themes/twentytwelve/style.css?ver=3.5.1 HTTP/1.1" 200 35292 192.168.1.3 - - [17/Sep/2013:22:18:27 -0700] "GET /wp/wp-content/themes/twentytwelve/js/navigation.js?ver=1.0 HTTP/1.1" 200 863 Your program’s output should print: 192.168.1.2 3 192.168.1.3 1
Explanation / Answer
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
int main(){
ifstream fin;
int found;
int count;
string inp;
vector<string> data;
fin.open("input24.txt"); //Assuming the log is kept in input24.txt
if (fin){
while (fin >> inp){
count = 0;
for(int i = 0; i<inp.size(); i++){
if (inp[i] != '.'){
if (inp[i] > '9' || inp[i] < '0'){
break;
}
}
if (inp[i] == '.')
count++;
}
if (count == 3){
found = 0;
for(int i = 0; i<data.size(); i++){
if (data[i] == inp)
found = 1;
}
if (found == 0)
data.push_back(inp);
}
}
}
else {
cout << "Error in opening file" << endl;
}
cout << "There are " << data.size() << " remote ip entries" << endl;
for (int i = 0; i<data.size(); i++)
cout << data[i] << endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.