Useful One Liner Scripts For Server Administration

Top 5 user process currently running on the server

ps aux | awk '{print $1}' | sort | uniq -c | sort -n | tail -5

List the count of currently running  MySQL user process

mysqladmin proc | awk '{print $4}' | sort | uniq -c | sort -n

Kill all running process of a MySQL user

for t in `mysqladmin proc | grep username |awk '{print $2}'`; do mysqladmin kill $t;done

Find number of IP hits to port 80

netstat -plan | grep :80 | awk '{print $5}' | cut -d':' -f1| sort | uniq -c | sort

Find currently running Top Users and Scripts

TEST="$(ps aux | grep php | grep -v root | awk '{print $1,$NF}')"; U="$(echo "$TEST" | awk '{print $1}' | sort | uniq -c | sort -n |tail -n 5)"; S="$(echo "$TEST" | awk '{print $2}' | sort | uniq -c | sort -n | tail -n 5)"; P="$(echo "$TEST" | sort | uniq -c | sort -n | tail -n 10)"; echo "Top Users:"; echo "$U"; echo ""; echo "Top Scripts:"; echo "$S"; echo ""; echo "User/Script Pairs:"; echo "$P"

Complete Resource usage report of a particular User (Compatible for cPanel server)

echo "Enter the User Name:"; read u; user=$u ;for blah in seq; do echo ; uptime;echo; echo "Running Proceses:" ;echo;for pass in 1 2 3 4; do echo "---Pass $pass---"; x=1 ; while [ $x -lt 2 ]; do ps faux | grep "^${user}" ; [ $? -eq 0 ] && echo "The above processes were found to be running for user ${user} when this was executed during each pass at a 2 second interval `date` ..." || echo "No processes were found to be running for user ${user} when this was executed  during each pass at `date` ..."; sleep ".2"; let x++ ;done;echo;sleep 2;done; echo "MySQL Processes:";mysqladmin proc --verb | grep "${user}_" | perl -p -e 's/\s+\|/\|/g' | grep "" ; [ $? -eq 0 ] && echo "The above queries were found to be running for user ${user} when this was executed at `date` ..." || echo "No queries were running for user ${user} when this was executed at `date` ..."; echo $'\n'; echo "Incoming Connections:"; lynx --dump --width=10000 localhost/whm-server-status | grep "GET\|POST" | awk '{print $11,$12,$14}' | column -t | sort | grep "/~$/|Client\|$(grep ": ${user}$" /etc/userdomains | cut -d : -f1 | cut -c 1-31)"; echo; echo; done

List mail script location and the frequency

grep cwd /var/log/exim_mainlog | grep -v /var/spool | awk -F"cwd=" '{print $2}' | awk '{print $1}' | sort | uniq -c | sort -n

Discover and Probing the connectivity of all IPs in the server

for t in `ifconfig | grep "inet addr" | tr ":" " " | awk '{print $3}'`; do arping -c 3 $t > /dev/null ;done

List all running php process percentage values of CPU and Memory usage(cPanel)

for i in `ps aux|grep php |awk '{print $1}'|sort|uniq -c|sort -n|awk '{print $2}'`; do  /usr/local/cpanel/bin/dcpumonview | sed -e 's/<[^>]*>/ /g' | grep $i; done

To block IPs morethan 150 connections

 netstat -ntu | awk '{print $5}' | sed 's/::ffff://g' | cut -d: -f1 | sort | uniq -c | sort -n | grep -v -e '127.0.0.1' | awk '{ if ($1 > 150) printf("echo \"%s > %s connections\"; iptables -A INPUT -s %s -j DROP \n",$2,$1,$2,$1) | "sh" }'

Another one to Find resource usage

ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END{printf("NPROC\tUSER\tCPU\tMEM\n"); for (user in cpu) printf("%d\t%s\t%.2f%\t%.2f%\n",num[user], user, cpu[user], mem[user]) }'

 

Leave a Reply

Your email address will not be published. Required fields are marked *