When debugging performance issues on a LAMP stack server, real-time monitoring tools like top
or htop
only show current state. What we really need is historical data to identify patterns and spikes. Here's how to capture and analyze resource usage over time.
Ubuntu comes with several powerful tools pre-installed:
# Basic real-time monitoring
top
htop
vmstat 1
For historical data, we need to configure logging:
The sysstat
package is the gold standard for historical resource tracking:
# Install sysstat
sudo apt-get install sysstat -y
# Enable data collection (edit /etc/default/sysstat)
sudo sed -i 's/ENABLED="false"/ENABLED="true"/' /etc/default/sysstat
# Restart the service
sudo systemctl restart sysstat
This collects data every 10 minutes by default. Adjust the interval in /etc/cron.d/sysstat
if needed.
View CPU usage history:
# Show CPU usage for today
sar -u
# Show memory usage
sar -r
# Show combined report for specific date
sar -q -r -u -f /var/log/sysstat/saXX # Replace XX with day number
For graphical analysis, consider these options:
# Install necessary tools
sudo apt-get install gnuplot
# Generate CPU usage graph
sadf -g -- -u /var/log/sysstat/saXX | gnuplot > cpu_usage.png
For more detailed application-level monitoring:
# Install netdata for real-time dashboard
bash <(curl -Ss https://my-netdata.io/kickstart.sh)
# Use atop for advanced process accounting
sudo apt-get install atop
sudo service atop start
For targeted LAMP stack monitoring:
#!/bin/bash
# Log CPU and memory usage every minute
while true; do
echo "$(date) $(top -bn1 | grep "Cpu(s)" | awk '{print $2}') $(free -m | awk '/Mem:/ {print $3}')" >> /var/log/resource_usage.log
sleep 60
done
To specifically track LAMP components:
# Show historical Apache memory usage
sudo grep 'Max processes' /var/log/apache2/error.log
# Monitor PHP-FPM processes
sudo pmap $(pgrep php-fpm | head -1) | tail -n 1
When debugging performance issues on a LAMP server, real-time monitoring tools like top
or htop
only show current status. What we really need is historical data to identify patterns or spikes in resource usage. Here's how to implement proper logging.
The sysstat
package provides sar
(System Activity Reporter), which collects and reports system metrics:
sudo apt-get install sysstat
sudo systemctl enable sysstat
sudo systemctl start sysstat
View CPU history (10 minute intervals):
sar -u -f /var/log/sysstat/sa$(date +%d -d yesterday)
View memory usage:
sar -r -f /var/log/sysstat/sa$(date +%d)
For more advanced monitoring:
# Install Prometheus Node Exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
tar xvfz node_exporter-*.tar.gz
cd node_exporter-*
./node_exporter &
Then configure Prometheus to scrape metrics every 15 seconds for detailed historical data.
For monitoring specific processes:
pip install psrecord
psrecord $(pgrep mysqld) --interval 1 --duration 60 --plot output.png
Create a simple monitoring script:
#!/bin/bash
echo "$(date) - CPU: $(grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage}')%" >> /var/log/system_monitor.log
echo "$(date) - Memory: $(free -m | awk 'NR==2{printf "%.2f%%", $3*100/$2}')" >> /var/log/system_monitor.log
Then add to cron:
* * * * * /path/to/script.sh
For LAMP-specific monitoring:
# Log top PHP processes every 5 minutes
*/5 * * * * ps -eo pid,user,comm,pcpu,pmem --sort=-pcpu | grep -E 'apache2|php' >> /var/log/php_processes.log
When using Prometheus, Grafana provides excellent visualization:
wget https://dl.grafana.com/oss/release/grafana_10.1.5_amd64.deb
sudo dpkg -i grafana_10.1.5_amd64.deb
sudo systemctl enable grafana-server
sudo systemctl start grafana-server
Configure dashboards to display CPU/memory trends over time.