📊 Automation – Monthly SAR Report Using KSAR

• Originally published on old blog

Note: This post was originally published in 2014. While the core concepts remain valid, modern system monitoring often uses cloud-based solutions and automated dashboards. The principles of system performance analysis and reporting remain relevant.

Currently, in most organizations, people use Oracle Enterprise Manager (OEM) to analyze and monitor the CPU, memory, and I/O utilization of Linux machines in pictorial representation. But think of a situation where you have no such tools to analyze the trends of memory, CPU, and I/O. That is the moment when KSAR is going to be very handy.

Do you know that interesting trick? Now with the graphical SAR tool called KSAR, it's easy to automate report generation by just following the below steps.

Step 1: Generate System Activity Report

At the end of 23:59 minutes, generate the system activity report and redirect it to a text file.

[thiru@localhost kSar-5.0.6]$ LC_ALL=C sar -A -f /var/log/sa/sa13 > sar13.txt

Step 2: Generate PDF Report Using KSAR

Just download KSAR from the following site and unzip it to some directory. Now you can go ahead and execute the following command to generate the SAR report in PDF format.

[thiru@localhost kSar-5.0.6]$ java -jar kSar.jar -input sar13.txt -outputPDF sar13.pdf
time to parse: 1022ms number of line: 4357 line/msec: 4.0

Automation Script

Here's a complete automation script to generate monthly SAR reports:

#!/bin/bash
# Program Name: monthly_sar_report.sh
# Purpose: Generate monthly SAR report using KSAR

# Configuration
KSAR_PATH="/opt/ksar"
SAR_LOG_DIR="/var/log/sa"
OUTPUT_DIR="/opt/reports/sar"
EMAIL_RECIPIENTS="admin@company.com,manager@company.com"

# Create output directory if it doesn't exist
mkdir -p $OUTPUT_DIR

# Get yesterday's date
YESTERDAY=$(date -d "yesterday" +%d)
MONTH=$(date -d "yesterday" +%m)
YEAR=$(date -d "yesterday" +%Y)

# Generate SAR report
echo "Generating SAR report for $YESTERDAY..."
LC_ALL=C sar -A -f $SAR_LOG_DIR/sa$YESTERDAY > $OUTPUT_DIR/sar$YESTERDAY.txt

# Check if SAR file was generated successfully
if [ ! -f $OUTPUT_DIR/sar$YESTERDAY.txt ]; then
    echo "Error: SAR report generation failed"
    exit 1
fi

# Generate PDF using KSAR
echo "Generating PDF report..."
java -jar $KSAR_PATH/kSar.jar -input $OUTPUT_DIR/sar$YESTERDAY.txt -outputPDF $OUTPUT_DIR/sar_report_$YEAR-$MONTH.pdf

# Check if PDF was generated successfully
if [ ! -f $OUTPUT_DIR/sar_report_$YEAR-$MONTH.pdf ]; then
    echo "Error: PDF generation failed"
    exit 1
fi

echo "Report generated successfully: $OUTPUT_DIR/sar_report_$YEAR-$MONTH.pdf"

# Send email with attachment
echo "Sending email report..."
echo "Monthly SAR Report - $YEAR-$MONTH" | mail -s "Monthly SAR Report - $YEAR-$MONTH" -a $OUTPUT_DIR/sar_report_$YEAR-$MONTH.pdf $EMAIL_RECIPIENTS

echo "Monthly SAR report process completed successfully"

Advanced Automation with Multiple Reports

For more comprehensive reporting, you can generate multiple types of reports:

#!/bin/bash
# Advanced SAR reporting script

# Configuration
KSAR_PATH="/opt/ksar"
SAR_LOG_DIR="/var/log/sa"
OUTPUT_DIR="/opt/reports/sar"
REPORT_DATE=$(date -d "yesterday" +%d)

# Function to generate specific SAR reports
generate_cpu_report() {
    echo "Generating CPU utilization report..."
    sar -u -f $SAR_LOG_DIR/sa$REPORT_DATE > $OUTPUT_DIR/cpu_$REPORT_DATE.txt
    java -jar $KSAR_PATH/kSar.jar -input $OUTPUT_DIR/cpu_$REPORT_DATE.txt -outputPDF $OUTPUT_DIR/cpu_report_$REPORT_DATE.pdf
}

generate_memory_report() {
    echo "Generating memory utilization report..."
    sar -r -f $SAR_LOG_DIR/sa$REPORT_DATE > $OUTPUT_DIR/memory_$REPORT_DATE.txt
    java -jar $KSAR_PATH/kSar.jar -input $OUTPUT_DIR/memory_$REPORT_DATE.txt -outputPDF $OUTPUT_DIR/memory_report_$REPORT_DATE.pdf
}

generate_io_report() {
    echo "Generating I/O utilization report..."
    sar -b -f $SAR_LOG_DIR/sa$REPORT_DATE > $OUTPUT_DIR/io_$REPORT_DATE.txt
    java -jar $KSAR_PATH/kSar.jar -input $OUTPUT_DIR/io_$REPORT_DATE.txt -outputPDF $OUTPUT_DIR/io_report_$REPORT_DATE.pdf
}

# Generate all reports
generate_cpu_report
generate_memory_report
generate_io_report

echo "All reports generated successfully"

Setting Up Cron Job

To automate the report generation, add it to your crontab:

# Edit crontab
crontab -e

# Add this line to run daily at 1:00 AM
0 1 * * * /opt/scripts/monthly_sar_report.sh

# Or run monthly on the 1st of each month at 2:00 AM
0 2 1 * * /opt/scripts/monthly_sar_report.sh

What KSAR Provides

KSAR generates comprehensive graphical reports including:

  • CPU Utilization: User, system, idle, and wait percentages over time
  • Memory Usage: Physical memory, swap usage, and buffer/cache statistics
  • I/O Performance: Disk read/write operations and transfer rates
  • Network Statistics: Network interface utilization and packet statistics
  • Process Statistics: Process creation and context switching rates
  • Load Average: System load over different time intervals

Benefits of Automated SAR Reporting

  • Historical Analysis: Track system performance trends over time
  • Capacity Planning: Identify when systems need upgrades or optimization
  • Performance Troubleshooting: Correlate issues with system metrics
  • Compliance: Maintain records for audit and compliance requirements
  • Proactive Monitoring: Identify potential issues before they become problems

Modern Alternatives

While KSAR is still useful, modern alternatives include:

  • Prometheus + Grafana: Real-time monitoring and alerting
  • Datadog: Cloud-based monitoring and analytics
  • New Relic: Application performance monitoring
  • Nagios: Infrastructure monitoring and alerting
  • Zabbix: Enterprise-grade monitoring solution

Best Practices

  • Regular Cleanup: Archive old reports and clean up temporary files
  • Error Handling: Implement proper error checking and logging
  • Storage Management: Monitor disk space usage for report storage
  • Email Configuration: Ensure proper email server configuration
  • Security: Secure access to reports containing sensitive system information

Key Takeaways

  • KSAR provides graphical analysis of SAR data without expensive tools
  • Automation eliminates manual report generation tasks
  • Historical system performance data is valuable for capacity planning
  • Regular reporting helps identify performance trends and issues
  • Combining SAR data with visualization tools creates powerful insights