Linux – Useful Commands

#Linux Logging
/var/log/messages
/var/log/syslog
/var/log/kernlog

for num in `seq 1 1 10`; do free -m; sleep 10; done

free -m
##################################################################################
             total       used       free     shared    buffers     cached
Mem:         28203      20010       8193        683        156       3850
-/+ buffers/cache:      16002      12200
Swap:         8159        156       8003
##################################################################################
# buffers" and "cached" - amount of memory that the kernel is using for filesystem buffers
#                       - freed/released by kernel if required
# "-/+ buffers/cache    - the most important line - (=free+buffers+cache from Mem line)
#						- free on second line is the most important number
# and look at used swap to see if system is swapping


# some ed stuff that may be useful sometime
# using the old ed because can't create temp file on /etc (sed wants to create a temp file)
# sed -i '/^ORCL/s/:N/:Y/' /etc/oratab # would have been nice but can't create temporary file in /etc
# printf "%s\n" '/^ORCL/s/:N/:Y/' wq  | ed -s /etc/oratab

# who is logged in
w                    # Show who is logged on and what they are doing.

w|sort -k3b         # show show is logged in and sort on 3rd column

# disk usage

# print line if first column is greater than 500000 
du|awk '$1>500000 {print $0}'

du -k -x ./ | sort -rn|more   -- disk usage - size in K

du -m -x ./ | sort -rn|more   -- disk usage - size in M

du -h / | grep '[0-9]G'

du -h / | grep '[0-9]G'          - look for big files

df /opt   - find out what mount point/file system it is on

du -sh /opt  -- monitor the size of /opt
 
df $PWD - df current directory

du -sh .

du -sm * | sort -n

#--------------------------------------------
#Processes and memory:

mpstat -P ALL

ps auxww |grep tnslsnr
ps -ef |grep tnslsnr

cat /proc/meminfo

egrep --color 'Mem|Cache|Swap' /proc/meminfo

# cpu available
top -bn1 | grep "Cpu(s)" |sed "s/.*, *\([0-9.]*\)%* id.*/\1/" |awk '{print 100 - $1"%"}'

# Displays a line containing the totals memory in MB
free -t -m


# run vmstat outputting MB (m) (note, this assumes 1000K = 1M (not 1024K = 1M)
vmstat -S m 5

# run vmstat outputting MB (M) (note, 1024K = 1M)
vmstat -S M 5

# Output
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     48    212    222   1253    0    0     1    32    0    1  1  0 99  0  0
 0  0     48    212    222   1253    0    0     0    22  112  201  0  0 100  0  0
 0  0     48    212    222   1253    0    0     0    13  126  212  0  0 100  0  0

 # memory section has meaning same as free -m below

# Memory usage free -m
oracle@vmlinux1: free -m
             total       used       free     shared    buffers     cached
Mem:          3949       3737        211          0        222       1253
-/+ buffers/cache:       2261       1687
Swap:         8191         48       8143

Most important Used figure is buffers/cache used - this is how much RAM apps are using - must be less than total Mem
(Out of a total 3949 megabytes of memory (RAM), the system is using 3737 megabytes, and has 211 megabytes free - this
is not actually correct because Linux uses spare memory to cache disk blocks) - so "Used Mem" usually nearly equals "total Mem"

Simply adding memory by increasing SGA_TARGET gives Linux the possibility to use this memory - unless HugePages are used
so that the memory is really allocated to Oracle SGA, Linux will steal the memory to use for caching
Check difference between HugePages and Transaparent HugePages
My understanding at present is that the SGA gets locked into HugePages, essentially caching at Linux level, the SGA
So it seems logical that Huge Pages must at least equal the SGA.
Note pages and AMM are NOT compatible (but ASMM OK)
Should also put minima on Oracle buffer cache and shared pool


#Huge Pages
----------
Huge pages are locked in physical memory
memlock  (ulimit parameter) - how much memory oracle user can lock into address space - 
memlock should match memory size of the number of Huge pages (Hugepagesize*nr_hugepages  --> cat /proc/sys/vm/nr_hugepages)
grep HugePages /proc/meminfo to check Huge Page measures

# huge pages checks
/proc/meminfo |grep -i huge
cat /proc/sys/vm/nr_hugepages

------------------------------------------------------------------------------
[root@vmlinux~]# free -m
                total       used       free     shared    buffers     cached
Mem:         12011       9825       2186          0        243       5829
-/+ buffers/cache:       3752       8259
Swap:        16378        313      16065

Add buffers and cache together, you get: 6072. 
If you subtract 6072 from used you get 3752 used 
If you add it to free you get 8259 free

Used1 - (buffers + cached) = used2 
9825 - (243 + 5829) = 3753 (3752 on second row)
 
free1 + (buffers + cache) = free2
2186 + (243 + 5829) =  8285 (8259 on second row)

compare to:
vmstat -s -S M | grep mem 

free -glt (show mem in GB, and show totals)

#update every 5 seconds
free -glt -s 5

# Huge pages
#(AMM must be disabled, memory_target=0)
ipcs -ma

cat /etc/sysctl.conf| grep -i huge

grep ^Huge /proc/meminfo

example HugePages
[oracle@vmlinux1 ~]$ grep ^Huge /proc/meminfo
HugePages_Total:    7684
HugePages_Free:     2672
HugePages_Rsvd:     2669
HugePages_Surp:        0
Hugepagesize:       2048 kB

HugePages_Total * Hugepagesize = size of HugePage memeory

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

#-------------------------------------------------------------
#---- user stuff
#-- cut the id string at first bracket, then at second bracket
#-- e.g uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)
#-- extracts root from above string
id |cut -d"(" -f2 | cut -d ")" -f1

# list of all users on the system
cat /etc/passwd |cut -d: -f1

# list users having /home as home directories
cat /etc/passwd |grep "/home" |cut -d: -f1

# determine current user
echo $LOGNAME
echo $USER
id
id -u -n                   # username
id -u                      # userid


#!/bin/bash
_user="$(id -u -n)"
_uid="$(id -u)"
echo "User name : $_user"
echo "User name ID (UID) : $_uid"

# make sure only root can execute the script
#!/bin/bash
## get UID 
uid=$(id -u)
## Check for it
[ $uid -ne 0 ] && { echo "Only root may enable the nginx-chroot environment to the system."; exit 1; }

# grep

# grep OR  (Mem or Active or Swap)
cat /proc/meminfo|grep 'Mem\|Active\|Swap'
cat /proc/meminfo|grep -E 'Mem|Active'
cat /proc/meminfo|egrep 'Mem|Active'
cat /proc/meminfo|grep -e Mem -e Active -e Swap

#----------------------------------------------------------------
#mounting and unmounting

# for the problem of stale NFS handle: df: /opt/NetApp/smo/mnt/-oracle-ORCL: Stale NFS file handle
umount /opt/NetApp/smo/mnt/-oracle-ORCL


#-----------------------------------------------------------------
#sed and environment variables

REPOS=/foxrepos/software/Oracle

# globally replace XXX in file ${REPOS}/scripts/.bash_profile with ORCL, YYY with ORCL2, output to file "tmpprofile"
sed -e "s:XXX:ORCL:g" -e "s:YYY:ORCL2:g" < ${REPOS}/scripts/.bash_profile > ./tmpprofile

# copying files around
scp oracle@vmlinux1:/oracle/tmp/export1.DMP oracle@vmlinux2:/oracle/tmp/export1.DMP

#copy a file with a timestamp appended

 cp grub.conf grub.conf.$(date +"%Y%m%d%H%M%S")

 #------------------------------------------------------------
#  delete hidden files - be careful!
find . -iname ".*"  -exec rm {} \;

-- generate a list of files and grep through each of them
ls -ltr alert*| awk '{print $9}'|xargs grep cdmp -A1

grep 'cdmp' $(ls -ltr alert*)

ls -ltr alert* -exec grep 'cdmp' {} \;

# top
# when executing top, type Z at the command line and the colour of the elements can be changed. For example
# type S, 4, [RET] and the Summary data headings turn green 

# see crontab of all users:
for user in $(cut -f1 -d: /etc/passwd); do echo $user; crontab -u $user -l; done
awk -F: '{print $1}' /etc/passwd | xargs -l1 crontab -lu 2>/dev/null

# df and sort by the 4th column (%Used)
df -h|grep "\/oracle\/" | ( sort -rn -k 4)

df -h |grep ORCL|sort -k5
----------------------------------------------------------------------------

grep -i "dictionary objn" *.trc|awk '{print $5}' | sort|uniq

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

# deleting files older than 30 days example.
find . -name "*.trc"  -mtime +30 -exec rm -rf {} \;

find . -name "*.aud"  -mtime +35 -exec rm -rf {} \;


---------------------------------------------------------------------------
# execute command and see output differences
watch -d=cumulative free -m