Saturday, August 4, 2018

User Management commands


user add:


db.auth({"user”:”dbtest”,”pwd”:”ChangeMe”});

db.createUser(
   {
     user: "dbtest",
     pwd: "ChangeMe",
     "roles" : [
{
"role" : "clusterAdmin",
"db" : "admin"
},
{
"role" : "appDBARole",
"db" : "admin"
}
]
  }
)


db.createUser(
   {
     user: "dbtest",
     pwd: "ChangeMe",
     "roles" : [
{
"role" : "clusterMonitor”,
"db" : "admin"
}
]
  }
)


use $external
db.createUser({
"user" : "abc",
"roles" : [
{
"role" : "publicRole",
"db" : "admin"
},
{
"role" : "appReadRole",
"db" : "pdb01"
}
]
})


db.system.users.find({"db":"$external",_id:"$external.dm_admin"})


db.runCommand({ usersInfo: { user: “dm_admin”,db:"admin" } })

Current user:

db.runCommand({connectionStatus : 1})


Grant additional Role to user


show users or
db.system.users.find({user:”dpxabc"})

prod => db.grantRolesToUser( "dm_admin", ["clusterAdmin"])

non-prod

db.grantRolesToUser( "dm_read", ["clusterMonitor"])
db.grantRolesToUser( "dm_write", ["clusterMonitor"])
db.grantRolesToUser( "dm_read", [{ role: "read", db: "local"}])

db.grantRolesToUser( "dm_write", [{ role: "read", db: "local"}])

Friday, August 3, 2018

Activity and Lock Monitoring


db.currentOp()


Long Running queries:
=================
query active on particular db and more than x amount of seconds

db.currentOp(    {      "active" : true,      "secs_running" : { "$gt" : 30000 },      "ns" : /^qptdb01\./    } )

Active Indexing Operations
=====================

db.currentOp(     {       $or: [         { op: "command", "query.createIndexes": { $exists: true } },         { op: "none", ns: /\.system\.indexes\b/ }       ]     } )

Write waiting for lock
================

db.currentOp(    {      "waitingForLock" : true,      $or: [         { "op" : { "$in" : [ "insert", "update", "remove" ] } },         { "query.findandmodify": { $exists: true } }     ]    } )

All the active transactions waiting to complete
===================================
db.currentOp(    {      "active" : true,      "numYields" : 0,      "waitingForLock" : false    } )

Sunday, February 18, 2018

Linux CPU & Memory monitoring


#!/bin/bash

cpu=$(grep -c '^processor' /proc/cpuinfo)

cpuuse=0

while [ $cpuuse -le 20 ]

do

#cpuuse=$(top -bn1 | grep load | awk '{printf "2f%%", $(NF-2)}')
cpuused=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
memKB=$(cat /proc/meminfo | grep MemTotal|awk -F ":" '{print $2}'|awk -F "kB" '{print $1}')
memGB=`expr $memKB / 1024 / 1024`"GB"
#freemem=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')

usedmem=$(free -m | awk 'NR==2{printf "%.1fGB", $3/1024 }')
freemem=$(free -m | awk 'NR==2{printf "%.1fGB", $4/1024 }')
echo "Total# of CPUs :  $cpu"
echo "CPU ussage                :  $cpuused"
echo "Total memory :  $memGB"
echo "Used memory :  $usedmem"
echo "Free memory :  $freemem"
echo "**********************************************************"
echo " CTRL + c to exit"
sleep 2


done

Sunday, February 11, 2018

AWS - PV-VG-LV-FS - EBS steps

#************************************************************************************************
# Steps to Extend PV/VG -> LVs -> FS
#
# Dt:  Jul 14 2017
#*************************************************************************************************


Step1:

Resize the respective volume using AWS volume modify option (Disk addition to PV)

Step2:

Check Current Volumen Size

[root@mymongohost0102 ~]# pvs
  PV         VG        Fmt  Attr PSize   PFree
  /dev/xvdv  mongodbvg lvm2 a--u   4.00t     0
  /dev/xvdw  mongodbvg lvm2 a--u   4.00t     0
  /dev/xvdz  mongodbvg lvm2 a--u 512.00g 16.00g

Step3:

 Add space into PV

[root@mymongohost0102 ~]# pvresize /dev/xvdz
  Physical volume "/dev/xvdz" changed
  1 physical volume(s) resized / 0 physical volume(s) not resized

Step4:

Check the modified PV Physical Volumen Size

[root@mymongohost0102 ~]# pvs
  PV         VG        Fmt  Attr PSize PFree
  /dev/xvdv  mongodbvg lvm2 a--u 4.00t    0
  /dev/xvdw  mongodbvg lvm2 a--u 4.00t    0
  /dev/xvdz  mongodbvg lvm2 a--u 4.10t 3.61t


Step4:

Check the VG(s) size (it should show new size)


[root@mymongohost0102 ~]# vgs
  VG        #PV #LV #SN Attr   VSize  VFree
  mongodbvg   3   5   0 wz--n- 12.10t 3.61t

Step 5:

  Check the LVs and File System size (No change in the LV and FS size)

[root@mymongohost0102 ~]# lvs
  LV                 VG        Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  fs_imdb001_config  mongodbvg -wi-ao----   2.00g                                                 
  fs_imdb001_data    mongodbvg -wi-ao----   8.29t                                                 
  fs_imdb001_journal mongodbvg -wi-ao---- 128.00g                                                 
  fs_imdb001_log     mongodbvg -wi-ao----  64.00g                                                 
  fs_imdb001_sw      mongodbvg -wi-ao----   2.00g

                                                   
[root@mymongohost0102 ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/xvda1             99G  2.9G   91G   4% /
tmpfs                  15G     0   15G   0% /dev/shm
/dev/mapper/mongodbvg-fs_imdb001_sw
                      2.0G  247M  1.8G  13% /imdb001/sw
/dev/mapper/mongodbvg-fs_imdb001_log
                       64G  4.7G   60G   8% /imdb001/log
/dev/mapper/mongodbvg-fs_imdb001_journal
                      128G  233M  128G   1% /imdb001/journal
/dev/mapper/mongodbvg-fs_imdb001_config
                      2.0G   33M  2.0G   2% /imdb001/config
/dev/mapper/mongodbvg-fs_imdb001_data
                      8.0T  8.0T   72K 100% /imdb001/data
Step 6:

Extending LV size to consume the increased VG space (in this example vg size was increased from 8.29TB to 10TB)
                   
 lvextend -L11T /dev/mapper/mongodbvg-fs_imdb001_data -r                       
[root@mymongohost0102 ~]# lvextend -L10T /dev/mapper/mongodbvg-fs_imdb001_data -r
  Size of logical volume mongodbvg/fs_imdb001_data unchanged from 10.00 TiB (2621440 extents).
  Logical volume fs_imdb001_data successfully resized.
meta-data=/dev/mapper/mongodbvg-fs_imdb001_data isize=256    agcount=8, agsize=268435455 blks
         =                       sectsz=512   attr=2, projid32bit=0
data     =                       bsize=4096   blocks=2147481600, imaxpct=5
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0
log      =internal               bsize=4096   blocks=521728, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 2147481600 to 2684354550

Step 7:

Check lv size increased using lvs and FS size using df -h



Mise:  In case XFS is not installed

echo "Now installing the xfs filesystem..."
wget -t 1 -T 10 http://mymongohost:9090/NoSQL/xfsprogs-3.1.1-16.el6.x86_64.rpm
rpm -ivh /root/xfsprogs-3.1.1-16.el6.x86_64.rpm



Checking which file system (Ext4 or xfs)

[root@mymongohost0102 ~]# mount
/dev/xvda1 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/mapper/mongodbvg-fs_imdb001_sw on /imdb001/sw type xfs (rw,noatime)
/dev/mapper/mongodbvg-fs_imdb001_log on /imdb001/log type xfs (rw,noatime)
/dev/mapper/mongodbvg-fs_imdb001_journal on /imdb001/journal type xfs (rw,noatime)
/dev/mapper/mongodbvg-fs_imdb001_config on /imdb001/config type xfs (rw,noatime)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
/dev/mapper/mongodbvg-fs_imdb001_data on /imdb001/data type xfs (rw,noatime)



Installing xfs filesystem RPM

[root@mymongohost0102 ~]# rpm -ivh xfsprogs-3.1.1-16.el6.x86_64.rpm
Preparing...                ########################################### [100%]
   1:xfsprogs               ########################################### [100%]
[root@mymongohost0102 ~]# xfs_
xfs_admin      xfs_check      xfs_db         xfs_freeze     xfs_growfs     xfs_io         xfs_mdrestore  xfs_mkfile     xfs_quota      xfs_rtcp     
xfs_bmap       xfs_copy       xfs_estimate   xfs_fsr        xfs_info       xfs_logprint   xfs_metadump   xfs_ncheck     xfs_repair



LV Creation:
===========
xfs ---check xfs is installed or not if not then
wget -t 1 -T 10 http://mymongohost:9090/NoSQL/xfsprogs-3.1.1-16.el6.x86_64.rpm
rpm -ivh xfsprogs-3.1.1-16.el6.x86_64.rpm

lvcreate --name fs_opt_mongo_scripts --size 8GB mongodbvg
mkfs.xfs /dev/mongodbvg/fs_opt_mongo_scripts
mount /opt/mongo/scripts

lvcreate --name fs_opt_mongo_sw --size 8GB mongodbvg
mkfs.xfs /dev/mongodbvg/fs_opt_mongo_scripts
mount /opt/mongo/sw


Extent the existing LV
================
Eg: 

lvextend -L +800G is to add 800GB to existing LV

[root@manimongodb ~]# lvextend -L +800G /dev/mongodbvg/fs_imdb001_data
  Size of logical volume mongodbvg/fs_imdb001_data changed from 1.66 TiB (435200 extents) to 2.44 TiB (640000 extents).
  Logical volume fs_imdb001_data successfully resized.
[root@manimongodb ~]# xfs_growfs  -d  /imdb001/data




Mongo heath Check


$ cat run_validate_mongo.sh
export server_name=$1
cat /dev/null > ${server_name}_validate_mongo_ssh.out
#typeset -u 
#export iname=$server_name
scp ./validate_mongo.sh $server_name:/opt/mongo/scripts/validate_mongo.sh
ssh  $server_name " chmod u+x /opt/mongo/scripts/validate_mongo.sh"
ssh  $server_name "/opt/mongo/scripts/validate_mongo.sh" > ./${server_name}_validate_mongo_ssh.out 2>&1
ssh  $server_name "df -k" >> ./${server_name}_validate_mongo_ssh.out 2>&1
ssh  $server_name "lsblk" >> ./${server_name}_validate_mongo_ssh.out 2>&1
cat ./${server_name}_validate_mongo_ssh.out

$ cat validate_mongo.sh
#!/bin/sh
pth=`ps -ef|grep -v grep|grep mongod|tail -1|awk -F " " '{print $8}'|cut -d '/'  -f 1,2,3,4,5`
port=`ps -ef|grep -v grep|grep mongod|tail -1|awk -F " " '{print $10}'|awk -F "/" '{print $4}'|cut -d "-" -f 2|cut -d "." -f 1`
pth=$pth"/mongo --port "$port
$pth --quiet admin  -u username -p pwd <<EOF
rs.slaveOk()
rs.status()
rs.printReplicationInfo()
rs.printSlaveReplicationInfo()
use admin;
db.runCommand({ logRotate : 1 });
EOF
exit

Extracing Mongo Collection data and GRIDFS Pictures using Multi Thread

#!/bin/bash

#Color setting variables

MENU=`echo "\033[36m"` #Blue
RED_TEXT=`echo "\033[31m"`
NORMAL=`echo "\033[m"`
RED=`echo "\033[01;31m"`
GREEN=`echo "\033[01;32m"`
YELLOW=`echo "\033[01;33m"`
PURPLE=`echo "\033[01;35m"`
CYAN=`echo "\033[01;36m"`
WHITE=`echo "\033[01;37m"`
BOLD=`echo "\033[1m"`
UNDERLINE=`echo "\033[4m"`

############################################################################################################
############################################### USAGE ######################################################
############################################################################################################
usage()
{
echo -e  "
${BOLD}
Usage: pic-migration -i inputfile -t threads [-h mongohost] [-d dbname] [-p port]
${NORMAL} 
${PURPLE}  -i ${NORMAL}    : Input file with id list
${PURPLE}  -t ${NORMAL}         : Number of parallel threads (default 4)
${PURPLE}  -h ${NORMAL} : Mongo server hostname (default hostname)
${PURPLE}  -d ${NORMAL} : Mongo database name  (default dbname)
${PURPLE}  -p ${NORMAL} : Mongo port  (default 27108)

 ${GREEN} Example : ${NORMAL}
    ./pic-migration -i=inputfile -t=<threads count>  -h=<host> -d=<db> -p=<port#>
or
            ./pic-migration -i=inputfile
  "
}
##################################################################################################################
######################################### INPUT VALIDATION  ######################################################
##################################################################################################################

if [ "$#" -eq 0 ]
then
   usage
   exit 0
fi

mongohost=<hostname>
dbname=<dbname>
port=27108
thread_count=4
mpath=/mongo/sw/mongo-3.0.1/bin
picpath=/opt/pic
usr=testuser
pwd=user1234

pause()
{
echo -e "Press ${BOLD} any key to exit ${NORMAL} "
read -p "                "
}


#echo "$#"

#echo "$@"

if [[ "$#" > 0 ]]; then
  for i in "$@"
  do
#        echo  "$i"
case $i in

-i=*|--inputfile=*)
                  inputfile="${i#*=}"
                        shift # past argument=value
                        ;;
-t=*|--threads=*)
                        thread_count="${i#*=}"
                        shift # past argument=value
                        ;;
    -h=*|--mongohost=*)
    mongohost="${i#*=}"
    shift # past argument=value
    ;;
    -d=*|--dbname=*)
    dbname="${i#*=}"
    shift # past argument=value
    ;;
    -p=*|--port=*)
    port="${i#*=}"
    shift # past argument=value
    ;;
    *)
          echo "input error.."
usage
    exit
    ;;
esac
   done
fi

TS=./log/pic_downloaded_$(date +"%m-%d-%y-%T")

#######################################################################################
###################### SSO_ID to PIC_ID CONVERSION ####################################
###################### VALIDATE GIVEN SSOIDS ARE VALID ################################
###################### VALIDATE GIVEN SSOIDS ARE HAVING PIC/IMAGE MAPPED OR NOT########
#######################################################################################

#echo "DBQuery.shellBatchSize = 90000000000000000;" > img_cnt.py
#rm -f picid_list.py
rm -f "$inputfile"_ssoid_notfound.lst
rm -f "$inputfile"_picid.lst
rm -f "$inputfile"_imgid_notfound.lst
rm -f "$inputfile"_prefdata.out
rm -f "$inputfile"_picdata.out

while read ssoid
do
echo $ssoid
echo "db.employee.find({'"employee.DigitalProfiles.ssoid"':'"$ssoid"'},{_id:0,'"Profiles.DigitalProfiles.profilePictureId"':1})" >imgqry.js
        echo "db.employee.find({'"employee.DigitalProfiles.ssoid"':'"$ssoid"'})" >pref_data.js
        #echo "db.ProfilePicture.files.find(
#cat imgqry.js
imgqry_output=$($mpath/mongo  --host $mongohost  $dbname  --port=$port -u $usr -p $pwd --authenticationDatabase=admin --quiet < imgqry.js)
#echo "img query out   $imgqry_output"
qryout_length=`printf "%s" "$imgqry_output" | wc -m`

if [ $qryout_length -ne 0 ]; then
$mpath/mongo  --host $mongohost  $dbname  --port=$port -u $usr -p $pwd --authenticationDatabase=admin --quiet < pref_data.js >> ./data/"$inputfile"_prefdata.out
        fi
if [ $qryout_length -eq 0 ]; then
echo -e "${RED}$ssoid <<ssoid not found>> ${NORMAL}"
echo -e "${RED}$ssoid ${NORMAL}" >> ./log/"$inputfile"_ssoid_notfound.lst
elif [[ ${imgqry_output} != *"ObjectId"* ]];then
echo -e "${RED}$ssoid ${NORMAL}" >> ./log/"$inputfile"_imgid_notfound.lst
echo -e "${RED}$ssoid <<pic not found>> ${NORMAL}"
else
        #echo "awk output"
        #picid=$(echo -e $imgqry_output|awk -F ':' '{print $4}'|awk -F '}' '{print $1}')
        picid=$(echo -e $imgqry_output|awk -F ':' '{print $4}'|awk -F '}' '{print $1}')
picid=$(echo "db.ProfilePicture.files.find({_id : $picid })")
echo -e "$picid" > picdata_qry.js
#cat picdata_qry.js
$mpath/mongo  --host $mongohost  $dbname  --port=$port -u $usr -p $pwd --authenticationDatabase=admin --quiet < picdata_qry.js  >> ./data/"$inputfile"_picdata.out
echo -e $imgqry_output|awk -F ':' '{print $4}'|awk -F '}' '{print $1}'>>"$inputfile"_picid.lst
fi
done < "$inputfile"

#read -p "waiting for cancel "
###########################################################################################
############################# PIC DOWNLOAD ################################################
###########################################################################################

picids="$(cat <<EOF
`cat "$inputfile"_picid.lst`
EOF
)"

total_files=$(echo "$picids"|wc -l|awk {'print $1'})
lines_per_file="$(((($total_files + $thread_count) - 1) / $thread_count))"

#echo "total files :  $total_files"
#echo "lines per file $lines_per_file"

#read -p "wait"

#echo "dollar picids ------------> $picids"

#TS=Pic_download_"$inputfile"_report_$(date +"%m-%d-%y-%T")
echo -e " ${BOLD} ----------------------------------------------------------------------------------------------------------------------------------${NORMAL}" >>"$TS".out
echo "Host         : $mongohost              ">>"$TS".out
echo "Databse      : $dbname              ">>"$TS".out
echo "Begin Date   : $(date)                                    ">>"$TS".out
echo -e " ${BOLD}-----------------------------------------------------------------------------------------------------------------------------------${NORMAL}" >>"$TS".out

pic_download()
{
FILENAME=`echo $pic_id | awk -F '"' '{print $2}'`
echo "PIC FILENAME $FILENAME -------------> `date`"
$mpath/mongofiles --quiet -d $dbname get_id $pic_id --host $mongohost --port $port  --prefix ProfilePicture --authenticationDatabase admin -u $usr -p $pwd --local $picpath/$FILENAME.jpg  >> $TS.out 2>&1
#echo "Image downloaded list"
#cat pic_downloaded.out
}

##################################################################################################
##############################  Thread Loop ######################################################
##################################################################################################
#echo "`date`"

# param: $1 worker identity number
# param: $2 starting line number in the file to process
process_lines () {
  #cat x
  while read -r line; do
    #get filename
    pic_id=$line
    pic_id=$(echo -e "$line" | awk -F'\t' '{ print $1 }')
    #if connected message then continue
    [[ $file == 'connected to'* ]] && continue
    # sync the file with the server
    pic_download $pic_id
  done < x$i
}

#######################################################################################################
#######################  Multi Thread initiator #######################################################
#######################################################################################################

for ((i=1; i <= $thread_count; ++i)); do
  #echo "starting worker $i"
  #echo "lines per file : $lines_per_file,   counter i :  $i"
  headpos=$(($lines_per_file * $i))
  #echo "head pos : " $headpos
  echo -e "$picids" | head -n $headpos | tail -n $lines_per_file > x$i 
  #cat x$i
  #call process on this chunk of files
  #process_lines $i $(($lines_per_file * i)) &
  process_lines  &
  #record the pid for cleanup and waiting
  #echo "starting line number $i"
  _worker_pids+=($!)
  echo -e "${BOLD}${PURPLE}worker process id : $_worker_pids[$!] ${NORMAL}"
done

#######################################################################################################
#######################   Wait for Threads to complete ################################################
#######################################################################################################

#wait for each process to finish
for ((i=0; i <= ${#_worker_pids[@]}; ++i)); do
  wait ${_worker_pids[i]} > /dev/null 2>&1
done

#if no errors say we are complete
if [ $? -eq 0 ]; then
        cnt=$(grep -i "finished writing" $TS.out|wc -l|awk -F " " {'print $1'})
echo -e "${BOLD}-------------------------------------------------------------------------------------------------------------${NORMAL}">>$TS.out
echo "End Time  :      $(date)                   Number of images downloaded : $cnt ">>$TS.out
echo -e "${BOLD}-------------------------------------------------------------------------------------------------------------${NORMAL}">>$TS.out
echo -e "${BOLD}==================================================================================================================${NORMAL}"
echo -e "${BOLD}${GREEN}   Execution complete!! Please validate execution output in the  log and results in data & pic folders !! ${NORMAL}"
echo -e "${BOLD}==================================================================================================================${NORMAL}"
fi

Tuesday, June 27, 2017

Login to Mongo prompt

pth=`ps -ef|grep -v grep|grep mongod|tail -1|awk -F " " '{print $8}'|cut -d '/'  -f 1,2,3,4,5`
port=`ps -ef|grep -v grep|grep mongod|tail -1|awk -F " " '{print $10}'|awk -F "/" '{print $4}'|cut -d "-" -f 2|cut -d "." -f 1`
pth=$pth"/mongo --port "$port
$pth --quiet admin  -u Admin -p pwd
rs.status()