Value driven web development

Acts_as_ferret is a great plugin for adding search capability to your active records. If you use the default setup, you might find that ferret does not return some expected results. For example, if you have an article:

1
2
3
4
5
6
7
8
9
10
11
12
article: 
  id: 1
  title: this is an article

class Article < ActiveRecord
  
  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} )

end

Article.find_by_contents(“article”)

return 1 result

Article.find_by_contents(“this is the article title”)

return 0 result

How to solve this?

Omit Stopword

The default analyser will remove common stop-words like “and”, “the”, “a” and “for”. You can create a new StandardAnalyzer that doesn’t remove those stopwords.

1
2
3
4
5
6
7
8
class Article < ActiveRecord
  
  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} ,  {:analyzer => Ferret::Analysis::StandardAnalyzer.new([nil]) } )

end

PerFieldAnalyzer

Using the way above will include all the stopwords in every field, that is usually not what you want. What you need is to use PerFieldAnalyzer and only omit stopwords for title.

From the mailing list archive, there’s reported problem with the C version of PerFieldAnalayser. “Thanks to Ben from omdb.org for tracking this down and creating this workaround. You can read more about the issue there: blog.omdb-beta.org/2007/7/29/tracking-down-a-memory-leak-in-ferret-0-11-4”

Save this file as per_field_analaysis.rb in /lib

http://pastie.caboo.se/83194

1
2
3
4
5
6
7
8
9
10
11
class Article < ActiveRecord
  
  pfa = PerFieldAnalyzer.new( Ferret::Analysis::StandardAnalyzer.new )
  pfa[:name] = Ferret::Analysis::StandardAnalyzer.new([])

  acts_as_ferret( {:fields => {
    :name => {:boost => 3 },
    :published_at_for_sort => {:index => :untokenized_omit_norms, :term_vector => :no} 
  }, :remote => AAF_REMOTE} ,  {:analyzer => pfa } )

end

Nagios 4: notify by twitter

November 5th, 2007

Install Net::Twitter (perl version)

download from search.cpan.org

http://search.cpan.org/~cthom/Net-Twitter/

1
2
3
4
5
6
tar zxvf Net-Twitter-1.06.tar.gz
cd Net-Twitter-1.06
perl Makefile.PL
make
make test
sudo make install

Install other perl package if required(HTML-Tagset, libwww-perl, JSON-Any)

download them from http://search.cpan.org/

The script for twitter notify( change the user_name and password for twitter )

vi /usr/local/nagios/libexec/twitter.pl

1
2
3
4
5
6
7
8
9
#!/usr/bin/perl -w

use strict;
use Net::Twitter;
my $msg = shift;
my $twit = new Net::Twitter(username => "yourname",password => "yourpass");
if (defined($msg) && $msg !~ /^\s*$/) {
  $twit->update($msg);
}

sudo chown nagios:nagios twitter.pl

sudo chmod +x twitter.pl

send the test message to twitter

/usr/local/nagios/libexec/twitter.pl ‘hello world’

Add new command for notify in nagios

sudo vi /usr/local/nagios/etc/objects/commands.cfg
1
2
3
4
define command {
        command_name notify-by-twitter
        command_line /usr/local/nagios/libexec/twitter.pl "$NOTIFICATIONTYPE$ -$HOSTNAME$-$SERVICEDESC$ - $SERVICESTATE$ - $SERVICEOUTPUT$"
}

Then in the contacts.cfg, use this:

service_notification_commands notify-by-twitter

Reference: Twitter as Nagios notification gateway

Nagios 3: Install nrpe

November 5th, 2007

“NRPE: Nagios Remote Plugin Executor allows you to execute local plugins on remote hosts.”

1, Add user and group named ‘nagios’

/usr/sbin/useradd nagios

passwd nagios

2, compile nagios-plugin

download: http://sourceforge.net/project/showfiles.php?group_id=29880
1
2
3
4
5
6
7
8
tar zxvf nagios-pluginxxx.tar.gz
cd nagios-plugin
./configure
make
make install

chown nagios:nagios /usr/local/nagios
chown -R nagios:nagios /usr/local/nagios/libexec

3, compile nrpe

download: http://sourceforge.net/project/showfiles.php?group_id=26589
1
2
3
4
5
6
7
tar zxvf  nrpexxx.tar.gz
cd nrpe
./configure
make all
sudo make install-plugin
sudo make install-daemon
sudo make install-daemon-config

4, Start nrpe

vi /usr/local/nagios/etc/nrpe.cfg

allowed_hosts=127.0.0.1,123.12.1.2

123.12.1.2 is your nagios server address

start nagios:

/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

Check the nrpe on nagios server

/usr/local/nagios/libexec/check_nrpe -H 123.12.1.5

123.12.1.5 is the ip adress of the nrpe deamon(the remote pc)

You should see something like:

NRPE v2.9

Congratulate! your nrpe works now.

Nagios 2: config the server

November 5th, 2007

1, Apache

modify the apache configuration file, httpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ScriptAlias /nagios/cgi-bin/ /usr/local/nagios/sbin/

<Directory "/usr/local/nagios/sbin/">
    AllowOverride AuthConfig
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>

Alias /nagios/ /usr/local/nagios/share/

<Directory "/usr/local/nagios/share">
    Options None
    AllowOverride AuthConfig
    Order allow,deny
    Allow from all
</Directory>

2, Access control

1
2
3
4
5
6
7
8
9
10
11
12
13
/usr/local/nagios/share
vi .htaccess
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/.htpasswd
require valid-user

cd /usr/local/nagios/sbin
vi .htpasswd
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/.htpasswd
require valid-user
3, generate the username and password for web login

usr/local/apache2/bin/htpasswd -c /usr/local/nagios/etc/.htpasswd nagios
If you want to add another user, use the command:

/usr/local/apache/bin/htpasswd  /usr/local/nagios/etc/.htpasswd user2
4, check the configuration file of apache

/usr/local/apache2/bin/apachctl -t

5, start setup nagios

generate the blank config file
1
2
3
sudo chown -R nagios:nagios /usr/local/nagios
cd /usr/local/nagios/etc/objects/ 
touch contactgroups.cfg contacts.cfg hostgroups.cfg hosts.cfg services.cfg timeperiods.cfg
link the config file to nagios
1
2
3
4
5
6
7
8
vi nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg         
cfg_file=/usr/local/nagios/etc/objects/services.cfg

and then, you can setup the contacts, hosts, services based on the demo.

After this, check the configuration:

sudo /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Start the nagios if there is no error:

sudo /etc/init.d/nagios restart

visit the site http://example.com/nagios/.

Nagios 1: install the server

November 2nd, 2007

Firstly, add new user and group named ‘nagios’

For linux

  • groupadd nagios
  • useradd -g nagios-M nagios (-M do not create HOME directory for this user)

For mac

  • Add a new user and group ‘nagios’ with System Preference
1
2
3
4
5
6
7
zlib setup
wget http://downloads.sourceforge.net/libpng/zlib-1.2.3.tar.gz?modtime=1121680730&big_mirror=0
tar zxvf zlib-1.2.3.tar.gz
./configure
make 
sudo make install
sudo ranlib /usr/local/lib/libz.a
libpng setup
1
2
3
4
5
6
7
wget http://downloads.sourceforge.net/libpng/libpng-1.2.18.tar.gz?modtime=1179259677&big_mirror=0
tar zxvf libpng-1.2.18.tar.gz
cd libpng-1.2.18
./configure  #maybe cp ./scripts/makefile.darwin ./makefile to get the make file
make 
sudo make install
sudo ranlib /usr/local/lib/libpng.a
libjpeg setup
1
2
3
4
5
6
7
wget ftp://ftp.uu.net/graphics/jpeg/jpegsrc.v6b.tar.gz
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b/
./configure
make 
sudo make install-lib 
sudo ranlib /usr/local/lib/libjpeg.a
freetype setup
1
2
3
4
wget http://nchc.dl.sourceforge.net/sourceforge/freetype/freetype-2.3.5.tar.gz
tar freetype-2.3.5.tar.gz
freetype-2.3.5
./configure && make && sudo make install
GD lib setup
1
2
3
4
5
6
7
wget http://www.libgd.org/releases/gd-2.0.35.tar.gz
sudo ln -s /usr/X11R6/include/fontconfig /usr/local/include
tar zxvf gd-2.0.35.tar.gz
cd gd-2.0.35/
./configure
make && sudo make install
./gdtest test/gdtest.png   #test GD
Nagios setup
1
2
3
4
5
6
7
8
In System Preferences, create a new user called nagios. This will, under Panther, also create the nagios group.
http://sourceforge.net/project/showfiles.php?group_id=26589
sudo mkdir /usr/local/nagios
./configure --with-gd-lib=/usr/local/lib --with-gd-inc=/usr/local/include --prefix=/usr/local/nagios --with-cgirul=/cgi-bin/ -with-htmlurl=/ --with-nagios-user=nagios --with-nagios-group=nagios
make all
sudo make install
sudo make install-commandmode
sudo make install-config
Install plubin
1
2
3
http://sourceforge.net/project/showfiles.php?group_id=29880
 ./configure prefix=/usr/local/nagios
make && sudo make install
Install imagepak-base
1
2
3
http://www.nagiosexchange.org/Image_Packs.75.0.html?&tx_netnagext_pi1[p_view]=104
tar zxf imagepak-base.tar.tar
sudo cp -r base /usr/local/nagios/share/images/logos/