# Script for gathering system status information from a linux server. This has # only been tested on Debian linux systems. It outputs HTML text that is meant # to be piped into the mail command for delivery: # # ruby status.rb | mail -a "Content-type: text/html;" -s "Status Report: $HOSTNAME" "some@address.com" # # In Debian, you just need the mailx package installed for the above to work # along with Ruby (of course). require 'erb' ENV['COLUMNS'] = '200' timestamp = Time.now.strftime("%B %d, %Y @ %I:%M%p %Z") hostname = `hostname` load_avg = `uptime | awk -F'average(s)?:' '{print $2}'` # Memory usage totalmem = `free -m | grep Mem: | awk '{print $2}'`.to_i usedmem = `free -m | grep Mem: | awk '{print $3}'`.to_i cachedmem = `free -m | grep Mem: | awk '{print $7}'`.to_i memused = `free -m | grep buffers/cache: | awk '{print $3}'`.to_i actual_usedmem = usedmem - cachedmem actual_freemem = totalmem - actual_usedmem php_procs = `ps -ef | grep -v grep | grep -c php` httpd_procs = `ps -ef|grep -v grep|grep -c httpd` tcp_connections = `netstat -nat | grep tcp | awk '{ print $5}' | cut -d: -f1 | sed -e '/^$/d' | uniq | wc -l` open_tcp_connections = `netstat -atun | grep tcp | awk '{print $5}' | cut -d: -f1 | sed -e '/^$/d' | sort | uniq -c | sort -n` top_memory_procs = `ps aux --sort=-rss | head -11`.chomp.strip top_cpu_procs = `ps aux --sort=-pcpu | grep -v grep | grep -E "(^([^ ]*?)[ ]*[0-9]*[ ]*(([0-9]{1,2}\.[1-9])|([1-9]{1,2}\.[0-9])))|USER" | head -11`.chomp.strip netstat = `netstat -a --tcp | sort`.chomp.strip top_snapshot = `top -c -b -n1`.chomp.strip # Determine graph color memused_percent = ((actual_usedmem.to_f / totalmem.to_f) * 100).round if memused_percent < 33 graph_color = "ACE97C" elsif memused_percent > 33 && memused_percent < 66 graph_color = "E9C981" else graph_color = "E9B19A" end template = %q{ System Status

Load averages: <%= load_avg %>

Memory Usage

Used:
Used: <%= actual_usedmem %>MB • Total: <%= totalmem %>MB • Free: <%= actual_freemem %>MB

Process / Connection Counts

connections: <%= tcp_connections %>
php: <%= php_procs %>
httpd: <%= httpd_procs %>

Open Connections

<%= open_tcp_connections %>

Top 10 Memory Processes

<%= top_memory_procs %>

Top 10 CPU Processes

<%= top_cpu_procs %>

Netstat

<%= netstat %>

System Snapshot from top

<%= top_snapshot %>
} puts ERB.new(template).result