Monday, March 7, 2016

linux CPU usage checking commands



ADD  NEW COMMANDS TO GITHUB
https://github.com/sjcode236/linux/blob/master/lnxCPUcmds.py


To See CPU information
cat /proc/cpuinfo
procinfo     >>  procinfo is a small program that gathers some system information
lsdev  
top  >>   display  system summary info and a list of tasks currently being managed by the kernel


linux CPU usage  checking commands 

1)  top
1a) iostat
 # iostat -c
Linux 2.6.32-573.34.1.el6.x86_64 (pceccto0140)  01/05/2017      _x86_64_        (2 CPU)
avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           8.64    0.00   21.05    0.01    0.00   70.29
2)  mpstat
mpstat -A
mpstat -P ALL
3)   sar -u 2 5

4) Find CPU , MEM  usage by a  process
ps   -p 8564   -o  %cpu,%mem,cmd

5)Find out who is monopolizing or eating the CPUs
  ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10
or
  ps -eo pcpu,pid,user,args | sort -r -k 1 | less

5a)Find out who is monopolizing or eating the Memory
  ps -eo pmem,pid,user,args | sort -k 1 -r | head -10
or
  ps -eo pmem,pid,user,args | sort -r -k 1 | less

6) Sort  process  by  cpu and memory usage
 ps aux --sort=-pcpu |head -10
ps aux --sort -pcpu   |head -10
ps aux --sort -pmem |head -10
ps aux   |sort -r  -k 3|more      ----> sort on  cpu usage  colum 3
ps aux   |sort -r   -k 4|more     ---->  sort on mem usage  colum 4

----------------------------------------------------------------
# lscpu
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                8
On-line CPU(s) list:   0-7
Thread(s) per core:    1
Core(s) per socket:    1
Socket(s):             8
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 45
Model name:            Intel(R) Xeon(R) CPU E5-4620 0 @ 2.20GHz
Stepping:              7
CPU MHz:               2198.906
BogoMIPS:              4400.00
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              16384K
NUMA node0 CPU(s):     0-7
---------------------------------------------------------------------------------

8 commands to check cpu information on Linux
http://www.binarytides.com/linux-cpu-information/


24 iostat, vmstat and mpstat Examples for Linux Performance Monitoring
http://www.thegeekstuff.com/2011/07/iostat-vmstat-mpstat-examples/?utm_source=feedburner


Use pgrep to find an applications process ID:
root@pcevoxf001o058:~# pgrep rabbitmq
9852
 find the exact command used to start a process:
root@pcevoxf001o058:~# ps auxf | head -1 && ps auxf | grep 9852 | grep -v "color=auto"
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      9852  0.0  0.0   4440   652 ?        S    Oct22   0:00 /bin/sh /usr/sbin/rabbitmq-server

from the example above – take the rabbitmq process ID of 9852 and see its child processes using the pstree command:
root@pcevoxf001o058:~# pstree -p 9852
rabbitmq-server(9852)───su(9868)───sh(9869)───beam.smp(9870)─inet_gethost(10163)───inet_gethost(10164)
                                                             {beam.smp}(9925)
                                                             {beam.smp}(9926)
                                                             {beam.smp}(9927)
how to view all the VASd processes and their related memory and CPU activity.  
root@pcevoxf001o058:~# ps auxf | head -1; ps uaxf | grep vasd | grep -v "color=auto"
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1409  0.0  0.0  53388  6828 ?        Ss   Oct22   0:14 /opt/quest/sbin/.vasd -D -p /var/opt/quest/vas/vasd/.vasd.pid
daemon    1581  0.5  0.6 152444 105832 ?       S    Oct22  12:55  \_ /opt/quest/sbin/.vasd -D -p /var/opt/quest/vas/vasd/.vasd.pid
daemon    1590  1.2  0.2  94024 47584 ?        S    Oct22  29:28      \_ /opt/quest/sbin/.vasd -D -p /var/opt/quest/vas/vasd/.vasd.pid

Use pstree to see a clear “tree” breakdown of the processes:
root@pcevoxf001o058:~# pstree -p | grep vas
init(1)-+-.vasd(1409)---.vasd(1581)-+-.vasd(1590)
        |                           |-.vasd(1591)
        |                           |-.vasd(1592)





Sunday, March 6, 2016

find cmd scripts


====New commands  check github  ===============
https://github.com/sjcode236/linux/blob/master/scripts/findCmds.rb

find command
scripts
awk , cut ,  etc

---------find commads -------------

To change ownership of link itself instead of pointed file
chown -h edw01:edw  sched
chown -h -R cauagt pcm

command to find link files
 find . -type l -print -exec chown -h root:system {} \;
  find . -type l -print
find . -type l -print -exec ls -al {} \;

find   xargs  usage
find /abinitio -print0 |xargs -0  chown -h root:sys
find /abinitio -print0 |xargs -0  ls –dl

command to find file with specific userid find  -exec  usage 
find . –user  userid/username
find . -user   501
find /users  -user sobyjose -exec ls -l {} \; -exec echo {}  \;
   
command to find file with specific group
find / -group groupid/groupname
      find / -group   503/osa
command to find files with specific name
  find . -name wk_mers_da* -print -exec chown -h edw01:edw {} \;
find . -name wk_mers_da -print -exec ls -al {} \;

command to find the file with specific sizes
find . –size 2048      (file with 1MB size)
find . –size +2048    (files more than 1MB size)
find . –size -2048    (file more than  1MB size)

To merge  Directories using find command 
cd ${SOURCE};
find . -type d -exec mkdir -p ${DEST}/\{} \;
find . -type f -exec mv \{} ${DEST}/\{} \;
find . -type d -empty -delete

-----!! can use  rsyc command also to merge directories 
rsync -av /images /images2 
If images with the same name exist in both directories, the command above will overwrite /images2/SOMEPATH/SOMEFILE with /images/SOMEPATH/SOMEFILE. 
If you want to replace only older files, add the option -u. If you want to always keep the version in /images2, add the option --ignore-existing.
If you want to move the files from /images, with rsync, you can pass the option --remove-source-files.


  list files with size more than 20MB and sort in revers order

find ../apps  -size +40960 -exec ls -l {} \; |sort -r -k 5

----- awk , cut , sed ,grep   ----------------------------------

More  than one command in a line is possible with  “;”

   date;pwd

->  grep commands
dmidecode -t baseboard |egrep -i "Desig|Bus|type|status"
dmidecode -t baseboard |grep -B1 -A3 Ethernet |egrep -i "Desig|Bus|type|status"
      -L, --files-without-match
grep -L : /sys/class/net/*/address
     -l, --files-with-matches
grep -l : /sys/class/net/*/address
       -H, --with-filename
grep -H : /sys/class/net/*/address
        -h, --no-filename
grep -h : /sys/class/net/*/address
       -n, --line-number

grep -n : /sys/class/net/*/address





---------script   examples --------------

Header of a cript
#!/bin/sh
set –x      -----------> to display commads on prompt
chfs  -m    /p1n4/edwprod1/partition25/mptsc1             /edwprod1/partition25/mptsc1


To list  the lines in a file
for i in `cat ./soby/userlist`
do
 print $i
done

To read names from a file and create homedirectories and set  ownerships
#!/bin/sh
#set -x

for i in `cat ./soby/userlist`
do
dirc=$i
mkdir  $dirc
owne=$i
grp=`lsuser -a pgrp $i |awk '{print $2}' |cut -c 6-`

chown $owne:$grp  /home/$dirc
print $dirc
done

To list all the physical volumes with pvid in the order of  bcu volume groups (used in dwpf3n1)

for i in $(lsvg  |grep bcu)
do                        
for j  in  $(lsvg -p  $i |awk '{print $1}' )
   do                     
        lspv  |grep $j    
    done                  
done                      

To send mail with bcc by reading the ids from file idlist
#!/usr/bin/ksh
if (( $#  <  2 )); then
print "no subject and To address "
exit
fi
subject=$1
to=$2
bcclist="~b "
for i in `cat  idlist`
do
bcclist=`echo "$bcclist " "$i"`
done
echo $bcclist
echo "$bcclist" > finalmail
echo "this is the body of message\n" >> finalmail
cat /etc/motd >> finalmail

mail -s "$subject"   $to  <  finalmail

to fill filesystem  with dummy files
i=11
while (( $i  <  330000 ))
do
if mkdir tst${i}
then
echo tst${i}
touch   tstfile${i}
cat  firstfile > tstfile${i}
cd tst${i}
touch tstfile${i}a
cat /testfs/firstfile > tstfile${i}a
cd /testfs

else
break
fi
i=$(($i + 1))
done

script to list the Physical volumes in bcu volume groups (server dwpf3n2)
for i in $(lsvg |grep bcu |awk '{print $1}')
do
lsvg -p $i
sleep 1
done
---------------------------------------
Loop for 0 to 100

i=0
while [ $i -le 100 ]
do
echo "Hello Soby checking the value of i=$i"
i=`expr $i + 1`
sleep 2
done
-----------------------------------------------------------------------
List all the  wwwn of all fcs adapters in system
  for i in $(lsdev -Cc adapter |grep fcs |awk '{print $1}')
 do
 k=`lscfg -vl $i |grep -i network `
 echo $i "---->  "$k
 done
---------------------------------------------------------------------------------
for i in $(lsvg)^Jdo^Jecho $i;lsvg -p $i  ;echo;echo;sleep 2^Jdone

---------------------------------------------------------------------------
Recreate  user after remove it

lsuser -f sobtest2  > /tmp/sobtest2.0
  vi /tmp/sobtest2.1     --> put the stirng values in double quotes
cat /tmp/sobtest2.0  |tr  '\n'  ' ' >/tmp/sobtest2.1

  vi /tmp/sobtest2.1     --> add mkuser command at begining and username at end
chmod 755 /tmp/sobtest2.1
rmuser -p sobtest2
rm -r sobtest2
/tmp/sobtest2.1
echo "username:passwd" |chpasswd


lsuser -f  larsk01  > /tmp/larsk01.0
  vi /tmp/larsk01.0     --> put the stirng values in double quotes
cat /tmp/larsk01.0  |tr  '\n'  ' ' >/tmp/larsk01.1
  vi /tmp/larsk01.1     --> add mkuser command at begining and username at end
chmod 755 /tmp/larsk01.1
rmuser -p larsk01
mv  /home/larsk01  /home/larsk01.old2
/tmp/larsk01.1
echo  “larsk01:temppass” | chpasswd

-------------------------------------------------------------------



    ln /tmp/t.sh
    ls -al
    ln /tmp/t.sh a
    ls -al
    cat *
    echo *
    ls *
    ls *.*
    ls *.
    ls *.
    echo ?.*
    echo ?
    echo *.?
    echo .[a-z]
    echo *.[a-z]
    echo *.[!a-p]*
    sort  file1

sort
34
67
87
2
6
7
CTRL d
   sort 34 56 2 8 45 75
    ls x* 2> err      redirecting stndard output
    cat e*
    wc
    wc err
    wc
    ls | sort
    ls | wc –l

echo line 1 > file1  ( write to file1)
echo  line 2  >> file1  (append to file1)
cat file1 >> file2

ed  file1
 r.   ->period matches any single character
1,$p     print all the lines
/ ... /  look for three chars surrounded by blanks
/   repeat  last search
1,$s/p.o/xxx/g  change all p.o  to xxx
1,$p  print all the lines

/^the/    find the line that starts with the
1,$s/^/xx/      insert  xx at the begining of each line
^     beging of the line
$     end of the line

abc$   matches characters 'abc' at the end of a line
^abc   matches chractes 'abc'  at the begining of line

\.$    matches any line ends with period
^\.    matches any line start with period

1,$s/..$//   delete the two characters form each line

^$  matches any line contains no characters
^ $   matches lines with single space character
/the/  find line containing the
/[tT]he/  look for the or The
/      continue serach  , 'n' also do same thing
/     once again

1,$s/[aeiouAEIOU]//G   delete all the vowels
/[0-9]/ find a line containing a digit
/^[A-z]/  Find a line that starts with an uppercase letter
1,$s/[A-Z]/*/g    change all uppercase letters to *
[^A-Z]   matches any character except an uppercase letter
[^A-Za-z]   matches any nonalphabetic character

1,$s/[^a-zA-Z]//g   delete all nonalphabetic characters