Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Find out Memory Usage of each process in Linux

In this post, I have listed out some commands to show the Memory Usage of each process running.
Hope this will help you out to know memory utilization per process.
Also shared reference links where I found this commands.


1. Show the processes memory in megabytes and the process path.
Reference Link: https://superuser.com/

#ps aux  | awk '{print $6/1024 " MB\t\t" $11}'  | sort -n

2. Total memory consumption by the current user.
Reference Link: https://superuser.com/

#echo "------------------------------------" && mem=0 && while read -r rss comm ; do mbs=$((rss/1024)); mem=$((mbs + mem)); echo $mbs"MB - $comm"; done <<< "$(ps -u $USER -wo rss=,comm= --sort -rss)" && echo "------------------------------------" && echo $mem"MB: Memory used by user '$USER'"

All user processes sorted by the highest memory usage in MB.

#ps -u $USER -wo rss=,comm= --sort -rss | while read -r rss comm ; do echo $((rss/1024))"MB -" $comm; done


3. Display Processes Sorted By Memory Usage in Linux.
Reference Link:https://www.shellhacks.com

#ps axo rss,comm,pid \
| awk '{ proc_list[$2]++; proc_list[$2 "," 1] += $1; } \
END { for (proc in proc_list) { printf("%d\t%s\n", \
proc_list[proc "," 1],proc); }}' | sort -n | tail -n 10 | sort -rn \
| awk '{$1/=1024;printf "%.0fMB\t",$1}{print $2}'

4. Top 10 processes sorted by memory usage.
Reference Link: https://www.linuxquestions.org/

#ps -eo rss,pid,user,comm | sort -rn | head -10 | awk '{ hr[1024**2]="GB"; hr[1024]="MB";
 for (x=1024**3; x>=1024; x/=1024) {
 if ($1>=x) { printf ("%-6.2f %s ", $1/x, hr[x]); break }
 } } { printf ("%-6s %-10s ", $2, $3) }
 { for ( x=4 ; x<=NF ; x++ ) { printf ("%s ",$x) } print ("\n") }'

Add Linux Host to Nagios Monitoring Server (Complete Script)

In this post, I have created a script which involves all the steps which we perform while adding Linux host in Nagios monitoring server.

Download script here: plugin-nrpe-install script

#!/bin/bash

Path1=/opt/softwares/nagios-plugins-2.1.1  ## this path may varies with the current veriosn
Path2=/opt/softwares/nrpe-2.15                   ##  of plugin and nrpe.

echo -e "\e[1;32mNagios Client Installation...\e[0m"

Packages="wget gcc glibc glibc-common gd gd-devel make net-snmp openssl-devel"
for pkg in $Packages; do

if [ -z $(rpm -qa $pkg) ];
then
echo -e "\e[1;31m$pkg not installed\e[0m"
echo "$pkg" >> /tmp/notinstalled.txt
else
echo -e "\e[1;32m$pkg installed\e[0m"
fi
done
##############################################
for i in `cat /tmp/notinstalled.txt`;
   do
   /usr/bin/yum install $i -y
#    echo "$i is not installed."
#   echo -e "\e[1;32mAll the Packages are installed\e[0m"
   >/tmp/notinstalled.txt
done
##############################################
USERID="nagios"
ID='/usr/bin/id'
/bin/egrep  -i "^${USERID}:" /etc/passwd
if [ $? -eq 0 ]; then
   echo -e "\e[1;32mUser $USERID exists in /etc/passwd \e[0m"
   echo -e "\e[1;32mNagios ID is $ID nagios \e[0m"
else
   echo -e "\e[1;32mUser $USERID does not exists in /etc/passwd \e[0m"
   echo  -e "\e[1;32mAdding user Nagios \e[0m"
   useradd nagios
   echo "nagios123" | passwd nagios --stdin
fi
##########################################
echo -e "\e[1;32mDownloading the nagios and nrpe plugins\e[0m"
mkdir /opt/softwares/
cd /opt/softwares/
/usr/bin/wget "http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz" && tar xf nagios-plugins-2.1.1.tar.gz
/usr/bin/wget --no-check-certificate "https://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.15/nrpe-2.15.tar.gz" && tar xf nrpe-2.15.tar.gz
################################################
if [ ! -d "/usr/local/nagios" ]
then
    echo -e "\e[1;31Directory "/usr/local/nagios" does not exists.\e[0m"
    echo -e "\e[1;32Instaling nagios plugins\e[0m"
    cd $Path1
    ./configure
    make
    make install
    /bin/chown nagios.nagios /usr/local/nagios
    /bin/chown -R nagios.nagios /usr/local/nagios/libexec
    sleep 5
    echo -e "\e[1;32mInstalling xinetd package\e[0m"
    yum install xinetd -y
###########################################
    echo -e "\e[1;32mInstalling nrpe plugin\e[0m"
    cd $Path2
    ./configure
    make all
    make install-plugin
    make install-daemon
    make install-daemon-config
    make install-xinetd
    sleep 5
/bin/sed -i 's/127.0.0.1/127.0.0.1 localhost 192.168.1.207/g' /etc/xinetd.d/nrpe
 ## Higlighted IP will be replaced with your Nagios server IP      
/bin/echo "nrpe            5666/tcp                #NRPE" >> /etc/services

    service xinetd start
    chkconfig xinetd on
/bin/netstat -tlpn | grep xinetd
/usr/local/nagios/libexec/check_nrpe -H localhost
/bin/echo -e "\e[1;32mNAGIOS CLIENT SUCCESSFULLY INSTALLED\e[0m"
else
    echo -e "\e[1;32mDirectory /usr/local/nagios exists.\e[0m"
fi


Refernce Link: Tecmint

Any Changes/Suggestions are appreciated.