-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockingHistory.php
More file actions
277 lines (248 loc) · 10.7 KB
/
blockingHistory.php
File metadata and controls
277 lines (248 loc) · 10.7 KB
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
<?php
/*
*
* aql - Active Query Listing
*
* Copyright (C) 2018 Kevin Benton - kbcmdba [at] gmail [dot] com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
namespace com\kbcmdba\aql ;
session_start() ;
require( 'vendor/autoload.php' ) ;
require( 'utility.php' ) ;
use com\kbcmdba\aql\Libs\Config ;
use com\kbcmdba\aql\Libs\DBConnection ;
use com\kbcmdba\aql\Libs\Exceptions\DaoException;
use com\kbcmdba\aql\Libs\Tools ;
use com\kbcmdba\aql\Libs\WebPage ;
// ///////////////////////////////////////////////////////////////////////////
$page = new WebPage( 'Blocking History' ) ;
$page->setTop( "<h2>AQL: Blocking History</h2>\n"
. "<p>This page shows queries that have been observed blocking other queries. "
. "Query text is normalized (strings/numbers replaced) to avoid storing sensitive data.</p>\n"
) ;
$body = '' ;
try {
$dbc = new DBConnection() ;
$dbh = $dbc->getConnection() ;
$dbh->set_charset( 'utf8' ) ;
// Get filter parameters
$filterHostId = Tools::param( 'hostId' ) ;
$filterUser = Tools::param( 'user' ) ;
$filterQuery = Tools::param( 'query' ) ;
$filterDateFrom = Tools::param( 'dateFrom' ) ;
$filterDateTo = Tools::param( 'dateTo' ) ;
// Build host dropdown
$hostOptions = "<option value=\"\">-- All Hosts --</option>\n" ;
$hostQuery = "SELECT host_id, hostname, port_number FROM aql_db.host ORDER BY hostname, port_number" ;
$hostResult = $dbh->query( $hostQuery ) ;
if ( $hostResult !== false ) {
while ( $row = $hostResult->fetch_assoc() ) {
$selected = ( $filterHostId == $row['host_id'] ) ? ' selected' : '' ;
$hostOptions .= "<option value=\"" . intval( $row['host_id'] ) . "\"$selected>"
. htmlspecialchars( $row['hostname'] . ':' . $row['port_number'] )
. "</option>\n" ;
}
$hostResult->close() ;
}
// Build user list for fuzzy autocomplete
$userList = [] ;
$userQuery = "SELECT DISTINCT user FROM aql_db.blocking_history ORDER BY user" ;
$userResult = $dbh->query( $userQuery ) ;
if ( $userResult !== false ) {
while ( $row = $userResult->fetch_assoc() ) {
$userList[] = $row['user'] ;
}
$userResult->close() ;
}
// Build query list for fuzzy autocomplete (top 100 most frequent, truncated)
$queryList = [] ;
$queryListQuery = "SELECT DISTINCT LEFT(query_text, 80) AS query_preview
FROM aql_db.blocking_history
ORDER BY blocked_count DESC
LIMIT 100" ;
$queryListResult = $dbh->query( $queryListQuery ) ;
if ( $queryListResult !== false ) {
while ( $row = $queryListResult->fetch_assoc() ) {
$queryList[] = $row['query_preview'] ;
}
$queryListResult->close() ;
}
// Get total count of entries
$totalCount = 0 ;
$countResult = $dbh->query( "SELECT COUNT(*) AS cnt FROM aql_db.blocking_history" ) ;
if ( $countResult !== false ) {
$countRow = $countResult->fetch_assoc() ;
$totalCount = intval( $countRow['cnt'] ) ;
$countResult->close() ;
}
// Check if any filters are applied
$hasFilters = !empty( $filterHostId ) || !empty( $filterUser ) || !empty( $filterQuery )
|| !empty( $filterDateFrom ) || !empty( $filterDateTo ) ;
// Show total count
$body .= "<p><strong>Total blocking history entries:</strong> " . number_format( $totalCount ) . "</p>\n" ;
// Filter form
$body .= "<form method=\"get\" action=\"blockingHistory.php\">\n"
. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n"
. "<tr>\n"
. " <th>Host</th>\n"
. " <td><select name=\"hostId\">$hostOptions</select></td>\n"
. " <th>User</th>\n"
. " <td><input type=\"text\" name=\"user\" id=\"userFilter\" value=\"" . htmlspecialchars( $filterUser ?? '' ) . "\" size=\"20\" placeholder=\"Fuzzy search user\" autocomplete=\"off\"></td>\n"
. " <th>Query Pattern</th>\n"
. " <td><input type=\"text\" name=\"query\" id=\"queryFilter\" value=\"" . htmlspecialchars( $filterQuery ?? '' ) . "\" size=\"40\" placeholder=\"Fuzzy search query\" autocomplete=\"off\"></td>\n"
. "</tr>\n"
. "<tr>\n"
. " <th>From Date</th>\n"
. " <td><input type=\"date\" name=\"dateFrom\" value=\"" . htmlspecialchars( $filterDateFrom ?? '' ) . "\"></td>\n"
. " <th>To Date</th>\n"
. " <td><input type=\"date\" name=\"dateTo\" value=\"" . htmlspecialchars( $filterDateTo ?? '' ) . "\"></td>\n"
. " <td colspan=\"2\"><input type=\"submit\" value=\"Apply Filters\"> <a href=\"blockingHistory.php\">Clear Filters</a></td>\n"
. "</tr>\n"
. "</table>\n"
. "</form>\n"
. "<script>\n"
. "var userOptions = " . json_encode( $userList ) . ";\n"
. "var queryOptions = " . json_encode( $queryList ) . ";\n"
. "document.addEventListener('DOMContentLoaded', function() {\n"
. " initFuzzyAutocomplete('userFilter', userOptions, 10);\n"
. " initFuzzyAutocomplete('queryFilter', queryOptions, 10);\n"
. "});\n"
. "</script>\n"
. "<p />\n" ;
// Build the results query with filters
$sql = "SELECT h.hostname
, h.port_number
, bh.query_hash
, bh.user
, bh.source_host
, bh.db_name
, bh.blocked_count
, bh.total_blocked
, bh.max_block_secs
, bh.query_text
, bh.first_seen
, bh.last_seen
FROM aql_db.blocking_history bh
JOIN aql_db.host h ON h.host_id = bh.host_id
WHERE 1=1" ;
$params = [] ;
$types = '' ;
if ( !empty( $filterHostId ) && is_numeric( $filterHostId ) ) {
$sql .= " AND bh.host_id = ?" ;
$params[] = (int) $filterHostId ;
$types .= 'i' ;
}
if ( !empty( $filterUser ) ) {
$sql .= " AND bh.user LIKE ?" ;
$params[] = '%' . $filterUser . '%' ;
$types .= 's' ;
}
if ( !empty( $filterQuery ) ) {
$sql .= " AND bh.query_text LIKE ?" ;
$params[] = '%' . $filterQuery . '%' ;
$types .= 's' ;
}
if ( !empty( $filterDateFrom ) ) {
$sql .= " AND bh.last_seen >= ?" ;
$params[] = $filterDateFrom . ' 00:00:00' ;
$types .= 's' ;
}
if ( !empty( $filterDateTo ) ) {
$sql .= " AND bh.last_seen <= ?" ;
$params[] = $filterDateTo . ' 23:59:59' ;
$types .= 's' ;
}
// Default: most recent first. With filters: by blocked_count then last_seen
if ( $hasFilters ) {
$sql .= " ORDER BY bh.blocked_count DESC, bh.last_seen DESC" ;
} else {
$sql .= " ORDER BY bh.last_seen DESC" ;
}
$sql .= " LIMIT 100" ;
$stmt = $dbh->prepare( $sql ) ;
if ( !empty( $params ) ) {
$stmt->bind_param( $types, ...$params ) ;
}
$stmt->execute() ;
$result = $stmt->get_result() ;
// Results table
$body .= "<table id=\"blockingHistoryTable\" class=\"tablesorter aql-listing\" border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n"
. "<thead>\n"
. "<tr>\n"
. " <th>Host</th>\n"
. " <th>Query Hash</th>\n"
. " <th>User</th>\n"
. " <th>Source Host</th>\n"
. " <th>Database</th>\n"
. " <th>Times Seen</th>\n"
. " <th>Total Seen Blocked</th>\n"
. " <th>Max Block Secs Seen</th>\n"
. " <th>Query</th>\n"
. " <th>First Seen</th>\n"
. " <th>Last Seen</th>\n"
. "</tr>\n"
. "</thead>\n"
. "<tbody>\n" ;
$rowCount = 0 ;
while ( $row = $result->fetch_assoc() ) {
$rowCount++ ;
$host = htmlspecialchars( $row['hostname'] . ':' . $row['port_number'] ) ;
$queryHash = htmlspecialchars( $row['query_hash'] ) ;
$user = htmlspecialchars( $row['user'] ) ;
$sourceHost = htmlspecialchars( $row['source_host'] ) ;
$dbName = htmlspecialchars( $row['db_name'] ?? '' ) ;
$blockedCount = intval( $row['blocked_count'] ) ;
$totalBlocked = intval( $row['total_blocked'] ) ;
$maxBlockSecs = intval( $row['max_block_secs'] ?? 0 ) ;
// Collapse multiple blank lines into single newline for compact display
$queryText = preg_replace( '/\n\s*\n/', "\n", $row['query_text'] ) ;
$queryText = htmlspecialchars( $queryText ) ;
$firstSeen = htmlspecialchars( $row['first_seen'] ) ;
$lastSeen = htmlspecialchars( $row['last_seen'] ) ;
$body .= "<tr>\n"
. " <td>$host</td>\n"
. " <td><span class=\"query-hash\">$queryHash</span></td>\n"
. " <td>$user</td>\n"
. " <td>$sourceHost</td>\n"
. " <td>$dbName</td>\n"
. " <td class=\"text-right\">$blockedCount</td>\n"
. " <td class=\"text-right\">$totalBlocked</td>\n"
. " <td class=\"text-right\">$maxBlockSecs</td>\n"
. " <td><pre class=\"query-pre\">$queryText</pre></td>\n"
. " <td>$firstSeen</td>\n"
. " <td>$lastSeen</td>\n"
. "</tr>\n" ;
}
$body .= "</tbody>\n"
. "</table>\n" ;
if ( $rowCount === 0 ) {
if ( $hasFilters ) {
$body .= "<p><em>No blocking history found matching the current filters.</em></p>\n" ;
} else {
$body .= "<p><em>No blocking history recorded yet.</em></p>\n" ;
}
} else {
$showing = $hasFilters ? "Showing $rowCount matching record" : "Showing $rowCount most recent record" ;
$body .= "<p>" . $showing . ( $rowCount !== 1 ? 's' : '' ) . " (limit 100)</p>\n" ;
}
$stmt->close() ;
} catch ( \Exception $e ) {
$body .= "<p class=\"errorNotice\">Error: " . htmlspecialchars( $e->getMessage() ) . "</p>\n" ;
}
$page->setBody( $body ) ;
$page->displayPage() ;