Help convert sh -> rb

Hello, I am new to the forum and ruby, thank you for any help you may
have.

The goal I am trying to accomplish is converting this shell script into
a ruby script. What the script does is run the
"rds-describe-db-instances" command then pull out two variables and
input them into the JSON output. Any help would be great, thanks.

#!/bin/sh
echo "
{
     \"checks\": {"
rds-describe-db-instances|awk '$1 == "DBINSTANCE" { print $6,$9 }'|
while read S I; do
echo "
        \"mysql_disk_usage_$I\": {
            \"handlers\": [\"default\",\"mailer_alerts\"],
            \"command\": \"/etc/sensu/plugins/mysql-disk.rb -h$I u -p
-s$S\",
            \"interval\": 300,
            \"occurrences\": 1,
            \"subscribers\": [
                \"urls\"
            ]
        },"
done
echo "}}"

···

--
Posted via http://www.ruby-forum.com/.

What have you written so far?

···

On Fri, May 10, 2013 at 7:51 AM, Micah Hoffmann <lists@ruby-forum.com> wrote:

The goal I am trying to accomplish is converting this shell script into
a ruby script.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

What have you written so far?

Haven't worked it much yet, I've only got...I need to figure out how to
save the output so I can store my variables.

#!/usr/bin/env ruby
rds_describe = `rds-describe-db-instances`

puts "
{
    \"checks\": {
        \"mysql_disk_usage_$I\": {
            \"handlers\": [\"default\",\"mailer_alerts\"],
            \"command\": \"/etc/sensu/plugins/mysql-disk.rb -h$I u -p
-s$S\",
            \"interval\": 300,
            \"occurrences\": 1,
            \"subscribers\": [
                \"urls\"
            ]
        },
"

···

--
Posted via http://www.ruby-forum.com/.

OK, so you can save the output to a file, to a DB, to a key-value
data store (e.g. Redis) -- plenty of choices.

What will you be doing with the data after you store it? What is
the volume of data being generated (is performance an issue)?

···

On Fri, May 10, 2013 at 3:17 PM, Micah H. <lists@ruby-forum.com> wrote:

Haven't worked it much yet, I've only got...I need to figure out how to
save the output so I can store my variables.

#!/usr/bin/env ruby
rds_describe = `rds-describe-db-instances`

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
http://about.me/hassanschroeder
twitter: @hassan

Perhaps setting it up as a hash of hashes, or a class of classes might
be more useful, and then using #to_json to emit the json.

A simple hash of the above would look something like this:

check_hash =
  { "checks" =>
      "mysql_disk_usage+#{i}" => {
        "handlers" =>
          ["default", "mailer_alerts"],
        "command" => "/etc/sensu/plugins/mysql-desk.rb -h#{i} u -p -s {#s}",
        "interval" => 300,
        "occurances" => 1,
        "subscribers" => ["urls"]
      }
   }

then you can get the json by:

puts check_hash.to_json

which saves all the quote escaping and what-not.

Note the #{i} and #{s} in the above -- these correspond to the I and S
variables in the bash script, but you'd need to get at them
differently.

Starting off, you read in the output from the command by:

rds_describe = `rds-describe-db-instances`

you can walk through it with something like:

rds_describe.split("\r?\n").each do |line|

  s, i = line.split()

  puts { "checks" =>
          "mysql_disk_usage+#{i}" => {
            "handlers" => ["default", "mailer_alerts"],
            "command" => "/etc/sensu/plugins/mysql-desk.rb -h#{i} u -p -s {#s}",
            "interval" => 300,
            "occurances" => 1,
            "subscribers" => ["urls"]
          }
       }.to_json

end

(Note you need a require 'json' at the beginning of the script.)

This will basically replicate your bash script in ruby form.

···

On Fri, May 10, 2013 at 5:17 PM, Micah H. <lists@ruby-forum.com> wrote:

What have you written so far?

Haven't worked it much yet, I've only got...I need to figure out how to
save the output so I can store my variables.

#!/usr/bin/env ruby
rds_describe = `rds-describe-db-instances`

puts "
{
    \"checks\": {
        \"mysql_disk_usage_$I\": {
            \"handlers\": [\"default\",\"mailer_alerts\"],
            \"command\": \"/etc/sensu/plugins/mysql-disk.rb -h$I u -p
-s$S\",
            \"interval\": 300,
            \"occurrences\": 1,
            \"subscribers\": [
                \"urls\"
            ]
        },
"

What will you be doing with the data after you store it? What is
the volume of data being generated (is performance an issue)?

I will most likely set it up as a cronjob, but the cmd isn't very
intensive, so that is not an issue. Now that I think about it,
out-putting to a file, and continually re-writing that file, should be
how I do this.

···

--
Posted via http://www.ruby-forum.com/.