Thursday, October 7, 2010

Memory leakage in Linux

What is Memory leakage?

Memory Leakage basically refers to a situation where the memory allocated to an application (can be a Database query as well) is not getting freed up. This can be due to bug in the program which utilize the memory resource. In my opinion, when an application closes, it should issue the proper exit statement so that it will free up the memory which it occupied and kill all its child process. 
So this Memory leakage is basically an application issue, and any leaked memory will be freed up after the application is killed or stopped.


How to find the Memory leakage?

We would first need to determine which application/process is consuming more memory.  This can be done through data gathering. We need to periodically check what is running on a server and what is the Memory utilization . With the time when the server runs out of available memory to allocate, the leaking application might crash or it may even crash the system.

Valgrind is a popular Open source tool available for detecting the memory leakage of an application (URL:  http://valgrind.org/ ). A sample Valgrind output attached.


How to fix it?

We need to correct the application from using more memory ( Done by a Programmer or DBA).  I don’t think we can do much on the server side since the job of kernel is release the requested memory by an application. Certain things we can do at Server end are, tuning some kernel parameter like restricting the number of child process forked by a single process, restricting number of process forked by an user etc. If the kernel have the intelligence to distinguish, which request for more memory is valid and which one causes memory leaks, it would be great but am not sure if it’s possible to do such level of tuning J


In Linux, Memory leakage can be found using the below given 'ps' command. This will give you an idea about Memory usage of each process in a sorted manner.
# ps aux --sort pmem

Finding the empty folders, files and hidden files

1.     How to find all the empty folders which are there in a filesystem?

# find /path -depth -empty -type d –print

Example:

[root@hostx ~]# find /etc -depth -empty -type d -print
/etc/skel/.mozilla/plugins
/etc/skel/.mozilla/extensions
/etc/opt
/etc/prelink.conf.d
/etc/logwatch/scripts
/etc/X11/sysconfig
/etc/rwtab.d
/etc/oddjob
/etc/jvm-commmon
/etc/racoon/certs
/etc/sasl2
/etc/subversion
/etc/dbus-1/session.d
/etc/gconf/gconf.xml.mandatory
/etc/dnsmasq.d
/etc/NetworkManager/VPN
/etc/NetworkManager/dispatcher.d
/etc/hal/fdi/preprobe
/etc/hal/fdi/information
.
[Output truncated]
.
[root@hostx ~]#
[root@hostx ~]# cd /etc/sysconfig/pgsql
[root@hostx pgsql]# ls -la
total 16
drwxr-xr-x  2 root root 4096 Jan 12  2008 .
drwxr-xr-x 12 root root 4096 Jul 14 05:23 ..                            ß Nothing exists
[root@hostx pgsql]# cd /etc/pm/hooks
[root@hostx hooks]# ls -la
total 16
drwxr-xr-x 2 root root 4096 Jan 22  2009 .                              ß Nothing exists
drwxr-xr-x 5 root root 4096 Oct 12  2009 ..
[root@hostx hooks]#




How to search for hidden files which are there in a particular folder?


# find /path -type f -name ".*" -print


Example:


[root@hostx ~]# find /etc -type f -name ".*" -print
/etc/skel/.bash_profile
/etc/skel/.kde/Autostart/.directory
/etc/skel/.bash_logout
/etc/skel/.zshrc
/etc/skel/.bashrc
/etc/skel/.emacs
/etc/.pwd.lock
/etc/lvm/cache/.cache
/etc/news/.profile
[root@hostx ~]#

How to find empty files in a given folder ?

# find /path -type f -size 0 -print

Example:

# find /etc -type f -size 0 -print
/etc/wvdial.conf
/etc/cron.deny
/etc/security/opasswd
/etc/security/console.apps/xserver
/etc/motd
/etc/dumpdates
/etc/environment
/etc/.pwd.lock
/etc/lsb-release.d/core-3.0-noarch
/etc/lsb-release.d/core-3.0-amd64

Steps to reset MySQL root password

                         [root@xyzhost conf]# mysql -u root -p         <-- Mysql root password isn't working
Enter password:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@xyzhost conf]#
Check if the Mysql process is running (its running here)
[root@xyzhost init.d]# ps -ef | grep -v grep | grep mysql
root      2104     1  0 Jan05 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --user=mysql
mysql     2154  2104  0 Jan05 ?        00:00:01 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --socket=/var/lib/mysql/mysql.sock
[root@xyzhost conf]#

Time to reset the Mysql root password !!

Stop the mysql service
[root@xyzhost init.d]# /etc/init.d/mysqld stop
Stopping MySQL:                                            [  OK  ]
Start the Mysql in Safe-mode
[root@xyzhost init.d]# mysqld_safe --skip-grant-tables &
[1] 7256
[root@xyzhost init.d]# Starting mysqld daemon with databases from /var/lib/mysql
[root@xyzhost init.d]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.0.82sp1 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=PASSWORD("mysqlnewp@$$") where User='root';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
Stop the Mysql service
[root@xyzhost init.d]# /etc/init.d/mysqld stop
STOPPING server from pid file /var/run/mysqld/mysqld.pid
110721 12:58:44  mysqld ended
Stopping MySQL:                                            [  OK  ]
[1]+  Done                    mysqld_safe --skip-grant-tables
Start the Mysql service
[root@xyzhost init.d]# /etc/init.d/mysqld start
Starting MySQL:                                            [  OK  ]
Connect to Mysql DB using new password:
[root@xyzhost init.d]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.82sp1 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql                   |
| customers            |
| test                      |
| clients_db            |
+--------------------+
5 rows in set (0.00 sec)
mysql>

Thats it !!

More about 'touch' command in Linux

touch” resemble like a simple command for creating empty file but it is widely used by system for changing the Time stamp of files and for Service management.

Here’s an example of touch command that change the Time stamp of a file:

[adevaraju@host01-tx xivnew_64]$ ls -l
total 3904
-rw-r--r-- 1 adevaraju domain users   52422 Nov 12  2009 sg3_utils-libs-1.25-4.el5.x86_64.rpm
[adevaraju@host01-tx xivnew_64]$


[adevaraju@host01-tx xivnew_64]$ touch sg3_utils-libs-1.25-4.el5.x86_64.rpm
[adevaraju@host01-tx xivnew_64]$ ls -l
total 3904
-rw-r--r-- 1 adevaraju domain users   52422 Sep 15 07:15 sg3_utils-libs-1.25-4.el5.x86_64.rpm                                       # Time stamp changed


[adevaraju@host01-tx xivnew_64]$ touch -t 1009240615 sg3_utils-libs-1.25-4.el5.x86_64.rpm
[adevaraju@host01-tx xivnew_64]$ ls -l sg3_utils-libs-1.25-4.el5.x86_64.rpm
Fri Nov 26 06:30:34 CST 2010           <- Time stamp advanced to 06:30 11/26/2010
[adevaraju@host01-tx xivnew_64]$


Service management:

When you get into the folder /var/lock/subsys, you could see list of empty files with service names. These files are basically created using touch command, when a service (say ntpd) is started/restarted.  Presence of these files ensures that same service willnot be started again and it gives an error message saying the service is already running. Further these files will let us know how long a particular service is being running.


[adevaraju@host01-tx xivnew_64]$ cd /var/lock/subsys
[adevaraju@host01-tx subsys]$ ls -l
total 0
-rw-r--r-- 1 root root 0 Jul 12 20:25 acpid
-rw-r--r-- 1 root root 0 Jul 12 20:25 atd
-rw-r--r-- 1 root root 0 Jul 12 20:25 auditd
-rw-r--r-- 1 root root 0 Jul 12 20:25 autofs
-rw-r--r-- 1 root root 0 Jul 12 20:25 bluetooth
-rw-r--r-- 1 root root 0 Jul 12 20:24 cpuspeed
-rw-r--r-- 1 root root 0 Jul 12 20:25 crond
-rw-r--r-- 1 root root 0 Jul 12 20:25 cups
-rw-r--r-- 1 root root 0 Jul 12 20:26 firstboot
-rw-r--r-- 1 root root 0 Sep 12 04:02 funcd
-rw-r--r-- 1 root root 0 Jul 12 20:25 gpm
-rw-r--r-- 1 root root 0 Jul 12 20:26 haldaemon
-rw-r--r-- 1 root root 0 Jul 12 20:25 hcid
-rw-r--r-- 1 root root 0 Jul 12 20:25 hidd
-rw-r--r-- 1 root root 0 Jul 12 20:25 hpiod
-rw-r--r-- 1 root root 0 Jul 12 20:25 hplip
-rw-r--r-- 1 root root 0 Jul 12 20:25 hpssd.py
-rw-r--r-- 1 root root 0 Jul 12 20:25 iscsi
-rw-r--r-- 1 root root 0 Jul 12 20:24 iscsid
-rw-r--r-- 1 root root 0 Jul 12 20:24 kudzu
-rw-r--r-- 1 root root 0 Jul 12 20:28 local
-rw-r--r-- 1 root root 0 Jul 12 20:25 messagebus
-rw-r--r-- 1 root root 0 Jul 12 20:24 microcode_ctl
-rw-r--r-- 1 root root 0 Jul 12 20:24 multipathd
-rw-r--r-- 1 root root 0 Jul 12 20:25 netfs
-rw-r--r-- 1 root root 0 Jul 12 20:25 network
-rw-r--r-- 1 root root 0 Jul 12 20:25 nfslock
-rw-r--r-- 1 root root 0 Jul 12 20:32 ntpd
-rw------- 1 root root 0 Jul 12 20:25 pcscd
-rw-r--r-- 1 root root 0 Jul 12 20:32 portmap
-rw-r--r-- 1 root root 0 Aug 13 07:44 postfix
-rw-r--r-- 1 root root 0 Jul 12 20:25 rhnsd
-rw-r--r-- 1 root root 0 Jul 12 20:25 rpcidmapd
-rw-r--r-- 1 root root 0 Jul 12 20:25 sdpd
-rw-r--r-- 1 root root 0 Aug 17 03:08 sendmail
-rw-r--r-- 1 root root 0 Jul 12 20:28 smartd
-rw-r--r-- 1 root root 0 Jul 12 20:32 smb
-rw-r--r-- 1 root root 0 Aug 17 03:08 sm-client
-rw-r--r-- 1 root root 0 Jul 12 20:25 sshd
-rw------- 1 root root 0 Jul 12 20:25 syslog
-rw-r--r-- 1 root root 0 Sep  9 14:48 winbindd   # Indicates winbind service was restarted on Sep 9th.
-rw-r--r-- 1 root root 0 Jul 12 20:25 xfs
-rw-r--r-- 1 root root 0 Jul 12 20:25 xinetd
-rw-r--r-- 1 root root 0 Jul 12 20:25 yum-updatesd
[adevaraju@host01-tx subsys]$

Thursday, September 16, 2010

Providing Sudo access to commands with arguments

Situation:
You need to grant sudo access to a command that takes some parameters.

For example the following command takes “username” as the parameter.

#nxserver --useradd  <username>

If we just give “nxserver --useradd” in the /etc/sudoers file, it wouldn’t work.


Solution:
Use wild characters in /etc/sudoers file as shown in the following:

%itops ALL=(ALL)  nxserver --useradd  [A-Za-z]*      (or)

%itops ALL=(ALL)   nxserver --useradd ?*          # Where %itops is the groupname


Converting Bash script to Binary code

Bash Shell basically comprises of commands written in C language. Hence we basically need a Compiler that can convert the C code into executable Binary file.  I guess there are many C compilers available;  the one which I use is SHC (SHell script Compiler).  When we compile using this tool, it creates an execute file with .x extension, which is basically a executable version of your Bash script which can’t be readable or modifiable.

Illustration:

Extract the shc tar file.
Go into the shc-3.8.7 folder
Install the package by executing 'make'. It will create the 'shc' executable.

Create a sample bash script "sample.sh".

[root@host01 opt]$ cat sample.sh
#!/bin/bash

echo "Hello how r u"
echo
[root@host01 opt]$
[root@host01 opt]# ./sample.sh                              #  Take a note on output
Hello how r u

[root@host01 opt]#
[root@host01 shc-3.8.7]$ ./shc -f /opt/sample.sh
[root@host01 opt]# ls -l sample.sh*
-rwxr-xr-x 1 root root    39 Sep 13 12:11 sample.sh
-rwx--x--x 1 root root 11720 Sep 13 12:12 sample.sh.x     #  .x file created
-rw-r--r-- 1 root root  9406 Sep 13 12:12 sample.sh.x.c
[root@host01 opt]#
[root@host01 opt]# ./sample.sh.x                                #  Same result as before
Hello how r u

[root@host01 opt]# tail sample.sh.x                            #  File is in Binary format
@
@
 @
  @.
    @>
      @N
        @OÞ}¬÷ú=hòL³})âïM 3ði¹_=ÄÊÚî¦kpsJ=¸­¡`h]L1ôÌ7©+Éj3
                                                          ÌÜ.*Å=ßBl`H>û²ÞÎþýºD`44vÉáÛÑJ-h2»ñûMBpñ<D#ë0óÃH(rAÊß~jÀ

Searching files with particular string in Linux

Let say you want to search all "*.conf" files under  “/home/jboss”  having string “cache” in it and display the output.

# find /home/jboss -name *.conf -type f | xargs grep “cache”
(or)
# find /home/jboss -name *.conf -type f -exec grep -HI "cache" {} \;

Now you have a slightly different requirement: 
You want to search all "*.conf" files under  “/home/jboss”  having string “cache” and replace it with "core".
# perl -p -i -e 's/cache/core/g'  `find /home/jboss -type f -name *.conf`
Another version:
#  find /home/jboss -type f -name *.conf | xargs grep -l cache | xargs perl -p -i -e 's/cache/core/g'

More about Linux Man page

Q1) How to know the options of ‘man’ command?   
# man man    (or)  # info man

Q2) How can we search for man pages?
# man  -k  <command / string>

Q3) How can we convert the Man pages into file so that we can take a printout or save it for future reference?   
# man [command name] | col -b > filename
E.g: # man vim | col -b > vim_manpage
Note:  Using 'col -b' will filter reverse line feeds.


Q4) When we get into the folder /usr/share/man, we would see folders such as man1, man2, man3, man4 ......man9. what are all those?  
At times you would have noticed something like "refer man5 page" etc. What it refers to ? 

Explanation:
Some commands have multiple man pages. For instance, the ‘passwd’ command has a man page in section 1 and another in section 5. By default, the man page with the lowest number is shown.
If you want to see another section than the default, specify it after the man command:

# man 5 passwd

If you want to see all man pages about a command, one after the other, use the -a to man:
# man -a passwd
This way, when you reach the end of the first man page and press SPACE again, the man page from the next section will be displayed.

Sorting TOP output in Linux

You can sort the “Top” output on various criteria such as CPU Usage, Memory Usage, Swap, CPU time, User etc. 
To do this, while running “Top”, press ‘O’ (capital) key to access the sorting options.  From there you can choose sorting criteria (for example, press ‘l’ for CPU time – shown below)
and press ‘Enter’ to apply it.





Comparing two files in Unix

You got 2 files which contains a list of servernames (as shown below). You want to compare the entries between two files and get the list of server names which aren’t present in both files:

1st file:
server_a
server_b
server_c
server_d
server_e

2nd file:
server_c
server_d
server_g
server_a

Output should be:

server_b
server_g
server_e



Solution 1:  
# sort file1 file2 | uniq –u

Solution 2:
# sort file1 > file1.sorted
# sort file2 > file2.sorted
# comm -3 file1.sorted file2.sorted