Sending System Logs to a Web Interface Using PHP and Shell Scripts

Sending System Logs to a Web Interface Using PHP and Shell Scripts

Learn how to send system logs to a web interface using PHP and shell scripts. Monitor logs in real-time, filter data, and create a log viewer dashboard.

Introduction

Monitoring system logs is essential for troubleshooting, security auditing, and performance optimization. Instead of manually checking logs from the command line, PHP and shell scripts can be used to display logs dynamically on a web interface.

With PHP and shell scripting, you can:

  • Retrieve system logs dynamically from the server
  • Filter logs based on keywords or severity
  • Create a real-time log viewer dashboard
  • Monitor server activity from a web-based interface

This guide covers:

  • Reading system logs using PHP and shell commands
  • Displaying logs dynamically on a web page
  • Filtering logs based on search criteria
  • Setting up real-time log monitoring

1. Accessing System Logs Using Shell Commands

Listing Available Logs in Linux

ls /var/log/

Common system logs include:

Log File Description
/var/log/syslog General system activity logs
/var/log/auth.log Authentication and login attempts
/var/log/nginx/access.log Nginx access logs
/var/log/nginx/error.log Nginx error logs
/var/log/mysql/error.log MySQL error logs

2. Reading System Logs Using PHP

Example: Fetching the Last 50 Lines of a Log File

$logFile = "/var/log/syslog";  // Change to the desired log file
$output = shell_exec("tail -n 50 $logFile");
echo "<pre>$output</pre>";

Why Use This?

  • Retrieves recent system logs
  • Allows quick log inspection from a web interface

3. Creating a Web-Based Log Viewer in PHP

PHP Code to Display Logs Dynamically

$logFile = "/var/log/auth.log";

if (!file_exists($logFile)) {
    die("Log file not found.");
}

$output = shell_exec("tail -n 100 $logFile");
echo "<h2>System Log Viewer</h2>";
echo "<pre>$output</pre>";

Why Use a Web Log Viewer?

  • Provides real-time access to logs
  • Avoids manual SSH access for log inspection

4. Adding Search and Filtering to Logs in PHP

Filtering Logs by Keywords

$logFile = "/var/log/syslog";
$keyword = escapeshellarg($_GET['keyword'] ?? '');

$output = shell_exec("grep $keyword $logFile | tail -n 50");
echo "<pre>$output</pre>";

Why Use Search Filters?

  • Allows quick identification of specific log events
  • Helps in debugging server issues faster

5. Creating a Real-Time Log Monitoring Dashboard

PHP + AJAX for Auto-Updating Logs

PHP Backend (fetch_logs.php)

$logFile = "/var/log/nginx/error.log";
$output = shell_exec("tail -n 50 $logFile");
echo "<pre>$output</pre>";

Frontend (index.html with JavaScript Refresh)

<!DOCTYPE html>
<html>
<head>
    <title>Live Log Viewer</title>
    <script>
        function fetchLogs() {
            fetch("fetch_logs.php")
                .then(response => response.text())
                .then(data => document.getElementById("log-container").innerHTML = data);
        }
        setInterval(fetchLogs, 5000);
        window.onload = fetchLogs;
    </script>
</head>
<body>
    <h2>Live System Log Viewer</h2>
    <div id="log-container" style="background:#000; color:#0f0; padding:10px;"></div>
</body>
</html>

Why Use Live Log Monitoring?

  • Enables real-time log tracking
  • Helps monitor system issues dynamically

6. Displaying Log Data in a Table Format

PHP Code to Format Log Entries into a Table

$logFile = "/var/log/auth.log";
$output = shell_exec("tail -n 50 $logFile");

echo "<table border='1'><tr><th>Log Entry</th></tr>";
foreach (explode("\n", trim($output)) as $line) {
    echo "<tr><td>".htmlspecialchars($line)."</td></tr>";
}
echo "</table>";

Why Use a Table Format?

  • Makes log entries easier to read
  • Supports structured log analysis

7. Setting Up Log File Rotation and Cleanup

Log files can grow too large, impacting server performance. To manage log size, use log rotation.

Checking Log Rotation Configuration

cat /etc/logrotate.conf

Manually Rotating Logs

sudo logrotate -f /etc/logrotate.conf

Deleting Old Logs Using Cron Jobs

0 3 * * * find /var/log -type f -mtime +30 -delete

Why Use Log Rotation?

  • Prevents logs from consuming too much disk space
  • Keeps logs organized and manageable

8. Sending Logs to a Remote Server Using PHP and SCP

To transfer logs to a remote monitoring server, use scp.

PHP Code to Send Logs to Remote Server

$remoteServer = "user@192.168.1.100";
$logFile = "/var/log/syslog";
$remotePath = "/backup/logs/";

shell_exec("scp $logFile $remoteServer:$remotePath");
echo "Log file transferred successfully.";

Why Send Logs to Remote Storage?

  • Ensures log retention for audits
  • Protects logs from accidental deletion

9. Securing Log Access in PHP

Restricting Access to Log Viewer

session_start();
if (!isset($_SESSION['admin'])) {
    die("Access denied.");
}

Why Secure Log Access?

  • Prevents unauthorized users from accessing system logs
  • Ensures log integrity and security

10. Best Practices for System Log Monitoring Using PHP and Shell Scripts

  • Use live updates to monitor logs dynamically
  • Restrict log viewer access to prevent unauthorized access
  • Use cron jobs to clean up old logs and prevent disk overflow
  • Format logs in tables for better readability
  • Send logs to a remote server for backup and auditing

Conclusion

Sending system logs to a web interface using PHP and shell scripts provides real-time server monitoring, easier debugging, and structured log analysis.

This guide covered:

  • Fetching and displaying system logs in PHP
  • Filtering logs based on keywords
  • Implementing live log tracking with JavaScript
  • Securing log access to prevent unauthorized viewing
  • Automating log rotation and cleanup

By implementing these techniques, system monitoring becomes seamless and efficient for administrators and developers.

Leave a Reply