I like knowing what's going on on my servers, but I haven't had time to set up a real monitoring system. Usually when I want to check on my servers, I just remote in with SSH and fire up top and a couple other utilities. After I realized I tend to always check the same things again and again (CPU usage, RAM usage, free disk space), I decided to dump all those things into a page that my server would then serve up. This way I can check my server's health with from anywhere with an internet connection, and the time it took to set up was just a couple minutes.


  def status
    authorize! :status, User
    @monitors = []

    # Source: http://stackoverflow.com/questions/2142377/how-to-correctly-save-the-unix-top-command-output-into-a-variable
    # Source: http://man7.org/linux/man-pages/man1/top.1.html
    @monitors << {command: "top -b -n1 -o +%MEM -w 120 | head -n20", label: "top MEM"}
    @monitors << {command: "top -b -n1 -o +%CPU -w 120 | head -n20", label: "top CPU"}
    # Source: https://linux.die.net/man/1/free
    @monitors << {command: "free -m", label: "Free memory, in megabytes"}
    # Source: https://linux.die.net/man/1/df
    @monitors << {command: "df -h", label: "Disk usage, human-readable"}
    @monitors << {command: "passenger-status", label: "Phusion Passenger status"}
    @monitors << {command: "passenger-memory-stats", label: "passenger-memory-stats"}

    @monitors.each do |monitor|
      # Source: http://stackoverflow.com/questions/2232/calling-shell-commands-from-ruby
      monitor[:result] = `#{monitor[:command]}`.gsub("\n", "<br/>")


<% @monitors.each do |monitor| %>
  <h1><%= monitor[:label] %></h1>
  <p>Command: <%= monitor[:command] %></p>
  <pre><%= monitor[:result].html_safe %></pre>
<% end %>


Photo by Antony Hollingworth