Mendiagnosis Masalah Pemuatan Server Linux dengan Skrip Sederhana

Mendiagnosis Masalah Pemuatan Server Linux dengan Skrip Sederhana
Mendiagnosis Masalah Pemuatan Server Linux dengan Skrip Sederhana

Video: Mendiagnosis Masalah Pemuatan Server Linux dengan Skrip Sederhana

Video: Mendiagnosis Masalah Pemuatan Server Linux dengan Skrip Sederhana
Video: TWK 10 Pilar Negara. Belajar Menyelesaikan Soal soal HOTS TWK Kedinasan dan CPNS 2020 - YouTube 2024, April
Anonim

Jika Anda telah menjadi admin untuk waktu yang lama, Anda pasti menemukan situasi di mana lonjakan server dalam penggunaan CPU atau penggunaan memori dan / atau tingkat beban. Menjalankan `top` tidak akan selalu memberi Anda jawaban. Jadi bagaimana Anda menemukan proses licik yang mengunyah sumber daya sistem Anda untuk dapat membunuh mereka?

Naskah berikut mungkin bisa membantu. Itu ditulis untuk server web, sehingga memiliki beberapa bagian yang secara khusus mencari proses httpd dan beberapa bagian yang berhubungan dengan MySQL. Tergantung pada penyebaran server Anda, cukup komentar / hapus bagian tersebut dan tambahkan yang lain. Itu harus digunakan untuk titik awal.

Prasyarat untuk versi skrip ini adalah beberapa freeware yang dirilis di bawah Lisensi Publik Umum GNU yang disebut mytop (tersedia di https://jeremy.zawodny.com/mysql/mytop/) yang merupakan alat yang luar biasa untuk memeriksa bagaimana kinerja MySQL. Sudah semakin tua, tetapi masih berfungsi dengan baik untuk tujuan kita di sini. Selain itu, saya menggunakan mutt sebagai mailer - Anda mungkin ingin mengubah skrip untuk hanya menggunakan linux yang dibangun di utilitas `mail`. Saya menjalankannya melalui cron setiap jam; sesuaikan sesuai keinginan Anda. Oh - dan skrip ini harus dijalankan sebagai root karena ia membaca dari beberapa area yang dilindungi dari server.

Jadi mari kita mulai, ya?

Pertama, atur variabel skrip Anda:

#!/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

Selanjutnya, periksa tingkat beban Anda untuk melihat apakah skrip harus melanjutkan:

# 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

Dan lanjutkan melalui pemeriksaan, tulis hasilnya ke file sementara. Tambahkan atau hapus item dari sini jika berlaku untuk situasi Anda:

# 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

Perhatikan dengan perintah atas, kita menulis ke dua file temp. Salah satunya adalah untuk pesan yang jauh lebih kecil ke ponsel. Jika Anda tidak ingin urgensi lansiran ponsel di jam tiga pagi, Anda dapat mengambil ini (dan keluarkan rutinitas email kedua nanti di skrip).

# 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

Pemeriksaan lebih lanjut:

# 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

Kemudian tuliskan konten file sementara ke file log yang lebih permanen dan kirim hasilnya ke pihak yang sesuai. Pengiriman kedua adalah hasil yang dikurangkan hanya terdiri dari standar dari `top`:

# 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

Dan kemudian beberapa rumah tangga dan keluar:

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

# exit 0

Semoga ini membantu seseorang di luar sana. Skrip yang dirakit sepenuhnya adalah:

#!/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

Direkomendasikan: