Tuesday, August 5, 2008

Fedora + Ruby + Rails

  • yum install httpd mysql-server mysql mysql-devel
  • yum install ruby ruby-devel ruby-irb ruby-libs ruby-rdoc ruby-ri
  • download gem and install from source
  • gem install rails mongrel mechanize newgem
  • gem install -y mysql -- --with-mysql-config=/usr/bin/mysql_config

Monday, August 4, 2008

Parse XML with ruby

Use Hpricot:

# @my_xml contains my xml in a String object

doc = Hpricot::XML(@my_xml)

Now you can do regular Hpricot to extract the xml content. E.g.(doc/"//entry/title/text()")

Monday, July 28, 2008

Misc Handy Linux Commands

Create a new nic with the following IP. The netmask will restrict access to that subnet.
ifconfig eth0:0 172.16.41.13 netmask 255.255.255.0

Disable Firewall
iptables -F

Tuesday, May 27, 2008

HO-MADE PIES - YUM!


My boss went turkey hunting, and sent me this picture. He said he claims it is a real sign, and he even bought a t-shirt from them. He also mentioned waiters are only female.

Wednesday, May 7, 2008

Ruby + Rails + Debian

I tried a million different tutorials, and they all sucked, then my friend showed me the way.

  1. comment or delete the lines currently in /etc/apt/sources.list
  2. add the follow lines to /etc/apt/sources.list
    deb http://ftp.debian.org/debian/ etch main

    deb-src http://ftp.debian.org/debian/ etch main

    deb http://security.debian.org/ etch/updates main contrib

    deb-src http://security.debian.org/ etch/updates main contrib

    deb http://ftp.debian.org/debian/ testing main

    deb-src http://ftp.debian.org/debian/ testing main

    deb http://security.debian.org/ testing/updates main contrib

    deb-src http://security.debian.org/ testing/updates main contrib

  3. Run the follow command:
    apt-get update

  4. /etc/apt/apt.conf should contain:
    APT::Default-Release "stable";
    APT::Cache-Limit "16777216";

  5. run:
    apt-get -t testing install ruby1.8
    apt-get -t testing install ruby1.8-dev
    apt-get -t testing install ri1.8
    apt-get -t testing install rubygems
    ln -s /usr/bin/ruby1.8 /usr/bin/ruby
    ln -s /usr/bin/ri1.8 /usr/bin/ri

  6. install all the gems you want and use them!

All DONE!

Monday, May 5, 2008

run snmpd with perl or c modules

Make sure you run snmpd like so, when extending it with either perl or C modules:
snmpd -f -L -DnstAgentPluginObject,dlmod

haml and emacs

Download the haml-mode.el file here.

Then, do likewise with the ruby-mode.el file I showed you how to load before.

Rock on!

Ruby and Emacs!!

I usually use textmate, but when I am on Linux I am always frustrated, so I finally got emacs to recognize ruby!

Here is a link to where I got my ruby-mode.el file.

It does not matter where you put the ruby-mode.el file, just put it somewhere pertinent. Many people recommend:

$HOME/.emacs.d/ruby-mode.el


Then add the following to your .emacs file:

(load "~/.emacs.d/ruby-mode")


Now, when you open a ruby file you need to tell emacs that it is a ruby file, so open the buffer and type:
Meta-x ruby-mode


And everything should work

This is also a good reference on loading an el file in emacs.

Thursday, May 1, 2008

Extending Net-SNMP with Perl

I am the first to avoid perl, so much so that I've never really done any perl. However, I do have some good ruby experience, which was helpful.

You can extend Net-SNMP with C, but the 3rd party libs I was interfacing with were C++ and it just was not playing nicely. Furthermore, if you extend Net-SNMP with C you have to compile a shared lib, which isn't bad, but it must be compiled with gcc not g++, or you get warning, which is bad.

If you go the perl route you get all the functionality, but it way easier to code, and much easier to debug and test!

Here are a few road blocks I ran into using the examples and documentation provided by Net-SNMP.

This is the way I got setValue return either a OctString or Integer?
There are probably some constants defined but not exported or something, because their example will produce the error: "unknown var value type: 0"
so what I need is define two new vars:


my $ASN_OCTET_STR = 4;
my $ASN_INTEGER = 2;

then I called setValue() like so:


$result->setValue($ASN_OCTET_STR, $my_value);

or

$result->setValue($ASN_INTEGER, $my_value);


Ta-Da!! Now it works. So if you see that error you know what is going on and how to fix it.

5 Rails Tips

Here are 5 tips I think everyone should know, that I use regularly in all of my rails projects.

  1. associate_with_list
  2. change_column
  3. new.html.haml and edit.html.haml form.html.haml
  4. haml
  5. html + form.html.haml + make_resourceful

associate_with_list

Lets say I have a model called posts, and I have a tag for each post. Since I plan on using the same tag over and over again, I want to just have a drop down list of all the previous tags I've used. And I want any new tags I use to be added to the list. Furthermore, some models might have fields that use the same list as others.

This problem leads to a bunch of code duplication and messy table generation.

So I decided to create associate_with_list, which is a plugin that will manage all of these lists for you, but leaves the front end up to you to make as simple or fancy as you like.

First, get the plugin: http://www.tomonrails.com/docs/associate_with_list.tar.gz

After you've unzipped the plugin, and placed it in your /vendor/plugins directory, from you rails project root, run:

ruby ./vendor/plugins/associate_with_list/install.rb

This will create a migration file and run it. The migration file creates a table in your database which will manage all these lists.

Now the awesome part, tell associate_with_list to associate a column with a list. In your model you need to add the following line:
associate_with_list :tag

You can give it an optional second parameter for the name of list which is helpful if you need to manage the same list across different fields, potentially in different models, or you might want to make sure your list is distinct for other lists you want to create. (NOTE: this plugin will even work with accessors, not just ActiveRecord::Base fields)

To access my new list I now have a new instance method called tag_list which is the column_name + _list, and returns all the previous entries into the list. This is how you use it:


@post.tag_list


You can even pass this method a hash which is used just like a hash passed to ActiveRecord::Base's find method. So for instance if I wanted to define an order in which the list is returned:

 class="ruby">
@post.tag_list(:order => 'value')


So where is the magic happening? How are all the values getting stored into this list? The plugin defines an after_save filter which takes the value of the field, after it has been saved, and stores it into the list if it is not already there.

To delete an entry in the list just use the new instance method tag_list_remove which takes a parameter which is the value you wish to remove. E.g.:


@post.remove_from_tag_list 'jungle'


So to recap, lets create a new list for the posted_by field in my post model, and this time I want to define the list name, because I'll use the same list in another model.

post.rb

associate_with_list :posted_by, :author_list


Done!

Now I can use the generated methods posted_by_list and remove_from_posted_by_list to my hearts content.

UPDATE: Also if you wanted to just add values to your list you can use the dynamically generated method add_to_[column_name]_list, which takes a single parameter, which is the value you wish to store. So for instance in the previous example I could do:


add_to_posted_by_list 'tdoggy'


This would check to see if tdoggy was already added to the :author_list (remember we associated posted_by with the author_list), and add it if not.

haml + form.html.haml + make_resourceful

make_resourceful is an awesome plugin to get rid of more code duplication that is common between restfully built controllers. But how do you form.html.haml with make_resourceful? Simple:


make_resourceful do
actions :all

response_for :new, :edit do |format|
format.html { render :template => 'controller_name/form' }
end
end


Haml, form.html.haml, and make_resource are all great but it gets old generating a scaffold or controller and now you have to go convert all the files to haml, delete new and edit files and create form.html.haml, and delete your controller code to replace it with make_resourcefull. With that much editing it hardly makes using the scaffold generator worthwhile.

This is why I created my_sleek_rails_generators. Which has all the same functionality as rails scaffold and controller generators. You even use the respective generators with the same syntax on the command line.

You can can get the plugin here: http://www.tomonrails.com/docs/my_sleek_rails_generators.tar.gz

Now you have a scaffold generator that you can actually enjoy using without a whole bunch of code re-write.

NOTE: I plan to keep updating this plugin with more generators for different things, so make sure to keep it updated for the latest and greatest generators.

new.html.haml and edit.html.haml form.html.haml

More often than not, new.html.haml and edit.html.haml are virtually identical. Well this is very un-rails-ish. So instead I delete new and edit views, and replace them with a single form view.

You can then use the following in your controller's new and edit methods to make them both use the form.html.haml template instead of their respective defaults:

render :template => 'controller/form'

Before rails 2.0 this would not have been a very elegant thing to do, but with the new form_for method, it assumes a restful implementation and will therefore submit to the appropriate create or update action. So without any extra code you can elegantly have just one form for both new and edit.

haml

Many have heard of and started using haml already, but since this is my list for 5 tips for rails, I cannot leave this out.

It is very simple to install and use haml, like most things ruby and/or/on rails. There are a couple of things that took me a few minutes that I would like to show quickly.

  • To install the haml gem:
    sudo gem install haml
  • Get it working on your rails project after installing the gem:

    cd /path/to/rails_project
    haml --rails .

Here is a quick haml demo to create the layout


!!! XML
!!! STRICT
%html
{html_attrs}
%head
%title
Application:
=controller.action_name
%body
-if is_admin?
You are admin
%br{:clear => 'all'}

#main
=yield


Some things to note:
  • erb uses <%= ... %> and <%- ... %> haml just uses = and - respectively.
  • haml does NOT use end tags of any kind, it only looks at the spacing/tabs (tab = 2 spaces, and yes you must use spaces). This is easily seen with the if statement. the yield is not part of the if block because it is aligned with the if, not tabbed under it.
  • The braces beside a %tag, for instance with %b, constitue a hash that allows you to set the properties of the tag. You can set styles, ids, or anything else you want
  • #name creates a div with id = name
  • .name create a class with id = name


Why is this cool? It is faster to write, easier to for designers to look at it. Furthermore, erb will not tab the views inside of a layout or partials inside of a view to line up with the rest of the document. Haml does, and this make it so much easier to look at the read.

for more on haml go to http://haml.hamptoncatlin.com

change_column

I know this might seem basic, but I did not know that for many of the operations change_column will retain the data in the column.

Here are some of the thing change_column can do without loosing data:
  • changing or setting default values
  • changing the name of the column
  • you can even change the column data type for certain data type mappings
Remember that it might make a difference as to which db you're using. I did my tests on a sqlite3 db.

Friday, April 25, 2008

snmp manage load

snmp will monitor the load over a 1, 5, and 15 minute average.

e.g.:

load 5 6 7

This tells the load should be under 5 for 1 min ave
This tells the load should be under 6 for 5 min ave
This tells the load should be under 7 for 15 min ave

snmp manage proc

Add proc monitoring to snmpd.conf:

proc my_proc_name [max_instances] [min_instances]

This will monitor dbid:
proc dbid

This will make sure dbid has no more than 3 instances
proc dbid 3

This will makesure dbid has not more than 3 and no less than 1 instance
proc dbid 3 1

snmp manage disk

Add disk monitoring to snmpd.conf:

disk /
disk /boot
disk /usr

Thursday, April 24, 2008

snmp remote gui browser

Normally I am all about command line, but if you need to use windows I really recommend using SNMP-Probe. It has a 10 day trial and is only 15 USD for a license. It is really nice for us snmp noobs.

snmp config setup for snmpd

Snmp is really cool, but seems to difficult to setup. I kept getting a "Timeout: No Response from localhost" when I would try to run snmpget to query the mib. I checked the snmpd log and it said that conf was probably bad and that snmpd would probably not be very useful.

I then stumbled on:
snmpconf -g basic_setup

If you're a noob to , like me, this will probably save your bacon. I just followed in guide. Oh btw, you probably want to make a community name of public. This will be helpful when following tutorials that will often use the -c option and assign the value public. e.g. snmpget -v 1 -c public localhost SNMPv2-MIB::sysUpTime.0

Btw, remember to move the snmpd.conf file to /path/to/snmp/share/snmp/