Coverage Report

Created: 2026-07-25 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ntopng/include/HTTPstats.h
Line
Count
Source
1
/*
2
 *
3
 * (C) 2014-26 - ntop.org
4
 *
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software Foundation,
18
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
 *
20
 */
21
22
#ifndef _HTTP_STATS_H_
23
#define _HTTP_STATS_H_
24
25
#include "ntop_includes.h"
26
27
class Host;
28
29
struct http_query_stats {
30
  u_int32_t num_get, num_post, num_head, num_put, num_other;
31
};
32
struct http_response_stats {
33
  u_int32_t num_1xx, num_2xx, num_3xx, num_4xx, num_5xx;
34
};
35
36
// to be more efficient, we keep rates as 16 bit integers
37
// rather than float. Indeed, decimal places in request rates
38
// are not so informative to justify the use of floats. For example
39
// if a host is making 198 or 198.1 reqs/sec there is not a big deal of
40
// difference
41
struct http_response_rates {
42
  u_int16_t rate_1xx, rate_2xx, rate_3xx, rate_4xx, rate_5xx;
43
};
44
struct http_query_rates {
45
  u_int16_t rate_get, rate_post, rate_head, rate_put, rate_other;
46
};
47
48
enum { AS_SENDER = 0, AS_RECEIVER };
49
50
class HTTPstats {
51
 private:
52
  struct http_query_stats query[2];
53
  struct http_response_stats response[2];
54
  struct http_query_rates query_rate[2];
55
  struct http_response_rates response_rate[2];
56
  struct http_query_stats last_query_sample[2];
57
  struct http_response_stats last_response_sample[2];
58
  struct timeval last_update_time;
59
60
  HostHash* h;
61
  Host* host;
62
  bool warning_shown;
63
  VirtualHostHash* virtualHosts;
64
65
  void getRequests(const struct http_query_stats* q, u_int32_t* num_get,
66
                   u_int32_t* num_post, u_int32_t* num_head, u_int32_t* num_put,
67
                   u_int32_t* num_other);
68
69
  void getResponses(const struct http_response_stats* r, u_int32_t* num_1xx,
70
                    u_int32_t* num_2xx, u_int32_t* num_3xx, u_int32_t* num_4xx,
71
                    u_int32_t* num_5xx);
72
73
  void getRequestsRates(const struct http_query_rates* dq, u_int16_t* rate_get,
74
                        u_int16_t* rate_post, u_int16_t* rate_head,
75
                        u_int16_t* rate_put, u_int16_t* rate_other);
76
77
  void getResponsesRates(const struct http_response_rates* dr,
78
                         u_int16_t* rate_1xx, u_int16_t* rate_2xx,
79
                         u_int16_t* rate_3xx, u_int16_t* rate_4xx,
80
                         u_int16_t* rate_5xx);
81
82
  void getRequestsDelta(const struct http_query_stats* q0,
83
                        const struct http_query_stats* q1, u_int32_t* delta_get,
84
                        u_int32_t* delta_post, u_int32_t* delta_head,
85
                        u_int32_t* delta_put, u_int32_t* delta_other);
86
87
  void getResponsesDelta(const struct http_response_stats* r0,
88
                         const struct http_response_stats* r1,
89
                         u_int32_t* delta_1xx, u_int32_t* delta_2xx,
90
                         u_int32_t* delta_3xx, u_int32_t* delta_4xx,
91
                         u_int32_t* delta_5xx);
92
93
  void luaAddCounters(lua_State* vm, bool as_sender);
94
  void luaAddRates(lua_State* vm, bool as_sender);
95
  void JSONObjectAddCounters(json_object* j, bool as_sender);
96
  void JSONObjectAddRates(json_object* j, bool as_sender);
97
0
  inline u_int16_t makeRate(u_int16_t v, float tdiff) {
98
0
    return ((u_int16_t)((((float)v * 1000) / tdiff) + .5f));
99
0
  }
100
101
 public:
102
  HTTPstats(Host* host);
103
  ~HTTPstats();
104
105
0
  inline u_int32_t get_num_virtual_hosts() {
106
0
    return (virtualHosts ? virtualHosts->getNumEntries() : 0);
107
0
  }
108
109
  void incStats(bool as_client, const FlowHTTPStats* fts);
110
  json_object* getJSONObject();
111
112
  u_int32_t getSentNumQueries();
113
  u_int32_t getSentNumResponses();
114
  u_int32_t getRcvdNumQueries();
115
  u_int32_t getRcvdNumResponses();
116
117
  void lua(lua_State* vm);
118
  u_int32_t luaVirtualHosts(lua_State* vm, char* virtual_host, Host* h);
119
120
  void updateStats(const struct timeval* tv);
121
  bool updateHTTPHostRequest(time_t t, char* virtual_host_name,
122
                             u_int32_t num_requests, u_int32_t bytes_sent,
123
                             u_int32_t bytes_rcvd);
124
};
125
126
#endif /* _HTTP_STATS_H_ */