Diagnozuoti Linux serverio apkrovimo problemas naudojant paprastą scenarijų

Diagnozuoti Linux serverio apkrovimo problemas naudojant paprastą scenarijų
Diagnozuoti Linux serverio apkrovimo problemas naudojant paprastą scenarijų

Video: Diagnozuoti Linux serverio apkrovimo problemas naudojant paprastą scenarijų

Video: Diagnozuoti Linux serverio apkrovimo problemas naudojant paprastą scenarijų
Video: How to Display Only the Unread Email in Your Primary Inbox Category in Gmail - YouTube 2024, Balandis
Anonim

Jei turite kokį nors laiko administratorių, jūs tikrai atskleidėte situacijas, kai serveris padidina procesoriaus naudojimą arba atminties panaudojimą ir (arba) apkrovos lygius. Veikia "viršuje", ne visada jums atsakys. Taigi, kaip jūs galite rasti tuos niekšiškus procesus, kurie kramtina jūsų sistemos išteklius, kad juos būtų galima nužudyti?

Šis scenarijus gali padėti. Jis buvo parašytas žiniatinklio serveriui, todėl jis turi tam tikrų jo dalių, kurios konkrečiai ieško httpd procesų ir kai kurių dalių, susijusių su MySQL. Priklausomai nuo jūsų serverio diegimo, paprasčiausiai komentuokite / ištrinkite tuos skirsnius ir pridėkite kitų. Jis turėtų būti naudojamas atspirties taškui.

Šios scenarijaus versijos prielaidos yra kai kurios nemokamos programinės įrangos versijos, išleistos pagal GNU Bendrąją viešąją licenciją, vadinamą mytop (galima rasti https://jeremy.zawodny.com/mysql/mytop/), kuri yra fantastinis įrankis, skirtas patikrinti, kaip MySQL veikia. Tai senėja, bet čia puikiai tinka mūsų tikslams. Be to, aš naudoju "mutt" kaip "mailer" - galbūt norėsite pakeisti scenarijų, kad paprasčiausiai naudotumėte "mail" naudingą "linux". Aš ją paleisti kas valandą per cron; koreguokite, kaip matote. Oi - ir šis scenarijus turi veikti kaip root, nes jis nuskaito iš kai kurių saugomų serverio sričių.

Taigi pradėkime, ar mes?

Pirmiausia nustatykite savo scenarijaus kintamuosius:

#!/bin/bash # # Script to check system load average levels to try to determine # what processes are taking it overly high… # # 07Jul2010 tjones # # set environment dt=`date +%d%b%Y-%X` # Obviously, change the following directories to where your log files actually are kept tmpfile='/tmp/checkSystemLoad.tmp' logfile='/tmp/checkSystemLoad.log' msgLog='/var/log/messages' mysqlLog='/var/log/mysqld.log' # the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report) mailstop='[email protected]' mailstop1='[email protected]' machine=`hostname` # The following three are for mytop use - use a db user that has decent rights dbusr='username' dbpw='password' db='yourdatabasename' # The following is the load level to check on - 10 is really high, so you might want to lower it. levelToCheck=10

Tada patikrinkite savo apkrovos lygį, norėdami pamatyti, ar scenarijus turėtų tęstis:

# Set variables from system: loadLevel=`cat /proc/loadavg | awk '{print $1}'` loadLevel=$( printf '%0.f' $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

if [ $loadLevel -gt $levelToCheck ]; then echo '' > $tmpfile echo '**************************************' >>$tmpfile echo 'Date: $dt ' >>$tmpfile echo 'Check System Load & Processes ' >>$tmpfile echo '**************************************' >>$tmpfile

Ir tęskite patikrinimus, rašydami rezultatus į laikiną failą. Pridėti ar ištrinti elementus iš čia, kai tai taikoma jūsų situacijai:

# Get more variables from system: httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo 'Load Level Is: $loadLevel' >>$tmpfile echo '*************************************************' >>$tmpfile

# Show number of httpd processes now running (not including children): echo 'Number of httpd processes now: $httpdProcesses' >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show process list: echo 'Processes now running:' >>$tmpfile ps f -ef >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show current MySQL info: echo 'Results from mytop:' >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

Pastaba su aukščiausią komandą, mes rašome du temp failai. Vienas iš jų yra daug mažesnis pranešimas mobiliajam telefonui. Jei nenorite, kad skambučiai būtų rodomi mobiliuoju telefonu tris ryte, galite tai padaryti (ir vėliau pasiimkite antrąją pašto tvarką scenarijuje).

# Show current top: echo 'top now shows:' >>$tmpfile echo 'top now shows:' >>$topfile /usr/bin/top -b -n1 >>$tmpfile /usr/bin/top -b -n1 >>$topfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

Daugiau patikrinimų:

# Show current connections: echo 'netstat now shows:' >>$tmpfile /bin/netstat -p >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Check disk space echo 'disk space:' >>$tmpfile /bin/df -k >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

Tada įrašykite laikiną failo turinį į nuolatinį žurnalo failą ir išsiųskite rezultatus atitinkamoms šalims. Antroji siunta yra išrinkti rezultatai, sudaryti tik iš standarto iš viršaus:

# Send results to log file: /bin/cat $tmpfile >>$logfile

# And email results to sysadmin: /usr/bin/mutt -s '$machine has a high load level! - $dt' -a $mysqlLog -a $msgLog $mailstop <$tmpfile /usr/bin/mutt -s '$machine has a high load level! - $dt' $mailstop1 <$topfile echo '**************************************' >>$logfile

Ir tada kai kurie namų ūkio ir išeiti:

# And then remove the temp file: rm $tmpfile rm $topfile fi

# exit 0

Tikimės, kad tai padės kažkas ten. Visiškai surinktas scenarijus yra:

#!/bin/bash # # Script to check system load average levels to try to determine what processes are # taking it overly high… # # set environment dt=`date +%d%b%Y-%X` # Obviously, change the following directories to where your log files actually are kept tmpfile='/tmp/checkSystemLoad.tmp' logfile='/tmp/checkSystemLoad.log' msgLog='/var/log/messages' mysqlLog='/var/log/mysqld.log' # the first mailstop is standard email for reports. Second one is for cell phone (with a pared down report) mailstop='[email protected]' mailstop1='[email protected]' machine=`hostname` # The following three are for mytop use - use a db user that has decent rights dbusr='username' dbpw='password' db='yourdatabasename' # The following is the load level to check on - 10 is really high, so you might want to lower it. levelToCheck=10 # Set variables from system: loadLevel=`cat /proc/loadavg | awk '{print $1}'` loadLevel=$( printf '%0.f' $loadLevel )

# if the load level is greater than you want, start the script process. Otherwise, exit 0

if [ $loadLevel -gt $levelToCheck ]; then echo '' > $tmpfile echo '**************************************' >>$tmpfile echo 'Date: $dt ' >>$tmpfile echo 'Check System Load & Processes ' >>$tmpfile echo '**************************************' >>$tmpfile

# Get more variables from system: httpdProcesses=`ps -def | grep httpd | grep -v grep | wc -l`

# Show current load level: echo 'Load Level Is: $loadLevel' >>$tmpfile echo '*************************************************' >>$tmpfile

# Show number of httpd processes now running (not including children): echo 'Number of httpd processes now: $httpdProcesses' >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show process list: echo 'Processes now running:' >>$tmpfile ps f -ef >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show current MySQL info: echo 'Results from mytop:' >>$tmpfile /usr/bin/mytop -u $dbusr -p $dbpw -b -d $db >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show current top: echo 'top now shows:' >>$tmpfile echo 'top now shows:' >>$topfile /usr/bin/top -b -n1 >>$tmpfile /usr/bin/top -b -n1 >>$topfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Show current connections: echo 'netstat now shows:' >>$tmpfile /bin/netstat -p >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Check disk space echo 'disk space:' >>$tmpfile /bin/df -k >>$tmpfile echo '*************************************************' >>$tmpfile echo '' >>$tmpfile

# Send results to log file: /bin/cat $tmpfile >>$logfile

# And email results to sysadmin: /usr/bin/mutt -s '$machine has a high load level! - $dt' -a $mysqlLog -a $msgLog $mailstop <$tmpfile /usr/bin/mutt -s '$machine has a high load level! - $dt' $mailstop1 <$topfile echo '**************************************' >>$logfile

# And then remove the temp file: rm $tmpfile rm $topfile fi

# exit 0

Rekomenduojamas: