Ruby development for system administrators
Koen Vervloesem doesn’t like shell scripts that are difficult to maintain, therefore he uses Ruby for his sysadmin tasks. Do the same with his four-page guide…
XML processing
Among the many tasks of a system administrator, one that comes back as part of a lot of scripts is converting data to different formats. More and more, file formats are using XML. Ruby has the REXML (Ruby Electric XML) module for this purpose in the standard library. So let’s show how we convert our router’s CSV file to XML, with the daily download data for each host:
#!/usr/bin/env ruby
require ‘csv’
require ‘rexml/document’
ip = ARGV[0]
xml = REXML::Document.new
root = xml.add_element(“statistics”)
CSV::Reader.parse(File.open(‘bandwidth.csv’)) do |row|
if row[0].to_s == “download” and row[1].to_s == “day” and row[3].to_s != ‘COMBINED’
ip = row[3].to_s
host = root.elements[“host[@ip=’#{ip}’]”]
if host == nil
host = root.add_element(“host”, {“ip” => ip})
end
day = host.add_element(“day”)
day.attributes[“begin”] = Time.at(row[4].to_i)
day.attributes[“end”] = Time.at(row[5].to_i)
day.add_element(“bandwidth”, {“bytes” => row[6].to_i})
end
end
puts xml
So next to the CSV library, we also use the REXML/Document library. We create an empty XML document and add an element ‘statistics’ as the root element. Then we read the CSV file like in our previous script, but instead of just showing our findings we construct an XML document with the findings. For each IP address we add a host element, and we first check if the element doesn’t exist yet. Then we add a day element to the host and then a bandwidth element to the day element. The beginning and end of the day and the number of bytes are added to their respective elements as attributes. At the end, we show the whole XML tree as output to stdout, so you can pipe it to the input of another program (maybe one that reads an XML file and outputs a SVG graph) or write it to a file.
Working with files
Most system administrators do file manipulation manually in their favourite shell, but there comes a time when some things have to be automated. This can be done in a pure Bash script, but the file manipulation commands in Bash are not the most easy to read. The matching methods are easier to read in Ruby, although they are somewhat scattered among different classes: File, Dir, FileUtils and Find. Unfortunately, these classes don’t behave very Ruby-like: most of the methods are class methods instead of instance methods, which means that the object-oriented approach is not used consistently for files.















The guide is quite informative.
But since your site engine does not support proper code formatting, examples require tweaking before use.
Or Ruby’s really capable of interpreting the WinWord-style single quotes – “require ‘csv’”?
Nice introduction, the said “when men were men….” is forgotten, but the tools help more than “prestige”. Thanks for this article.
@Dummy00001 Ruby can use single or double quoted strings. Double quoted strings give you some extra functionality when it comes to string interpolation but it is a fairly standard Ruby practice to use single quoted otherwise.