Wednesday, December 11, 2013

HowTo: Add Jobs To cron Under Linux or UNIX?

ow do I add cron job under Linux or UNIX like operating system?

Cron job are used to schedule commands to be executed periodically. You can setup commands or scripts, which will repeatedly run at a set time. Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron.*/ directories. It also checks the /var/spool/cron/ directory.

crontab command











crontab is the command used to install, deinstall or list the tables (cron configuration file) used to drive the cron(8) daemon in Vixie Cron. Each user can have their own crontab file, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. You need to use crontab command for editing or setting up your own cron jobs.

Types of cron configuration files

There are different types of configuration files:
  1. The UNIX / Linux system crontab : Usually, used by system services and critical jobs that requires root like privileges. The sixth field (see below for field description) is the name of a user for the command to run as. This gives the system crontab the ability to run commands as any user.
  2. The user crontabs: User can install their own cron jobs using the crontab command. The sixth field is the command to run, and all commands run as the user who created the crontab
Note: This faq features cron implementations written by Paul Vixie and included in many Linux distributions and Unix like systems such as in the popular 4th BSD edition. The syntax is compatible with various implementations of crond.

How Do I install or create or edit my own cron jobs?

To edit your crontab file, type the following command at the UNIX / Linux shell prompt:
$ crontab -e

Syntax of crontab (field description)

The syntax is:
 
1 2 3 4 5 /path/to/command arg1 arg2
 
OR
 
1 2 3 4 5 /root/backup.sh
 
Where,
  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command - Script or command name to schedule
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Your cron job looks as follows for system jobs:
1 2 3 4 5 USERNAME /path/to/command arg1 arg2
OR
1 2 3 4 5 USERNAME /path/to/script.sh

Example: Run backup cron job script

If you wished to have a script named /root/backup.sh run every day at 3am, your crontab entry would look like as follows. First, install your cronjob by running the following command:
# crontab -e
Append the following entry:
0 3 * * * /root/backup.sh
Save and close the file.

More examples

To run /path/to/command five minutes after midnight, every day, enter:
5 0 * * * /path/to/command
Run /path/to/script.sh at 2:15pm on the first of every month, enter:
15 14 1 * * /path/to/script.sh
Run /scripts/phpscript.php at 10 pm on weekdays, enter:
0 22 * * 1-5 /scripts/phpscript.php
Run /root/scripts/perl/perlscript.pl at 23 minutes after midnight, 2am, 4am ..., everyday, enter:
23 0-23/2 * * * /root/scripts/perl/perlscript.pl
Run /path/to/unixcommand at 5 after 4 every Sunday, enter:
5 4 * * sun /path/to/unixcommand

How do I use operators?

An operator allows you to specifying multiple values in a field. There are three operators:
  1. The asterisk (*) : This operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to every hour or an asterisk in the month field would be equivalent to every month.
  2. The comma (,) : This operator specifies a list of values, for example: "1,5,10,15,20, 25".
  3. The dash (-) : This operator specifies a range of values, for example: "5-15" days , which is equivalent to typing "5,6,7,8,9,....,13,14,15" using the comma operator.
  4. The separator (/) : This operator specifies a step value, for example: "0-23/" can be used in the hours field to specify command execution every other hour. Steps are also permitted after an asterisk, so if you want to say every two hours, just use */2.

How do I disable email output?

By default the output of a command or a script (if any produced), will be email to your local email account. To stop receiving email output from crontab you need to append >/dev/null 2>&1. For example:
0 3 * * * /root/backup.sh >/dev/null 2>&1
  you need to define MAILTO variable as follows:
MAILTO="abc@abc.info"
0 3 * * * /root/backup.sh >/dev/null 2>&1

See "Disable The Mail Alert By Crontab Command" for more information.

Task: List all your cron jobs

Type the following command:
# crontab -l
# crontab -u username -l

To remove or erase all crontab jobs use the following command:
# Delete the current cron jobs #
crontab -r

## Delete job for specific user. Must be run as root user ##
crontab -r -u username

Use special string to save time

Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.
Special stringMeaning
@rebootRun once, at startup.
@yearlyRun once a year, "0 0 1 1 *".
@annually(same as @yearly)
@monthlyRun once a month, "0 0 1 * *".
@weeklyRun once a week, "0 0 * * 0".
@dailyRun once a day, "0 0 * * *".
@midnight(same as @daily)
@hourly Run once an hour, "0 * * * *".

Examples

Run ntpdate command every hour:
@hourly /path/to/ntpdate
Make a backup everyday:
@daily /path/to/backup/script.sh

More about /etc/crontab file and /etc/cron.d/* directories

/etc/crontab is system crontabs file. Usually only used by root user or daemons to configure system wide jobs. All individual user must must use crontab command to install and edit their jobs as described above. /var/spool/cron/ or /var/cron/tabs/ is directory for personal user crontab files. It must be backup with users home directory.

Understanding Default /etc/crontab

Typical /etc/crontab file entries:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
First, the environment must be defined. If the shell line is omitted, cron will use the default, which is sh. If the PATH variable is omitted, no default will be used and file locations will need to be absolute. If HOME is omitted, cron will use the invoking users home directory.
Additionally, cron reads the files in /etc/cron.d/ directory. Usually system daemon such as sa-update or sysstat places their cronjob here. As a root user or superuser you can use following directories to configure cron jobs. You can directly drop your scripts here. The run-parts command run scripts or programs in a directory via /etc/crontab file:
DirectoryDescription
/etc/cron.d/ Put all scripts here and call them from /etc/crontab file.
/etc/cron.daily/ Run all scripts once a day
/etc/cron.hourly/ Run all scripts once an hour
/etc/cron.monthly/Run all scripts once a month
/etc/cron.weekly/Run all scripts once a week

How do I use above directories to put my own scripts or jobs?

Here is a sample shell script called clean.cache. This script is created to clean up cached files every 10 days. This script is directly created at /etc/cron.daliy/ directory. In other words create a text file called /etc/cron.daily/clean.cache as follows.
 #!/bin/bash
# A sample shell script to clean cached file from lighttpd web server
CROOT="/tmp/cachelighttpd/"
 
# Clean files every $DAYS
DAYS=10
 
# Web server username and group name
LUSER="lighttpd"
LGROUP="lighttpd"
 
# Okay, let us start cleaning as per $DAYS
/usr/bin/find ${CROOT} -type f -mtime +${DAYS} | xargs -r /bin/rm
 
# Failsafe 
# if directory deleted by some other script just get it back 
if [ ! -d $CROOT ]
then
        /bin/mkdir -p $CROOT
        /bin/chown ${LUSER}:${LGROUP} ${CROOT}
fi
Save and close the file. Set the permissions:
# chmod +x /etc/cron.daily/clean.cache

How do I backup installed cron jobs entries?

Simply type the following command to backup your cronjobs to a nas server mounted at /nas01/backup/cron/users.root.bakup directory:
# crontab -l > /nas01/backup/cron/users.root.bakup
# crontab -u userName -l > /nas01/backup/cron/users.userName.bakup

Wednesday, November 13, 2013

Re: Missing LSB Information (Start Script Install)

Hello.


I had the same problem: I had written a script e.g. mystartupscript and then I have done the following:


Code:
user@computer:/etc/init.d$ sudo update-rc.d mystartupscript defaults
Then after some time I modified my script and thought to have to do this again. So I again wrote ...

Code:
user@computer:/etc/init.d$ sudo update-rc.d mystartupscript defaults
... and so I got the mysterious failure:

Code:
update-rc.d: warning: /etc/init.d/mystartupscript missing LSB information
I have solved the problem as follows:

Code:
user@computer:/etc/init.d$ sudo update-rc.d -f mystartupscript remove
user@computer:/etc/init.d$ sudo update-rc.d mystartupscript defaults
And don't forget to make the script executable:
Code:
user@computer:/etc/init.d$ sudo chmod +x ./mystartupscript

I hope it can help you.

Tuesday, November 12, 2013

How to Run GlassFish V3 as a Service on Linux Ubuntu/Debian

We already support running GlassFish V3 as a service on Solaris 10 and Windows platforms (see my blog).  I have been investigating how to provide support for automatically starting GlassFish V3 as a service on Linux.  Of course before I can hope to do that -- I must be able to set it up manually.  In this blog I will take you through the manual steps needed to run GlassFish V3 as a service on Linux.
This procedure was worked out on my Linux system which happens to be Ubuntu.  Other flavors of Linux may have slightly different procedures.
One decision you need to make right up front is what Linux user should "own" GlassFish V3.  Typically root is used as the owner.  If you choose root as the user you get the advantage that you can use ports < 1024 without complex configuration changes to the system.  For this blog I used root.
 Here are the steps  -- the file named "glassfish" in step 4 is the simple init script which appears at the end of this blog.
  1. Install JDK 6 if needed
  2. Have root install GlassFish like so:
    1. cd /opt
    2. wget  http://download.java.net/glassfish/v3/release/glassfish-v3.zip
    3. unzip glassfish-v3.zip
    4. rm glassfish-v3.zip
  3. cd /etc/init.d
  4. cp glassfish .
  5. update-rc.d glassfish defaults
  6. OPTIONAL /etc/init.d/glassfish start
  7. OPTIONAL Make sure GlassFish is running OK
  8. reboot -- you are done!

To start, stop, restart GlassFish simply run these commands:
sudo /etc/init.d/glassfish start
sudo /etc/init.d/glassfish stop
sudo /etc/init.d/glassfish restart




#!/bin/sh
#
# glassfish init script for Linux
# Simplest possible case -- no password file, one default domain
# it would be simple to add such options

GLASSFISH_HOME=${GLASSFISH_HOME:-"/opt/glassfishv3/glassfish"}

case "$1" in
start)
    $GLASSFISH_HOME/bin/asadmin start-domain >/dev/null
    ;;
stop)
    $GLASSFISH_HOME/bin/asadmin stop-domain >/dev/null
    ;;
restart)
    $GLASSFISH_HOME/bin/asadmin restart-domain >/dev/null
    ;;
\*)
    echo "usage: $0 (start|stop|restart|help)"
esac

Tuesday, November 5, 2013

25 Essential MySQL Select Command Examples

If you are using MySQL database, it is essential that you become comfortable with mysql command line.
In this tutorial we’ll explain how to use the MySQL select command with several practical examples.

1. Basic Select command Example

First, to connect to MySQL command line, do the following from your operating system prompt.
# mysql -u root -p
Password:
Next, view all available databases.
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| thegeekstuff       |
| crm                |
| bugzilla           |
+--------------------+
8 rows in set (0.00 sec)
Use the database where you want to work on. In this example, I’m selecting “thegeekstuff” database, where the “employee” table is located, which is used as an example for all the select commands explained in this article.
mysql> USE thegeekstuff;
Database changed

mysql> DESC employee;
The basic usage of select command is to view rows from a table. The following select command example will display all the rows from the “employee” table.
mysql> SELECT * FROM employee;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 100 | Thomas | Sales      |   5000 |
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 400 | Nisha  | Marketing  |   9500 |
| 500 | Randy  | Technology |   6000 |
| 501 | Ritu   | Accounting |   NULL |
+-----+--------+------------+--------+
6 rows in set (0.01 sec)
Or, select specific columns by specifying the column names (Instead of * which will give all columns).
mysql> SELECT name, salary FROM employee;
+--------+--------+
| name   | salary |
+--------+--------+
| Thomas |   5000 |
| Jason  |   5500 |
| Sanjay |   7000 |
| Nisha  |   9500 |
| Randy  |   6000 |
| Ritu   |   NULL |
+--------+--------+
6 rows in set (0.00 sec)
Note: If you are new to MySQL, read our previous article on how to create a MySQL database and table before you continue this tutorial.

2. Select from Dual – Virtual Table

dual is a virtual table. This really doesn’t exist. But, you can use this table to perform some non-table activities.
For example, you can use select on dual table to perform arithmetic operations as shown below.
mysql> SELECT 2+3 FROM DUAL;
+-----+
| 2+3 |
+-----+
|   5 |
+-----+
1 row in set (0.00 sec)
You can also use dual table to view the current date and time. The now() function in MySQL is similar to the sysdate() function in Oracle database.
mysql> SELECT NOW() FROM DUAL;
+---------------------+
| now()               |
+---------------------+
| 2013-09-14 09:15:35 |
+---------------------+
1 row in set (0.00 sec)
When you don’t specify any table, MySQL will assume that you want to use dual. The following example are exactly same as the above. Just to avoid confusion, I recommend that you use “from dual” in these situation for better readability and clarity.
mysql> SELECT 2+3;
+-----+
| 2+3 |
+-----+
|   5 |
+-----+
1 row in set (0.00 sec)

mysql> SELECT NOW();
+---------------------+
| now()               |
+---------------------+
| 2013-09-14 09:16:45 |
+---------------------+
1 row in set (0.00 sec)

3. Basic WHERE Condition to Restrict Records

Instead of display all the records from a table, you can also use WHERE condition to view only recrods that matches a specific condition as shown below.
mysql> SELECT * FROM employee WHERE salary > 6000;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 300 | Sanjay | Technology |   7000 |
| 400 | Nisha  | Marketing  |   9500 |
+-----+--------+------------+--------+
2 rows in set (0.00 sec)
Similar to “greater than >” you can also use “less than=”, “not equal to !=” as shown below.
mysql> SELECT * FROM employee WHERE salary < 6000; mysql> SELECT * FROM employee WHERE salary  SELECT * FROM employee WHERE salary >= 6000;

mysql> SELECT * FROM employee WHERE salary = 6000;

mysql> SELECT * FROM employee WHERE salary != 6000;

4. Match Strings in WHERE Condition

The previous example displays how to restrict records based on numerical conditions. This example explains how to restrict records based on string values.
The exact match of strings works like numeric match using “equal to =” as shown below. This example will display all employees who belong to Technology department.
mysql> SELECT * FROM employee WHERE dept = 'Technology';
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 500 | Randy  | Technology |   6000 |
+-----+--------+------------+--------+
3 rows in set (0.00 sec)
Please note that this is case insensitive comparison. So, the following is exactly the same as above select command.
mysql> SELECT * FROM employee WHERE dept = 'TECHNOLOGY';
You can also use != to display all the employee who does not belong to Technology department as shown below.
mysql> SELECT * FROM employee WHERE dept != 'TECHNOLOGY';
You can also perform partial string match using % in the keywords. The following will display all employees whos last name begins with “John”.
mysql> SELECT * FROM employee WHERE name LIKE 'JOHN%';
The following will display all employees whos name ends with “Smith”.
mysql> SELECT * FROM employee WHERE name LIKE '%SMITH';
You can also give % at both beginning and end. In which case, it will search for the given keyword anywhere in the string. The following will display all employees who contain “John” in their name anywhere.
mysql> SELECT * FROM employee WHERE name LIKE '%JOHN%';

5. Combine WHERE Conditions Using OR, AND

You can also use OR, AND, NOT in WHERE condition to combine multiple conditions. The following example displays all employees who are in “Technology” department AND with salary >= 6000. This will display records only when both the conditions are met.
mysql> SELECT * FROM employee WHERE dept = 'TECHNOLOGY' AND salary >= 6000;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 300 | Sanjay | Technology |   7000 |
| 500 | Randy  | Technology |   6000 |
+-----+--------+------------+--------+
2 rows in set (0.00 sec)
The following is same as above, but uses OR condition. So, this will display records as long as any one of the condition matches.
mysql> SELECT * FROM employee WHERE dept = 'TECHNOLOGY' OR salary >= 6000;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 400 | Nisha  | Marketing  |   9500 |
| 500 | Randy  | Technology |   6000 |
+-----+--------+------------+--------+
4 rows in set (0.00 sec)

6. Combine column values using CONCAT in select

You can use CONCAT function in select commanda to combine values from multiple columns and display it. The following example combines name and department field (for display only) as shown below.
mysql> SELECT ID, CONCAT(NAME, ' FROM ', DEPT) AS NAME, SALARY FROM employee;
+-----+------------------------+--------+
| id  | name                   | salary |
+-----+------------------------+--------+
| 100 | Thomas from Sales      |   5000 |
| 200 | Jason from Technology  |   5500 |
| 300 | Sanjay from Technology |   7000 |
| 400 | Nisha from Marketing   |   9500 |
| 500 | Randy from Technology  |   6000 |
| 501 | Ritu from Accounting   |   NULL |
+-----+------------------------+--------+
6 rows in set (0.00 sec)

7. Count Total Number of Records

Use count(*) in select command to display the total number of records in a table.
mysql> SELECT COUNT(*) FROM employee;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set (0.00 sec)

8. Group By in Select Command

Group By commands will group records based on certain conditions. The following example displays the total number of employees in every department.
mysql> SELECT DEPT, COUNT(*) FROM employee GROUP BY DEPT;
+------------+----------+
| dept       | count(*) |
+------------+----------+
| Accounting |        1 |
| Marketing  |        1 |
| Sales      |        1 |
| Technology |        3 |
+------------+----------+
4 rows in set (0.00 sec)
Please note that when you use GROUP BY, you can use certain functions to get more meaningful output. IN the above example, we’ve used count(*) group by commands. Similarly you can use sum(), avg(), etc, when you specify GROUP BY.

9. Use HAVING along with GROUP BY

When you use GROUP BY, you can also use HAVING to restrict the records further.
In the following example, it displays only the departments where the number of employee is more than 1.
mysql> SELECT COUNT(*) AS CNT, DEPT FROM employee GROUP BY DEPT HAVING CNT > 1;
+-----+------------+
| CNT | dept       |
+-----+------------+
|   3 | Technology |
+-----+------------+
1 row in set (0.00 sec)

10. Define Alias using ‘AS’ Keyword

Instead of display the column name as specified in the table, you can use your own name in the display using AS keyword.
In the following example, even though the real column name is ID, it is displayed as EMPID.
mysql> SELECT ID AS EMPID, NAME AS EMPNAME, DEPT AS DEPARTMENT FROM employee;
+-------+---------+------------+
| EMPID | EMPNAME | DEPARTMENT |
+-------+---------+------------+
|   100 | Thomas  | Sales      |
|   200 | Jason   | Technology |
|   300 | Sanjay  | Technology |
|   400 | Nisha   | Marketing  |
|   500 | Randy   | Technology |
|   501 | Ritu    | Accounting |
+-------+---------+------------+
6 rows in set (0.00 sec)
Please note that the AS keyword is optional. The following example is exactly the same as the above.
mysql> SELECT id empid, name empname, dept department FROM employee;

11. Left Join in SELECT command

In the following example, the select command combines two tables. i.e employee and department. For combining these, it uses the common column between these two tables dept. The “Location” column shown in the output is from the department table.
mysql> SELECT employee.*, department.location FROM employee LEFT JOIN department ON ( employee.dept = department.dept );
+-----+--------+------------+--------+----------+
| id  | name   | dept       | salary | Location |
+-----+--------+------------+--------+----------+
| 100 | Thomas | Sales      |   5000 | USA      |
| 200 | Jason  | Technology |   5500 | USA      |
| 300 | Sanjay | Technology |   7000 | India    |
| 400 | Nisha  | Marketing  |   9500 | India    |
| 500 | Randy  | Technology |   6000 | UK       |
| 501 | Ritu   | Accounting |   NULL | USA      |
+-----+--------+------------+--------+----------+
You can also use table alias name in the JOIN command as shown below. In this example, I’ve used “E” as alias for employee table, and “D” as alias for department table. This makes the select command smaller and easier to read.
mysql> SELECT E.*, d.location FROM employee AS E LEFT JOIN department AS D ON ( e.dept = d.dept );
Note: Join itself is a huge topic, which we will discuss in detail as a separate tutorial.

12. Performance Analysis using EXPLAIN

When your select query is slow, or behaving in a way you don’t understand, use the EXPLAIN command, which will display additional details that MySQL is using internally to execute the query. This might give you some insight on the performance of your MySQL select command.
mysql> EXPLAIN SELECT E.*, D.LOCATION FROM employee AS E LEFT JOIN DEPARTMENT AS D ON ( E.DEPT = D.DEPT );
+----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------+
| id | select_type | table | type   | possible_keys | key     | key_len | ref             | rows | Extra |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------+
|  1 | SIMPLE      | PS    | ALL    | NULL          | NULL    | NULL    | NULL            |    6 |       |
|  1 | SIMPLE      | P     | eq_ref | PRIMARY       | PRIMARY | 3       | acme.E.dept     |    1 |       |
+----+-------------+-------+--------+---------------+---------+---------+-----------------+------+-------+
2 rows in set (0.00 sec)

13. Force Select Query to use an INDEX

While executing a select query, and joining two tables, MySQL will decide how to use any available Indexes on the tables effectively. The following are few ways of dealing with indexes in SELECT command.
  • USE INDEX (list_of_indexes) – This will use one of the indexes specified to query the records from the table.
  • IGNORE INDEX (list_of_indexes) – This will use the indexes specified to query the records from the table.
  • FORCE INDEX (index_name) – This will force MySQL to use the given index even when MySQL thinks a better and faster way of querying the records are available.
Before you decide to use any one of the above, you should really understand the impact of these commands, as if you don’t use these properly, it will slow down your select command.
The following examples forces MySQL to use the employee_emp_nm_idx for this query.
mysql> SELECT * FROM employee FORCE INDEX (EMPLOYEE_EMP_NM_IDX) WHERE NAME LIKE 'JOHN%';
To display all available indexes on a particular table, use the “show index” command. The following example displays all indexes available on employee table.
mysql> SHOW INDEX FROM PROFILES;
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table    | Non_unique | Key_name                | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| employee |          0 | PRIMARY                 |            1 | id          | A         |         156 |     NULL | NULL   |      | BTREE      |         |
| employee |          0 | employee_emp_nm_idx     |            1 | name        | A         |         156 |     NULL | NULL   |      | BTREE      |         |
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+

14. Sort Records using ORDER BY

Note: desc will short by descending. If you don’t give anything, it is ascending.
The following records will order the records in alphabetical order based on dept column.
mysql> SELECT * FROM employee ORDER BY DEPT;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 501 | Ritu   | Accounting |   NULL |
| 400 | Nisha  | Marketing  |   9500 |
| 100 | Thomas | Sales      |   5000 |
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 500 | Randy  | Technology |   6000 |
+-----+--------+------------+--------+
6 rows in set (0.01 sec)
Please note that by default it will sort by ascending order. If you want to sort by descending order, specify the keyword “DESC” after “ORDER BY” as shown below.
mysql> SELECT * FROM employee ORDER BY DEPT DESC;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 500 | Randy  | Technology |   6000 |
| 100 | Thomas | Sales      |   5000 |
| 400 | Nisha  | Marketing  |   9500 |
| 501 | Ritu   | Accounting |   NULL |
+-----+--------+------------+--------+
6 rows in set (0.00 sec)
You can also order by multiple columns as shown below.
mysql> SELECT * FROM employee ORDER BY DEPT, SALARY DESC;

15. Limit the Number of Records

Instead of displaying all the records you can just limit how many records mysql should display using the LIMIT as shown below.
Limit format:
LIMIT start_record, number_of_records
The following example will start from record number 0 (which is the 1st record), and display 3 records from there.
mysql> SELECT * FROM employee LIMIT 0,3;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 100 | Thomas | Sales      |   5000 |
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
+-----+--------+------------+--------+
3 rows in set (0.00 sec)
The following will start from record number 1 (which is the 2nd record), and display 3 records from there.
mysql> SELECT * FROM employee LIMIT 1,3;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 400 | Nisha  | Marketing  |   9500 |
+-----+--------+------------+--------+
3 rows in set (0.00 sec)
You can also omit the start_record, in which case, it will always start from record number 0 (i.e first record).
In the following example, we’ve specified only one value. So, this will start from record number 0, and display 3 records from there.
mysql> SELECT * FROM employee LIMIT 3;
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 100 | Thomas | Sales      |   5000 |
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
+-----+--------+------------+--------+
3 rows in set (0.00 sec)

16. Limit the Number of Records with OFFSET

Limit OFFSET format:
LIMIT number_of_records OFFSET start_record
You can also use the keyword OFFSET, where you’ll specify the start record after the keyword OFFSET.
The following will display total of 3 records. Since the offset is specified as 1, it will start from the 2nd record.
mysql> SELECT * FROM employee LIMIT 3 OFFSET 1
+-----+--------+------------+--------+
| id  | name   | dept       | salary |
+-----+--------+------------+--------+
| 200 | Jason  | Technology |   5500 |
| 300 | Sanjay | Technology |   7000 |
| 400 | Nisha  | Marketing  |   9500 |
+-----+--------+------------+--------+
3 rows in set (0.00 sec)

17. Get Unique Values from a Column

To display all unique values from a column, use DISTINCT.
The following example will display all the unique dept values from the employee table.
mysql> SELECT DISTINCT DEPT FROM employee;
+------------+
| dept       |
+------------+
| Sales      |
| Technology |
| Marketing  |
| Accounting |
+------------+

18. Sum of all Values in a Column

To add all the values from a column, use SUM() function.
The following example will display the sum of salary column for all the employees who belong to Technology department.
mysql> SELECT SUM(SALARY) FROM employee WHERE DEPT = 'TECHNOLOGY';
+-------------+
| sum(salary) |
+-------------+
|       18500 |
+-------------+
1 row in set (0.01 sec)

19. Average of all Values in a Column

To average all the values from a column, use AVG() function.
The following example will display the average salary of each and every department. This combines GROUP BY with AVG() function.
mysql> SELECT DEPT,AVG(SALARY) FROM employee GROUP BY DEPT;
+------------+-------------+
| dept       | avg(salary) |
+------------+-------------+
| Accounting |        NULL |
| Marketing  |   9500.0000 |
| Sales      |   5000.0000 |
| Technology |   6166.6667 |
+------------+-------------+
4 rows in set (0.03 sec)

20. SELECT within SELECT command

The example shown below is very lame. There is no reason to do it this way. But, this shows you how you can use select command. In this example the “AS ACTION” gives an alias name to the select subquery. You need to specify an alias in this example. The “ACTION” is just a name. You can change this to anything you like.
mysql> SELECT * FROM (SELECT * FROM employee) AS ACTION WHERE ID

21. Save the Select Output to a File

Using SELECT INTO, you can save the output of a select command into a file.
Instead of displaying the output on the screen, the following select command example will store the output of the select command into the /tmp/employee.txt file.
mysql> SELECT * INTO OUTFILE '/tmp/employee.txt' FROM employee;
Query OK, 6 rows affected (0.00 sec)

# cat /tmp/employee.txt
100     Thomas  Sales   5000
200     Jason   Technology      5500
300     Sanjay  Technology      7000
400     Nisha   Marketing       9500
500     Randy   Technology      6000
501     Ritu    Accounting      \N
You can also store the output into a comma delimited file by specifying the “FIELDS TERMINATED BY” as shown in the example below.
mysql> SELECT * INTO OUTFILE '/tmp/employee1.txt'   FIELDS TERMINATED BY ',' FROM employee;
Query OK, 6 rows affected (0.00 sec)

# cat /tmp/employee1.txt
100,Thomas,Sales,5000
200,Jason,Technology,5500
300,Sanjay,Technology,7000
400,Nisha,Marketing,9500
500,Randy,Technology,6000
501,Ritu,Accounting,\N

22. Execute a Procedure on the Data Set

You can also call a MySQL procedure that will process the data from the output of the select command.
The following example will execute the procedure salary_report() on the output of the given select command.
mysql> SELECT ID, SALARY FROM employee PROCEDURE SALARY_REPORT();

23. Display a Random Record from a table

Using the rand command you can display a random record from a table. This can be helpful in situations similar to where you are displaying some random tip of the day from a table.
mysql> SELECT * FROM employee ORDER BY RAND() LIMIT 1;
+-----+-------+------------+--------+
| id  | name  | dept       | salary |
+-----+-------+------------+--------+
| 200 | Jason | Technology |   5500 |
+-----+-------+------------+--------+
1 row in set (0.00 sec)
The same command executed next time, will give a different record as shown below.
mysql> SELECT * FROM employee ORDER BY RAND() LIMIT 1;
+-----+--------+-------+--------+
| id  | name   | dept  | salary |
+-----+--------+-------+--------+
| 100 | Thomas | Sales |   5000 |
+-----+--------+-------+--------+
1 row in set (0.00 sec)
You can also pass the current date and time as salt using the now() function to rand command as shown below.
mysql> SELECT * FROM employee ORDER BY RAND(NOW()) LIMIT 1;
+-----+-------+-----------+--------+
| id  | name  | dept      | salary |
+-----+-------+-----------+--------+
| 400 | Nisha | Marketing |   9500 |
+-----+-------+-----------+--------+
1 row in set (0.00 sec)

24. High Priority Select Command

When you use high_priority keyword in select statement, it will give that particular select statement higher priority than any update to the table.
Be very careful when you use this command, as you might slow down other updates. Use this only in situations where you need to get a record very quickly. Also make sure the select command you are giving itself is very well optimized before you execute it.
mysql> SELECT HIGH_PRIORITY * FROM employee WHERE ID = 100;

25. Consistent Read in Select Command

If you want a consistent read. i.e When you are selecting rows from a table, if you don’t want any other process to modify the values, you need to enable a share lock mode when you are reading the records.
If you don’t understand the impact of how these works, you might put yourself in a difficult situation if you try to use these on a large table.
The following command will not allow other MySQL sessions to modify the records that are queried by this select statement until it reads all these records.
mysql> SELECT * FROM employee WHERE ID = 100 LOCK IN SHARE MODE;
Please note that you can also do “FOR UPDATE” as shown below, which will block other sessions from doing “SELECT … LOCK in SHARE MODE” until this transaction is over.
mysql> SELECT * FROM employee WHERE ID = 100 FOR UPDATE;

How to Remove Files Older than N days using tmpreaper in Linux

tmpreaper is a tool to remove files which are not accessed for a certain period of time.
On Linux desktop distros, this is good for cleaning directories like “~/Downloads”, where files get accumulated over a period of time.
On Linux server distros, this is good for cleaning old log files or backup files that are not required any more.
tmpreaper recursively searches and removes files and directories which are not accessed for certain period of time.

WARNING: Before you install tmpreaper and start playing around with it, make sure you understand the implication of running tmpreaper, as it will delete all the files from your system that matches the given criteria. Do NOT run tmpreaper on / (root directory), which might delete critical files that are required to keep your system running. There is no safeguard built into the tmpreaper program to prevent you from running on root directory, as that would make it difficult to use tmpreaper in a chrooted environment.

1. Install tmpreaper

On debian based systems like Ubuntu, use apt-get:
$ sudo apt-get install tmpreaper
On RPM based systems like CentOS and RedHat, use yum:
$ sudo yum -y install tmpreaper
Tmpreaper command syntax:
$ tmpreaper [options]  <time_spec> <dirs>

2. Remove Files which are N Days Older

To remove files which are 5 days older, use “5d” as timespec.
For example, the following command will delete files from the ~/Downloads folder that are not accessed in the last 5 days.
$ tmpreaper 5d ~/Downloads

3. Remove Files which are Not Modified for N Days

By default tmpreaper, will delete files based on “Access Time”. You can use “-m” option to tell tmpreaper to delete files based on “Modification time”.
The following command will delete files which are not modified for 5 days in the Downloads folder.
$ tmpreaper -m 5d ~/Downloads
You can also use the following characters for time_spec parameter
  • d – for days
  • h – for hours
  • m – for minutes
  • s – for seconds

4. Remove Symbolic Links using -s

Use -s option to remove symbolic links also, not just files and directories.
Apart from cleaning up the files and directories, the following command will also clean-up the symbolic links that matches the given time specifications.
$ tmpreaper -s 5h ~/Downloads

5. Remove all File Types using -a Option

Use -a option to remove all type of files, not just regular files, directories, and symbolic links.
$ tmpreaper -a 5m ~/Downloads

6. Do a Dryrun – Test for Deletion using -t Option

Use -t option, to test what files are going to be deleted.
This is very helpful when you are running this against a important directory and you want to exactly what files will be deleted before it really gets deleted.
This option does not remove the files.
$ tmpreaper -t 5d ~/Downloads
(PID 5415) Pretending to clean up directory `/home/lakshmanan/Downloads'.
(PID 5416) Pretending to clean up directory `.tmp_versions'.
(PID 5416) Back from recursing down `.tmp_versions'.

7. Force Delete Files using -f Option

Use -f option to force delete files. Normally files owned by current user (EUID) with no write access are not removed. Using -f will remove those files also.
$ tmpreaper -f 5h ~/Downloads

8. Don’t Delete Files Matching a Pattern using –protect Option

Use –protect ‘<shell_pattern>’ to protect the pattern matching files from deletion.
For example, the following command will delete all files except “.c” files.
$ tmpreaper  --protect '*.c' -t 5h ~/my_prg
Entry matching `--protect' pattern skipped. `hello.c'
Pretending to remove file `./.hello.o'.

9. Using tmpreaper in Cron

By default when you install tmpreaper, it will put an entry in crontab ( /etc/cron.daily/tmpreaper ). It will read the options from /etc/tmpreaper.conf and execute tmpreaper command based on those options.
The /etc/tmpreaper.conf is self explanatory, and easy to understand. By default it will delete files that are 7 days old in /tmp expect some file types. If you plan to use this, then remove the ‘SHOWWARNING=true’ line from the /etc/tmpreaper.conf.
Once the line is removed, tmpreaper will run daily to clean the specified directories without user intervention.

Top 10 Mistakes Not to Make in a Programming Interview

If you are a programmer, you already know that when you apply for a programming job, the interview process is little different than any other technical job interviews.
In this article we will discuss 10 mistakes that you should avoid in a programming interview.

1. No Practice for Writing Code on Paper or Whiteboard

This is one of the biggest mistakes that candidates tend to make. Most of the programming interviews are either on paper or on a whiteboard. Most of the candidates have good practice of writing code on a computer but little practice of writing code on paper or white board.
A candidate who is used to an IDE (or a text editor) fumbles at the very first step of maintaining good indentation on paper. You must know that writing a well indented code is must in programming interviews. Also, while writing code on paper, there is no compiler to help you find some obvious compile time mistakes. Also most of the candidates get nervous while writing code on a white board in front of a interview panel. So, before a job interview, practice a little on writing code on a paper or whiteboard.

2. Don’t Just Memorize Code Snippets

Memorizing solutions to some popular problems is something that many of the fresh graduates tend to do. For example, most programmers know how to do ‘swapping two numbers using bitwise operators in C’. But not all of them know exactly what that code does and what it does to get the job done.
So, memorizing is not a short cut in a C programming interview. Even if you get away with one or two questions, you cannot get pass a complete interview without understanding the fundamental concepts. The key is to understand the concepts behind the programming. It’s not important how many programming languages you know, what’s important is how well you know them.

3. Less Interaction with the Interviewer

If you ever feel that there is something wrong with the question or if you have any other doubt then it is always advisable to talk to interviewer about it.
Even if you don’t know an answer to a particular question, instead of sitting quiet, or answering something totally unrelated to the questions, it is better to come-out honest and agree that you don’t know the answer. But, suggest them some other alternative topic that is related to the original question, and tell them that you are comfortable in that topic.

4. No Practice for Phone Interviews

These days most of the companies tend to arrange a telephonic interview to judge the candidate before calling them up for further rounds. Getting interviewed on phone is totally different than a face to face interview. In a telephonic interview you need to have good listening and speaking skills.
Make sure that you listen to the problem carefully before answering. Interviewers might give you a code snippet and then you have to answer some questions based on that piece of code. It is important to have a pen and paper handy before attending the telephone interview.
Also, don’t answer every question in exact bookish language because interviewer might get an impression that you are actually referring to some material online while you are answering the question.

5. Writing Messy Code

Please do not underestimate the importance of a well indented code. No matter how much you know, no matter how much innovative you are, if you are being judged for your programming skills then the interviewer has to understand your code before concluding anything and if you provide him with a messy code then interviewer might not even want to read it.
It’s not only the interviewer, a well indented code even helps the candidate to write a bug free program. There are high chances that a well indented code will have less bugs when compared to a messy one.

6. Hiding Your Approach

Interviewers are always interested in your approach to solve a programming problem. It is not expected that you would be able to solve each programming problem thrown to you. But, you are definitely expected to have correct approach towards the solution.
For example, if you are writing a program to swap two numbers using C pointers then it’s the correct approach (ie calling a function with address of variables to be swapped and receiving the addresses in pointer arguments) is what matters. The interviewer might guide you if your are stuck with any other problem.
So, it is always advised to talk out your approach while solving a problem. This is a good way to keep the interviewer engaged.

7. Bad Naming Convention for Functions and Variables

No matter which language you use to solve the interview problem, it is always advisable to use relevant variable and function names, as they help in maintaining code.
For example, suppose you have written thousands of lines of code using insane function and variable names. Now, think of some new who is asked to enhance or debug this code. Debugging this kind of code, where one cannot even understand what variables represent, would be a big nightmare.
Interviewers may or may not consider this as a negative point but it’s always good to be on safer side. Moreover, you will never be appreciated to write function name ‘func’ when you can use a name ‘swap’ for writing a function to swap the values of two variables.

8. Resume Filled with Technical Stuff You Don’t Know

This is another big mistake that beginners tend to do. In order to get their resume short listed, candidates list out various programming languages, and technology name that they don’t even know properly. Though this might land you in front of an interviewer but rest be assured that your experience will not be good. Within minutes into the interview, the interviewer will come to know about your level and skills.

9. Being Under-confident

As with any interview, self-confidence plays an important part in programming interviews too. It happens that sometimes interviewers deliberately ask you to solve a wrong question. Here, if you have good knowledge and self-confidence, you can easily figure out the flaw in question and communicate back to the interviewer.
Even if you answer the questions correctly but are not confident of your answers, your chances of getting selected for the job are not good. This is because no organization would want under confident people working for them.

10. Stressed-out and Deprived of Sleep

The night before the interview, don’t stay-up late to prepare for the interview. In an interview, you need to be present with a fresh mind and lot of energy.
Many candidates might get blanked out once they start solving a problem especially on a white board in front of an interview panel. This generally happens because of the stress candidates take just before the interview. It’s always advisable to sleep well and relax before an interview.

Monday, May 20, 2013

About “trunk”, “tags” & “branches”

Using the trunk and branches

Subversion is a very general purpose tool, and unlike some other version control systems, any specific structure you define in your repository is completely optional.
The most common structure people use to configure their Subversion repositories is to have three folders defined at the repository's top level, trunk, tags and branches.
In smaller software projects, the trunk folder usually contains the most current version of the project's files. Most people in the team will just have a working copy of the trunk stored on their own computers and do all of their work in there. In bigger projects, developers will often make a copy of the trunk in the branches folder, and do their work in a working copy of the branch they created.
Some teams let every developer work in their own development branch, periodically merging everyone's individual changes to the trunk. Another common approach is to create a seperate branch for major new versions of the project, or other large endeavors that will carry some dramatic changes to the entire project with them.

Tagging releases

The tags folder is used to create snapshots of the trunk at a specific revision. Most software teams use this method to tag individual releases of their software. Using tags in this way makes it easier for developers to get a copy of their application's source code for a specific given release.

It's just copying, mostly

To create a branch or tag, all you need to do is copying the entire trunk folder over to a new subfolder in the branches or tags folder. The easiest way to do this is to browse the repository (not the working copy) and make such a copy there. That way, Subversion can take care of the actual copying, instead of having to transfer the entire contents of the trunk to your local working copy and sending it back to the server again.
There is no need to be concerned about disk space, Subversion is quite smart about making copies. If two folders are nearly identical, Subversion will only store just enough information to be able to reliably reconstruct the files when you do a checkout or export.

Real world examples

To learn more about how to use the branches and tags folders effectively, take a look at any large open-source project's Subversion repository, like for instance WebKit or Python. You’ll see that new development usually takes place in a branch and you’ll find tags for just about any release that ever went out the door.
Even if you do not see a direct need for the trunk, tags and branches folders in your project right now, if you use Subversion for something that will be released to people outside your team, it is still recommended to set up this default structure. If you ever do need to tag or branch, it's a lot easier to already have those folders around than to re-organize your repository later on.

Wednesday, May 8, 2013

SVN Server on Ubuntu 12.04 LTS with Web Access


sudo apt-get install subversion libapache2-svn apache2

Make the directory where you want to keep the svn repositories and edit the dav_svn.conf file:
sudo mkdir /svn
sudo nano /etc/apache2/mods-enabled/dav_svn.conf

Delete all the data and make it simple like this :-)
<Location /svn>
DAV svn
SVNParentPath /svn
AuthType Basic
AuthName "Subversion Repository"
AuthUserFile /etc/apache2/dav_svn.passwd
Require valid-user
</Location>

To create a svn user , use the following command:
sudo htpasswd -cm /etc/apache2/dav_svn.passwd arbab

We only need to use the -c option for the FIRST TIME, when you create a user, after that you will only use the -m option.
Move to the folder, where you want to keep your repositories and create your first repository:
cd /svn
sudo svnadmin create test_repo

Make sure you set the permissions of the /svn directory to apache with the following command:
sudo chown -R www-data:www-data /svn

Restart the apache2 service:
sudo /etc/init.d/apache2 restart

On Windows machine, Right-Click  and select the SVN Checkout, inside the directory where we want to store the working copy (In my case, it’s on desktop).

Enter the repository’s URL and click OK, it will be prompted for a login and password.  Enter the login information and click OK.

We shall receive a screen that looks like this:

Create some test file inside the checkout repository, Right-Click and Select the SVN Commit:

Enter the comments that describe the purpose of this commit:

It will be prompted for a login and password.  Enter the login information and click OK.

The result of the commit will appear in a dialog window.

Congrats! now we have a working SVN server on Ubuntu 12.04 :-)
Hope this will help you!
Please Remember me in your prayers!

Thursday, May 2, 2013

Variety Wallpaper changer for Ubuntu 12.10/12.04/Linux Mint 14/13


Variety is a wallpaper changer for Ubuntu which is featureful, yet slim and very easy to use. It can automatically download wallpapers from various online sources, allows rotating them on a regular interval or on demand, and provides easy to use ways to separate the great images from the junk.

variety wallpaper changer

Ubuntu wallpaper changer

Ubuntu variety

variety wallpaper changer

Changes in this Variety 0.4.12:
  • Updated tips and changes txt
  • Bugfix: Save original wallpaper name when quotes are applied
  • Bugfix: a download was forcing the thumbs window to reshow when already hidden
  • Minor fix
  • Improvements related to image preparing; Minor bug-fixes
  • Trigger a download when too few images found; Livelier thumbs when showing folders, not only history and downloads (new downloads are auto-shown)
  • Merged in variety-instant-preferences
  • Fix: removed class Window was still referred at one place
  • Replaced some unnecessary timers with calls to GObject.timeout_add
  • Reordered prefs tabs. Command-line way to set options
  • Added UI preferences tab
  • Added Rating option to main menu - optional (off by default)
  • Added funtionality to set image ratings
  • Filtering by rating added; Preparations for adding simple functionality to rate images
  • Ensure wallpaper is changed even if the current background is a solid color in Gnome/Unity

To install Variety in Ubuntu/Linux Mint open Terminal (Press Ctrl+Alt+T) and copy the following commands in the Terminal:
  • sudo add-apt-repository ppa:peterlevi/ppa
  • sudo apt-get update
  • sudo apt-get install variety
That's it
Download source for other distributions
Related Posts Plugin for WordPress, Blogger...

Monday, April 8, 2013

Top 25 Best Linux Performance Monitoring and Debugging Tools

I’ve compiled 25 performance monitoring and debugging tools that will be helpful when you are working on Linux environment. This list is not comprehensive or authoritative by any means.
However this list has enough tools for you to play around and pick the one that is suitable your specific debugging and monitoring scenario.

1. SAR

Using sar utility you can do two things: 1) Monitor system real time performance (CPU, Memory, I/O, etc) 2) Collect performance data in the background on an on-going basis and do analysis on the historical data to identify bottlenecks.
Sar is part of the sysstat package. The following are some of the things you can do using sar utility.
  • Collective CPU usage
  • Individual CPU statistics
  • Memory used and available
  • Swap space used and available
  • Overall I/O activities of the system
  • Individual device I/O activities
  • Context switch statistics
  • Run queue and load average data
  • Network statistics
  • Report sar data from a specific time
  • and lot more..
The following sar command will display the system CPU statistics 3 times (with 1 second interval).
The following “sar -b” command reports I/O statistics. “1 3″ indicates that the sar -b will be executed for every 1 second for a total of 3 times.
$ sar -b 1 3
Linux 2.6.18-194.el5PAE (dev-db)        03/26/2011      _i686_  (8 CPU)

01:56:28 PM       tps      rtps      wtps   bread/s   bwrtn/s
01:56:29 PM    346.00    264.00     82.00   2208.00    768.00
01:56:30 PM    100.00     36.00     64.00    304.00    816.00
01:56:31 PM    282.83     32.32    250.51    258.59   2537.37
Average:       242.81    111.04    131.77    925.75   1369.90
More SAR examples: How to Install/Configure Sar (sysstat) and 10 Useful Sar Command Examples

2. Tcpdump

tcpdump is a network packet analyzer. Using tcpdump you can capture the packets and analyze it for any performance bottlenecks.
The following tcpdump command example displays captured packets in ASCII.
$ tcpdump -A -i eth0
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 96 bytes
14:34:50.913995 IP valh4.lell.net.ssh > yy.domain.innetbcp.net.11006: P 1457239478:1457239594(116) ack 1561461262 win 63652
E.....@.@..]..i...9...*.V...]...P....h....E...>{..U=...g.
......G..7\+KA....A...L.
14:34:51.423640 IP valh4.lell.net.ssh > yy.domain.innetbcp.net.11006: P 116:232(116) ack 1 win 63652
E.....@.@..\..i...9...*.V..*]...P....h....7......X..!....Im.S.g.u:*..O&....^#Ba...
E..(R.@.|.....9...i.*...]...V..*P..OWp........
Using tcpdump you can capture packets based on several custom conditions. For example, capture packets that flow through a particular port, capture tcp communication between two specific hosts, capture packets that belongs to a specific protocol type, etc.
More tcpdump examples: 15 TCPDUMP Command Examples

3. Nagios

Nagios is an open source monitoring solution that can monitor pretty much anything in your IT infrastructure. For example, when a server goes down it can send a notification to your sysadmin team, when a database goes down it can page your DBA team, when the a web server goes down it can notify the appropriate team.
You can also set warning and critical threshold level for various services to help you proactively address the issue. For example, it can notify sysadmin team when a disk partition becomes 80% full, which will give enough time for the sysadmin team to work on adding more space before the issue becomes critical.
Nagios also has a very good user interface from where you can monitor the health of your overall IT infrastructure.
The following are some of the things you can monitor using Nagios:
  • Any hardware (servers, switches, routers, etc)
  • Linux servers and Windows servers
  • Databases (Oracle, MySQL, PostgreSQL, etc)
  • Various services running on your OS (sendmail, nis, nfs, ldap, etc)
  • Web servers
  • Your custom application
  • etc.
More Nagios examples: How to install and configure Nagios, monitor remote Windows machine, and monitor remote Linux server.

4. Iostat

iostat reports CPU, disk I/O, and NFS statistics. The following are some of iostat command examples.
Iostat without any argument displays information about the CPU usage, and I/O statistics about all the partitions on the system as shown below.
$ iostat
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.68    0.00    0.52    2.03    0.00   91.76

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda             194.72      1096.66      1598.70 2719068704 3963827344
sda1            178.20       773.45      1329.09 1917686794 3295354888
sda2             16.51       323.19       269.61  801326686  668472456
sdb             371.31       945.97      1073.33 2345452365 2661206408
sdb1            371.31       945.95      1073.33 2345396901 2661206408
sdc             408.03       207.05       972.42  513364213 2411023092
sdc1            408.03       207.03       972.42  513308749 2411023092
By default iostat displays I/O data for all the disks available in the system. To view statistics for a specific device (For example, /dev/sda), use the option -p as shown below.
$ iostat -p sda
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           5.68    0.00    0.52    2.03    0.00   91.76

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda             194.69      1096.51      1598.48 2719069928 3963829584
sda2            336.38        27.17        54.00   67365064  133905080
sda1            821.89         0.69       243.53    1720833  603892838

5. Mpstat

mpstat reports processors statistics. The following are some of mpstat command examples.
Option -A, displays all the information that can be displayed by the mpstat command as shown below. This is really equivalent to “mpstat -I ALL -u -P ALL” command.
$ mpstat -A
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (4 CPU)

10:26:34 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
10:26:34 PM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.99
10:26:34 PM    0    0.01    0.00    0.01    0.01    0.00    0.00    0.00    0.00   99.98
10:26:34 PM    1    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
10:26:34 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
10:26:34 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

10:26:34 PM  CPU    intr/s
10:26:34 PM  all     36.51
10:26:34 PM    0      0.00
10:26:34 PM    1      0.00
10:26:34 PM    2      0.04
10:26:34 PM    3      0.00

10:26:34 PM  CPU     0/s     1/s     8/s     9/s    12/s    14/s    15/s    16/s    19/s    20/s    21/s    33/s   NMI/s   LOC/s   SPU/s   PMI/s   PND/s   RES/s   CAL/s   TLB/s   TRM/s   THR/s   MCE/s   MCP/s   ERR/s   MIS/s
10:26:34 PM    0    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    7.47    0.00    0.00    0.00    0.00    0.02    0.00    0.00    0.00    0.00    0.00    0.00    0.00
10:26:34 PM    1    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    4.90    0.00    0.00    0.00    0.00    0.03    0.00    0.00    0.00    0.00    0.00    0.00    0.00
10:26:34 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.04    0.00    0.00    0.00    0.00    0.00    3.32    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00
10:26:34 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.
mpstat Option -P ALL, displays all the individual CPUs (or Cores) along with its statistics as shown below.
$ mpstat -P ALL
Linux 2.6.32-100.28.5.el6.x86_64 (dev-db)       07/09/2011      _x86_64_        (4 CPU)

10:28:04 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle
10:28:04 PM  all    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00   99.99
10:28:04 PM    0    0.01    0.00    0.01    0.01    0.00    0.00    0.00    0.00   99.98
10:28:04 PM    1    0.00    0.00    0.01    0.00    0.00    0.00    0.00    0.00   99.98
10:28:04 PM    2    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00
10:28:04 PM    3    0.00    0.00    0.00    0.00    0.00    0.00    0.00    0.00  100.00

6. Vmstat

vmstat reports virtual memory statistics. The following are some of vmstat command examples.
vmstat by default will display the memory usage (including swap) as shown below.
$ vmstat
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0 305416 260688  29160 2356920    2    2     4     1    0    0  6  1 92  2  0

To execute vmstat every 2 seconds for 10 times, do the following. After executing 10 times, it will stop automatically.
$ vmstat 2 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 537144 182736 6789320    0    0     0     0    1    1  0  0 100  0  0
 0  0      0 537004 182736 6789320    0    0     0     0   50   32  0  0 100  0  0
..
iostat and vmstat are part of the sar utility. You should install sysstat package to get iostat and vmstat working.
More examples: 24 iostat, vmstat and mpstat command Examples

7. PS Command

Process is a running instance of a program. Linux is a multitasking operating system, which means that more than one process can be active at once. Use ps command to find out what processes are running on your system.
ps command also give you lot of additional information about the running process which will help you identify any performance bottlenecks on your system.
The following are few ps command examples.
Use -u option to display the process that belongs to a specific username. When you have multiple username, separate them using a comma. The example below displays all the process that are owned by user wwwrun, or postfix.
$ ps -f -u wwwrun,postfix
UID        PID  PPID  C STIME TTY          TIME CMD
postfix   7457  7435  0 Mar09 ?        00:00:00 qmgr -l -t fifo -u
wwwrun    7495  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7496  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7497  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7498  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun    7499  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10078  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
wwwrun   10082  7491  0 Mar09 ?        00:00:00 /usr/sbin/httpd2-prefork -f /etc/apache2/httpd.conf
postfix  15677  7435  0 22:23 ?        00:00:00 pickup -l -t fifo -u
The example below display the process Id and commands in a hierarchy. –forest is an argument to ps command which displays ASCII art of process tree. From this tree, we can identify which is the parent process and the child processes it forked in a recursive manner.
$ ps -e -o pid,args --forest
  468  \_ sshd: root@pts/7
  514  |   \_ -bash
17484  \_ sshd: root@pts/11
17513  |   \_ -bash
24004  |       \_ vi ./790310__11117/journal
15513  \_ sshd: root@pts/1
15522  |   \_ -bash
 4280  \_ sshd: root@pts/5
 4302  |   \_ -bash
More ps examples: 7 Practical PS Command Examples for Process Monitoring

8. Free

Free command displays information about the physical (RAM) and swap memory of your system.
In the example below, the total physical memory on this system is 1GB. The values displayed below are in KB.
# free
       total   used    free   shared  buffers  cached
Mem: 1034624   1006696 27928  0       174136   615892
-/+ buffers/cache:     216668      817956
Swap:    2031608       0    2031608
The following example will display the total memory on your system including RAM and Swap.
In the following command:
  • option m displays the values in MB
  • option t displays the “Total” line, which is sum of physical and swap memory values
  • option o is to hide the buffers/cache line from the above example.
# free -mto
                  total       used      free     shared    buffers     cached
Mem:          1010        983         27              0         170           601
Swap:          1983            0    1983
Total:          2994        983     2011

9. TOP

Top command displays all the running process in the system ordered by certain columns. This displays the information real-time.
You can kill a process without existing from top. Once you’ve located a process that needs to be killed, press “k” which will ask for the process id, and signal to send. If you have the privilege to kill that particular PID, it will get killed successfully.
PID to kill: 1309
Kill PID 1309 with signal [15]:
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1309 geek   23   0 2483m 1.7g  27m S    0 21.8  45:31.32 gagent
 1882 geek   25   0 2485m 1.7g  26m S    0 21.7  22:38.97 gagent
 5136 root    16   0 38040  14m 9836 S    0  0.2   0:00.39 nautilus
Use top -u to display a specific user processes only in the top command output.
$ top -u geek
While unix top command is running, press u which will ask for username as shown below.
Which user (blank for all): geek
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 1309 geek   23   0 2483m 1.7g  27m S    0 21.8  45:31.32 gagent
 1882 geek   25   0 2485m 1.7g  26m S    0 21.7  22:38.97 gagent
More top examples: 15 Practical Linux Top Command Examples

10. Pmap

pmap command displays the memory map of a given process. You need to pass the pid as an argument to the pmap command.
The following example displays the memory map of the current bash shell. In this example, 5732 is the PID of the bash shell.
$ pmap 5732
5732:   -bash
00393000    104K r-x--  /lib/ld-2.5.so
003b1000   1272K r-x--  /lib/libc-2.5.so
00520000      8K r-x--  /lib/libdl-2.5.so
0053f000     12K r-x--  /lib/libtermcap.so.2.0.8
0084d000     76K r-x--  /lib/libnsl-2.5.so
00c57000     32K r-x--  /lib/libnss_nis-2.5.so
00c8d000     36K r-x--  /lib/libnss_files-2.5.so
b7d6c000   2048K r----  /usr/lib/locale/locale-archive
bfd10000     84K rw---    [ stack ]
 total     4796K
pmap -x gives some additional information about the memory maps.
$  pmap -x 5732
5732:   -bash
Address   Kbytes     RSS    Anon  Locked Mode   Mapping
00393000     104       -       -       - r-x--  ld-2.5.so
003b1000    1272       -       -       - r-x--  libc-2.5.so
00520000       8       -       -       - r-x--  libdl-2.5.so
0053f000      12       -       -       - r-x--  libtermcap.so.2.0.8
0084d000      76       -       -       - r-x--  libnsl-2.5.so
00c57000      32       -       -       - r-x--  libnss_nis-2.5.so
00c8d000      36       -       -       - r-x--  libnss_files-2.5.so
b7d6c000    2048       -       -       - r----  locale-archive
bfd10000      84       -       -       - rw---    [ stack ]
-------- ------- ------- ------- -------
total kB    4796       -       -       -
To display the device information of the process maps use ‘pamp -d pid’.

11. Netstat

Netstat command displays various network related information such as network connections, routing tables, interface statistics, masquerade connections, multicast memberships etc.,
The following are some netstat command examples.
List all ports (both listening and non listening) using netstat -a as shown below.
# netstat -a | more
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 localhost:30037         *:*                     LISTEN
udp        0      0 *:bootpc                *:*                                

Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ACC ]     STREAM     LISTENING     6135     /tmp/.X11-unix/X0
unix  2      [ ACC ]     STREAM     LISTENING     5140     /var/run/acpid.socket
Use the following netstat command to find out on which port a program is running.
# netstat -ap | grep ssh
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        1      0 dev-db:ssh           101.174.100.22:39213        CLOSE_WAIT  -
tcp        1      0 dev-db:ssh           101.174.100.22:57643        CLOSE_WAIT  -
Use the following netstat command to find out which process is using a particular port.
# netstat -an | grep ':80'
More netstat examples: 10 Netstat Command Examples

12. IPTraf

IPTraf is a IP Network Monitoring Software. The following are some of the main features of IPTraf:
  • It is a console based (text-based) utility.
  • This displays IP traffic crossing over your network. This displays TCP flag, packet and byte counts, ICMP, OSPF packet types, etc.
  • Displays extended interface statistics (including IP, TCP, UDP, ICMP, packet size and count, checksum errors, etc.)
  • LAN module discovers hosts automatically and displays their activities
  • Protocol display filters to view selective protocol traffic
  • Advanced Logging features
  • Apart from ethernet interface it also supports FDDI, ISDN, SLIP, PPP, and loopback
  • You can also run the utility in full screen mode. This also has a text-based menu.
More info: IPTraf Home Page. IPTraf screenshot.

13. Strace

Strace is used for debugging and troubleshooting the execution of an executable on Linux environment. It displays the system calls used by the process, and the signals received by the process.
Strace monitors the system calls and signals of a specific program. It is helpful when you do not have the source code and would like to debug the execution of a program. strace provides you the execution sequence of a binary from start to end.
Trace a Specific System Calls in an Executable Using Option -e
Be default, strace displays all system calls for the given executable. The following example shows the output of strace for the Linux ls command.
$ strace ls
execve("/bin/ls", ["ls"], [/* 21 vars */]) = 0
brk(0)                                  = 0x8c31000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
mmap2(NULL, 8192, PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb78c7000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat64(3, {st_mode=S_IFREG|0644, st_size=65354, ...}) = 0
To display only a specific system call, use the strace -e option as shown below.
$ strace -e open ls
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib/libselinux.so.1", O_RDONLY)  = 3
open("/lib/librt.so.1", O_RDONLY)       = 3
open("/lib/libacl.so.1", O_RDONLY)      = 3
open("/lib/libc.so.6", O_RDONLY)        = 3
open("/lib/libdl.so.2", O_RDONLY)       = 3
open("/lib/libpthread.so.0", O_RDONLY)  = 3
open("/lib/libattr.so.1", O_RDONLY)     = 3
open("/proc/filesystems", O_RDONLY|O_LARGEFILE) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
open(".", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 3
More strace examples: 7 Strace Examples to Debug the Execution of a Program in Linux

14. Lsof

Lsof stands for ls open files, which will list all the open files in the system. The open files include network connection, devices and directories. The output of the lsof command will have the following columns:
  • COMMAND process name.
  • PID process ID
  • USER Username
  • FD file descriptor
  • TYPE node type of the file
  • DEVICE device number
  • SIZE file size
  • NODE node number
  • NAME full path of the file name.
To view all open files of the system, execute the lsof command without any parameter as shown below.
# lsof | more
COMMAND     PID       USER   FD      TYPE     DEVICE      SIZE       NODE NAME
init          1       root  cwd       DIR        8,1      4096          2 /
init          1       root  rtd       DIR        8,1      4096          2 /
init          1       root  txt       REG        8,1     32684     983101 /sbin/init
init          1       root  mem       REG        8,1    106397     166798 /lib/ld-2.3.4.so
init          1       root  mem       REG        8,1   1454802     166799 /lib/tls/libc-2.3.4.so
init          1       root  mem       REG        8,1     53736     163964 /lib/libsepol.so.1
init          1       root  mem       REG        8,1     56328     166811 /lib/libselinux.so.1
init          1       root   10u     FIFO       0,13                  972 /dev/initctl
migration     2       root  cwd       DIR        8,1      4096          2 /
skipped..
To view open files by a specific user, use lsof -u option to display all the files opened by a specific user.
# lsof -u ramesh
vi      7190 ramesh  txt    REG        8,1   474608   475196 /bin/vi
sshd    7163 ramesh    3u  IPv6   15088263               TCP dev-db:ssh->abc-12-12-12-12.
To list users of a particular file, use lsof as shown below. In this example, it displays all users who are currently using vi.
# lsof /bin/vi
COMMAND  PID  USER    FD   TYPE DEVICE   SIZE   NODE NAME
vi      7258  root   txt    REG    8,1 474608 475196 /bin/vi
vi      7300  ramesh txt    REG    8,1 474608 475196 /bin/vi

15. Ntop

Ntop is just like top, but for network traffic. ntop is a network traffic monitor that displays the network usage.
You can also access ntop from browser to get the traffic information and network status.
The following are some the key features of ntop:
  • Display network traffic broken down by protocols
  • Sort the network traffic output based on several criteria
  • Display network traffic statistics
  • Ability to store the network traffic statistics using RRD
  • Identify the identify of the users, and host os
  • Ability to analyze and display IT traffic
  • Ability to work as NetFlow/sFlow collector for routers and switches
  • Displays network traffic statistics similar to RMON
  • Works on Linux, MacOS and Windows
More info: Ntop home page

16. GkrellM

GKrellM stands for GNU Krell Monitors, or GTK Krell Meters. It is GTK+ toolkit based monitoring program, that monitors various sytem resources. The UI is stakable. i.e you can add as many monitoring objects you want one on top of another. Just like any other desktop UI based monitoring tools, it can monitor CPU, memory, file system, network usage, etc. But using plugins you can monitoring external applications.
More info: GkrellM home page

17. w and uptime

While monitoring system performance, w command will hlep to know who is logged on to the system.
$ w
09:35:06 up 21 days, 23:28,  2 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM          LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     :0            24Oct11  21days 1:05   1:05 /usr/bin/Xorg :0 -nr -verbose
ramesh   pts/0    192.168.1.10  Mon14    0.00s  15.55s 0.26s sshd: localuser [priv]
john     pts/0    192.168.1.11  Mon07    0.00s  19.05s 0.20s sshd: localuser [priv]
jason    pts/0    192.168.1.12  Mon07    0.00s  21.15s 0.16s sshd: localuser [priv]
For each and every user who is logged on, it displays the following info:
  • Username
  • tty info
  • Remote host ip-address
  • Login time of the user
  • How long the user has been idle
  • JCPU and PCUP
  • The command of the current process the user is executing
Line 1 of the w command output is similar to the uptime command output. It displays the following:
  • Current time
  • How long the system has been up and running
  • Total number of users who are currently logged on the system
  • Load average for the last 1, 5 and 15 minutes
If you want only the uptime information, use the uptime command.
$ uptime
 09:35:02 up 106 days, 28 min,  2 users,  load average: 0.08, 0.11, 0.05
Please note that both w and uptime command gets the information from the /var/run/utmp data file.

18. /proc

/proc is a virtual file system. For example, if you do ls -l /proc/stat, you’ll notice that it has a size of 0 bytes, but if you do “cat /proc/stat”, you’ll see some content inside the file.
Do a ls -l /proc, and you’ll see lot of directories with just numbers. These numbers represents the process ids, the files inside this numbered directory corresponds to the process with that particular PID.
The following are the important files located under each numbered directory (for each process):
  • cmdline – command line of the command.
  • environ – environment variables.
  • fd – Contains the file descriptors which is linked to the appropriate files.
  • limits – Contains the information about the specific limits to the process.
  • mounts – mount related information
The following are the important links under each numbered directory (for each process):
  • cwd – Link to current working directory of the process.
  • exe – Link to executable of the process.
  • root – Link to the root directory of the process.
More /proc examples: Explore Linux /proc File System

19. KDE System Guard

This is also called as KSysGuard. On Linux desktops that run KDE, you can use this tool to monitor system resources. Apart from monitoring the local system, this can also monitor remote systems.
If you are running KDE desktop, go to Applications -> System -> System Monitor, which will launch the KSysGuard. You can also type ksysguard from the command line to launch it.
This tool displays the following two tabs:
  • Process Table – Displays all active processes. You can sort, kill, or change priority of the processes from here
  • System Load – Displays graphs for CPU, Memory, and Network usages. These graphs can be customized by right cliking on any of these graphs.
To connect to a remote host and monitor it, click on File menu -> Monitor Remote Machine -> specify the ip-address of the host, the connection method (for example, ssh). This will ask you for the username/password on the remote machine. Once connected, this will display the system usage of the remote machine in the Process Table and System Load tabs.

20. GNOME System Monitor

On Linux desktops that run GNOME, you can use the this tool to monitor processes, system resources, and file systems from a graphical interface. Apart from monitoring, you can also use this UI tool to kill a process, change the priority of a process.
If you are running GNOME desktop, go to System -> Administration -> System Monitor, which will launch the GNOME System Monitor. You can also type gnome-system-monitor from the command line to launch it.
This tool has the following four tabs:
  • System – Displays the system information including Linux distribution version, system resources, and hardware information.
  • Processes – Displays all active processes that can be sorted based on various fields
  • Resources – Displays CPU, memory and network usages
  • File Systems – Displays information about currently mounted file systems
More info: GNOME System Monitor home page

21. Conky

Conky is a system monitor or X. Conky displays information in the UI using what it calls objects. By default there are more than 250 objects that are bundled with conky, which displays various monitoring information (CPU, memory, network, disk, etc.). It supports IMAP, POP3, several audio players.
You can monitor and display any external application by craeting your own objects using scripting. The monitoring information can be displays in various format: Text, graphs, progress bars, etc. This utility is extremly configurable.
More info: Conky screenshots

22. Cacti

Cacti is a PHP based UI frontend for the RRDTool. Cacti stores the data required to generate the graph in a MySQL database.
The following are some high-level features of Cacti:
  • Ability to perform the data gathering and store it in MySQL database (or round robin archives)
  • Several advanced graphing featurs are available (grouping of GPRINT graph items, auto-padding for graphs, manipulate graph data using CDEF math function, all RRDTool graph items are supported)
  • The data source can gather local or remote data for the graph
  • Ability to fully customize Round robin archive (RRA) settings
  • User can define custom scripts to gather data
  • SNMP support (php-snmp, ucd-snmp, or net-snmp) for data gathering
  • Built-in poller helps to execute custom scripts, get SNMP data, update RRD files, etc.
  • Highly flexible graph template features
  • User friendly and customizable graph display options
  • Create different users with various permission sets to access the cacti frontend
  • Granular permission levels can be set for the individual user
  • and lot more..
More info: Cacti home page

23. Vnstat

vnstat is a command line utility that displays and logs network traffic of the interfaces on your systems. This depends on the network statistics provided by the kernel. So, vnstat doesn’t add any additional load to your system for monitoring and logging the network traffic.
vnstat without any argument will give you a quick summary with the following info:
  • The last time when the vnStat datbase located under /var/lib/vnstat/ was updated
  • From when it started collecting the statistics for a specific interface
  • The network statistic data (bytes transmitted, bytes received) for the last two months, and last two days.
# vnstat
Database updated: Sat Oct 15 11:54:00 2011

   eth0 since 10/01/11

          rx:  12.89 MiB      tx:  6.94 MiB      total:  19.82 MiB

   monthly
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
       Sep '11     12.90 MiB |    6.90 MiB |   19.81 MiB |    0.14 kbit/s
       Oct '11     12.89 MiB |    6.94 MiB |   19.82 MiB |    0.15 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        29 MiB |      14 MiB |      43 MiB |

  daily
                     rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
     yesterday      4.30 MiB |    2.42 MiB |    6.72 MiB |    0.64 kbit/s
         today      2.03 MiB |    1.07 MiB |    3.10 MiB |    0.59 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated         4 MiB |       2 MiB |       6 MiB |
Use “vnstat -t” or “vnstat –top10″ to display all time top 10 traffic days.
$ vnstat --top10

 eth0  /  top 10

    #      day          rx      |     tx      |    total    |   avg. rate
   -----------------------------+-------------+-------------+---------------
    1   10/12/11       4.30 MiB |    2.42 MiB |    6.72 MiB |    0.64 kbit/s
    2   10/11/11       4.07 MiB |    2.17 MiB |    6.24 MiB |    0.59 kbit/s
    3   10/10/11       2.48 MiB |    1.28 MiB |    3.76 MiB |    0.36 kbit/s
    ....
   -----------------------------+-------------+-------------+---------------
More vnstat Examples: How to Monitor and Log Network Traffic using VNStat

24. Htop

htop is a ncurses-based process viewer. This is similar to top, but is more flexible and user friendly. You can interact with the htop using mouse. You can scroll vertically to view the full process list, and scroll horizontally to view the full command line of the process.
htop output consists of three sections 1) header 2) body and 3) footer.
Header displays the following three bars, and few vital system information. You can change any of these from the htop setup menu.
  • CPU Usage: Displays the %used in text at the end of the bar. The bar itself will show different colors. Low-priority in blue, normal in green, kernel in red.
  • Memory Usage
  • Swap Usage
Body displays the list of processes sorted by %CPU usage. Use arrow keys, page up, page down key to scoll the processes.
Footer displays htop menu commands.
More info: HTOP Screenshot and Examples

25. Socket Statistics – SS

ss stands for socket statistics. This displays information that are similar to netstat command.
To display all listening sockets, do ss -l as shown below.
$ ss -l
Recv-Q Send-Q   Local Address:Port     Peer Address:Port
0      100      :::8009                :::*
0      128      :::sunrpc              :::*
0      100      :::webcache            :::*
0      128      :::ssh                 :::*
0      64       :::nrpe                :::*
The following displays only the established connection.
$ ss -o state established
Recv-Q Send-Q   Local Address:Port   Peer Address:Port
0      52       192.168.1.10:ssh   192.168.2.11:55969    timer:(on,414ms,0)
The following displays socket summary statistics. This displays the total number of sockets broken down by the type.
$ ss -s
Total: 688 (kernel 721)
TCP:   16 (estab 1, closed 0, orphaned 0, synrecv 0, timewait 0/0), ports 11

Transport Total     IP        IPv6
*         721       -         -
RAW       0         0         0
UDP       13        10        3
TCP       16        7         9
INET      29        17        12
FRAG      0         0         0