System Administration with Ruby

Ruby gets a lot of (well deserved) press because of Ruby on Rails, but recently I’ve found it to also be an excellent choice for scripting tasks, jobs that I otherwise would have used Perl for.

The first benefit of using Ruby is that its built-in code blocks allow for simpler, shorter code. A Ruby block is basically an anonymous function that can be passed to another function as a parameter. For example, here’s how you could take an array of numbers and square each one:

[ruby]

nums = [1, 2, 3, 4, 5]
nums.collect { |x| x*x } # produces [1, 4, 9, 16, 25]
[/ruby]

Blocks are used for all sorts of purposes, like looping, iterating, sorting and mapping. Once you get used to them, they become quite intuitive and save a lot of coding time.

In particular I’ve found code blocks to very useful with database programming. Here’s an example that uses the mysql Ruby plugin to do a query:

[ruby]
require ‘mysql’

db = Mysql.new(“localhost”, “root”, “password”)
db.select_db(“a_database”)

result = db.query “select id,name from users”
result.each do |row|
puts “ID: #{id}, Name: #{name}”
end
[/ruby]

Note that “do/end” is a multi-line alternative to brackets.

In Ruby, everything is an object, even primitive types like numbers and strings. The language makes it quite easy to create your own classes as well, and because of this I find myself creating classes more often than I normally would while writing scripts. Currently in Perl, OO programming is still rather obtuse, so it’s often simpler just to use scalars, arrays and hashes in various combinations (for example, “a hash where the keys are names, and the values are arrays where the first element is a number”, etc). The problem with this is it can create brittle code that can make bugs common.

Ruby classes can be written inline with procedural statements, and are usually very short:

[ruby]
# A simple class to represent a person
class Person
attr_accessor :first_name, :last_name, :age

def initialize(first_name, last_name, age)
@first_name = first_name
@last_name = last_name
@age = age
end
end

joe = new Person(“Joe”, “Schmo”, 25)
[/ruby]

The “attr_accessor” line is a useful declaration that creates a private member variable along with public reader and writer methods.

Two common tasks of scripts are working with operating system calls, and string parsing/manipulation. In these aspects, Ruby works just like Perl. You can use hash marks (the ` character) to run a command return its output. For working with strings, Ruby regular expressions work just like Perl. Here’s an example that uses both:

[ruby]
# Look for a resolve error
resolve = `resolveip #{address}`
puts “Host not found” if resolve =~ /host not found/
[/ruby]

All of this being said, there are still many places where I feel that Perl is a more appropriate choice for sysadmins. While Ruby has a growing set of plugins, nothing can beat the breadth of Perl modules available through CPAN. For a very specific task (say, interfacing with the /proc filesystem) Perl is often a better choice simply because of the amount of publicly available code out there.

The next time you have to do some administrative scripting, consider giving Ruby a try. Even if you decide to stick with Perl (or whatever your language of choice), it’s a great way to try out Ruby if you’re not familiar with it.

  • http://www.vivaserver.com.ar Cristian R. Arroyo

    I’m just starting to discover Ruby outside Rails, and yes it’s, it’s great for system administration. Just one more little advice: when your Ruby administration scripts began to grow (in both size and importance), it’ll make sense to consider converting them into Rake tasks.

  • http://www.dracoware.com/blog/2007/05/14/dealing-with-really-big-log-files/ Dealing with (really) big log files » Draconis Software Blog

    [...] package. The easiest way, of course, is to write a simple Perl (or any other language, such as Ruby) script, iterate through the file line-by-line, and parse each line. The problem here was speed and [...]

  • http://www.dracoware.com/blog/2007/05/23/scripting-with-php/ Scripting with PHP » Draconis Software Blog

    [...] if I’m going to write a script to automate a task via cron, I’ll use Perl (or lately, Ruby). Recently though I needed to use PHP in order to take advantage of common code from a web [...]