summaryrefslogtreecommitdiffstatsabout
path: root/mod_log_sql.c
diff options
context:
space:
mode:
authorEdward Rudd <urkle@outoforder.cc>2003-12-20 07:16:04 (GMT)
committer Edward Rudd <urkle@outoforder.cc>2003-12-20 07:16:04 (GMT)
commit4f4926ce12f15fc65df8fa1bfbfef1dfce61dbd9 (patch)
tree6172c767068ec41c4060bdd9e8b1dcb96c34cf78 /mod_log_sql.c
parent6cefab259d0b783b85df4518d0d48dab0e11389b (diff)
Initial revision
Diffstat (limited to 'mod_log_sql.c')
-rw-r--r--mod_log_sql.c212
1 files changed, 47 insertions, 165 deletions
diff --git a/mod_log_sql.c b/mod_log_sql.c
index d2c4d1d..82d0483 100644
--- a/mod_log_sql.c
+++ b/mod_log_sql.c
@@ -16,17 +16,7 @@
16/* ---------* 16/* ---------*
17 * INCLUDES * 17 * INCLUDES *
18 * ---------*/ 18 * ---------*/
19#include "config.h" 19#include <time.h>
20#if TIME_WITH_SYS_TIME
21 #include <sys/time.h>
22 #include <time.h>
23#else
24 #if TM_IN_SYS_TIME
25 #include <sys/time.h>
26 #else
27 #include <time.h>
28 #endif
29#endif
30#include <stdio.h> 20#include <stdio.h>
31#include <stdlib.h> 21#include <stdlib.h>
32#include <string.h> 22#include <string.h>
@@ -62,7 +52,6 @@ MYSQL logsql_server, *logsql_server_p = NULL;
62int logsql_massvirtual = 0; 52int logsql_massvirtual = 0;
63int logsql_createtables = 0; 53int logsql_createtables = 0;
64int logsql_forcepreserve = 0; 54int logsql_forcepreserve = 0;
65char *logsql_tabletype = NULL;
66char *logsql_dbname = NULL; 55char *logsql_dbname = NULL;
67char *logsql_dbhost = NULL; 56char *logsql_dbhost = NULL;
68char *logsql_dbuser = NULL; 57char *logsql_dbuser = NULL;
@@ -300,7 +289,7 @@ static const char *extract_request_duration(request_rec *r, char *a)
300{ 289{
301 char duration[22]; /* Long enough for 2^64 */ 290 char duration[22]; /* Long enough for 2^64 */
302 291
303 ap_snprintf(duration, sizeof(duration), "%ld", (long) time(NULL) - r->request_time); 292 ap_snprintf(duration, sizeof(duration), "%ld", time(NULL) - r->request_time);
304 return pstrdup(r->pool, duration); 293 return pstrdup(r->pool, duration);
305} 294}
306 295
@@ -505,7 +494,7 @@ static const char *extract_request_timestamp(request_rec *r, char *a)
505{ 494{
506 char tstr[32]; 495 char tstr[32];
507 496
508 ap_snprintf(tstr, 32, "%ld", (long) time(NULL)); 497 ap_snprintf(tstr, 32, "%ld", time(NULL));
509 return pstrdup(r->pool, tstr); 498 return pstrdup(r->pool, tstr);
510} 499}
511 500
@@ -826,8 +815,6 @@ int safe_create_tables(logsql_state *cls, request_rec *r)
826 char *create_hin = NULL; 815 char *create_hin = NULL;
827 char *create_cookies = NULL; 816 char *create_cookies = NULL;
828 817
829 char *type_suffix = NULL;
830
831 char *createprefix = "create table if not exists `"; 818 char *createprefix = "create table if not exists `";
832 char *access_suffix = 819 char *access_suffix =
833 "` (id char(19),\ 820 "` (id char(19),\
@@ -846,7 +833,7 @@ int safe_create_tables(logsql_state *cls, request_rec *r)
846 request_method varchar(10),\ 833 request_method varchar(10),\
847 request_protocol varchar(10),\ 834 request_protocol varchar(10),\
848 request_time char(28),\ 835 request_time char(28),\
849 request_uri varchar(50),\ 836 request_uri varchar(255),\
850 request_args varchar(255),\ 837 request_args varchar(255),\
851 server_port smallint unsigned,\ 838 server_port smallint unsigned,\
852 ssl_cipher varchar(25),\ 839 ssl_cipher varchar(25),\
@@ -854,7 +841,7 @@ int safe_create_tables(logsql_state *cls, request_rec *r)
854 ssl_maxkeysize smallint unsigned,\ 841 ssl_maxkeysize smallint unsigned,\
855 status smallint unsigned,\ 842 status smallint unsigned,\
856 time_stamp int unsigned,\ 843 time_stamp int unsigned,\
857 virtual_host varchar(50))"; 844 virtual_host varchar(255))";
858 845
859 char *notes_suffix = 846 char *notes_suffix =
860 "` (id char(19),\ 847 "` (id char(19),\
@@ -871,15 +858,12 @@ int safe_create_tables(logsql_state *cls, request_rec *r)
871 item varchar(80),\ 858 item varchar(80),\
872 val varchar(80))"; 859 val varchar(80))";
873 860
874 if (logsql_tabletype)
875 type_suffix = ap_pstrcat( r->pool, " TYPE=", logsql_tabletype, NULL );
876
877 /* Find memory long enough to hold the whole CREATE string + \0 */ 861 /* Find memory long enough to hold the whole CREATE string + \0 */
878 create_access = ap_pstrcat(r->pool, createprefix, cls->transfer_table_name, access_suffix, type_suffix, NULL); 862 create_access = ap_pstrcat(r->pool, createprefix, cls->transfer_table_name, access_suffix, NULL);
879 create_notes = ap_pstrcat(r->pool, createprefix, cls->notes_table_name, notes_suffix, type_suffix, NULL); 863 create_notes = ap_pstrcat(r->pool, createprefix, cls->notes_table_name, notes_suffix, NULL);
880 create_hout = ap_pstrcat(r->pool, createprefix, cls->hout_table_name, headers_suffix, type_suffix, NULL); 864 create_hout = ap_pstrcat(r->pool, createprefix, cls->hout_table_name, headers_suffix, NULL);
881 create_hin = ap_pstrcat(r->pool, createprefix, cls->hin_table_name, headers_suffix, type_suffix, NULL); 865 create_hin = ap_pstrcat(r->pool, createprefix, cls->hin_table_name, headers_suffix, NULL);
882 create_cookies= ap_pstrcat(r->pool, createprefix, cls->cookie_table_name, cookies_suffix, type_suffix, NULL); 866 create_cookies= ap_pstrcat(r->pool, createprefix, cls->cookie_table_name, cookies_suffix, NULL);
883 867
884 #ifdef DEBUG 868 #ifdef DEBUG
885 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_sql: create string: %s", create_access); 869 ap_log_error(APLOG_MARK,DEBUGLEVEL,r->server,"mod_log_sql: create string: %s", create_access);
@@ -903,8 +887,7 @@ int safe_create_tables(logsql_state *cls, request_rec *r)
903 } 887 }
904 888
905 if ((create_results = safe_sql_query(r, create_hin))) { 889 if ((create_results = safe_sql_query(r, create_hin))) {
906 ap_log_error(APLOG_MARK, 890 ap_log_error(APLOG_MARK,ERRLEVEL,r->server,"mod_log_sql: failed to create header_out table");
907 ERRLEVEL,r->server,"mod_log_sql: failed to create header_out table");
908 retval = create_results; 891 retval = create_results;
909 } 892 }
910 893
@@ -953,17 +936,6 @@ const char *set_log_sql_create(cmd_parms *parms, void *dummy, int flag)
953 return NULL; 936 return NULL;
954} 937}
955 938
956const char *set_log_sql_tabletype(cmd_parms *parms, void *dummy, char *arg)
957{
958 /* These are the legal table types according to the MySQL docs:
959 * TYPE = {BDB | HEAP | ISAM | InnoDB | MERGE | MRG_MYISAM | MYISAM }
960 * However, for now the module does no checking. If MySQL is passed
961 * a table type it does not understand, it defaults to MyISAM. */
962
963 logsql_tabletype = arg;
964 return NULL;
965}
966
967const char *set_log_sql_db(cmd_parms *parms, void *dummy, char *arg) 939const char *set_log_sql_db(cmd_parms *parms, void *dummy, char *arg)
968{ 940{
969 logsql_dbname = arg; 941 logsql_dbname = arg;
@@ -1228,12 +1200,12 @@ void *log_sql_make_state(pool *p, server_rec *s)
1228 1200
1229 /* These defaults are overridable in the httpd.conf file. */ 1201 /* These defaults are overridable in the httpd.conf file. */
1230 cls->transfer_table_name = NULL; /* No default b/c we want its absence to disable logging */ 1202 cls->transfer_table_name = NULL; /* No default b/c we want its absence to disable logging */
1231 cls->transfer_log_format = NULL; 1203 cls->transfer_log_format = "AbHhmRSsTUuv";
1232 cls->notes_table_name = NULL; 1204 cls->notes_table_name = "notes";
1233 cls->hin_table_name = NULL; 1205 cls->hin_table_name = "headers_in";
1234 cls->hout_table_name = NULL; 1206 cls->hout_table_name = "headers_out";
1235 cls->cookie_table_name = NULL; 1207 cls->cookie_table_name = "cookies";
1236 cls->preserve_file = NULL; 1208 cls->preserve_file = "/tmp/sql-preserve";
1237 1209
1238 cls->transfer_ignore_list = make_array(p, 1, sizeof(char *)); 1210 cls->transfer_ignore_list = make_array(p, 1, sizeof(char *));
1239 cls->transfer_accept_list = make_array(p, 1, sizeof(char *)); 1211 cls->transfer_accept_list = make_array(p, 1, sizeof(char *));
@@ -1247,106 +1219,6 @@ void *log_sql_make_state(pool *p, server_rec *s)
1247 return (void *) cls; 1219 return (void *) cls;
1248} 1220}
1249 1221
1250static void *log_sql_merge_state(pool *p, void *basev, void *addv)
1251{
1252 /* Make room for the merged state */
1253 logsql_state *merged =
1254 (logsql_state*)ap_pcalloc(p, sizeof(logsql_state));
1255
1256 /* Fetch the two states to merge */
1257 logsql_state *parent = (logsql_state *) basev;
1258 logsql_state *child = (logsql_state *) addv;
1259
1260 /* Child can override these, otherwise they default to parent's choice.
1261 * If the parent didn't set them, create reasonable defaults for the
1262 * ones that should have such default settings. Leave the others null. */
1263
1264 merged->transfer_table_name = child->transfer_table_name ?
1265 child->transfer_table_name : parent->transfer_table_name;
1266 /* No default for transfer_table_name because we want its absence
1267 * to disable logging. */
1268
1269
1270 merged->transfer_log_format = child->transfer_log_format ?
1271 child->transfer_log_format : parent->transfer_log_format;
1272 if (!merged->transfer_log_format)
1273 merged->transfer_log_format = "AbHhmRSsTUuv";
1274
1275
1276 merged->preserve_file = child->preserve_file ?
1277 child->preserve_file : parent->preserve_file;
1278 if (!merged->preserve_file)
1279 merged->preserve_file = "/tmp/sql-preserve";
1280
1281
1282 merged->notes_table_name = child->notes_table_name ?
1283 child->notes_table_name : parent->notes_table_name;
1284 if (!merged->notes_table_name)
1285 merged->notes_table_name = "notes";
1286
1287
1288 merged->hin_table_name = child->hin_table_name ?
1289 child->hin_table_name : parent->hin_table_name;
1290 if (!merged->hin_table_name)
1291 merged->hin_table_name = "headers_in";
1292
1293
1294 merged->hout_table_name = child->hout_table_name ?
1295 child->hout_table_name : parent->hout_table_name;
1296 if (!merged->hout_table_name)
1297 merged->hout_table_name = "headers_out";
1298
1299
1300 merged->cookie_table_name = child->cookie_table_name ?
1301 child->cookie_table_name : parent->cookie_table_name;
1302 if (!merged->cookie_table_name)
1303 merged->cookie_table_name = "cookies";
1304
1305
1306 merged->transfer_ignore_list = child->transfer_ignore_list ?
1307 child->transfer_ignore_list : parent->transfer_ignore_list;
1308
1309 merged->transfer_accept_list = child->transfer_accept_list ?
1310 child->transfer_accept_list : parent->transfer_accept_list;
1311
1312 merged->remhost_ignore_list = child->remhost_ignore_list ?
1313 child->remhost_ignore_list : parent->remhost_ignore_list;
1314
1315 merged->notes_list = child->notes_list ?
1316 child->notes_list : parent->notes_list ;
1317
1318 merged->hin_list = child->hin_list ?
1319 child->hin_list : parent->hin_list ;
1320
1321 merged->hout_list = child->hout_list ?
1322 child->hout_list : parent->hout_list ;
1323
1324 merged->cookie_list = child->cookie_list ?
1325 child->cookie_list : parent->cookie_list ;
1326
1327 merged->cookie_name = child->cookie_name ?
1328 child->cookie_name : parent->cookie_name ;
1329
1330 return (void*) merged;
1331
1332 /* Here is how mod_log_config does it: */
1333
1334 /*
1335 multi_log_state *base = (multi_log_state *) basev;
1336 multi_log_state *add = (multi_log_state *) addv;
1337
1338 add->server_config_logs = base->config_logs;
1339 if (!add->default_format) {
1340 add->default_format_string = base->default_format_string;
1341 add->default_format = base->default_format;
1342 }
1343 add->formats = ap_overlay_tables(p, base->formats, add->formats);
1344
1345 return add;
1346 */
1347}
1348
1349
1350 1222
1351/* Setup of the available httpd.conf configuration commands. 1223/* Setup of the available httpd.conf configuration commands.
1352 * Structure: command, function called, NULL, where available, how many arguments, verbose description 1224 * Structure: command, function called, NULL, where available, how many arguments, verbose description
@@ -1373,7 +1245,7 @@ command_rec log_sql_cmds[] = {
1373 {"LogSQLMachineID", set_log_sql_machine_id, NULL, RSRC_CONF, TAKE1, 1245 {"LogSQLMachineID", set_log_sql_machine_id, NULL, RSRC_CONF, TAKE1,
1374 "Machine ID that the module will log, useful in web clusters to differentiate machines"} 1246 "Machine ID that the module will log, useful in web clusters to differentiate machines"}
1375 , 1247 ,
1376 {"LogSQLRequestAccept", add_log_sql_transfer_accept, NULL, RSRC_CONF, ITERATE, 1248 {"LogSQLRequestAccept", add_log_sql_transfer_accept, NULL, RSRC_CONF, ITERATE,
1377 "List of URIs to accept for logging. Accesses that don't match will not be logged"} 1249 "List of URIs to accept for logging. Accesses that don't match will not be logged"}
1378 , 1250 ,
1379 {"LogSQLRequestIgnore", add_log_sql_transfer_ignore, NULL, RSRC_CONF, ITERATE, 1251 {"LogSQLRequestIgnore", add_log_sql_transfer_ignore, NULL, RSRC_CONF, ITERATE,
@@ -1405,9 +1277,6 @@ command_rec log_sql_cmds[] = {
1405 , 1277 ,
1406 {"LogSQLSocketFile", set_log_sql_socket_file, NULL, RSRC_CONF, TAKE1, 1278 {"LogSQLSocketFile", set_log_sql_socket_file, NULL, RSRC_CONF, TAKE1,
1407 "Name of the file to employ for socket connections to database"} 1279 "Name of the file to employ for socket connections to database"}
1408 ,
1409 {"LogSQLTableType", set_log_sql_tabletype, NULL, RSRC_CONF, TAKE1,
1410 "What kind of table to create (MyISAM, InnoDB...) when creating tables"}
1411 , 1280 ,
1412 {"LogSQLTCPPort", set_log_sql_tcp_port, NULL, RSRC_CONF, TAKE1, 1281 {"LogSQLTCPPort", set_log_sql_tcp_port, NULL, RSRC_CONF, TAKE1,
1413 "Port number to use for TCP connections to database, defaults to 3306 if not set"} 1282 "Port number to use for TCP connections to database, defaults to 3306 if not set"}
@@ -1453,23 +1322,36 @@ int log_sql_transaction(request_rec *orig)
1453 char *i_tablename; 1322 char *i_tablename;
1454 char *o_tablename; 1323 char *o_tablename;
1455 char *c_tablename; 1324 char *c_tablename;
1456 1325 unsigned int i;
1457 /* Determine the hostname and convert it to all-lower-case.
1458 * Also change any dots to underscores.
1459 */
1460 char *p;
1461 char *servname = (char *)ap_get_server_name(orig);
1462 for (p = servname; *p != '\0'; p++) {
1463 *p = tolower((unsigned char) *p);
1464 if (*p == '.') *p = '_';
1465 }
1466 1326
1467 /* Find memory long enough to hold the table name + \0. */ 1327 /* Find memory long enough to hold the table name + \0. */
1468 a_tablename = ap_pstrcat(orig->pool, access_base, servname, NULL); 1328 a_tablename = ap_pstrcat(orig->pool, access_base, ap_get_server_name(orig), NULL);
1469 n_tablename = ap_pstrcat(orig->pool, notes_base, servname, NULL); 1329 n_tablename = ap_pstrcat(orig->pool, notes_base, ap_get_server_name(orig), NULL);
1470 i_tablename = ap_pstrcat(orig->pool, hin_base, servname, NULL); 1330 i_tablename = ap_pstrcat(orig->pool, hin_base, ap_get_server_name(orig), NULL);
1471 o_tablename = ap_pstrcat(orig->pool, hout_base, servname, NULL); 1331 o_tablename = ap_pstrcat(orig->pool, hout_base, ap_get_server_name(orig), NULL);
1472 c_tablename = ap_pstrcat(orig->pool, cookie_base, servname, NULL); 1332 c_tablename = ap_pstrcat(orig->pool, cookie_base, ap_get_server_name(orig), NULL);
1333
1334 /* Transform any dots to underscores */
1335 for (i = 0; i < strlen(a_tablename); i++) {
1336 if (a_tablename[i] == '.')
1337 a_tablename[i] = '_';
1338 }
1339 for (i = 0; i < strlen(n_tablename); i++) {
1340 if (n_tablename[i] == '.')
1341 n_tablename[i] = '_';
1342 }
1343 for (i = 0; i < strlen(i_tablename); i++) {
1344 if (i_tablename[i] == '.')
1345 i_tablename[i] = '_';
1346 }
1347 for (i = 0; i < strlen(o_tablename); i++) {
1348 if (o_tablename[i] == '.')
1349 o_tablename[i] = '_';
1350 }
1351 for (i = 0; i < strlen(c_tablename); i++) {
1352 if (c_tablename[i] == '.')
1353 c_tablename[i] = '_';
1354 }
1473 1355
1474 /* Tell this virtual server its transfer table name, and 1356 /* Tell this virtual server its transfer table name, and
1475 * turn on create_tables, which is implied by massvirtual. 1357 * turn on create_tables, which is implied by massvirtual.
@@ -1785,7 +1667,7 @@ module sql_log_module = {
1785 NULL, /* create per-dir config */ 1667 NULL, /* create per-dir config */
1786 NULL, /* merge per-dir config */ 1668 NULL, /* merge per-dir config */
1787 log_sql_make_state, /* create server config */ 1669 log_sql_make_state, /* create server config */
1788 log_sql_merge_state, /* merge server config */ 1670 NULL, /* merge server config */
1789 log_sql_cmds, /* config directive table */ 1671 log_sql_cmds, /* config directive table */
1790 NULL, /* [9] content handlers */ 1672 NULL, /* [9] content handlers */
1791 NULL, /* [2] URI-to-filename translation */ 1673 NULL, /* [2] URI-to-filename translation */