From: tamouse mailing lists Date: 2013-05-13T06:10:37+09:00 Subject: Re: Help convert sh -> rb On Fri, May 10, 2013 at 5:17 PM, Micah H. 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\" > ] > }, > " 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.