Watch my tails

ript>

When in charge of server monitoring, one has to pay great attention on server’s log files. Be them web logs, mail logs or other application’s logs, sometimes they offer us too much information, either to help locating the origin of a certain problem or to confuse by displaying many messages, informational but useless!

Monitoring log files in real time is very easy by using the tail command in its follow (“-f”) mode. For example:

tail -f /var/log/syslog

will show on screen every line that appears at the end of the syslog. Additionally, filtering any undesired lines will require the use of a grep filter, like:

tail -f /var/log/syslog | grep -v -i “smtp”

But what if you want to watch more than one log file or if you want to highlight certain parts, such as IP addresses or error codes?

Multitail to the rescue!
Multitail is very versatile and highly configurable utility. It can monitor many log files in parallel, either in a window of their own or in a single one by merging them. It can also display the (differential or not) output of other commands, such as “ping -c 1 my.host.ip” or “ls /tmp” and, of course, it can colorize or filter certain fields or lines the way we want.

When I discovered (via DebianPackageADay), installed and run it, I felt very happy because I could have all the running information I needed in just one window and the more useful bits of it highlighted and easily spotted!

A very nice work indeed!


Visit The Light of the LAMP blog for more…

Replace in place

What can you do when you want to replace certain text strings with their “equivalents” or “substitutes” very fast?

Of course you can fire up your favorite text editor and start the well-known and frequently used ‘search-and-replace’ procedure.

Or you can do the same thing using sed, like this:

$ sed -i -e ‘s/this/that/g’ -e ‘s/foo/bar/g’ filename

But, if you have MySQL installed, you can find the replace utility very handy, like this:

$ replace this that foo bar — filename

So? Except for the fewer characters typed (in the case of replace), both commands had the same result, right? Right!

But replace can do the following in just one step:

$ replace this that that this — filename

or even

$ replace foo bar bar baz baz foo — filename1 filename2 filename3

replace is a utility written just to replace text strings and it does it very good.
sed, on the other hand, can do a lot (and then some) more, but it’s not for someone less experienced.


Visit The Light of the LAMP blog for more…

It’s time to relax, my friend!

Do you often find yourself working with your keyboard and mouse continuously for many hours? There are many things you can do to avoid eye strain and hand/wrist pain.

Besides the possible ergonomic (table/chair/light) rearrangements, one must consider relaxing for a few seconds every now and then, when the workload reaches a high limit.

For this task, there is a very useful utility, called RSIBreak, which monitors the time and speed a user is working with the keyboard and mouse and, when it’s necessary, recommends a short break. Additionally, in case the user didn’t “obey” this advice, it can force her to obey by disabling temporarily the user interface (keyboard/screen)!

Personally, I find it very valuable and, some times, I found myself looking at the small clock icon at the tray with the fear that it will prompt me to relax before I complete whatever I’m writing that moment. [Don’t worry though… for situations like this, the developers included the use of the “Escape” key!]

Think about it…
a short break helps the mind too!


Visit The Light of the LAMP blog for more…

Environmental issues (part 2)

When working a lot with command line utilities, I usually find myself retyping certain commands. This fact is a good reason for someone to use aliases to save typing time and errors.

At first we have to check our most loved/typed commands. This can be done using:

history | cut -c 8- | sort | uniq -c | sort -nr | head -20

in order to show the 20 most frequently used [1]. Of course, the above commands combination must be an alias too [2].

After analyzing the output, we can decide which commands could be substituted by aliases, insert them in the ~/.alias file and use them after we “source” it [3].

References:
[1] Explanation of the commands used:

  • history: show the last commands entered
  • cut -c 8-: cut the first 7 characters from each line of “history” output (cut off the number)
  • sort: sort the commands alphabetically
  • uniq -c: count same commands in a frequency table
  • sort -nr: sort the frequency table by descending counts
  • head -20: show only the first 20 lines

[2] alias hist_top20=’history | cut -c 8- | sort | uniq -c | sort -nr | head -20′ >> ~/.alias
[3] after editing ~/.alias, `source ~/.alias`


Visit The Light of the LAMP blog for more…

Environmental issues (part 1)

As a developer, I prefer working with command line utilities. That’s why I have devoted my first workspace area to an almost full-screen sized terminal window (I prefer gnome-terminal, although I’m working with KDE and konsole is just as good to work with) comprising of 3 or more tabs.

I want the first tab to always open mutt (the e-mail client that does almost everything), the second one to change to my most recent project’s directory (where I can edit the source files with vim — tabbed, using “vim -p”) and make a backup of the project’s database, the third to check some log-file, and the list goes on.

So, what’s the best way to automate these procedures?

Currently I’m using the output of the “tty” command and a case statement at the end of my ~/.bashrc file (comments included):


# show me from where I logged in
echo Logged in from $(tty)
case "$(tty)" in
"/dev/pts/1")
# run mutt
mutt
;;
"/dev/pts/2")
# first change directory
cd ~/projects/projectName
# then perform a quick database backup
make back
;;
"/dev/pts/3")
# show me web visits, ignoring some of them based on certain criteria
tail -f /var/log/apache2/access.log | egrep -v "localhost|127.0.0|/(Thumb|images|Photo)/|favicon"
;;
esac

With arrangements like the above, one can “feel like home” by just logging in!


Visit The Light of the LAMP blog for more…

bash: quickly rename files

Since one cannot always have the tools she likes, here are some one-liners to rename certain files using only bash.
Rename all ‘jpeg’ files to ‘jpg’:

  • for a in *.jpeg; do mv $a ${a%jpeg}jpg; done

Remove the ‘photo-‘ prefix:

  • for a in photo-*; do mv $a ${a/photo-}; done

Rename ‘dsc’ prefix to ‘photo-‘:

  • for a in dsc*; do mv $a ${a/dsc/photo-}; done

Where can I find these recipes?
man bash 🙂


Visit The Light of the LAMP blog for more…