Friday, March 19, 2010

Vnstat a console based network traffic monitor

vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won't actually be sniffing any traffic and also ensures light use of system resources.

The beautiy of Vnstat is that its very simple to install and configure

Installing vnstat on Fedora

Thanks to Fedora folks vnstat can now be installed from Fedora repository using 'yum', please refer to your distro package management commands for installing vnstat on other distros.

yum install vnstat

Monitoring Interface traffic

The commmand forces a database update for interface or creates the database if it doesn’t exist. This is usually the  first  command used after a fresh install.

vnstat  -u  -i Interface

Replace with the Interface name eg, eth0, wlan0 etc

Displaying statistics:

After installation and creating a interface database vnstat can be used from command line to display network traffic statistics

$ vnstat --help

vnStat 1.6 by Teemu Toivola

         -q,  --query          query database
         -h,  --hours          show hours
         -d,  --days           show days
         -m,  --months         show months
         -w,  --weeks          show weeks
         -t,  --top10          show top10
         -s,  --short          use short output
         -u,  --update         update database
         -i,  --iface          select interface (default: eth0)
         -?,  --help           short help
         -v,  --version        show version
         -tr, --traffic        calculate traffic
         -l,  --live           show transfer rate in real time

Tuesday, March 2, 2010

Sending SMS Notifications From Nagios

In my last article I have discuses how to install Gnokii for sending/receiving SMS from your computer. Today I'll explain how we are using Gnokii + Nagios for sending SMS notifications to our cell phones. Its a great way to get notify of the problems while on road.

I assume that you have working Nagios and its monitoring the devices in your infrastructure and sending notifications via Email and you are looking how to get these problem notifications on your phones.

Gnokii is also working and you can send SMS from CLI.

Lets cut it short and back to actual business.

In my setup we have Nagios and Gnokii install on same host running Centos 5.4, but it can easily be followed for any other Linux distro or even with setup where Gnokii is install on separate host.

1. Make sure you can send SMS from CLI with "gnokii --sendsms +92xxxxx" using root or the user under which Nagios process is running normally its 'nagios' user, sending under nagios user requires to add nagios to groups which have permission to access the device files.

a) So add nagios to 'uucp' group (you can do this with usermod command)

Gnokii also acquire a lock under /var/lock

b) So add nagios user to 'lock' group also.

su to nagios user and send sms from CLI using gnokii --sendsms, when it works move forward for defining commands.

2. Define command for send notification via SMS in commands.cfg

# 'notify-service-by-sms' command definition
define command{
command_name notify-service-by-sms
command_line /usr/bin/printf "%.120s" "*** Nagios Alert*** $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$" | /usr/local/bin/gnokii --sendsms $CONTACTPAGER$

# 'notify-host-by-sms' command definition
define command{
command_name notify-host-by-sms
command_line /usr/bin/printf "%.120s" "*** Nagios Alert*** $NOTIFICATIONTYPE$ : Host $HOSTALIAS$ is $HOSTSTATE$" | /usr/local/bin/gnokii --sendsms $CONTACTPAGER$

3. Modify contacts.cfg and add or modify a contact by calling new commands

define contact{
contact_name askarali
use generic-contact
alias Askar Ali Khan
email emailaddress
pager +92xxxxxx
service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-host-by-sms

The key in the contact detail is the service/host notifications commands

service_notification_commands notify-service-by-email,notify-service-by-sms
host_notification_commands notify-host-by-email,notify-host-by-sms

I have configured a contact so that he can receive notifications via Email 'notify-service-by-email' as well as via SMS 'notify-service-by-sms'

That's all, finally reload nagios, before reload better to run syntax check

'nagios -v PathTo nagios.cfg'

and then reload

/etc/init.d/nagios reload

Now Nagios will send SMS notifications on your phone whenever there is problem with any host/service which being monitor with Nagios.

I hope this could help.

Bash Scripting - Adding pipe "|" to each line of text file

Here is a simple script which will add a pipe symbol '|' to the start and end of every line in a text file.

Well do not ask why I need to do this, it was needed for some nasty text files which we have to deal with in our day to day works.

Someone will argue why the hell we need a script for this simple task when we can do the same with 'vi', well I must say chill mate...yes there are more then one way to do the work done, and using the script way you don't have to open a file or what if you have to work with more then one txt file? its easy to run a script "add_pipe" and sit back instead of vi file1;vi file2. :)

Script can be easily modified to accept the "SOURCEDIR" from command line or adding any other symbol, currently I put all such text files in /tmp/manual before running the script.

The Script:

# Purpose: To add pipe '|' to begin and end of line in a txt file.
# Date: 2010-02-25
# add_pipes

# Directory to where files need to process for adding pipe

# Change to source directory
if [ -d "$SOURCEDIR" ];then
 exit 1

# First convert the files to Unix (if imported from M$ files carries the carrige return
CR='\015'  # Carriage return.
           # 015 is octal ASCII code for CR.
           # Lines in a DOS text file end in CR-LF.
           # Lines in a UNIX text file end in LF only.

for file in $(ls)
 if [ ! -d "$file" ];then
  tr -d $CR < $file >newname
  mv newname $file

# Finally add pipe by loop through files
for file in $(ls)
 if [ ! -d "$file" ];then
  #sed -e 's/^/|/g' -e 's/\r$/|/g' < $file >newname
  sed -e 's/^/|/g' -e 's/$/|/g' < $file >newname
  mv newname $file
# End