Skip to content

Commit

Permalink
Implemented host HTTP traffic reports
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.ntop.org/svn/ntop/trunk/ntopng@8662 a02cd0c1-8e76-42e1-a119-56f9641475e2
  • Loading branch information
deri committed Dec 8, 2014
1 parent 93fb4c1 commit 7565243
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 60 deletions.
8 changes: 4 additions & 4 deletions Flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1391,14 +1391,14 @@ void Flow::dissectHTTP(bool src2dst_direction, char *payload, u_int payload_len)

if(src2dst_direction) {
// payload[10]=0; ntop->getTrace()->traceEvent(TRACE_WARNING, "[len: %u][%s]", payload_len, payload);
h = cli_host->getHTTPStats();
if(h) h->incRequest(payload);
h = cli_host->getHTTPStats(); if(h) h->incRequest(payload); /* Sent */
h = srv_host->getHTTPStats(); if(h) h->incRequest(payload); /* Rcvd */
dissect_next_http_packet = true;
} else {
if(dissect_next_http_packet) {
// payload[10]=0; ntop->getTrace()->traceEvent(TRACE_WARNING, "[len: %u][%s]", payload_len, payload);
h = srv_host->getHTTPStats();
if(h) h->incResponse(payload);
h = cli_host->getHTTPStats(); if(h) h->incResponse(payload); /* Rcvd */
h = srv_host->getHTTPStats(); if(h) h->incResponse(payload); /* Sent */
dissect_next_http_packet = false;
}
}
Expand Down
90 changes: 45 additions & 45 deletions HTTPStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@
/* *************************************** */

HTTPStats::HTTPStats() {
memset(&req, 0, sizeof(req));
memset(&rsp, 0, sizeof(rsp));
memset(&query, 0, sizeof(query));
memset(&response, 0, sizeof(response));
}

/* *************************************** */

void HTTPStats::lua(lua_State *vm) {
lua_newtable(vm);

lua_push_int_table_entry(vm, "req.total", req.num_get+req.num_get+req.num_post+req.num_head+req.num_put);
lua_push_int_table_entry(vm, "req.num_get", req.num_get);
lua_push_int_table_entry(vm, "req.num_post", req.num_post);
lua_push_int_table_entry(vm, "req.num_head", req.num_head);
lua_push_int_table_entry(vm, "req.num_put", req.num_put);
lua_push_int_table_entry(vm, "req.num_other", req.num_other);
lua_push_int_table_entry(vm, "query.total", query.num_get+query.num_get+query.num_post+query.num_head+query.num_put);
lua_push_int_table_entry(vm, "query.num_get", query.num_get);
lua_push_int_table_entry(vm, "query.num_post", query.num_post);
lua_push_int_table_entry(vm, "query.num_head", query.num_head);
lua_push_int_table_entry(vm, "query.num_put", query.num_put);
lua_push_int_table_entry(vm, "query.num_other", query.num_other);

lua_push_int_table_entry(vm, "rsp.total", rsp.num_100x+rsp.num_200x+rsp.num_300x+rsp.num_400x+rsp.num_500x);
lua_push_int_table_entry(vm, "rsp.num_100x", rsp.num_100x);
lua_push_int_table_entry(vm, "rsp.num_200x", rsp.num_200x);
lua_push_int_table_entry(vm, "rsp.num_300x", rsp.num_300x);
lua_push_int_table_entry(vm, "rsp.num_400x", rsp.num_400x);
lua_push_int_table_entry(vm, "rsp.num_500x", rsp.num_500x);
lua_push_int_table_entry(vm, "response.total", response.num_1xx+response.num_2xx+response.num_3xx+response.num_4xx+response.num_5xx);
lua_push_int_table_entry(vm, "response.num_1xx", response.num_1xx);
lua_push_int_table_entry(vm, "response.num_2xx", response.num_2xx);
lua_push_int_table_entry(vm, "response.num_3xx", response.num_3xx);
lua_push_int_table_entry(vm, "response.num_4xx", response.num_4xx);
lua_push_int_table_entry(vm, "response.num_5xx", response.num_5xx);

lua_pushstring(vm, "http");
lua_insert(vm, -2);
Expand All @@ -71,49 +71,49 @@ void HTTPStats::deserialize(json_object *o) {

if(!o) return;

memset(&req, 0, sizeof(req)), memset(&rsp, 0, sizeof(rsp));
memset(&query, 0, sizeof(query)), memset(&response, 0, sizeof(response));

if(json_object_object_get_ex(o, "req.num_get", &obj)) req.num_get = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "req.num_post", &obj)) req.num_post = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "req.num_head", &obj)) req.num_head = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "req.num_put", &obj)) req.num_put = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "req.num_other", &obj)) req.num_other = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "query.num_get", &obj)) query.num_get = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "query.num_post", &obj)) query.num_post = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "query.num_head", &obj)) query.num_head = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "query.num_put", &obj)) query.num_put = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "query.num_other", &obj)) query.num_other = json_object_get_int64(obj);

if(json_object_object_get_ex(o, "rsp.num_100x", &obj)) rsp.num_100x = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "rsp.num_200x", &obj)) rsp.num_200x = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "rsp.num_300x", &obj)) rsp.num_300x = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "rsp.num_400x", &obj)) rsp.num_400x = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "rsp.num_100x", &obj)) rsp.num_100x = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "response.num_1xx", &obj)) response.num_1xx = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "response.num_2xx", &obj)) response.num_2xx = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "response.num_3xx", &obj)) response.num_3xx = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "response.num_4xx", &obj)) response.num_4xx = json_object_get_int64(obj);
if(json_object_object_get_ex(o, "response.num_1xx", &obj)) response.num_1xx = json_object_get_int64(obj);
}

/* ******************************************* */

json_object* HTTPStats::getJSONObject() {
json_object *my_object = json_object_new_object();

if(req.num_get > 0) json_object_object_add(my_object, "req.num_get", json_object_new_int64(req.num_get));
if(req.num_post > 0) json_object_object_add(my_object, "req.num_post", json_object_new_int64(req.num_post));
if(req.num_head > 0) json_object_object_add(my_object, "req.num_head", json_object_new_int64(req.num_head));
if(req.num_put > 0) json_object_object_add(my_object, "req.num_put", json_object_new_int64(req.num_put));
if(req.num_other > 0) json_object_object_add(my_object, "req.num_other", json_object_new_int64(req.num_other));
if(query.num_get > 0) json_object_object_add(my_object, "query.num_get", json_object_new_int64(query.num_get));
if(query.num_post > 0) json_object_object_add(my_object, "query.num_post", json_object_new_int64(query.num_post));
if(query.num_head > 0) json_object_object_add(my_object, "query.num_head", json_object_new_int64(query.num_head));
if(query.num_put > 0) json_object_object_add(my_object, "query.num_put", json_object_new_int64(query.num_put));
if(query.num_other > 0) json_object_object_add(my_object, "query.num_other", json_object_new_int64(query.num_other));

if(rsp.num_100x > 0) json_object_object_add(my_object, "rsp.num_100x", json_object_new_int64(rsp.num_100x));
if(rsp.num_200x > 0) json_object_object_add(my_object, "rsp.num_200x", json_object_new_int64(rsp.num_200x));
if(rsp.num_300x > 0) json_object_object_add(my_object, "rsp.num_300x", json_object_new_int64(rsp.num_300x));
if(rsp.num_400x > 0) json_object_object_add(my_object, "rsp.num_400x", json_object_new_int64(rsp.num_400x));
if(rsp.num_500x > 0) json_object_object_add(my_object, "rsp.num_500x", json_object_new_int64(rsp.num_500x));
if(response.num_1xx > 0) json_object_object_add(my_object, "response.num_1xx", json_object_new_int64(response.num_1xx));
if(response.num_2xx > 0) json_object_object_add(my_object, "response.num_2xx", json_object_new_int64(response.num_2xx));
if(response.num_3xx > 0) json_object_object_add(my_object, "response.num_3xx", json_object_new_int64(response.num_3xx));
if(response.num_4xx > 0) json_object_object_add(my_object, "response.num_4xx", json_object_new_int64(response.num_4xx));
if(response.num_5xx > 0) json_object_object_add(my_object, "response.num_5xx", json_object_new_int64(response.num_5xx));

return(my_object);
}

/* ******************************************* */

void HTTPStats::incRequest(char *method) {
if(method[0] == 'G') req.num_get++;
else if((method[0] == 'P') && (method[0] == 'O')) req.num_post++;
else if(method[0] == 'H') req.num_head++;
else if((method[0] == 'P') && (method[0] == 'U')) req.num_put++;
else req.num_other++;
if(method[0] == 'G') query.num_get++;
else if((method[0] == 'P') && (method[0] == 'O')) query.num_post++;
else if(method[0] == 'H') query.num_head++;
else if((method[0] == 'P') && (method[0] == 'U')) query.num_put++;
else query.num_other++;
}

/* ******************************************* */
Expand All @@ -125,10 +125,10 @@ void HTTPStats::incResponse(char *return_code) {
if(!code) return;

switch(code[1]) {
case '1': rsp.num_100x++; break;
case '2': rsp.num_200x++; break;
case '3': rsp.num_300x++; break;
case '4': rsp.num_400x++; break;
case '5': rsp.num_500x++; break;
case '1': response.num_1xx++; break;
case '2': response.num_2xx++; break;
case '3': response.num_3xx++; break;
case '4': response.num_4xx++; break;
case '5': response.num_5xx++; break;
}
}
12 changes: 6 additions & 6 deletions HTTPStats.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* (C) 2013-14 - ntop.org
* (C) 2014 - ntop.org
*
*
* This program is free software; you can redistribute it and/or modify
Expand All @@ -24,18 +24,18 @@

#include "ntop_includes.h"

struct http_request_stats {
struct http_query_stats {
u_int32_t num_get, num_post, num_head, num_put, num_other;
};

struct http_responses_stats {
u_int32_t num_100x, num_200x, num_300x, num_400x, num_500x;
struct http_response_stats {
u_int32_t num_1xx, num_2xx, num_3xx, num_4xx, num_5xx;
};

class HTTPStats {
private:
struct http_request_stats req;
struct http_responses_stats rsp;
struct http_query_stats query;
struct http_response_stats response;

public:
HTTPStats();
Expand Down
76 changes: 71 additions & 5 deletions scripts/lua/host_details.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ if(page == "http") then
print("<li class=\"active\"><a href=\"#\">HTTP</a></li>\n")
else
if((host["http"] ~= nil)
and ((host["http"]["req.total"]+ host["http"]["rsp.total"]) > 0)) then
and ((host["http"]["query.total"]+ host["http"]["response.total"]) > 0)) then
print("<li><a href=\""..url.."&page=http\">HTTP</a></li>")
end
end
Expand Down Expand Up @@ -888,7 +888,6 @@ print [[/lua/host_dns_breakdown.lua', { ]] print(hostinfo2json(host_info)) print
]]
end


print("<tr><th>Rcvd</th><td class=\"text-right\"><span id=dns_rcvd_num_queries>".. formatValue(host["dns"]["rcvd"]["num_queries"]) .."</span> <span id=trend_rcvd_num_queries></span></td>")
print("<td class=\"text-right\"><span id=dns_rcvd_num_replies_ok>".. formatValue(host["dns"]["rcvd"]["num_replies_ok"]) .."</span> <span id=trend_rcvd_num_replies_ok></span></td>")
print("<td class=\"text-right\"><span id=dns_rcvd_num_replies_error>".. formatValue(host["dns"]["rcvd"]["num_replies_error"]) .."</span> <span id=trend_rcvd_num_replies_error></span></td><td colspan=2>")
Expand All @@ -911,13 +910,51 @@ end

print("</table>\n")
end
elseif(page == "http") then
if(host["http"] ~= nil) then
print("<table class=\"table table-bordered table-striped\">\n")
print("<tr><th rowspan=6 width=20%><A HREF=http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods>HTTP Queries</A></th><th width=20%>Method</th><th width=20%>Requests</th><th>Distribution</th></tr>")
print("<tr><th>GET</th><td style=\"text-align: right;\"><span id=http_query_num_get>".. formatValue(host["http"]["query.num_get"]) .."</span> <span id=trend_http_query_num_get></span></td><td rowspan=5>")

print [[
<div class="pie-chart" id="httpQueries"></div>
<script type='text/javascript'>
do_pie("#httpQueries", ']]
print (ntop.getHttpPrefix())
print [[/lua/host_http_breakdown.lua', { ]] print(hostinfo2json(host_info)) print [[, mode: "queries" }, "", refresh);
</script>
]]

elseif(page == "epp") then

if(host["epp"] ~= nil) then
print("</td></tr>")
print("<tr><th>POST</th><td style=\"text-align: right;\"><span id=http_query_num_post>".. formatValue(host["http"]["query.num_post"]) .."</span> <span id=trend_http_query_num_post></span></td></tr>")
print("<tr><th>HEAD</th><td style=\"text-align: right;\"><span id=http_query_num_head>".. formatValue(host["http"]["query.num_head"]) .."</span> <span id=trend_http_query_num_head></span></td></tr>")
print("<tr><th>PUT</th><td style=\"text-align: right;\"><span id=http_query_num_put>".. formatValue(host["http"]["query.num_put"]) .."</span> <span id=trend_http_query_num_put></span></td></tr>")
print("<tr><th>Other Method</th><td style=\"text-align: right;\"><span id=http_query_num_other>".. formatValue(host["http"]["query.num_other"]) .."</span> <span id=trend_http_query_num_other></span></td></tr>")
print("<tr><th colspan=4>&nbsp;</th></tr>")
print("<tr><th rowspan=6 width=20%><A HREF=http://en.wikipedia.org/wiki/List_of_HTTP_status_codes>HTTP Responses</A></th><th width=20%>Response code</th><th width=20%>Responses</th><th>Distribution</th></tr>")
print("<tr><th>1xx (Informational)</th><td style=\"text-align: right;\"><span id=http_response_num_1xx>".. formatValue(host["http"]["response.num_1xx"]) .."</span> <span id=trend_http_response_num_1xx></span></td><td rowspan=5>")

print [[
<div class="pie-chart" id="httpResponses"></div>
<script type='text/javascript'>
do_pie("#httpResponses", ']]
print (ntop.getHttpPrefix())
print [[/lua/host_http_breakdown.lua', { ]] print(hostinfo2json(host_info)) print [[, mode: "responses" }, "", refresh);
</script>
]]
print("</td></tr>")
print("<tr><th>2xx (Success)</th><td style=\"text-align: right;\"><span id=http_response_num_2xx>".. formatValue(host["http"]["response.num_2xx"]) .."</span> <span id=trend_http_response_num_2xx></span></td></tr>")
print("<tr><th>3xx (Redirection)</th><td style=\"text-align: right;\"><span id=http_response_num_3xx>".. formatValue(host["http"]["response.num_3xx"]) .."</span> <span id=trend_http_response_num_3xx></span></td></tr>")
print("<tr><th>4xx (Client Error)</th><td style=\"text-align: right;\"><span id=http_response_num_4xx>".. formatValue(host["http"]["response.num_4xx"]) .."</span> <span id=trend_http_response_num_4xx></span></td></tr>")
print("<tr><th>5xx (Server Error)</th><td style=\"text-align: right;\"><span id=http_response_num_5xx>".. formatValue(host["http"]["response.num_5xx"]) .."</span> <span id=trend_http_response_num_5xx></span></td></tr>")
print("</table>\n")
end


elseif(page == "epp") then
if(host["epp"] ~= nil) then
print("<table class=\"table table-bordered table-striped\">\n")
print("<tr><th>EPP Breakdown</th><th>Queries</th><th>Positive Replies</th><th>Error Replies</th><th colspan=2>Reply Breakdown</th></tr>")
print("<tr><th>Sent</th><td class=\"text-right\"><span id=epp_sent_num_queries>".. formatValue(host["epp"]["sent"]["num_queries"]) .."</span> <span id=trend_sent_num_queries></span></td>")
Expand Down Expand Up @@ -1991,6 +2028,19 @@ if(host["dns"] ~= nil) then
print("var last_dns_rcvd_num_replies_error = " .. host["dns"]["rcvd"]["num_replies_error"] .. ";\n")
end

if(host["http"] ~= nil) then
print("var last_http_query_num_get = " .. host["http"]["query.num_get"] .. ";\n")
print("var last_http_query_num_post = " .. host["http"]["query.num_post"] .. ";\n")
print("var last_http_query_num_head = " .. host["http"]["query.num_head"] .. ";\n")
print("var last_http_query_num_put = " .. host["http"]["query.num_put"] .. ";\n")
print("var last_http_query_num_other = " .. host["http"]["query.num_other"] .. ";\n")
print("var last_http_response_num_1xx = " .. host["http"]["response.num_1xx"] .. ";\n")
print("var last_http_response_num_2xx = " .. host["http"]["response.num_2xx"] .. ";\n")
print("var last_http_response_num_3xx = " .. host["http"]["response.num_3xx"] .. ";\n")
print("var last_http_response_num_4xx = " .. host["http"]["response.num_4xx"] .. ";\n")
print("var last_http_response_num_5xx = " .. host["http"]["response.num_5xx"] .. ";\n")
end

if(host["epp"] ~= nil) then
print("var last_epp_sent_num_queries = " .. host["epp"]["sent"]["num_queries"] .. ";\n")
print("var last_epp_sent_num_replies_ok = " .. host["epp"]["sent"]["num_replies_ok"] .. ";\n")
Expand Down Expand Up @@ -2083,6 +2133,22 @@ print [[
]]
end

if(host["http"] ~= nil) then
methods = { "get", "post", "head", "put", "other" }
for i, method in ipairs(methods) do
print('\t$("#http_query_num_'..method..'").html(addCommas(host["http"]["query.num_'..method..'"]));\n')
print('\tif(host["http"]["query.num_'..method..'"] == last_http_query_num_'..method..') {\n\t$("#trend_http_query_num_'..method..'").html(\'<i class=\"fa fa-minus\"></i>\');\n')
print('} else {\n\tlast_http_query_num_'..method..' = host["http"]["query.num_'..method..'"];$("#trend_http_query_num_'..method..'").html(\'<i class=\"fa fa-arrow-up\"></i>\'); }\n')
end

retcodes = { "1xx", "2xx", "3xx", "4xx", "5xx" }
for i, retcode in ipairs(retcodes) do
print('\t$("#http_response_num_'..retcode..'").html(addCommas(host["http"]["response.num_'..retcode..'"]));\n')
print('\tif(host["http"]["response.num_'..retcode..'"] == last_http_response_num_'..retcode..') {\n\t$("#trend_http_response_num_'..retcode..'").html(\'<i class=\"fa fa-minus\"></i>\');\n')
print('} else {\n\tlast_http_response_num_'..retcode..' = host["http"]["response.num_'..retcode..'"];$("#trend_http_response_num_'..retcode..'").html(\'<i class=\"fa fa-arrow-up\"></i>\'); }\n')
end
end

if(host["epp"] ~= nil) then
print [[
$('#epp_sent_num_queries').html(addCommas(host["epp"]["sent"]["num_queries"]));
Expand Down
Loading

0 comments on commit 7565243

Please sign in to comment.