SNMP in rails

Hello,

Can any one please help me in converting this php code into ruby on
rails?

$oid = ‘1.3.6.1.4.1.318.1.1.12.3.3.1.1.4’;
$row[‘address’] = xx.xx.xx.xx //contains an IP address.
$row[‘snmp_community_str’] = 6xrt3bY //stores the community string
$row[‘reset_port’] = 66 //stores a port number
where ‘3’ is immediate reboot

snmpset($row[‘address’],$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’],‘i’,‘3’);


I have no idea of what ‘i’ is…

and

$status=snmpget($row[‘address’],$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’]);

Any help will be appreciated… thnx in advance:)

Hello,
You should use: net/snmp library for ruby and get and set methods of
SNMP
Manager.
http://snmplib.rubyforge.org/
I’m guessing that the first snmpset command is used to reset port status
(i
is iterator - you can have
many ports to reset - just a guess).
The second command snmpget is used to read port reset status.
Could you provide more source code ?

2010/1/18 Atheeq P. [email protected]

Miroslaw N. wrote:

Hello,
You should use: net/snmp library for ruby and get and set methods of
SNMP
Manager.
http://snmplib.rubyforge.org/
I’m guessing that the first snmpset command is used to reset port status
(i
is iterator - you can have
many ports to reset - just a guess).
The second command snmpget is used to read port reset status.
Could you provide more source code ?

2010/1/18 Atheeq P. [email protected]

Thank you very much…
I am using snmp library of ruby but need to pass the snmpset php
function’s parameter to the SNMP Manager set and get functions.

Here is a part of the php code

global $pdb;
$oid = ‘1.3.6.1.4.1.318.1.1.12.3.3.1.1.4’;
$toret = false;
if($pdb->connected) {
$query = “SELECT snmp_community_str, address, reset_port FROM
remote_reboot WHERE device_id=$device_id”;
if($result = $pdb->query($query)) {
if($pdb->num_rows($result)) {
$row = $pdb->get_row($result);
snmpset($row[‘address’]$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’],‘i’,‘3’);
$count = 0;
while($count < 20 && $toret == false) {
$status =
snmpget($row[‘address’],$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’]);
if($status == ‘INTEGER: 2’ || $status == ‘INTEGER: 3’) {
sleep(1);
$count++;
} else if($status == ‘INTEGER: 1’) {
$toret = true;
} else {
$_SESSION[‘reboot_error’] = ‘The server could not be
rebooted because a proper reboot command could not be issued.’;
$count = 20;
}
}
if($count == 10) {
$_SESSION[‘reboot_error’] = ‘Timed out waiting for reboot
confirmation.’;
}
} else {
$_SESSION[‘reboot_error’] = “Unable to locate APC for this
server”;
}
$pdb->free_result($result);
} else {
$_SESSION[‘reboot_error’] = “Unable to locate APC for this
server”;
}
}
return $toret;

Thanks in advance:)

2010/1/18 Atheeq P. [email protected]

snmpset($row[‘address’]$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’],‘i’,‘3’);

snmpget($row[‘address’],$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’]);

Heh,
Wrong guess :slight_smile: From php documentation of snmpset/get:

The “type” parameter must be one of the following, depending on the type
of
variable to set on the SNMP host:

i INTEGER
u unsigned INTEGER
t TIMETICKS
a IPADDRESS
o OBJID
s STRING
x HEX STRING
d DECIMAL STRING
n NULLOBJ
b BITS

So I think that the following code should work in ruby:

require ‘snmp’
include SNMP
manager = Manager.new(:Host => host)
varbind = VarBind.new(oid.to_s + “.” + row[“reset_port”] ,
Integer.new(3))
manager.set(varbind)

And get:


response = manager.get(oid.to_s + “.” + row[“reset_port”])

response.each_varbind do |vb|
puts “#{vb.name.to_s} #{vb.value.to_s}
#{vb.value.asn1_type}”
end

manager.close

Miroslaw N. wrote:

2010/1/18 Atheeq P. [email protected]

snmpset($row[‘address’]$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’],‘i’,‘3’);

snmpget($row[‘address’],$row[‘snmp_community_str’],$oid.".".$row[‘reset_port’]);

Heh,
Wrong guess :slight_smile: From php documentation of snmpset/get:

The “type” parameter must be one of the following, depending on the type
of
variable to set on the SNMP host:

i INTEGER
u unsigned INTEGER
t TIMETICKS
a IPADDRESS
o OBJID
s STRING
x HEX STRING
d DECIMAL STRING
n NULLOBJ
b BITS

So I think that the following code should work in ruby:

require ‘snmp’
include SNMP
manager = Manager.new(:Host => host)
varbind = VarBind.new(oid.to_s + “.” + row[“reset_port”] ,
Integer.new(3))
manager.set(varbind)

And get:


response = manager.get(oid.to_s + “.” + row[“reset_port”])

response.each_varbind do |vb|
puts “#{vb.name.to_s} #{vb.value.to_s}
#{vb.value.asn1_type}”
end

manager.close

Thanks a lot.

Almost got it working but got an error

TypeError: can’t convert Fixnum into String

at this line

varbind = VarBind.new(oid.to_s + “.” + result.reset_port ,
Integer.new(3))

I fixed it by converting reset_port to string i.e.,
result.reset_port.to_s

but it again gave an error

NoMethodError: undefined method `new' for Integer:Class

for the modified line

varbind = VarBind.new(oid.to_s + “.” + result.reset_port.to_s ,
Integer.new(3))

Thanks in advance:)

Thanks a lot.

Almost got it working but got an error

TypeError: can’t convert Fixnum into String

at this line

varbind = VarBind.new(oid.to_s + “.” + result.reset_port ,
Integer.new(3))

I fixed it by converting reset_port to string i.e.,
result.reset_port.to_s

but it again gave an error

NoMethodError: undefined method `new' for Integer:Class

for the modified line

varbind = VarBind.new(oid.to_s + “.” + result.reset_port.to_s ,
Integer.new(3))

Thanks in advance:)

I got that working, but what settings do i have to configure on the host
or install because it says SNMP::RequestTimeout xx.xx.xx.xx not
responding
what might be the problem?

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