-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathdriver_sqlite3.c
299 lines (254 loc) · 8.86 KB
/
driver_sqlite3.c
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
+----------------------------------------------------------------------+
| APM stands for Alternative PHP Monitor |
+----------------------------------------------------------------------+
| Copyright (c) 2008-2014 Davide Mendolia, Patrick Allaert |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Patrick Allaert <[email protected]> |
+----------------------------------------------------------------------+
*/
#include <sqlite3.h>
#include <time.h>
#include "php_apm.h"
#include "php_ini.h"
#include "ext/standard/php_filestat.h"
#include "driver_sqlite3.h"
#ifdef NETWARE
#include <netinet/in.h>
#endif
#if HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
ZEND_EXTERN_MODULE_GLOBALS(apm);
APM_DRIVER_CREATE(sqlite3);
static void disconnect(TSRMLS_D)
{
if (APM_G(sqlite3_event_db) != NULL) {
sqlite3_close(APM_G(sqlite3_event_db));
APM_G(sqlite3_event_db) = NULL;
}
}
static int perform_db_access_checks(const char *path TSRMLS_DC)
{
// php_stat() crashes with ZTS, see later
#ifndef ZTS
zend_bool is_dir;
#if PHP_VERSION_ID >= 70000
zval stat;
php_stat(path, strlen(path), FS_IS_DIR, &stat);
is_dir = (Z_TYPE(stat) == IS_TRUE);
zval_dtor(&stat);
#else
zval *stat;
MAKE_STD_ZVAL(stat);
php_stat(path, strlen(path), FS_IS_DIR, stat TSRMLS_CC);
is_dir = Z_BVAL_P(stat);
zval_dtor(stat);
FREE_ZVAL(stat);
#endif
/* Does db_path exists ? */
if (!is_dir && !php_stream_mkdir((char *)path, 0777, PHP_STREAM_MKDIR_RECURSIVE, NULL)) {
zend_error(E_CORE_WARNING, "APM cannot be enabled, '%s' is not a directory or it cannot be created", path);
return FAILURE;
}
if (VCWD_ACCESS(path, W_OK | R_OK | X_OK) != SUCCESS) {
zend_error(E_CORE_WARNING, "APM cannot be enabled, %s needs to be readable, writable and executable", path);
return FAILURE;
}
#endif
return SUCCESS;
}
PHP_INI_MH(OnUpdateDBFile)
{
#if PHP_VERSION_ID >= 70000
if (APM_G(enabled) && APM_G(sqlite3_enabled)) {
if (new_value && new_value->len > 0) {
snprintf(APM_G(sqlite3_db_file), MAXPATHLEN, "%s/%s", new_value->val, DB_FILE);
disconnect(TSRMLS_C);
if (perform_db_access_checks(new_value->val) == FAILURE) {
APM_G(sqlite3_enabled) = 0;
}
} else {
APM_G(sqlite3_enabled) = 0;
}
}
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
#else
if (APM_G(enabled) && APM_G(sqlite3_enabled)) {
if (new_value && new_value_length > 0) {
snprintf(APM_G(sqlite3_db_file), MAXPATHLEN, "%s/%s", new_value, DB_FILE);
disconnect(TSRMLS_C);
if (perform_db_access_checks(new_value TSRMLS_CC) == FAILURE) {
APM_G(sqlite3_enabled) = 0;
}
} else {
APM_G(sqlite3_enabled) = 0;
}
}
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
#endif
}
/* Returns the SQLite instance (singleton) */
sqlite3 * sqlite_get_instance(TSRMLS_D)
{
int code;
if (APM_G(sqlite3_event_db) == NULL) {
/* Opening the sqlite database file */
APM_DEBUG("[SQLite driver] Connecting to db...");
if (sqlite3_open(APM_G(sqlite3_db_file), &APM_G(sqlite3_event_db))) {
APM_DEBUG("FAILED!\n");
/*
Closing DB file and stop loading the extension
in case of error while opening the database file
*/
disconnect(TSRMLS_C);
return NULL;
}
APM_DEBUG("OK\n");
sqlite3_busy_timeout(APM_G(sqlite3_event_db), APM_G(sqlite3_timeout));
/* Making the connection asynchronous, not waiting for data being really written to the disk */
sqlite3_exec(APM_G(sqlite3_event_db), "PRAGMA synchronous = OFF", NULL, NULL, NULL);
APM_DEBUG("[SQLite driver] Setting up database\n");
if ((code = sqlite3_exec(
APM_G(sqlite3_event_db),
"\n\
CREATE TABLE IF NOT EXISTS request (\n\
id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
application TEXT NOT NULL,\n\
ts INTEGER NOT NULL,\n\
script TEXT NOT NULL,\n\
uri TEXT NOT NULL,\n\
host TEXT NOT NULL,\n\
ip INTEGER UNSIGNED NOT NULL,\n\
cookies TEXT NOT NULL,\n\
post_vars TEXT NOT NULL,\n\
referer TEXT NOT NULL,\n\
method TEXT NOT NULL\n\
);\n\
CREATE TABLE IF NOT EXISTS event (\n\
id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
request_id INTEGER,\n\
ts INTEGER NOT NULL,\n\
type INTEGER NOT NULL,\n\
file TEXT NOT NULL,\n\
line INTEGER NOT NULL,\n\
message TEXT NOT NULL,\n\
backtrace BLOB NOT NULL\n\
);\n\
CREATE INDEX IF NOT EXISTS event_request ON event (request_id);\n\
CREATE TABLE IF NOT EXISTS stats (\n\
id INTEGER PRIMARY KEY AUTOINCREMENT,\n\
request_id INTEGER,\n\
duration FLOAT UNSIGNED NOT NULL,\n\
user_cpu FLOAT UNSIGNED NOT NULL,\n\
sys_cpu FLOAT UNSIGNED NOT NULL,\n\
mem_peak_usage INTEGER UNSIGNED NOT NULL\n\
);\n\
CREATE INDEX IF NOT EXISTS stats_request ON stats (request_id);",
NULL, NULL, NULL)) != SQLITE_OK) {
zend_error(E_CORE_WARNING, "APM's schema cannot be created, error code: %d", code);
}
}
return APM_G(sqlite3_event_db);
}
/* Insert a request in the backend */
static void apm_driver_sqlite3_insert_request(TSRMLS_D)
{
char *sql;
int ip_int = 0, code;
struct in_addr ip_addr;
sqlite3 *connection;
extract_data(TSRMLS_C);
APM_DEBUG("[SQLite driver] Begin insert request\n");
if (APM_G(sqlite3_is_request_created)) {
APM_DEBUG("[SQLite driver] SKIPPED, request already created.\n");
return;
}
SQLITE_INSTANCE_INIT
if (APM_RD(ip_found) && (inet_pton(AF_INET, APM_RD_STRVAL(ip), &ip_addr) == 1)) {
ip_int = ntohl(ip_addr.s_addr);
}
/* Builing SQL insert query */
sql = sqlite3_mprintf(
"INSERT INTO request (application, ts, script, uri, host, ip, cookies, post_vars, referer, method) VALUES (%Q, %d, %Q, %Q, %Q, %d, %Q, %Q, %Q, %Q)",
APM_G(application_id) ? APM_G(application_id) : "",
(long)time(NULL),
APM_RD(script_found) ? APM_RD_STRVAL(script) : "",
APM_RD(uri_found) ? APM_RD_STRVAL(uri) : "",
APM_RD(host_found) ? APM_RD_STRVAL(host) : "",
ip_int,
APM_RD(cookies_found) ? APM_RD_SMART_STRVAL(cookies) : "",
APM_RD(post_vars_found) ? APM_RD_SMART_STRVAL(post_vars) : "",
APM_RD(referer_found) ? APM_RD_STRVAL(referer) : "",
APM_RD(method_found) ? APM_RD_STRVAL(method) : ""
);
/* Executing SQL insert query */
APM_DEBUG("[SQLite driver] Sending: %s\n", sql);
if ((code = sqlite3_exec(connection, sql, NULL, NULL, NULL)) != SQLITE_OK)
APM_DEBUG("[SQLite driver] Error occured with previous query. Error code: %d\n", code);
sqlite3_free(sql);
APM_G(sqlite3_request_id) = sqlite3_last_insert_rowid(connection);
APM_G(sqlite3_is_request_created) = 1;
APM_DEBUG("[SQLite driver] End insert request\n");
}
/* Insert an event in the backend */
void apm_driver_sqlite3_process_event(PROCESS_EVENT_ARGS)
{
char *sql;
sqlite3 *connection;
apm_driver_sqlite3_insert_request(TSRMLS_C);
SQLITE_INSTANCE_INIT
/* Builing SQL insert query */
sql = sqlite3_mprintf(
"INSERT INTO event (request_id, ts, type, file, line, message, backtrace) VALUES (%d, %d, %d, %Q, %d, %Q, %Q)",
APM_G(sqlite3_request_id), (long)time(NULL), type, error_filename ? error_filename : "", error_lineno, msg ? msg : "", trace ? trace : ""
);
/* Executing SQL insert query */
APM_DEBUG("[SQLite driver] Sending: %s\n", sql);
if (sqlite3_exec(connection, sql, NULL, NULL, NULL) != SQLITE_OK)
APM_DEBUG("[SQLite driver] Error occured with previous query\n");
sqlite3_free(sql);
}
int apm_driver_sqlite3_minit(int module_number TSRMLS_DC)
{
return SUCCESS;
}
int apm_driver_sqlite3_rinit(TSRMLS_D)
{
APM_G(sqlite3_is_request_created) = 0;
return SUCCESS;
}
int apm_driver_sqlite3_mshutdown(SHUTDOWN_FUNC_ARGS)
{
return SUCCESS;
}
int apm_driver_sqlite3_rshutdown(TSRMLS_D)
{
disconnect(TSRMLS_C);
return SUCCESS;
}
void apm_driver_sqlite3_process_stats(TSRMLS_D)
{
char *sql;
sqlite3 *connection;
apm_driver_sqlite3_insert_request(TSRMLS_C);
SQLITE_INSTANCE_INIT
/* Building SQL insert query */
sql = sqlite3_mprintf(
"INSERT INTO stats (request_id, duration, user_cpu, sys_cpu, mem_peak_usage) VALUES (%d, %f, %f, %f, %d)",
APM_G(sqlite3_request_id), USEC_TO_SEC(APM_G(duration)), USEC_TO_SEC(APM_G(user_cpu)), USEC_TO_SEC(APM_G(sys_cpu)), APM_G(mem_peak_usage)
);
/* Executing SQL insert query */
APM_DEBUG("[SQLite driver] Sending: %s\n", sql);
if (sqlite3_exec(connection, sql, NULL, NULL, NULL) != SQLITE_OK)
APM_DEBUG("[SQLite driver] Error occured with previous query\n");
sqlite3_free(sql);
}