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)