Archive for the ‘Ruby’ Category

How to import a helper function into rails script/console

Thursday, March 19th, 2009
>> foo = ActionView::Base.new
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={}, 
   @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>

>> foo.extend YourHelperModule
=> #<ActionView::Base:0x2aaab0ac2af8 @assigns_added=nil, @assigns={},
    @helpers=#<ActionView::Base::ProxyModule:0x2aaab0ac2a58>, @controller=nil, @view_paths=[]>

>> foo.your_helper_method(args)
=> "<html>created by your helper</html>"

SNMP and Ruby

Wednesday, February 27th, 2008

Working with SNMP can be challenging. If you are a Newbie to SNMP and are trying to do something a little unusual (Writing a Ruby agent that sends snmp traps) you probably have trouble finding examples. So I’ve been fighting SNMP for a few days and here are some notes.

Before you start writing ruby code make sure you can send and recieve snmp messages. In my case I was working with traps. This is how I got it working in Ubuntu.

  1. Install net-snmp (This is the snmp library and its daemons.) also install net-snmp-utils which is a lot of the command line tool. On Ubuntu <code> apt-get install net-snmp ; apt-get install net-snmp-utils</code> should work.
  2. On Ubuntu the /etc/default/snmpd file overrides a lot of the default snmp settings. On my version the SNMPDOPTS variable included ‘127.0.0.1′ as the address list. You can just remove this if you want to recieve snmp traps from other servers. Also change ‘TRAPDRUN’ to ‘yes’. This will start snmptrapd daemon.
  3. Try the following command.<code>snmptrap -v 1 -c public localhost 1.3.6.1.4.1.10300.1.1.1.12 localhost 3 0 ”</code> This should work. We are not using any mibs. Something should be written to your /var/log/syslog

If you still have trouble with snmp you can use tcpdump to make sure you are getting a udp message on port 162. I won’t get into that. Its just a sanity check.

Now for the ruby code. To send a version 1 message from ruby you need the snmp gem. So do your ‘gem install snmp’

Some sample code to do the same call


require 'snmp'
SNMP::Manager.open(:Host => "localhost",:Version => :SNMPv1) do |snmp|
x=snmp.trap_v1(
"1.3.6.1.4.1.10300.1.1.1.12",
'localhost',
:enterpriseSpecific, #Generic Trap Type
0,1234
)

end

Notice the :Host key in the Manager object. This is the address of the manager that you want to send the snmp message to. The localhost in the parameters is the ip or domain name of the agent that is sending the trap.

These are just some quick notes mostly for myself and for others who get stuck. It is probably not enough but there are so few examples of this out there on the net it should be useful.

Zed’s Rant.

Thursday, January 3rd, 2008

I just read Zed’s rant. (Zed is the developer of Mongrel, a recent & popular web server written in Ruby) Interesting stuff. Not just because he obviously has an Axe to grind but he does give a look at all the stuff going on behind the scenes with Ruby and Rails.

One item that caught my interest was the assertion that Japanese developers ignoring patch submissions from developers. Ruby does have some major performance issues. There is a reason there are at least 2 virtual machines (Rubinus and JRuby) being developed to compete with the current Ruby. And I do wonder if part of the problem is the (spoken) language barrier between the core team and a good chunk of the user market.

Maybe it is time for the language to branch.