Topic: Create a Rails System Information with 1 controller - phpsysinfo like.

Create a Rails System Information with 1 controller - phpsysinfo like.

This tutorial will show you how to create a very simple but powerful Rails System Information application in only 7 steps or 1 controller.

1.- Create the rails applications Rayis System Information "rsi"

rails rsi -d mysql

2.- Create a controller sysinfo

script\generate controller sysinfo

3.- Before start the server you must configure the database.yml and create at least the si_development database.

Edit config/database.yml

Create the si_development database.

rake db:create

[ Note you don't have to create all the databases at this time.]

4.- Remove the default index.html form the public directory

rm public/index.html

5.- Configure the config/routes.rb to redirect to the proper controller in this case sysinfo

ActionController::Routing::Routes.draw do |map|
  map.root :controller => "sysinfo"

6.- If you start the webserver ./script/server right now you will have:

Unknown action
No action responded to index

Lets create the index method in the sysinfo controller, edit the file app/controllers/sysinfo_controller.rb

class SysinfoController < ApplicationController

  def index
    @server_hostname = %x[uname -a]
    @server_uptime = %x[uptime]
    @server_date = %x[date]
    @server_memory = %x[free]
    @server_release = %x[cat /etc/redhat-release]

    # Networking
    @server_ifconfig = %x[/sbin/ifconfig]

    # Hard Disk
    @server_partitions= %x[df -h |wc -l]
    @hard_disk_usage = %x[df -h]

    # For Fedora & RedHat Based...
    @startup_services_count = %x[/sbin/chkconfig --list |grep :on |wc -l]
    @startup_services = %x[/sbin/chkconfig --list |grep :on]

    # Email Queue / Fedora & RedHat Based...
    @mail_queue_count = %x[ls -l /var/spool/mail |grep rw |wc -l]
    @mail_queue = %x[ls -l /var/spool/mail |grep rw]

    # Server Last Login
    @server_last_login_count = %x[last |grep pts |wc -l]
    @server_last_login = %x[last |grep pts]

    # Server Processes
    @server_processes_count = %x[top -n 1 -b |wc -l]
    @server_processes = %x[top -n 1 -b]

    # Processes Tree - Fedora & RedHat Based...
    @server_process_tree_count = %x[pstree -p |wc -l]
    @server_process_tree = %x[pstree -p]

    # Linux Users
    @server_users_count = %x[cat /etc/passwd | wc -l]
    @server_users = %x[cat /etc/passwd]

    # Linux Groups
    @server_groups_count = %x[cat /etc/group | wc -l]
    @server_groups = %x[cat /etc/group]

    # Server Errors
    @server_errors_count = %x[cat /var/log/messages | grep error |wc -l]
    @server_errors = %x[cat /var/log/messages | grep error]

    # Server Alerts
    @server_alerts_count = %x[cat /var/log/messages | grep alert |wc -l]
    @server_alerts = %x[cat /var/log/messages | grep alert]

    # Server Panics
    @server_panics_count = %x[cat /var/log/messages | grep panic |wc -l]
    @server_panics = %x[cat /var/log/messages | grep panic]



7.- If you start the webserver ./script/server right now you will have:
Template is missing

Missing template sysinfo/index.html.erb in view path /home/blopez/rails/no/app/views

Lets create the index.html.erb in the app/views/sysinfo folder:

<b>Server Hostname:</b> <pre> <%= @server_hostname %> </pre> 
<b>Server Uptime:</b> <pre> <%= @server_uptime %> </pre>
<b>Server Date:</b> <pre> <%= @server_date %> </pre>
<b>Server Memory:</b> <pre> <%= @server_memory %> </pre>
<b>Server Release:</b> <pre> <%= @server_release %> </pre>
<b>Networking:</b> <pre> <%= @server_ifconfig %> </pre>

<b>Hard Disk Partitions [<%= @server_partitions %>]:</b>
<pre> <%= @hard_disk_usage %> </pre>

<b>Startup Services [<%= @startup_services_count %>]: </b>
<pre> <%= @startup_services %> </pre>

<b>Mail Queue [<%= @mail_queue_count %>]: </b>
<pre> <%= @mail_queue %> </pre>

<b>Server Last Login [<%= @server_last_login_count %>]:</b>
<pre> <%= @server_last_login %> </pre>

<b>Server Processes [<%= @server_processes_count %>]:</b>
<pre> <%= @server_processes %> </pre>

<b>Server Process Tree [<%= @server_process_tree_count %>]:</b>
<pre> <%= @server_process_tree %> </pre>

<b>Server Users [<%= @server_users_count %>]:</b>
<pre> <%= @server_users %> </pre>

<b>Server Groups [<%= @server_groups_count %>]:</b>
<pre> <%= @server_groups %> </pre>

<b>Server Errors [<%= @server_errors_count %>]:</b>
<pre> <%= @server_errors %> </pre>

<b>Server Alerts [<%= @server_alerts_count %>]:</b>
<pre> <%= @server_alerts %> </pre>

<b>Server Panic [<%= @server_panics_count %>]:</b>
<pre> <%= @server_panics %> </pre>

Rails SysInfo

That's all folks now we are ready to launch the web server and show your Rails System Information sumary with all your data:


Server Hostname:
Server Uptime:
Server Date:
Server Memory:
Server Release:
Hard Disk Partitions [#Number of Partitions ]:
Startup Services [# Number of Startup Services ]:
Mail Queue [# Number of Email accounts ]:
Server Last Login [# Number of entries show ]:
Server Processes [# Number of Processes]:
Server Process Tree [# Tree processes with PID]:
Server Users [# Number of entries in /etc/passwd ]:
Server Groups [0 ]:

Server Errors [0 ]:
Server Alerts [0 ]:
Server Panic [0 ]:

What RailsSysInfo is or is not:
This is a quick tool to show Systems Administrator Information.
SysInfo is inspired in: but now in Rails smile .
The script is not secure and is not designed to be, is just designed to show the concept of execute commands to provide information, use it wisely.
If you need to secure it, I recommend to check the post:
Yes this tutorial is for Linux/Unix based systems only ;-)

No actions are included in the script, but can be added to interact via remotely just like ActiveResource.
Execution from a cron could be added, then information collected can be inserted into a DB for historical purposes.
The script can add some features to check some values & trigger another events like email notifications.
System Performance Graphs can be added with usage of the "RRD - Round Robin Databse" just MRTG Style. [ But that is another project ] ;-)
I think overall this could be a really useful tool... will look into the posibility to create a plugin "Rails System Information" just one step at the time, my expertise in rails is not on the guru level LOL, in the mean time lets keep learning Ruby, Rails & Thin. smile

Best Regards Dinooz

Last edited by dinooz (2008-03-03 18:58:34)

Re: Create a Rails System Information with 1 controller - phpsysinfo like.

Just curious if you updated this at all...