Tuesday 28 June 2016

Linux Machine performance Monitoring with VMSTAT command


 


vmstat (virtual memory statistics) is a computer system monitoring tool that collects and displays summary information about operating system memory, processes, interrupts, paging and block I/O.
              
vmstat is a tool that collects and reports data about your system’s memory, swap, and processor resource utilization in real time. It can be used to determine the root cause of performance and issues related to memory use.

The vmstat tool is available on most Unix and Unix-like operating systems, such as FreeBSD, Linux or Solaris. The syntax and output of vmstat often differs slightly between different operating systems.

   Options:-

-a       The -a switch displays active/inactive memory, given a 2.5.41 kernel or better.

-f       The  -f  switch displays the number of forks since boot.  This includes   the fork, vfork, and clone system calls,
         and is equivalent to the total number  of  tasks  created.  Each process is represented by one or more tasks,
         depending on thread usage.  This display does not repeat.

-m       The -m displays slabinfo.

-n       The -n switch causes the header to be displayed only once  rather  than periodically.

-s       The  -s  switch  displays  a table of various event counters and memory statistics. This display does not repeat.

delay    delay is the delay between updates in seconds.  If no delay  is  specified, only one report is printed with the 
         average values since boot.

count    count  is the number of updates.  If no count is specified and delay is defined, count defaults to infinity.

-d       The -d reports disk statistics (2.5.70 or above required).

-p       The -p followed by some partition name for detailed statistics  (2.5.70 or above required).

-s       The  -S  followed  by  k  or K or m or M switches outputs between 1000, 1024, 1000000, or 1048576 bytes.

-v       The -V switch results in displaying version information.


FIELD DESCRIPTION FOR VM MODE:-

Proc:-
r:  The number of processes waiting for run time.
b:  The number of processes in uninterruptible sleep.


Memory:-
  swpd: the amount of virtual memory used.
  free: the amount of idle memory.
  buff: the amount of memory used as buffers.
  cache: the amount of memory used as cache.
  inact: the amount of inactive memory. (-a option)
  active: the amount of active memory. (-a option)


Swap:-
  si: Amount of memory swapped in from disk (/s).
  so: Amount of memory swapped to disk (/s).


IO:-
  bi: Blocks received from a block device (blocks/s).
  bo: Blocks sent to a block device (blocks/s).


System:-
  in: The number of interrupts per second, including the clock.
  cs: The number of context switches per second.


CPU:-
  These are percentages of total CPU time.
  us: Time spent running non-kernel code. (user time, including nice time)
  sy: Time spent running kernel code. (system time)
  id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time.
  wa: Time spent waiting for IO. Prior to Linux 2.5.41, shown as zero.


FIELD DESCRIPTION FOR DISK MODE
:-

Reads:-
   total:   Total reads completed successfully
   merged:  Grouped reads (resulting in one I/O)
   sectors: Sectors read successfully
   ms:      Milliseconds spent reading

Writes:-
   total:   Total writes completed successfully
   merged:  Grouped writes (resulting in one I/O)
   sectors: Sectors written successfully
   ms:      Milliseconds spent writing

IO:-
   cur:     I/O in progress
   s:       seconds spent for I/O

FIELD DESCRIPTION FOR DISK PARTITION MODE:-

reads:            Total number of reads issued to this partition
read sectors:     Total read sectors for partition
writes :          Total number of writes issued to this partition
requested writes: Total number of write requests made for partition

FIELD DESCRIPTION FOR SLAB MODE:-

cache:    Cache name
num:      Number of currently active objects
total:    Total number of available objects
size:     Size of each object
pages:    Number of pages with at least one active object
totpages: Total number of allocated pages
pslab:    Number of pages per slab

FILES:-

/proc/meminfo
/proc/stat
/proc/*/stat

vmstat commands output with examples:-

1.    To display active and inactive memory information status.

[root@liniuxbiginner ~]# vmstat -a
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
 r  b   swpd   free  inact active   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 140268  48416 673184    0    0     0   103    5    2  0  0 99  1  0


2.     To display no. of forks since last boot.

[root@liniuxbiginner ~]# vmstat -f
      3357326 forks

 

3.      To Display slab information.

[root@liniuxbiginner ~]# vmstat -m
Cache                       Num  Total   Size  Pages
ip6-frags                     0      0    232     35
UDPLITEv6                     0      0   1088     30
UDPv6                        30     30   1088     30
tw_sock_TCPv6                29     29    280     29
request_sock_TCPv6            0      0    328     24
TCPv6                        15     15   2112     15
ext4_groupinfo_4k           336    336    144     28
ext4_inode_cache          76380  76516   1032     31
ext4_free_data              192    192     64     64
ext4_allocation_context      32     32    128     32
ext4_io_end                 560    560     72     56
ext4_extent_status        30197  38454     40    102
jbd2_journal_handle          85     85     48     85
jbd2_journal_head          1088   1088    120     34

Timestamp:-

1.     To display the timestamp along with every line.

[root@liniuxbiginner ~]# vmstat -t
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 139708 158452 193744    0    0     0   103    5    2  0  0 99  1  0        2016-06-28 11:15:17 UTC

 2.     To display every 3 seconds for 10 lines with timestamp.

[root@liniuxbiginner ~]# vmstat -t 3 10
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ ---timestamp---
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 0  0      0 139660 158472 193896    0    0     0   103    5    2  0  0 99  1  0        2016-06-28 11:22:28 UTC
 0  0      0 139024 158472 193900    0    0     0    12   65  181  0  0 99  0  0        2016-06-28 11:22:31 UTC
 0  0      0 139552 158472 193900    0    0     0   364   80  222  0  0 97  2  0        2016-06-28 11:22:34 UTC
 0  0      0 139544 158472 193900    0    0     0    20   66  196  0  0 99  0  0        2016-06-28 11:22:37 UTC
 0  0      0 139544 158472 193900    0    0     0    13   53  140  0  0 100  0  0       2016-06-28 11:22:40 UTC
 0  0      0 139516 158472 193908    0    0     0   432   96  251  0  0 97  2  0        2016-06-28 11:22:43 UTC
 0  0      0 138900 158472 193908    0    0     0    13   81  197  0  0 100  0  0       2016-06-28 11:22:46 UTC
 0  0      0 139272 158472 193908    0    0     0    11   64  169  1  0 99  0  0        2016-06-28 11:22:49 UTC
 0  0      0 139272 158472 193908    0    0     0     9   55  153  0  0 100  0  0       2016-06-28 11:22:52 UTC
 0  0      0 139552 158472 193908    0    0     0   337   74  200  0  0 98  2  0        2016-06-28 11:22:55 UTC


3.     To check vmstat version.

[root@liniuxbiginner ~]# vmstat -V
procps version 3.2.8

4.     To display the event counter statistics in table format.

[root@liniuxbiginner ~]# vmstat -s
      1019344  total memory
       879884  used memory
       673880  active memory
        48312  inactive memory
       139460  free memory
       158496  buffer memory
       194044  swap cache
            0  total swap
            0  used swap
            0  free swap
      1455666 non-nice user cpu ticks
       267970 nice user cpu ticks
       360814 system cpu ticks
    522241945 idle cpu ticks
      3575447 IO-wait cpu ticks
            0 IRQ cpu ticks
        17393 softirq cpu ticks
       276374 stolen cpu ticks
      1125512 pages paged in
    542706588 pages paged out
            0 pages swapped in
            0 pages swapped out
    329226970 interrupts
    870644185 CPU context switches
   1461822708 boot time
      3358327 forks

 To do not rediplay header:-

[root@liniuxbiginner ~]# vmstat -n
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      0 139484 158504 194092    0    0     0   103    5    2  0  0 99  1  0

 1.     To displays the read,I/O statistic and write of the disk statistics.

[root@liniuxbiginner ~]# vmstat -d
disk- ------------reads------------ ------------writes----------- -----IO------
       total merged sectors      ms  total merged sectors      ms    cur    sec
xvda   80091     47 2248744  665380 28417545 8633645 1085463440 150978028      0  37475
xvdb     286      0    2288     272      0      0       0       0      0      0

2.      To display report summary statistics about disk activity.

[root@liniuxbiginner ~]# vmstat -D
            2 disks
            1 partitions
        80378 total reads
           47 merged reads
      2251040 read sectors
       665664 milli reading
     28418044 writes
      8633788 merged writes
   1085482896 written sectors
    150980548 milli writing
            0 inprogress IO
        37475 milli spent IO

3.     To display reports with memory sizes reported in megabytes.you choose units  (k, K, m, M) default is K (1024 bytes) in the default mode.

[root@liniuxbiginner ~]# vmstat -S m 3 1
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      0    142    162    198    0    0     0   103    5    2  0  0 99  1  0

 


Wednesday 22 June 2016

vipw -s & vigr -s Command use in Linux & Ubuntu for editing /etc/passwd, shadow, and group files / The best way to edit /etc/passwd, shadow, and group files.



The best way to edit /etc/passwd, or shadow or group file is to use vipw command. Traditionally (under UNIX and Linux) if you use vi to edit /etc/passwd file and same time a user try to change a password while root editing file, then the user’s change will not entered into file. To avoid this problem and to put a lock while editing file, use vipw and vigr command which will edit the files /etc/passwd and /etc/group respectively. If you pass -s option to these command, then they will edit the shadow versions of those files i.e. /etc/shadow and /etc/gshadow, respectively.

The main purpose of locks is to prevent file corruption. Do not use vi or other text editor to edit password file. Syntax:

  • vipw -s : Edit /etc/passwd file
  • vigr -s : Edit /etc/group file
Where,
  • -s : Secure file editing
An example :-

Login as a root user:


 [root@linuxbiginner ~]# vipw -s 


On other terminal login as normal user (for example anurag) and issue command passwd to change anurag’s password:

[anurag@linuxbiginner ~]# passwd
Changing password for user anurag.
Changing password for anurag.
(current) UNIX password:
New password:
Retype new password:
passwd: authentication tokens lock busy.

As you see it returned with an error “passwd: Authentication token lock busy”

This will avoid /etc/shadow file corruption.

Wednesday 15 June 2016

Linux and Unix pwd (Print Working Directory) command


 What is pwd?

pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or simply the directory user is, at present. It prints the current directory name with the complete path starting from root (/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.pwd is one of the simplest yet most popular and most widely used command. A good command over pwd is basic to use Linux terminal.

Basic syntax of pwd:

 pwd [OPTION]...

Options used with pwd
-L --logical         If the contents of the environment variable PWD provide an absolute name of the 
                     current directory with no "." or ".." components, then output those contents, 
                     even if they contain symbolic links. Otherwise, fall back to default -P handling.


-P --physical        Print a fully resolved name for the current directory, in which all components of
                     the name are actual directory names, and not symbolic links).

--help               Display a help message, and exit.

--version            Display version information, and exit.

This article aims at providing you a deep insight of Linux command ‘pwd‘.

1. Print your current working directory.

anurag@linuxgosolution:~$ /bin/pwd

/home/anurag

2. Print version of your ‘pwd’ command.

anurag@linuxgosolution:~$ /bin/pwd --version

pwd (GNU coreutils) 8.24
Packaged by Cygwin (8.24-3)
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering.

Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.


3.
What is the absolute path (starting from /) of the pwd binary file.

/bin/pwd

4. What is the absolute path (starting from /) of the pwd source file.

/usr/include/pwd.h