Monday, February 4, 2008

Admin Tools

VMSTAT

vmstat helps you to see, among other things, if your server is swapping. Take a look at the following run of vmstat doing a one second refresh for two iterations.

root@sexy [~]# vmstat 1 2
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 1172 1689332 333588 663092 0 0 19 113 1 2 3 1 95 1
0 0 1172 1690320 332920 663100 0 0 352 256 355 681 5 3 91 2

The first row shows your server averages. The si (swap in) and so (swap out) columns show if you have been swapping (i.e. needing to dip into 'virtual' memory) in order to run your server's applications. The si/so numbers should be 0 (or close to it). Numbers in the hundreds or thousands indicate your server is swapping heavily. This consumes a lot of CPU and other server resources and you would get a very significant benefit from adding more memory to your server.

Some other columns of interest: The r (runnable) b (blocked) and w (waiting) columns help see your server load. Waiting processes are swapped out. Blocked processes are typically waiting on I/O. The runnable column is the number of processes trying to something. These numbers combine to form the 'load' value on your server. Typically you want the load value to be one or less per CPU in your server.

The bi (bytes in) and bo (bytes out) column show disk I/O (including swapping memory to/from disk) on your server.

The us (user), sy (system) and id (idle) show the amount of CPU your server is using. The higher the idle value, the better.
PS

This command is used to know all the processes running in the server. It can be also used to find out process which is using most of the memory and cpu.
To find out top 3 memory consuming processes.

ps -auxf | sort -nr -k 4 | head -3

To find out top 3 cpu consuming processes

ps -auxf | sort -nr -k 3 | head -3

TOP

Say the system is slow and you want to find out who is gobbling up all the CPU and/or memory. To display the top processes, you use the command top.

Note that unlike other commands, top does not produce an output and sits still. It refreshes the screen to display new information. So, if you just issue top and leave the screen up, the most current information is always up. Top runs until you press "q" to quit top.
Let's examine the different types of information produced. The first line:

18:46:13 up 11 days, 21:50, 5 users, load average: 0.11, 0.19, 0.18

shows the current time (18:46:13), that system has been up for 11 days; that the system has been working for 21 hours 50 seconds. The load average of the system is shown (0.11, 0.19, 0.18) for the last 1, 5 and 15 minutes respectively. (By the way, you can also get this information by issuing the uptime command.)
If the load average is not required, press the letter "l" (lowercase L); it will turn it off. To turn it back on press l again. The second line: 151 processes: 147 sleeping, 4 running, 0 zombie, 0 stopped shows the number of processes, running, sleeping, etc. The third and fourth lines:

show the CPU utilization details. The above line shows that user processes consume 12.5% and system consumes 6.7%. The user processes include the Oracle processes. Press "t" to turn these three lines off and on. If there are more than one CPU, you will see one line per CPU. The next two lines: Mem: 1026912k av, 1000688k used, 26224k free, 0k shrd, 113624k buff 758668k actv, 146872k in_d, 14460k in_c Swap: 2041192k av, 122476k used, 1918716k free 591776k cached

show the memory available and utilized. Total memory is "1026912k av", approximately 1GB, of which only 26224k or 26MB is free. The swap space is 2GB; but it's almost not used. To turn it off and on, press "m".
The rest of the display shows the processes in a tabular format. Here is the explanation of the columns:

Column Description
PID The process ID of the process
USER The user running the process
PRI The priority of the process
NI The nice value: The higher the value, the lower the priority of the task
SIZE Memory used by this process (code+data+stack)
RSS The physical memory used by this process
SHARE The shared memory used by this process
STAT
The status of this process, shown in code. Some major status codes are:
R – Running
S –Sleeping
Z – Zombie
T – Stopped
You can also see second and third characters, which indicate:
W – Swapped out process
N – positive nice value
%CPU The percentage of CPU used by this process
%MEM The percentage of memory used by this process
TIME The total CPU time used by this process
CPU If this is a multi-processor system, this column indicates the ID of the CPU this process is running on.
COMMAND The command issued by this process

While the top is being displayed, you can press a few keys to format
the display as you like. Pressing the uppercase M key sorts the output
by memory usage. (Note that using lowercase m will turn the memory
summary lines on or off at the top of the display.) This is very useful
when you want to find out who is consuming the memory.

Now that you learned how to interpret the output, let's see how to use command line parameters.

The most useful is -d, which indicates the delay between the screen refreshes. To refresh every second, use top -d 1.

The other useful option is -p. If you want to monitor only a few processes, not all, you can specify only those after the -p option. To monitor processes 13609, 13608 and 13554, issue: top -p 13609 -p 13608 -p 13554
This will show results in the same format as the top command, but only those specific processes.


SKILL & SNICE
From the previous discussion you learned how to identify a CPU consuming resource. What if you find that a process is consuming a lot of CPU and memory, but you don't want to kill it?

$ skill -STOP 1

The process is effectively frozen. After some time, you may want to revive the process from coma:

$ skill -CONT 16514

The command is very versatile. If you want to stop all processes of the user "test"

$ skill -STOP test>

You can use a user, a PID, a command or terminal id as argument. The following stops all rman commands.
$ skill -STOP rman

As you can see, skill decides that argument you entered—a process ID, userid, or command—and acts appropriately. This may cause an issue in some cases, where you may have a user and a command in the same name. The best example is the "test" process, which is typically run by the user "test". So, when you want to stop the process called "test" and you issue:

$ skill -STOP test

all the processes of user "test" stop, including the session you may be on. To be completely unambiguous you can optionally give a new parameter to specify the type of the parameter. To stop a command called test, you can give:
$ skill -STOP -c test

The command snice is similar. Instead of stopping a process it makes its priority a lower one

lsof

The command lsof shows a list of processes attached to open files or network ports. List processes attached to a given file: lsof filenmame
List all open files on system:

#lsof

To kill the processes

kill
killall

This will perform an orderly shutdown of the process. If it hangs give a stronger signal with:

kill -9 .

This method is not as sanitary and thus less preferred.

A signal may be given to the process. The program must be programmed to handle the given signal. See /usr/include/bits/signum.h for a full list.

To restart a process after updating it's configuration file, issue the command

kill -HUP

The process attached to an open file can be killed using the command fuser:

fuser -ki filename

Now I am going indroduce you to a set of commands that may come handy
FIND

find -perm 777 -type d -exec chmod 755 {} \; #Command to change all the folders under present directory with 777 to 755

find -perm 755 -type f -exec chmod 644 {} \; #Command to change all the folders under present directory with 755 to 644

find -type d -maxdepth 3 -exec cp file {} \; #Copy file to 3 levels of directories below the present directory

find . -name "*.trn" -ctime +3 -exec rm -f {} \; #Forcible remove files with .trn extension and 3 days old.

find . -cmin -5 #Find all files created or updated in the last five minutes:
(Great for finding effects of make install)

LS

ls -lSh #List files by their size

ls -ltr #List files by date

ls -F #Appends a symbol after files and directories

RSYNC

rsync -e ssh -az /currentdirectory IP:/remotedirectory #Sync remote directory with our current directory.

rsync --bwlimit=1000 fromfile tofile #Locally copy with rate limit

GPG

gpg -c file #Encrypt file

gpg file.gpg #Decrypt file

DF

du -h --max-depth 1 #Show disk space used by all the files and directories.

du -s * | sort -k1,1rn | head #Show top disk users in current dir.

df -h #Show free disk space

df -i #Show free inodes

Add system swap space for virtual memory paging

Swap space may be a swap partition, a swap file or a combination of the two. One should size swap space to be at least twice the size of the computer's RAM. (but less than 2GB)

dd if=/dev/zero of=/swapfile bs=1024 count=265032 - #Create file filled with zeros of size 256Mb

mkswap /swapfile #Create swap file

swapon /swapfile #Begin use of given swap file. Assign a priority with the "-p" flag.

swapon -s #List swap files

scat /proc/swaps #Same as above

This example refers to a swap file. One may also use a swap partition. Make entry to /etc/fstab to permanently use swap file or partition.

/swapfile swap swap defaults 0 0

Note: To remove the use of swap space, use the command swapoff. If using a swap partition, the partition must be unmounted.
Debuggin Tools

strace -c ls >/dev/null #Summarise/profile system calls made by command

strace -f -e open ls >/dev/null #List system calls made by command

ltrace -f -e getenv ls >/dev/null #List library calls made by command

lsof -p $$ #List paths that process id has open

lsof -p PID #List paths PID has open

lsof ~ #List processes that have specified path open

last reboot #Indicates last reboot time

renice +15 PID #To give lower priority for a PID -19 is highest and +20 is lowest

To check number of IP's connecting to port 80

netstat -tanpu |grep :80 |awk {'print $5'} |cut -d: -f1 |sort -n |uniq -c

tcpdump not port 22 #To show network traffic except on port 22

Perl Administration

Installation of perl module can be done from tar file.

tar xzf yourmodule.tar.gz #Untar Module

perl Makefile.PL #Build with PERL makefile:
make
make install #Install

You can also do this from cpan shell

perl -MCPAN -e shell #First time through it will ask questions Answer "no" to the first question for
autoconfigure

cpan> install URI

cpan> i /PerlMagick/ #Inquire about module. (Search by keyword)
Distribution J/JC/JCRISTY/PerlMagick-5.36.tar.gz
Module Image::Magick (J/JC/JCRISTY/PerlMagick-5.36.tar.gz)

cpan> install Image::Magick

cpan> force install Image::Magick #Install a module forcefully.

YUM :RPM Updater

YUM (Yellowdog Updater, Modified) is a client command line application for updating an RPM based system from an internet repository (YUM "yum-arch" server) accessible by URL (http://xxx, ftp://yyy or even file://zzz local or NFS)

yum -y install package-name #To install a package along with its dependencies

yum remove package-name #To remove package

yum list #To list available packages version and state

yum list extras #To list packages not available in repositories but listed in config file

yum list obsoletes #To list packages which are obsoleted by repositories

yum clean all #To list packages which are obsoleted by packages in yum repository

yum update #Update all packages on your system

yum update package-name #Update a package

yum update package-name-prefix\* #Update all with same prefix

You can add new repos in /etc/yum.repos.d with files named file.repo For the option "gpgcheck=1" to work, use the "rpm --import GPG-KEY

rpm --import /usr/share/rhn/RPM-GPG-KEY

rpm --import /usr/share/rhn/RPM-GPG-KEY-fedora

File: /etc/yum.repos.d/fedora.repo with following entry

[base]
name=Fedora Core $releasever - $basearch - Base
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/$releasever/$basearch/os/
mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-$releasever
enabled=1
gpgcheck=1

Additional Commands

tzselect #To change time zone of the machine

command 2>&1 | tee outputfile.txt #Output of a command is send to a text file


wget --mirror http://www.example.com #To mirror a site

wget -c http://www.example.com/largefile #To continue downloading partially downloaded file

No comments: