Wednesday, May 28, 2014

Install monitoring Utilities like "sar" and "iostat" in Debian Based Distros

Today, I was looking for "sar" (Report System and CPU performance) and "iostat" (Report Disk and IO Performance) on Debian Based Distro i.e. lubuntu.
But didn't find the command, infact it gives me hint that which package contain this command.

root@lubuntu1:~# iostat
The program 'iostat' is currently not installed. You can install it by typing:
apt-get install sysstat

Another, option if you want to know about that which package may contain specific command, just make use of "apt-cache". Just run below command to find out the related packages. Here, I am looking for "iostat".

root@lubuntu1:~# apt-cache search iostat
sysstat - system performance tools for Linux
dstat - versatile resource statistics tool
ganglia-modules-linux - Ganglia extra modules for Linux (IO, filesystems, multicpu)
ifstat - InterFace STATistics Monitoring
nicstat - print network traffic statistics
pcp-import-iostat2pcp - Tool for importing data from iostat into PCP archive logs
r-cran-epi - GNU R epidemiological analysis
r-cran-epibasix - GNU R Elementary Epidemiological Functions
r-cran-rms - GNU R regression modeling strategies by Frank Harrell
Now, we can see that sysstat contains all system performance tools. Let us check further what actually it contains for system monitoring.

root@lubuntu1:~# apt-cache show sysstat
Package: sysstat
Priority: optional
Section: admin
Installed-Size: 848
Maintainer: Ubuntu Developers <>
Original-Maintainer: Robert Luberda <>
Architecture: i386
Version: 10.2.0-1
Depends: bzip2, lsb-base (>= 3.0-6), ucf (>= 2.003), debconf (>= 0.5) | debconf-2.0, libc6 (>= 2.7), libsensors4 (>= 1:3.0.0)
Recommends: cron
Suggests: isag
Filename: pool/main/s/sysstat/sysstat_10.2.0-1_i386.deb
Size: 271332
MD5sum: a7057ea16e428c8d9b6a6fc8fd6a3a46
SHA1: b769779a1397b25a121a3a8c47892289a82f1320
SHA256: 0e6bba036c211aa7aa5d8ae89ae7f0fa9a134f60d458ce8da390a8163889b0b1
Description-en: system performance tools for Linux
 The sysstat package contains the following system performance tools:
  - sar: collects and reports system activity information;
  - iostat: reports CPU utilization and disk I/O statistics;
  - mpstat: reports global and per-processor statistics;
  - pidstat: reports statistics for Linux tasks (processes);
  - sadf: displays data collected by sar in various formats;
  - nfsiostat: reports I/O statistics for network filesystems;
  - cifsiostat: reports I/O statistics for CIFS filesystems.

 The statistics reported by sar deal with I/O transfer rates,
 paging activity, process-related activities, interrupts,
 network activity, memory and swap space utilization, CPU
 utilization, kernel activities and TTY statistics, among
 others. Both UP and SMP machines are fully supported.
Description-md5: 53969a0c693353fe5d132e483e9ab770
Origin: Ubuntu
Supported: 5y

So, after installing sysstat, we'll be able to run all highlighted commands like iostat, mpstat, sar etc.

So, Finally Install the package using apt-get.

root@lubuntu1:~# apt-get install sysstat
Reading package lists... Done
Building dependency tree      
Reading state information... Done
Suggested packages:
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 128 not upgraded.
Need to get 271 kB of archives.
After this operation, 868 kB of additional disk space will be used.
Get:1 trusty/main sysstat i386 10.2.0-1 [271 kB]
Fetched 271 kB in 8s (30.2 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package sysstat.
(Reading database ... 139942 files and directories currently installed.)
Preparing to unpack .../sysstat_10.2.0-1_i386.deb ...
Unpacking sysstat (10.2.0-1) ...
Processing triggers for man-db ( ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up sysstat (10.2.0-1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Processing triggers for ureadahead (0.100.0-16) ...
Now, your system have some good commands that you can use for monitoring. Below are few basic example. I'll cover each in separate post with details.

root@lubuntu1:~# iostat
Linux 3.13.0-24-generic (     05/28/2014     _i686_    (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          24.73    0.03   74.29    0.19    0.00    0.77

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               7.33        97.11        59.68    1809233    1111804
dm-0              0.01         0.04         0.00        768          0
dm-1              9.04        96.38        59.55    1795609    1109536
dm-2              0.15         0.56         0.12      10377       2172

For checking CPU statistics only for every two 2 seconds use "-c 2" option as below:

root@lubuntu1:~# iostat -c   2
Linux 3.13.0-24-generic (     05/28/2014     _i686_    (1 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          24.73    0.03   74.29    0.19    0.00    0.77

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          21.39    0.00   78.61    0.00    0.00    0.00
root@lubuntu1:~# mpstat
Linux 3.13.0-24-generic (     05/28/2014     _i686_    (1 CPU)

09:56:25 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
09:56:25 AM  all   24.75    0.03   74.15    0.19    0.00    0.13    0.00    0.00    0.00    0.76
root@lubuntu1:~# mpstat -P ALL
Linux 3.13.0-24-generic (     05/28/2014     _i686_    (1 CPU)

09:56:31 AM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
09:56:31 AM  all   24.75    0.03   74.15    0.19    0.00    0.13    0.00    0.00    0.00    0.76
09:56:31 AM    0   24.75    0.03   74.15    0.19    0.00    0.13    0.00    0.00    0.00    0.76


Monday, May 26, 2014

How to take JVM Heap Dump using jmap

We have different options for taking a heap dump of running JAVA process and then analyze those dump for any kind for memory leak.
Today, We are going to do the same using "jmap" in-built utility provided with JDK. Using this you will get an advantage that you don't need to wait untill our JVM got crashed by using passing "-XX:+HeapDumpOnOutOfMemoryError" parameter to JVM process.

We can capture current status using jmap as below:
1.) Find out the jmap location(only if your JAVA_HOME variable not defined)
      #locate jmap
2.) After finding jmap location, move to that directory and run jmap to get live thread dump.
      #./jmap -dump:live,format=b,file=[file location] [pid]

For more details and options run the command with help option as below :

# ./jmap -help
    jmap [option] <pid>
        (to connect to running process)
    jmap [option] <executable <core>
        (to connect to a core file)
    jmap [option] [server_id@]<remote server IP or hostname>
        (to connect to remote debug server)

where <option> is one of:
    <none>               to print same info as Solaris pmap
    -heap                to print java heap summary
    -histo[:live]        to print histogram of java object heap; if the "live"
                         suboption is specified, only count live objects
    -permstat            to print permanent generation statistics
    -finalizerinfo       to print information on objects awaiting finalization
    -dump:<dump-options> to dump java heap in hprof binary format
                           live         dump only live objects; if not specified,
                                        all objects in the heap are dumped.
                           format=b     binary format
                           file=<file>  dump heap to <file>
                         Example: jmap -dump:live,format=b,file=heap.bin <pid>
    -F                   force. Use with -dump:<dump-options> <pid> or -histo
                         to force a heap dump or histogram when <pid> does not
                         respond. The "live" suboption is not supported
                         in this mode.
    -h | -help           to print this help message
    -J<flag>             to pass <flag> directly to the runtime system

Common Error that you can get is "[pid]: well-known file is not secure".
The error comes, when you run jmap with user other than with which Java process is running. To overcome this switch to the user with which java process is running and make sure that user has sufficient permission on the directory where you are going create heap dump file, otherwise you may get permission denied errors.

# ./jmap -dump:live,format=b,file=/[Path]/test.hprof 27114
27114: well-known file is not secure

Note: You can list running Java processes using jps(another in-built tool provided with JDK) somewhat similar to "ps" command.
$ ./jps | grep -vi jps

Hope this will help you :) !!

Wednesday, May 7, 2014

View Information about installed packages in Debian Based Dostro(Ubuntu/Lubuntu).

Some times we come to situations where we want to get information e.g. check if package is installed or not, if installed then all information related to that one.

Today, we are going to discuss the same on Lubuntu 14.04.There can be number of ways as in linux we have flexibility use number of commands and combined one to get desired results. Here, we will discuss about two most common methods i.e.

1.) Using dpkg to get list of all installed packages.

2.) View all information about installed package.

1.) Using dpkg to get list of all installed packages.

               dpkg(Debian Package Management System) is a package manager for all debian based systems. It is used to install, remove and list all the information about .deb packages. Mostly, dpkg package gets installed by default during installation time.

      a.) View All installed Software using dpkg -l:

                 In Below example I have used head to get just first few lines of output. Use without "head" option to get full list.
Fig-1 Using -l option
               Removing, first few unwanted lines using "sed".


              You can also save output to a file for later as below:

             Get Details with Name-Version-Arch together:

           Get total count of installed packages:


      b.) View All installed Software using dpkg --get-selections:

                  Another option we have available with dpkg is "--get-selections"(without quotes).
                 Get count using "wc" and you can compare this with last one :).

2.) View all information about installed package.

          Once, we get the list of installed packages, then we would really like to know more about particular package. So, we have different options available for the same. I am going share the tricks that I know :).

             a.) Using aptitude show "package" :


            b.) Using apt-cache show "package" :

Along with all above information, here I also want to share one more tip that can be more helpful. Sometime, you have a package installed and you don't know about the list of files created by that package. In that case "dpkg-query" can be tricky.

Suppose I have lighttpd installed and I want to get the list of all the files associated with this. Use below command as below :


Note: It will work on all debian based distros.