Monday, December 15, 2008

logging errors with erlang and mochiweb

If you would like to run mochiweb or any erlang application and log messages to disk to review later, all you have to do is create a simple config file and wrote something in it like as follows:

[{sasl, [
%% minimise shell error logging
{sasl_error_logger, false},
%% only report errors
{errlog_type, error},
%% define the parameters of the rotating log
%% the log file directory
{error_logger_mf_dir,"/var/logs/error_logs"},
%% # bytes per logfile
{error_logger_mf_maxbytes,10485760}, % 10 MB
%% maximum number of
{error_logger_mf_maxfiles, 10}
]}].

let's say we call this filename erlang_log.config. The trick here is that we have to create the log directory for erlang and make sure it's writable by the user which runs erlang process.
# mkdir /var/logs/error_logs/
you can also make it world writable if you don't care about who reads it on the server.
# chmod 777 /var/log/error_logs/

now pass the config file as parameter to your erl command (on mochiweb part, you can edit start.sh or start-dev.sh file).

erl -config erlang_log

when you first run your erlang process or mochiweb, first of all run on your erlang command prompt
rb:start().
to create index files in log directory and load rb module.Here is some useful commands to see log messages created by erlang. The log files in the directory are binary so you cannot simple read them.You have to read them through erlang (rb module).
rb:list().
%% shows log messages index
rb:show(number).
%% shows given indexed numbered message details
rb:rescan().
%% re-reads the error log directory index.

running mochiweb or any erlang application in background

Erlang is Ericsson's concurrent programming language.See Erlang Wikipedia for more information. Mochiweb is a web framework to create lightweight http servers written in Erlang language.
After creating a skeleton project with mochiweb (./scripts/new_mochiweb.erl). You can write your own code and compile it. Here is an article shows this in detail.
After all, running your new http server is done by star.sh or start-dev.sh in your application directory where you created by new_mochiweb.erl script. The script runs erl (The Erlang Emulator) command with some parameters. But it runs in foreground, if you want to run the erlang application in background simply add "-detached" parameter to your erl command. But, there is a drawback here. The erlang application runs in background and we cannot see the error and any information log messages now. I'll will explain how to log theses messages in log file and read them in another post.

Tuesday, November 25, 2008

profiling mysql queries

Mysql database server has a new query profiling option. It's a very useful feature to profile queries on server and find bottlenecks.
There is an article on mysql site which describes this feature in detail.
Using the New MySQL Query Profiler

Thursday, November 6, 2008

finding backup superblocks on ext2 and ext3 filesystems

If you have a situation where your ext2 or ext3 filesystem's primary superblock is corrupted, you need to pass one of the backup superblocks to fsck program. In this case you can find the backup superblocks by using mkfs program. All you have to do is, run mkfs with "-n" option, where mkfs is run in dry mode. It will not destroy your filesystem, it only prints out what it will do on real mkfs procedure.By the way, you have to add the parameters if you have used when your filesystem originally created to get correct values.

eg. mke2fs -n /dev/sda1

and now you can pass your backup superblock number to fsck (fsck.ext2,fsck.ext3) with "-b" parameter.

freeing cache on linux with drop_caches

Linux kernel 2.6.16 and later has a option which let's you free inode/dentry and pagecache caches on your system. The file is located on /proc filesystem.
It has 3 options. For example:

# echo 1 > /proc/sys/vm/drop_caches

frees pagecache on your system.

# echo 2 > /proc/sys/vm/drop_caches

frees inodes and dentry caches

# echo 3 > /proc/sys/vm/drop_caches

frees pagecache and inode/dentry caches.

tip: some dirty caches are not freeable so it's better to use "sync" command before using drop_caches for freeing much more memory on your linux system.

Wednesday, October 29, 2008

disabling bell beep sound on linux console

Sometimes its annoying to hear a beep sound everytime you hit on tab button for completion on linux console. Here is the quickway how to disable that beep sound.
All you have to do is add the following line in /etc/inputrc file or uncomment it if it presents.

set bell-style none

the following image is the default inputrc file on centos system. You can uncomment the line which says "set bell-style none" for disabling tab completion beep sound.

Monday, October 27, 2008

resolving latency problems on linux with latencytop utility

Finding out what is causing unresponsive behaviour, audio delays or stalls on your linux machines can be a hard task.
Latencytop utility gives information where latency is occured to diagnose problem.

Here is an example how to use latencytop on fedora core 9 distribution.

yum install latencytop


By default kernel does not enable latency statistics reporting.You have to enable it by hand.


echo 1 > /proc/sys/kernel/latencytop

this command enables it. disabling it is easy by echo 0 > /proc/sys/kernel/latencytop
latencytop uses the statistics from proc file system. statistics is in /proc/latency_stats file.

here is a screenshot where it shows latency caused disk io on ext3 filesystem and with which pid caused it.





note: if your linux system does not have /proc/latency_stats file. it's either is disabled when kernel is compiled or your kernel does not have latency stats support. In that case you have to patch your kernel an recompile it.
for patch see http://www.latencytop.org/download.php