Matching Values in a Perl Array

April 5th, 2010

Here’s some code to search for the existence of a value (in this case “ryan”) in a Perl array.

#!/usr/bin/perl

@array=("ryan","was","here");
print ryan_in_array(@array); 

sub ryan_in_array { ($_ eq "ryan") && return 1 for @_; 0 }

The subroutine is modified from http://perldoc.perl.org/List/Util.html.

Discussion:

In this “trivial” code, Graham Barr, uses a few tricks: (1) The && effectively works as an “if true” statement. Hence, if $_ is equal to ryan, then we perform the command “return 1″. (2) The “; 0″ works as an “else” statement. Our subroutine will return “0″, unless, of course, we successfully match “ryan”. Then it’s superseded by the “return 1″ call.

How to Create SNMP Test Trap on Linux

October 19th, 2009

I wanted to test Nagios‘ ability to alarm on an SNMP trap.  So I needed a good way to trigger an SNMP trap without, say… turning on and off a Cisco switch.

This document basically stitched together an example from the Net-SNMP tutorial and the Net-SNMP FAQs to create a step-by-step guide.  SNMP has a lot of technical jargon, but this how-to should work without knowing too many terms.

Creating the SNMP Trap

You’ll need root access to at least one Linux server (preferably two servers for proper testing) to complete this exercise.  Enjoy!

1) Install Net-SNMP and its tools, start the daemon and make sure it works

CentOS_5# yum install net-snmp-utils
CentOS_5# /etc/init.d/snmpd start
CentOS_5#snmpwalk -v 2c -c public localhost system

The last line should spew out a number of lines that look like this:

NMPv2-MIB::sysORDescr.1 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.3 = STRING: The MIB module for managing IP and ICMP implementations

2)  Create your MIB definition.  Drop this file into your MIBs directory, likely in /usr/share/snmp/mibs.

CentOS_5# cat /usr/share/snmp/mibs/TRAP-TEST-MIB.txt

TRAP-TEST-MIB DEFINITIONS ::= BEGIN
IMPORTS ucdExperimental FROM UCD-SNMP-MIB;

demotraps OBJECT IDENTIFIER ::= { ucdExperimental 990 }

demo-trap TRAP-TYPE
STATUS current
ENTERPRISE demotraps
VARIABLES { sysLocation }
DESCRIPTION "This is just a demo"
::= 17

END

3) Load the MIB.
CentOS_5# export MIBS=+TRAP-TEST-MIB

4) Run the snmptrap command to send a trap to your monitoring host. (Replace monitoring_host with the appropriate hostname).

CentOS_5# snmptrap -v 1 -c public monitoring_host TRAP-TEST-MIB::demotraps localhost 6 17 '' SNMPv2-MIB::sysLocation.0 s "Ryan was just here"

How to Verify your SNMP Trap

1) Run tcpdump on your monitoring host to see whether you’re seeing the host.  Note that an aggressively filtering firewall or SELinux may prevent your packet from showing up.

monitoring_host# tcpdump host CentOS_5
13:49:03.914746 IP CentOS_5.twosmallcoins.com.56970 > monitoring_host.snmptrap:  Trap(64)  E:2021.13.990 127.0.0.1 enterpriseSpecific s=17 33361154 [|snmp]

2)  Save the file and look at it in Wireshark (formerly known as Ethereal), to see the detailed packet.

mointoring_host# tcpdump -w ryan_test.dmp -s 0 host CentOS_5

This command will save it to the ryan_test.dmp file, so you can analyze it later.  If you look closely, you can see the part of the packet that says “Ryan was just here”.

How to Convert MySQL from MyISAM to InnoDB Using a Script

September 10th, 2009

Okay.  So I need to convert MySQL database using the default MyISAM engine to the InnoDB engine.  The command is simple:

ALTER TABLE table_name ENGINE = InnoDB;

However, this command needs to be done for EVERY table.  Yes, it’s a pain in the rear…  so here’s a way to do it by scripting.

Scripting a MySQL InnoDB Engine Conversion

0) Backup your database. You should probably be doing this already.  Now’s a good time to make sure that your backups ran.

1) Create the script. You’ll need the correct permissions to query the database. Here’s the command.  Be sure to change <DATABASE_NAME> as it fits.

Linux$ mysql -p -e "show tables in <DATABASE_NAME>;" | tail --lines=+2 | xargs -i echo "ALTER TABLE {} ENGINE=INNODB;" > alter_table.sql

2) Run the script.

Linux $ mysql --database=<DATABASE_NAME> -p < alter_table.sql

3) Verify it by running this command in mysql:

mysql> show table status;

Discussion (If You’re Interested)

The script is simply a bunch of ALTER TABLE commands for each of your tables in the database of question.

The mysql -p -e “show tables in <DATABASE_NAME>” command gets a list of the tables.  This list has a header that looks like “Tables_in_DATABASE”.  The tail command drops that first header line.  Now you have a clean list of tables.  The xargs -i echo command creates the ALTER TABLE command, inserting the table name where the squiggly braces {} are placed.   It’s now stored in alter_table.sql.

Step two simply runs the script that we stored in alter_table.  Both mysql commands will prompt for a password, based on the -p flag.

389 Directory Server Install Error: Exception in thread “main” java.lang.Error: Probable fatal error:No fonts found

September 9th, 2009

I just installed the open source LDAP software, 389 Directory Server, on a CentOS 5.3 server by following these very good instructions.  However, the last line stated

CentOS# 389-console -x nologo

     Exception in thread "main" java.lang.Error: Probable fatal error:No fonts found.
     at sun.font.FontManager.getDefaultPhysicalFont(FontManager.java:1088)
     at sun.font.FontManager.initialiseDeferredFont(FontManager.java:960)
     at sun.font.FontManager.findOtherDeferredFont(FontManager.java:899)
     at sun.font.FontManager.findDeferredFont(FontManager.java:916)
     at sun.font.FontManager.findFont2D(FontManager.java:1904)
     at sun.font.FontManager.getDefaultPhysicalFont(FontManager.java:1071)
     at sun.font.FontManager.initialiseDeferredFont(FontManager.java:960)
     at sun.font.FontManager.findOtherDeferredFont(FontManager.java:899)
     at sun.font.FontManager.findDeferredFont(FontManager.java:916)
     at sun.font.FontManager.findFont2D(FontManager.java:1904)
     ...

Dozens of un-informative commands continued.  (Dear Java programmer:  do we really need such verbose error messages?  Really?)

The Solution

Fortunately the solution for me was relatively easy.  I had installed CentOS in text mode, without X-server.

1) Installed X-server.  On CentOS, installing X is quite easy:

CentOS# yum groupinstall “X Window System”

Followed the prompts.

2) Logged off,  SSH’ed back in with X forwarding, and launched GUI.

Cygwin$ ssh -X root@CentOS
Warning: No xauth data; using fake authentication data for X11 forwarding.
CentOS# 389-console -x nologo

3) Celebrated with a cup o’ java. Hehe.  Okay… it’s really not that funny.

How to Upgrade Sun Grid Engine (SGE) and Migrate to New Server

August 26th, 2009

Sometimes you want to upgrade software and migrate hardware at the same time. If you want to do that with SGE, then you’re looking in the right place.

My architecture: old server SGE 6.1u2 on CentOS 5, migrating to SGE 6.2u3 on CentOS 5.3.

Upgrade Procedure

1) Download SGE onto the new server. If you’re feeling farsighted, fill out the Planning Checklist.

2) Unzip, untar, an set $SGE_ROOT to your untar’ed folder.

OldCentOS# export SGE_ROOT=/directory/to/sge/

Note: You may consider putting SGE root in an NFS directory in case you want to create a “high availability” fail-over environment. NFS may very well slow you down.

3) Find save_sge_config.sh and copy it over to the old host.

4) Create a copy of your configuration using save_sge_config.sh

OldCentOS# mkdir sge_config_folder
OldCentOS# /path/to/save_sge_config.sh sge_config_folder

5) Copy over your config folder

6) Edit save_config_folder/cell/qmaster and change the old hostname (OldCentOS) to the new hostname (NewCentOS). Otherwise you’ll get an error like this:

Upgrade must be started on a qmaster host!

7) Run upgrade

NewCentOS# $SGE_ROOT/inst_sge -upd

8) Follow prompts. This is when you should reach for that planning checklist from step 1.

9)  Be patient. Depending on the size your configuration, certain portions might take a long time.

10) Check your install

NewCentOS# ps -ef | grep sge
NewCentOS# qstat -f

How to Prepare AIX 6.1 for Oracle Install

June 22nd, 2009

First off, I’m not an Oracle DBA. But here are some of the things I did upon my DBA’s request, so that he can install Oracle.

1) Add user oracle and group dba. Edited the following files:

  • /etc/passwd
  • /etc/group
  • /etc/security/passwd

2) Edited the default ulimit and associated parameters.  In /etc/security/limits:

default:
fsize = -1
core = 2097151
cpu = -1
data = -1
rss = 65536
stack = 65536
nofiles = 20000

root:
fsize=-1
stack=-1
rss=-1

oracle:
fsize=-1
stack=-1
rss=-1

Hmm… I guess I am really trusting of the DBA.  At least this box is dedicated to Oracle.

3) Edit the kernel parameters. In /etc/tunables/nextboot, added the following:

vmo:
minfree = “1500″
maxfree = “2100″
minperm% = “5″
lru_file_repage = “0″

ioo:
j2_maxRandomWrite = “128″
j2_nPagesPerWriteBehindCluster = “128″
j2_nRandomCluster = “32″

4) Installed rsync.

5) Expanded /tmp and /oracle/home as needed.

AIX61# df | grep mnt

My /tmp file system was using /dev/hd3. Look at it and extend if needed. Here I extend by 1 physical partition (PP), which is 128 MB. Then I extend the /tmp file system too.

AIX61# lslv hd3
AIX61# extendlv hd3 1
AIX61# chfs -a size=+128M /tmp

6) Installed the fileset, rstc.basic, which is required by Oracle 11g.  Mounted AIX6.1 DVD on /mnt and ran this installp command:

AIX61# installp -ag -d /mnt/installp/ppc/ rsct.basic

How to install SSH on AIX 6.1 and turn off telnet

June 14th, 2009

One of my biggest gripes about AIX is how security un-conscious it is.  SSH now “comes with” AIX, but it’s on a separate cd.  Anyway, enough griping.  Here are the procedures.

Installing OpenSSH on AIX 6.1

1) Obtain the files. It’s downloadable here: http://sourceforge.net/projects/openssh-aix/.  Or you can find it on the expansion CD.  Or the Linux toolkit for AIX CD.

2)  Install.  If you’re using a CD, follow my directions for installing lsof from here (of course, replace openssh with lsof).  Otherwise, I have some installp examples here.

3) Start your SSH.

AIX61# startsrc -g ssh

4) Edit your /etc/hosts.allow file. Otherwise you’ll see an error that says this:

ssh_exchange_identification: Connection closed by remote host

5) Test it out.

Linux# ssh AIX61

How to Disable Telnet on AIX 6.1

1) Comment out telnet from /etc/inetd.conf.

2) Reload the configuration file

AIX61# refresh -s inetd

3) Test. You should get a nice error message like this:

Linux# telnet AIX6.1
Trying 10.1.1.10…
telnet: connect to address 10.1.1.10: Connection refused
telnet: Unable to connect to remote host: Connection refused

How to Add a Disk on AIX Logical Volume Manager (LVM)

May 12th, 2009

AIX, for all of its strange mainframe-ish ways, has some redeeming qualities. AIX’s disk management system would be one of them. It’s quite sophisticated given that it’s bundled in at no additional cost. But I digress.

For this “how-to”, I’ll walk through a simple scenario of adding a disk. In my case, it was adding a SAN disk on a DS4800 system. But this should work for a regular ol’ hard drive too.

1) Add the physical disk.

2) Rescan your hardware so that the OS is aware of your new disk.

AIX# cfgmgr

3) Check to see your disk. For the purpose of this example, let’s say the new disk is hdisk2.

AIX# lsdev -Cc disk
AIX# lspv

4) Associate your new disk to a volume group. In this case, let’s create a new group called ryanvg and put hdisk2 in there.

AIX# mkvg -y ryanvg hdisk2

5) Now you can look at the size of hdisk2. (This command won’t work if it’s not associated with a volume group).

AIX# lspv hdisk2

6) Create a log logical volume for jfs2. This needs to be part of ryanvg. Note: in the example below, the type is jfs2log and we’re giving it 1 physical partition (PP).

AIX# mklv -t jfs2log ryanvg 1

7) Look for your new logical volume (lv). Chances are that AIX named it loglv00.

AIX# lsvg
AIX# lsvg -l ryanvg

8) Create your production logical volume. Let’s make it, hmm… how about 30GB? At the risk of sounding narcissistic, I’ll name it ryanlv.

AIX# lsvg ryanvg
AIX# mklv -t jfs2 -y ryanlv ryanvg 30G

9) Lay down your file system on ryanlv.

AIX# mkfs -o log=/dev/loglv00 -V jfs2 /dev/ryanlv

10) Mount your filesystem.

AIX# mkdir /mountpoint
AIX# mount -o log=/dev/loglv00 /dev/ryanlv /mountpoint

11) Consider adding to /etc/filesystem, if everything comes up fine

How to Troubleshoot GRUB after Fedora Upgrade

May 3rd, 2009

I’ve recently upgraded my Fedora box from 7 to 10.  It all went quite well, except upon reboot.  I expected to see my nice GRUB menu, allowing me to select which kernel to boot.  Instead I got the command line prompt:

grub>

ugh.  Now what?

How to boot from GRUB command line

First things first.  Try to boot.  Then fix the menu.

0) Orient yourself.  Fortunately my version of grub (0.97) provides a nice tab completion feature.  This allows me to identify both commands and files.

1) Check your root disk.

grub> root
grub> cat /<TAB>

2)  If you can see files, you’re off to a good start.  Skip to step 3.  Otherwise you may need to search around for your root disk.  Try iterations of this:

grub> root (hd0,0)
grub> cat /<TAB>

Go along replacing (hd0,0) with (hd0,1) … (hd0,5).  Then try (hd1,0) and so forth.  For those familiar with Linux device naming schemes, (hd0,0) is the same as sda1 (or hda1).  In GRUB, (hd0,1) would be sda2 and (hd1,0) would be sdb1.  When you find a disk that you can read, go onto step 3.

3) Load your kernel and initrd.  Then boot.

grub> kernel /vmlinuz<TAB>
grub> initrd /initrd<TAB>
grub> boot

If you can’t find these files, look around with “cat /<TAB>”.  Try the /boot directory.

Upon successfully booting, go ahead and test your menu.lst file.

How to check your menu.lst file

Your menu.lst file holds the configuration for your menu.  It’s located in /boot/grub/menu.lst, and it’s often linked to grub.conf in the same directory.

grub> configfile /grub/menu.lst

This worked for me.

So, why doesn’t my menu.lst file load automatically?

Remember how I mentioned that my configuration file is located in /boot/grub/menu.lst?  My /boot directory is in a separate partition from my root (/) partition.  So upon mounting (hd0,2), which is where my /boot partition’s located, my config file is actually in a different place: /grub/menu.lst.  There’s no /boot directory anywhere.

Apparently, /boot/grub/menu.lst is hardcoded.  No problem. The solution is simple.  Boot the machine, and in the shell:

Fedora10# cd /boot
Fedora10# ln -s . /boot

I rebooted, and voila! Pretty menu once again!