Archive
Shinken thruk_server.pl compilation error fix
Heard about Shinken when searching for issues related with Nagios and decided to give it a try on my Ubuntu 10.10 (32bit) a try by following this script. Everything went well except then I try to start thruk (the http admin ui) I got the following error
ERROR: this is the wrong precompiled version, your archname is: i686-linux-gnu-thread-multi
BEGIN failed–compilation aborted at /opt/thruk/script/thruk_server.pl line 18.
By looking into the code, the perl script is expecting
/opt/thruk/local-lib/lib/perl5/i686-linux-gnu-thread-multi
but instead I got
/opt/thruk/local-lib/lib/perl5/i486-linux-gnu-thread-multi
So the fix can’t be simpler, simply do
cd /opt/thruk/local-lib/lib/perl5 ln -s i486-linux-gnu-thread-multi i686-linux-gnu-thread-multi
Something to watch out while using ‘%’ in crontab
% is a special character in crontab and it has to be escaped with another ‘%’ a ‘\’. I just found out through a fail-to-run crontab task:
Non-working version:
1 5 * * * /bin/bash /path/to/myscript.sh `date +%Y%m%d -d '5 days ago'`
When a crontab fails it will drop an email to user’s mailbox. So here are the relavant lines I found in the mailbox (via command mail):
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
With those error messages I googled a bit and changed the crontab to the following
working version:
1 5 * * * /bin/bash /path/to/myscript.sh `date +%%Y%%m%%d -d ’5 days ago’`
1 5 * * * /bin/bash /path/to/myscript.sh `date +\%Y\%m\%d -d '5 days ago'`
Speed up file copying between hosts
Here’s my test result with transferring a 1.8GB text file to a remote server:
1) scp data user@remote_server:/path/to
2 minutes 48 seconds
2) scp -C data user@remote_server:/path/to
2 minutes 14 seconds
3) rsync -z data user@remote_server:/path/to
1 minute 46 seconds
Method 1) doesn’t use compression at all.
Method 2) enables compression through scp’s -C option.
Method 3) uses rsync with -z (compression) and clearly this method provides the best performance.
Comparing huge text files with rdiff
I happened to need to compare two huge data files (plain texts), each of which is near 2GBs in size. When I use the good old diff program on a Centos with 2GB of Ram and 512MB of SWAP, pretty quickly I got stuck with this error: diff: memory exhausted.
A little googling pointed me to the following solution
Chopping lines with sed in Linux
1) Chop first 1 line from a file
sed -i '1d' filename
[ if need to chop more, simply change 1 to the number of lines to be removed ]
2) Chop last line from a file
sed -i '$d' filename
3) Chop first and last line together from a file
sed -i -e '1d' -e '$d' filename
Turning on/off display via Linux command line
Found the commands at http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/:
To turn off monitor:
xset dpms force off
To turn on, simply press a key, or move mouse/mousepad.
Diff text file remotely with local file
Found a pretty neat solution at http://www.genlinux.org/2009/04/remote-diff-in-linux.html for comparing remote and local files. For example, if I want to compare the file
remote_ip:~/myscript.sh with the local version, I no longer have to copy the remote file to a temporary location and do a local diff, instead, with the solution provided in the link above, I can simply do
ssh user@remote_ip 'cat ~/myscript.sh' | diff - path/to/local/myscript.sh
Running virtualbox Linux guests in headless mode
Sometimes it’s more convenient to start virtualbox Linux guests in headless mode and access them through ssh. This guide assumes the vm to be running in headless mode
1) has ssh server installed
2) uses static ip address or dhcp but ip address is known and doesn’t change
To run vb guest in headless mode, simply run the following command
VBoxHeadless -startvm vm_name&
To ensure vm to be accessible, its network setting under VirtualBox Manager needs to be set to Bridge mode and use static ip address (or dhcp if the ip doesn’t change upon reboots) for the vm. This needs to be done before the above command is run.
Wait for some moments after the headless command is issued, start pinging the vm to check if the vm is up by its ip (or use telnet vm_ip 22 if PING is blocked on the vm). SSH to it should be a piece of cake now.
If you are like me who doesn’t even want to type the command ssh (Yup I wrote a one-letter bash function just to wrap the ssh call to some machines that I use on a daily basis) , you might want to turn the VBoxHeadless command into a function in bash:
1) vim ~/.bash_profile
2) append the following
function headless {
VBoxHeadless -startvm $1&
}
headless() {
[ $# -lt 1 ] && echo "Usage: $FUNCNAME vm_name" && return
VBoxHeadless -startvm $1&
}
3) . ~/.bash_profile
4) headless vm_name
Notes: Step 3 just needs to be run once right after the function is added to your bash profile. Before step 4 is run please make sure the vm is not already running.
Words filtering with simple unix commands
Let’s say I have a dictionary file named allwords containing sorted words with one word per line and I also have a collection of “offensive” words in a file call badwords (also one word per line but not sorted). Now I want to take out all the offensive words listed in badwords from the original dictionary file. With Unix this task becomes a piece of cake
$ sort -u badwords > badwords.sort
$ comm -23 allwords badwords.sort > goodwords
The first command can be omitted if badwords is already sorted. In order to get the result as expected, both files need to have the same line terminators (which can be found out easily with command file). There are a number of ways to convert CRLF terminated text to *nix format, here’s my favorite:
$ perl -pi -e 's/\r\n/\n/g' input.file
[ credit goes to http://www.cyberciti.biz/faq/howto-unix-linux-convert-dos-newlines-cr-lf-unix-text-format/ ]
Useful use of pgrep
To list the PIDs of all running process_name, instead of
ps ax|grep process_name|grep -v grep|awk '{print $1}'
do
pgrep process_name
To kill all running process_name, instead of
for p in `ps ax|grep process_name|grep -v grep|awk '{print $1}'`; do kill $p; done
do
pkill process_name