Showing posts with label File Systems. Show all posts
Showing posts with label File Systems. Show all posts

Tuesday, August 4, 2020

Linux / ubuntu Server monitoring script and commands

Check server  Memory, CPU load, Storage with date time.

#! /bin/bash
printf "Memory\t\tOS_Disk\t\tHome_Disk\tCPU\t\tTime\n"
end=$((SECONDS+3600))
while [ $SECONDS -lt $end ]; do
MEMORY=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
DISK=$(df -h | awk '$NF=="/"{printf "%s\t\t", $5}')
DISKHOME=$(df -h | awk '$NF=="/home"{printf "%s\t\t", $5}')
CPU=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
DATELOG=$(date "+%Y-%m-%d %T")
echo "$MEMORY$DISK$DISKHOME$CPU$DATELOG"
sleep 20
done

save this file as stats.sh

run file 

#./stats.sh

Tools
Basic listed below 
strace – discover system calls and signals to a process.
tcpdump – raw network traffic monitoring.
netstat – network connections monitoring.
htop – real time process monitoring.
iftop – real time network bandwidth monitoring.
lsof – view which files are opened by which process.

Sysdig - A Powerful System Monitoring and Troubleshooting Tool for Linux

Command for sysdig

$sudo sysdig
$csysdig
$sysdig -cl
$Sysdig -r trace.scap
$sysdig -c ps
$sysdig -c ps
$sysdig -c list_login_shells
$sysdig -c spy_users

For more detail click here

Basic Command for  Server monitoring

$last
$last reboot | shutdown
$htop
$top
$jobs
$who -b
$ps -aux| grep apache|wc -l  (check apache threads)


Thursday, October 10, 2019

Setup Filesystem Quotas on Linux / Ubuntu Server

Installing the Quota Tools

sudo apt update
sudo apt install quota

Check Quota Version :

quota --version

Installing the Quota Kernel Module
First need  to check, we will use find to search for the quota_v1 and quota_v2 modules in the /lib/modules/... directory:

find /lib/modules/`uname -r` -type f -name '*quota_v*.ko*'

Output : 
/lib/modules/4.15.0-45-generic/kernel/fs/quota/quota_v1.ko
/lib/modules/4.15.0-45-generic/kernel/fs/quota/quota_v2.ko

If you get no output from the above command, install

sudo apt install linux-image-extra-virtual
OR 
Resolve this install kernel dependencies, like this:

apt-get -y install linux-image-generic
apt-get -y install linux-headers-generic
apt-get -y install linux-image-extra-`uname -r`

After that we need to add the quota modules to start with boot:

echo quota_v1 >> /etc/modules
echo quota_v2 >> /etc/modules

Updating Filesystem Mount Options

sudo nano /etc/fstab
LABEL=/home    /home   ext2   defaults,usrquota,grpquota  0 0
Remount the filesystem to make the new options take effect:

sudo mount -o remount /home

Enabling Quotas
sudo quotacheck -ugm /home

Now we’re ready to turn on the quota system:

sudo quotaon -v /home

Configuring Quotas for a User

Using edquota to Set a User Quota

sudo edquota -u  jagdeep

# edquota ramesh

Disk quotas for user jagdeep (uid 50001):
  Filesystem           blocks       soft       hard     inodes     soft     hard
  /dev/sda3           1419352          0          0       1686        0        0
Let’s update our sammy user to have a block quota with a 100MB soft limit, and a 110MB hard limit:

# edquota ramesh

Disk quotas for user jagdeep (uid 50001):
  Filesystem         blocks       soft       hard     inodes     soft     hard
  /dev/sda3        1419352       100M          110M   1686    0        0
Save and close the file

sudo quota -vs jagdeep

Using setquota to Set a User Quota
sudo setquota -u sammy 200M 220M 0 0 /home

Generating Quota Reports

sudo repquota -s /home

# repquota /home
*** Report for user quotas on device /dev/sda3
Block grace time: 7days; Inode grace time: 7days
                        Block limits                File limits
User            used    soft    hard  grace    used  soft  hard  grace
----------------------------------------------------------------------
root      --  566488       0       0           5401     0     0
nobody    --    1448       0       0             30     0     0
jagdeep    -- 1419352       0       0           1686     0     0
john      --   26604       0       0            172     0     0

Configuring a Grace Period for Overages

sudo setquota -t 864000 864000 /home


Output
Block grace time: 10days; Inode grace time: 10days
. . .

Disable quota for a Linux user or group on the shell
Example for disabling the quota for the user "testuser":
setquota -u testuser 0 0 0 0 -a /home
Example for disabling quota for the group "testgroup":
setquota -g testgroup 0 0 0 0 -a /home