Dynamically creating variables

Hi all,

I’m fairly new to Ruby, and programming in general (though I’m
reasonably proficient at shell scripting) so sorry if this is a dumb
question.

I’m trying to write a script for an SNMP poller but I’m getting stuck
trying to work out how to create my variables. The script will be
processing a CSV of unknown length. It might be just 2 servers or it
could be up to 20. I cant work out how I can find the list of servers
from the CSV and then create variables for each one that I can keep
track off through out the programs life to out put what I want.
this is what I’ve created so far
http://pastebin.com/bLPbnRv4
I envision it parsing a CSV like this


host,port,community,cluster,shortName,type,rpsTotal,clientTotal,serverTotal
172.16.0.1,161,Public,Library,mwg1,mwg7,0,0,0
172.16.0.1,161,Public,Library,mwg2,mwg7,0,0,0

after each run, it will need to update the CSV with the results of the
last poll as those are used to calculate the average throughput between
each time the script is called.

The whole process will be called as a cron job, and will be outputting
an HTML file.

Reading around has not really helped me as I’ve not found any examples
of others doing this, though I cant imagine others are not.

If any one can give me some pointers, that would be really helpful.

Thanks,

Tris


This email and any files transmitted with it are confidential
and intended solely for the use of the individual or entity
to whom they are addressed. If you have received this email
in error please notify [email protected]

The views expressed within this email are those of the
individual, and not necessarily those of the organisation


On Tue, Jan 24, 2012 at 5:13 AM, Tris H. [email protected] wrote:

through out the programs life to out put what I want.
after each run, it will need to update the CSV with the results of the

The views expressed within this email are those of the individual, and not
necessarily those of the organisation


So you can use CSV.parse and give it a string or a stream, or you can
use
CSV.foreach and give it a path to a file. Docs are here
http://rubydoc.info/stdlib/csv/frames

In Ruby, you can iterate over collections (e.g. 2…20 servers). A CSV
file
is a collection of rows. If we tell it the CSV has headers, it will
present
each row with a hash interface so that we can access the elements by
name.

csv = <<CSV
host,port,community,cluster,shortName,type,rpsTotal,clientTotal,serverTotal
172.16.0.1,161,Public,Library,mwg1,mwg7,0,0,0
172.16.0.1,161,Public,Library,mwg2,mwg7,0,0,0
CSV

require ‘csv’

this block of code will be invoked once for each row in the CSV

CSV.parse csv, :headers => true do |row|
host = row[‘host’]
port = row[‘port’]

etc

proxy = Proxy.new host, port # etc
proxy.write_stats # or something
end

On Tue, Jan 24, 2012 at 12:13 PM, Tris H. [email protected] wrote:

I’m trying to write a script for an SNMP poller but I’m getting stuck trying
to work out how to create my variables. The script will be processing a CSV
of unknown length. It might be just 2 servers or it could be up to 20. I
cant work out how I can find the list of servers from the CSV and then
create variables for each one that I can keep track off through out the
programs life to out put what I want.

You do not want to create variables. Instead, you use collections.
With a Hash you can pick an appropriate key (e.g. IP or domain name of
server). Yo could also store values in Hashes.

Example

13:25:53 Temp$ ruby19 csv.rb
{“172.16.0.1”=>
{“host”=>“172.16.0.1”,
“port”=>“161”,
“community”=>“Public”,
“cluster”=>“Library”,
“shortName”=>“mwg1”,
“type”=>“mwg7”,
“rpsTotal”=>“0”,
“clientTotal”=>“0”,
“serverTotal”=>“0”},
“172.16.0.2”=>
{“host”=>“172.16.0.2”,
“port”=>“161”,
“community”=>“Public”,
“cluster”=>“Library”,
“shortName”=>“mwg2”,
“type”=>“mwg7”,
“rpsTotal”=>“0”,
“clientTotal”=>“0”,
“serverTotal”=>“0”}}
13:26:12 Temp$ cat dat.csv
host,port,community,cluster,shortName,type,rpsTotal,clientTotal,serverTotal
172.16.0.1,161,Public,Library,mwg1,mwg7,0,0,0
172.16.0.2,161,Public,Library,mwg2,mwg7,0,0,0
13:26:27 Temp$ cat -n csv.rb
1 require ‘csv’
2 require ‘pp’
3
4 head = nil
5 data = {}
6
7 CSV.foreach(“dat.csv”) do |row|
8 if head
9 tmp = {}
10
11 head.zip row do |h,v|
12 tmp[h] = v
13 end
14
15 data[row.first] = tmp
16 else
17 head = row.each &:freeze
18 end
19 end
20
21 pp data
13:26:34 Temp$

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs