This commit is contained in:
Mateusz Gruszczyński
2025-11-04 08:54:29 +01:00
parent 0a027bbebd
commit 9949e34d68
2 changed files with 99 additions and 35 deletions

47
app.py
View File

@@ -121,21 +121,51 @@ def display_haproxy_logs():
@app.route('/api/logs', methods=['POST']) @app.route('/api/logs', methods=['POST'])
def api_get_logs(): def api_get_logs():
"""API endpoint for paginated and filtered logs"""
try: try:
log_file_path = '/var/log/haproxy.log' log_file_path = '/var/log/haproxy.log'
if not os.path.exists(log_file_path): if not os.path.exists(log_file_path):
return jsonify({'error': 'Log file not found'}), 404 return jsonify({'error': 'Log file not found', 'success': False}), 404
page = request.json.get('page', 1) page = request.json.get('page', 1)
per_page = request.json.get('per_page', 50) per_page = request.json.get('per_page', 50)
search_query = request.json.get('search', '').lower()
exclude_phrases = request.json.get('exclude', [])
if page < 1:
page = 1
if per_page < 1 or per_page > 500:
per_page = 50
print(f"[API] page={page}, per_page={per_page}, search={search_query}, exclude={len(exclude_phrases)}", flush=True)
# Parse all logs
all_logs = parse_log_file(log_file_path)
total_logs = len(all_logs)
# Reverse to show newest first
all_logs = all_logs[::-1]
# Apply filters
filtered_logs = all_logs
if search_query:
filtered_logs = [log for log in filtered_logs if search_query in
f"{log.get('timestamp', '')} {log.get('ip_address', '')} {log.get('http_method', '')} {log.get('requested_url', '')}".lower()]
if exclude_phrases:
filtered_logs = [log for log in filtered_logs if not any(
phrase in f"{log.get('message', '')}" for phrase in exclude_phrases
)]
total_filtered = len(filtered_logs)
# Paginate
offset = (page - 1) * per_page offset = (page - 1) * per_page
paginated_logs = filtered_logs[offset:offset + per_page]
logs = parse_log_file(log_file_path) print(f"[API] total={total_logs}, filtered={total_filtered}, returned={len(paginated_logs)}", flush=True)
total_logs = len(logs)
reversed_logs = logs[::-1]
paginated_logs = reversed_logs[offset:offset + per_page]
return jsonify({ return jsonify({
'success': True, 'success': True,
@@ -143,12 +173,15 @@ def api_get_logs():
'page': page, 'page': page,
'per_page': per_page, 'per_page': per_page,
'total': total_logs, 'total': total_logs,
'has_more': offset + per_page < total_logs 'total_filtered': total_filtered,
'loaded_count': len(paginated_logs),
'has_more': offset + per_page < total_filtered
}) })
except Exception as e: except Exception as e:
print(f"[API] Error: {e}", flush=True) print(f"[API] Error: {e}", flush=True)
return jsonify({'error': str(e), 'success': False}), 500 return jsonify({'error': str(e), 'success': False}), 500
@app.route('/home') @app.route('/home')
def home(): def home():
frontend_count, backend_count, acl_count, layer7_count, layer4_count = count_frontends_and_backends() frontend_count, backend_count, acl_count, layer7_count, layer4_count = count_frontends_and_backends()

View File

@@ -27,7 +27,7 @@ document.addEventListener('DOMContentLoaded', function() {
// Event Listeners // Event Listeners
searchFilter.addEventListener('keyup', debounce(function() { searchFilter.addEventListener('keyup', debounce(function() {
currentPage = 1; currentPage = 1;
applyFilters(); loadLogsWithPage();
}, 300)); }, 300));
excludeBtn.addEventListener('click', function() { excludeBtn.addEventListener('click', function() {
@@ -36,7 +36,8 @@ document.addEventListener('DOMContentLoaded', function() {
if (!excludePhrases.includes(phrase)) { if (!excludePhrases.includes(phrase)) {
excludePhrases.push(phrase); excludePhrases.push(phrase);
updateExcludeUI(); updateExcludeUI();
applyFilters(); currentPage = 1;
loadLogsWithPage();
} }
excludeFilter.value = ''; excludeFilter.value = '';
} }
@@ -52,7 +53,7 @@ document.addEventListener('DOMContentLoaded', function() {
excludeFilter.value = ''; excludeFilter.value = '';
updateExcludeUI(); updateExcludeUI();
currentPage = 1; currentPage = 1;
applyFilters(); loadLogsWithPage();
}); });
perPageSelect.addEventListener('change', function() { perPageSelect.addEventListener('change', function() {
@@ -105,28 +106,48 @@ document.addEventListener('DOMContentLoaded', function() {
} }
/** /**
* Load logs from API * Load initial logs from API
*/ */
function loadLogs() { function loadLogs() {
logsContainer.innerHTML = '<tr><td class="text-center text-muted py-4">Loading logs...</td></tr>'; logsContainer.innerHTML = '<tr><td class="text-center text-muted py-4">Loading logs...</td></tr>';
loadLogsWithPage();
}
/**
* Load logs with pagination from API
*/
function loadLogsWithPage() {
console.log(`[Logs] Loading page ${currentPage}, per_page ${perPage}`);
fetch('/api/logs', { fetch('/api/logs', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
page: 1, page: currentPage,
per_page: 200 // Load initial 200 per_page: perPage,
search: searchFilter.value.trim(),
exclude: excludePhrases
}) })
}) })
.then(r => r.json()) .then(r => r.json())
.then(data => { .then(data => {
if (data.success) { if (data.success) {
allLoadedLogs = data.logs; allLoadedLogs = data.logs;
loadedSpan.textContent = data.logs.length; loadedSpan.textContent = data.loaded_count;
totalLogs = data.total; totalLogs = data.total;
document.getElementById('total_count').textContent = data.total; document.getElementById('total_count').textContent = data.total;
console.log(`[Logs] Loaded ${data.logs.length}/${data.total}`, flush=true);
applyFilters(); const totalPages = Math.ceil(data.total_filtered / perPage) || 1;
totalPagesSpan.textContent = totalPages;
matchSpan.textContent = data.total_filtered;
currentPageSpan.textContent = data.page;
renderLogs(data.logs);
prevBtn.disabled = currentPage === 1;
nextBtn.disabled = !data.has_more;
console.log(`[Logs] Page ${data.page}/${totalPages}, ${data.logs.length} logs`, flush=true);
} else { } else {
showError(data.error); showError(data.error);
} }
@@ -138,7 +159,7 @@ document.addEventListener('DOMContentLoaded', function() {
} }
/** /**
* Get filtered logs * Get filtered logs (for local filtering)
*/ */
function getFilteredLogs() { function getFilteredLogs() {
let filtered = allLoadedLogs; let filtered = allLoadedLogs;
@@ -164,23 +185,19 @@ document.addEventListener('DOMContentLoaded', function() {
} }
/** /**
* Apply all filters and render * Apply local filters only
*/ */
function applyFilters() { function applyFilters() {
const filtered = getFilteredLogs(); const filtered = getFilteredLogs();
matchSpan.textContent = filtered.length; renderLogs(filtered);
const totalPages = Math.ceil(filtered.length / perPage) || 1; const totalPages = Math.ceil(filtered.length / perPage) || 1;
totalPagesSpan.textContent = totalPages; totalPagesSpan.textContent = totalPages;
currentPageSpan.textContent = currentPage; currentPageSpan.textContent = currentPage;
matchSpan.textContent = filtered.length;
const offset = (currentPage - 1) * perPage;
const paginated = filtered.slice(offset, offset + perPage);
renderLogs(paginated);
prevBtn.disabled = currentPage === 1; prevBtn.disabled = currentPage === 1;
nextBtn.disabled = offset + perPage >= filtered.length; nextBtn.disabled = (currentPage * perPage) >= filtered.length;
} }
/** /**
@@ -188,7 +205,7 @@ document.addEventListener('DOMContentLoaded', function() {
*/ */
function renderLogs(logs) { function renderLogs(logs) {
if (!logs || logs.length === 0) { if (!logs || logs.length === 0) {
logsContainer.innerHTML = '<tr><td class="text-center text-muted py-4">No logs matching criteria</td></tr>'; logsContainer.innerHTML = '<tr><td class="text-center text-muted py-4">No logs found</td></tr>';
return; return;
} }
@@ -197,22 +214,35 @@ document.addEventListener('DOMContentLoaded', function() {
if (entry.xss_alert) threat_badges.push('<span class="badge bg-danger me-1">XSS</span>'); if (entry.xss_alert) threat_badges.push('<span class="badge bg-danger me-1">XSS</span>');
if (entry.sql_alert) threat_badges.push('<span class="badge bg-danger me-1">SQL</span>'); if (entry.sql_alert) threat_badges.push('<span class="badge bg-danger me-1">SQL</span>');
if (entry.webshell_alert) threat_badges.push('<span class="badge bg-danger me-1">SHELL</span>'); if (entry.webshell_alert) threat_badges.push('<span class="badge bg-danger me-1">SHELL</span>');
if (entry.put_method) threat_badges.push('<span class="badge bg-warning me-1">PUT</span>'); if (entry.put_method) threat_badges.push('<span class="badge bg-danger me-1">PUT</span>');
if (entry.illegal_resource) threat_badges.push('<span class="badge bg-warning me-1">403</span>'); if (entry.illegal_resource) threat_badges.push('<span class="badge bg-warning me-1">403</span>');
const threat_html = threat_badges.length > 0 ? `<div class="mb-2">${threat_badges.join('')}</div>` : ''; const threat_html = threat_badges.length > 0 ? `<div class="mb-2">${threat_badges.join('')}</div>` : '';
let row_class = '';
if (entry.has_threat) {
row_class = 'table-danger';
} else if (entry.status_code.startsWith('5')) {
row_class = 'table-danger';
} else if (entry.status_code.startsWith('4')) {
row_class = 'table-warning';
} else if (entry.status_code.startsWith('2')) {
row_class = 'table-light';
} else {
row_class = 'table-light';
}
return ` return `
<tr class="table-${entry.status_class}" style="font-family: monospace; font-size: 11px;"> <tr class="${row_class}" style="font-family: monospace; font-size: 11px;">
<td> <td>
${threat_html} ${threat_html}
<small class="text-info">${escapeHtml(entry.timestamp)}</small><br> <small style="color: #0066cc;">${escapeHtml(entry.timestamp)}</small><br>
<small class="text-warning">${escapeHtml(entry.ip_address)}</small><br> <small style="color: #666;">${escapeHtml(entry.ip_address)}</small>
<strong>${escapeHtml(entry.http_method)}</strong> <strong style="color: #333;">${escapeHtml(entry.http_method)}</strong>
<code>${escapeHtml(entry.requested_url)}</code> <code style="color: #333;">${escapeHtml(entry.requested_url)}</code>
<span class="badge bg-secondary ms-2">${escapeHtml(entry.status_code)}</span> <span class="badge bg-dark" style="color: white; margin-left: 5px;">${escapeHtml(entry.status_code)}</span>
<br> <br>
<small style="color: #aaa;">${escapeHtml(entry.frontend)}~ ${escapeHtml(entry.backend)}</small> <small style="color: #666;">${escapeHtml(entry.frontend)}~ ${escapeHtml(entry.backend)}</small>
</td> </td>
</tr> </tr>
`; `;
@@ -251,7 +281,8 @@ document.addEventListener('DOMContentLoaded', function() {
window.removeExcludePhrase = function(idx) { window.removeExcludePhrase = function(idx) {
excludePhrases.splice(idx, 1); excludePhrases.splice(idx, 1);
updateExcludeUI(); updateExcludeUI();
applyFilters(); currentPage = 1;
loadLogsWithPage();
}; };
/** /**