Diagnose von Linux Server-Ladeproblemen mit einem einfachen Skript

Video: Diagnose von Linux Server-Ladeproblemen mit einem einfachen Skript

Video: Diagnose von Linux Server-Ladeproblemen mit einem einfachen Skript
Video: TASTENKOMBINATIONEN: 10 KÜRZEL, die DU kennen MUSST – Windows und Mac 2024, März
Diagnose von Linux Server-Ladeproblemen mit einem einfachen Skript
Diagnose von Linux Server-Ladeproblemen mit einem einfachen Skript
Anonim

Wenn Sie längere Zeit Administrator waren, haben Sie sicherlich Situationen entdeckt, in denen ein Server die CPU-Auslastung, die Speicherauslastung und / oder die Auslastung stark belastet. Wenn Sie "top" ausführen, erhalten Sie auch nicht immer die Antwort. Wie finden Sie diese hinterlistigen Prozesse, die Ihre Systemressourcen auffressen, um sie zu töten?

Das folgende Skript kann möglicherweise helfen. Es wurde für einen Webserver geschrieben, daher gibt es einige Teile, die speziell nach httpd-Prozessen suchen, und einige, die sich mit MySQL beschäftigen. Kommentieren / löschen Sie diese Abschnitte je nach Server-Bereitstellung und fügen Sie weitere hinzu. Es sollte als Ausgangspunkt verwendet werden.

Voraussetzung für diese Version des Skripts ist eine Freeware, die unter der GNU General Public License namens mytop (verfügbar unter https://jeremy.zawodny.com/mysql/mytop/) veröffentlicht wird. Dies ist ein fantastisches Werkzeug, um die Leistung von MySQL zu überprüfen. Es wird alt, aber es funktioniert immer noch gut für unsere Zwecke hier. Außerdem verwende ich mutt als Mailer. Vielleicht möchten Sie das Skript so ändern, dass es einfach das in `mail` integrierte Hilfsprogramm verwendet. Ich lasse es jede Stunde per cron laufen; passen Sie an, wie Sie es für richtig halten. Oh - und dieses Skript muss als root ausgeführt werden, da es aus einigen geschützten Bereichen des Servers liest.

Also lasst uns anfangen, oder?

Legen Sie zunächst Ihre Skriptvariablen fest:

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

Prüfen Sie als Nächstes Ihre Auslastungsstufe, um festzustellen, ob das Skript fortgesetzt werden soll:

# 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

Fahren Sie mit den Prüfungen fort und schreiben Sie die Ergebnisse in die temporäre Datei. Fügen Sie hier Elemente hinzu oder löschen Sie sie, wenn dies auf Ihre Situation zutrifft:

# 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

Beachten Sie mit dem Befehl top, dass wir in zwei temporäre Dateien schreiben. Eine ist für die viel kleinere Nachricht an das Handy. Wenn Sie die Dringlichkeit von Handybenachrichtigungen um drei Uhr morgens nicht möchten, können Sie dies herausnehmen (und die zweite Mailingroutine später im Skript herausnehmen).

# 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

Weitere Prüfungen:

# 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

Dann schreiben Sie den Inhalt der temporären Datei in eine dauerhaftere Protokolldatei und senden Sie die Ergebnisse per E-Mail an die entsprechenden Parteien. Das zweite Mailing ist das reduzierte Ergebnis, das einfach aus dem Standard von "top" besteht:

# 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

Und dann etwas Hauswirtschaft und Ausgang:

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

# exit 0

Hoffentlich hilft das jemandem da draußen. Vollständig zusammengesetztes Skript ist:

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

Empfohlen: