forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commons.h
270 lines (233 loc) · 6.19 KB
/
commons.h
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef COMMONS_H_INCLUDED
#define COMMONS_H_INCLUDED
#ifdef HAVE_LIBGEOIP
#include <GeoIP.h>
#endif
#include <time.h>
#include <stdint.h>
/* Remove the __attribute__ stuff when the compiler is not GCC. */
#if !__GNUC__
#define __attribute__(x) /**/
#endif
#define GO_UNUSED __attribute__((unused))
#define GO_VERSION "1.1.1"
#define GO_WEBSITE "http://goaccess.io/"
struct tm *now_tm;
/* common char array buffer size */
#define INIT_BUF_SIZE 1024
/* total number of modules */
#ifdef HAVE_LIBGEOIP
#define TOTAL_MODULES 15
#else
#define TOTAL_MODULES 14
#endif
/* maximum number of items within a panel */
#define MAX_CHOICES 366
/* real-time */
#define MAX_CHOICES_RT 50
/* date and time length - e.g., 2016/12/12 12:12:12 -0600 */
#define DATE_TIME 25 + 1
/* date length - e.g., 2016/12/12 */
#define DATE_LEN 10 + 1
/* date length - e.g., 12:12:12 */
#define TIME_LEN 8 + 1
/* hour + ':' + min length - e.g., 12:12 */
#define HRMI_LEN 4 + 1 + 1
#define YR_FMT "%Y"
#define MO_FMT "%M"
#define DT_FMT "%d"
/* maximum protocol string length */
#define REQ_PROTO_LEN 9
/* Type of IP */
typedef enum
{
TYPE_IPINV,
TYPE_IPV4,
TYPE_IPV6
} GTypeIP;
/* Type of Modules */
typedef enum MODULES
{
VISITORS,
REQUESTS,
REQUESTS_STATIC,
NOT_FOUND,
HOSTS,
OS,
BROWSERS,
VISIT_TIMES,
VIRTUAL_HOSTS,
REFERRERS,
REFERRING_SITES,
KEYPHRASES,
STATUS_CODES,
REMOTE_USER,
#ifdef HAVE_LIBGEOIP
GEO_LOCATION,
#endif
} GModule;
/* Metric totals. These are metrics that have a percent value and are
* calculated values. */
typedef struct GPercTotals_
{
int hits; /* total valid hits */
int visitors; /* total visitors */
uint64_t bw; /* total bandwidth */
} GPercTotals;
/* Metrics within GHolder or GDashData */
typedef struct GMetrics
{
/* metric id can be used to identify
* a specific data field */
uint8_t id;
char *data;
char *method;
char *protocol;
float hits_perc;
float visitors_perc;
float bw_perc;
int hits;
int visitors;
/* holder has a numeric value, while
* dashboard has a displayable string value */
union
{
char *sbw;
uint64_t nbw;
} bw;
/* holder has a numeric value, while
* dashboard has a displayable string value */
union
{
char *sts;
uint64_t nts;
} avgts;
/* holder has a numeric value, while
* dashboard has a displayable string value */
union
{
char *sts;
uint64_t nts;
} cumts;
/* holder has a numeric value, while
* dashboard has a displayable string value */
union
{
char *sts;
uint64_t nts;
} maxts;
} GMetrics;
/* Holder sub item */
typedef struct GSubItem_
{
GModule module;
GMetrics *metrics;
struct GSubItem_ *prev;
struct GSubItem_ *next;
} GSubItem;
/* Double linked-list of sub items */
typedef struct GSubList_
{
int size;
struct GSubItem_ *head;
struct GSubItem_ *tail;
} GSubList;
/* Holder item */
typedef struct GHolderItem_
{
GSubList *sub_list;
GMetrics *metrics;
} GHolderItem;
/* Holder of GRawData */
typedef struct GHolder_
{
GHolderItem *items; /* holder items */
GModule module; /* current module */
int idx; /* holder index */
int holder_size; /* number of allocated items */
int ht_size; /* size of the hash table/store */
int sub_items_size; /* number of sub items */
} GHolder;
/* Enum-to-string */
typedef struct GEnum_
{
const char *str;
int idx;
} GEnum;
/* A metric can contain a root/data/uniq node id */
typedef struct GDataMap_
{
int data;
int root;
} GDataMap;
typedef struct GAgentItem_
{
char *agent;
} GAgentItem;
typedef struct GAgents_
{
int size;
struct GAgentItem_ *items;
} GAgents;
#define FOREACH_MODULE(item, array) \
for (; (item < ARRAY_SIZE(array)) && array[item] != -1; ++item)
/* Processing time */
extern time_t end_proc;
extern time_t timestamp;
extern time_t start_proc;
/* list of available modules/panels */
extern int module_list[TOTAL_MODULES];
/* *INDENT-OFF* */
GAgentItem *new_gagent_item (uint32_t size);
GAgents *new_gagents (void);
void free_agents_array (GAgents *agents);
float get_percentage (unsigned long long total, unsigned long long hit);
int get_max_choices (void);
int get_module_enum (const char *str);
int has_timestamp (const char *fmt);
int str2enum (const GEnum map[], int len, const char *str);
int enable_panel (GModule mod);
int get_module_index (int module);
int get_next_module(GModule module);
int get_prev_module(GModule module);
int ignore_panel (GModule mod);
int init_modules (void);
int remove_module(GModule module);
uint32_t get_num_modules(void);
void verify_panels (void);
void display_default_config_file (void);
void display_storage (void);
void display_version (void);
/* *INDENT-ON* */
#endif