Inhaltsverzeichnis

Systembelastung ermitteln

Werkzeug

vmstat -a

# vmstat -a 1 10
procs ------------memory----------- ---swap-- -----io---- -system-- ----cpu-----
 r  b   swpd   free   inact  active   si   so    bi    bo   in   cs us sy  id wa
 0  0   5372  54552 2309996 4849848    0    0   269    28    0    0  1  0  98  0
 0  0   5372  54552 2310040 4849880    0    0    28     0  312  450  0  0 100  0
 0  0   5372  54552 2310040 4849880    0    0    16     0  184  234  0  0 100  0
 0  0   5372  54552 2310036 4849884    0    0     8     0  126  141  0  0 100  0
 0  0   5372  54552 2310036 4849884    0    0     0     0  179  288  0  0 100  0
 0  0   5372  54552 2310036 4849884    0    0    12     0  126  178  0  0 100  0
 0  0   5372  54552 2310080 4849896    0    0    12     0  143  217  0  0 100  0
 0  0   5372  54544 2310096 4849940    0    0    28   688  586 1114  0  0  99  0
 0  0   5372  54552 2310088 4849936    0    0     0     0  123  124  0  0 100  0
 0  0   5372  54552 2310100 4849936    0    0     4     0  160  113  0  0 100  0
  1. Spalte (procs - r): number of processes waiting for run time
  2. Spalte (procs - b): number of processes in uninterruptible sleep
  3. Spalte (memory - swpd): mem swap in use
  4. Spalte (memory - free): mem free/idle
  5. Spalte (memory - inact): mem inactive
  6. Spalte (memory - active): mem active
  7. Spalte (swap - si): swap read
  8. Spalte (swap - so): swap write
  9. Spalte (io - bi): io block in
  10. Spalte (io - bo): io block out
  11. Spalte (system - in): system interrupts
  12. Spalte (system - cs): system context switches per second
  13. Spalte (cpu - us): cpu user time
  14. Spalte (cpu - sy): cpu system time
  15. Spalte (cpu - id): cpu idle
  16. Spalte (cpu - wa): cpu waiting for io

Abfrage

belastung.sh
#!/bin/bash
 
# Linux
 
ANZAHL="10"                             # Messzeit für vmstat, RX und TX
                                        # ${ANZAHL} x 3 = mind. Laufzeit dieses Scripts
 
TICKER="$(echo "${ANZAHL}" | awk '{print 3+$1}')"
VMSTAT="$(vmstat -a 1 ${TICKER} | tail -n1 | awk -v z=${ANZAHL} '{ab+=$1; bb+=$2; cb+=$3; db+=$4; eb+=$5; fb+=$6; gb+=$7; hb+=$8; ib+=$9; jb+=$10; kb+=$11; lb+=$12; mb+=$13; nb+=$14; ob+=$15; pb+=$16} END {print "number_of_processes_waiting_for_run_time="ab/z,"number_of_processes_in_uninterruptible_sleep="bb/z,"mem_swap_in_use="cb/z,"mem_free="db/z,"mem_inactive="eb/z,"mem_active="fb/z,"swap_read="gb/z,"swap_write="hb/z,"io_block_in="ib/z,"io_block_out="jb/z,"system_interrupts="kb/z,"system_context_switches_per_second="lb/z,"cpu_user_time="mb/z,"cpu_system_time="nb/z,"cpu_idle="ob/z,"cpu_waiting_for_io="pb/z}')"
 
RXBPS="$(RXB="$(cat /sys/class/net/eth*/statistics/rx_bytes | awk '{z=$1;s+=z}END{print s}')";sleep ${ANZAHL};echo "$(cat /sys/class/net/eth*/statistics/rx_bytes | awk '{z=$1;s+=z}END{print s}') $RXB" | awk -v z=${ANZAHL} '{print "byte_je_sekunde_empfangen="($1-$2)/z}')"
 
TXBPS="$(TXB="$(cat /sys/class/net/eth*/statistics/tx_bytes | awk '{z=$1;s+=z}END{print s}')";sleep ${ANZAHL};echo "$(cat /sys/class/net/eth*/statistics/tx_bytes | awk '{z=$1;s+=z}END{print s}') $TXB" | awk -v z=${ANZAHL} '{print "byte_je_sekunde_gesendet="($1-$2)/z}')"
 
echo "hostname=$(hostname -f) ${VMSTAT} ${RXBPS} ${TXBPS} apache_processes=$(ps -ef | egrep '^www-data.*apache2 -k start' | wc -l) $(uptime | awk '{gsub(",","");print "load_average_1="$(NF-2),"load_average_5="$(NF-1),"load_average_15="$(NF)}')" | tr -s ' ' '\n'

Ausgabe

hostname=test.domain.de
number_of_processes_waiting_for_run_time=0
number_of_processes_in_uninterruptible_sleep=0
mem_swap_in_use=0
mem_free=131625
mem_inactive=38892.4
mem_active=23385.2
swap_read=0
swap_write=0
io_block_in=0
io_block_out=0
system_interrupts=3.6
system_context_switches_per_second=2
cpu_user_time=0
cpu_system_time=0
cpu_idle=10
cpu_waiting_for_io=0
byte_je_sekunde_empfangen=260.9
byte_je_sekunde_gesendet=377.1
apache_processes=10
load_average_1=0.02
load_average_5=0.06
load_average_15=0.02

Datenbank gestützte Überwachung

Datenbank, Tabelle und User anlegen

Datenbank anlegen:

# echo "CREATE DATABASE belastungswaechter;" | mysql -uroot -p

Tabelle anlegen:

# echo "CREATE TABLE belastungswaechter (hostname VARCHAR(254) not NULL, number_of_processes_waiting_for_run_time DECIMAL(16,4), number_of_processes_in_uninterruptible_sleep DECIMAL(16,4), mem_swap_in_use DECIMAL(16,4), mem_free DECIMAL(16,4), mem_inactive DECIMAL(16,4), mem_active DECIMAL(16,4), swap_read DECIMAL(16,4), swap_write DECIMAL(16,4), io_block_in DECIMAL(16,4), io_block_out DECIMAL(16,4), system_interrupts DECIMAL(16,4), system_context_switches_per_second DECIMAL(16,4), cpu_user_time DECIMAL(16,4), cpu_system_time DECIMAL(16,4), cpu_idle DECIMAL(16,4), cpu_waiting_for_io DECIMAL(16,4), byte_je_sekunde_empfangen DECIMAL(16,4), byte_je_sekunde_gesendet DECIMAL(16,4), apache_processes DECIMAL(16,4), load_average_1 DECIMAL(16,4), load_average_5 DECIMAL(16,4), load_average_15 DECIMAL(16,4),  startzeit_fuer_die_messung DATETIME);" | mysql -uroot -p belastungswaechter

User anlegen:

# echo "INSERT INTO user (host,user) VALUES ('%','bwaechter');" | mysql -uroot -p mysql
# echo "FLUSH PRIVILEGES;" | mysql -uroot mysql

dem User die nötigen Berechtigungen geben:

# echo "REVOKE ALL ON *.* TO bwaechter;" | mysql -uroot -p mysql
# echo "GRANT SELECT,INSERT ON * TO bwaechter IDENTIFIED BY 'geheim' WITH GRANT OPTION;FLUSH PRIVILEGES;" | mysql -uroot -p belastungswaechter

belastungswaechter.sh

#!/bin/bash

ANZAHL="10"                           # Messzeit für vmstat, RX und TX
                                      # ${ANZAHL} x 3 = mind. Laufzeit dieses Scripts
BELASTUNGSWAECHTER="$(awk -F':' '/:omrelp:/ {print $3}' /etc/rsyslog.d/50-default.conf)"   # DB-Host
BELASTUNGSUSER="bwaechter"            # DB-User
BELASTUNGSPASSWD="geheim"       # DB-Passwd
BELASTUNGSDB="belastungswaechter"     # DB-Name
BELASTUNGSTAB="belastungswaechter"    # DB-Tabellenname
STARTMESSUNG="$(date +'%F %T')"       # Startzeit für die Messung
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/bin"

if [ -z "${BELASTUNGSWAECHTER}" ] ; then
      exit 0
fi

TICKER="$(echo "${ANZAHL}" | awk '{print 3+$1}')"
VMSTAT="$(vmstat -a 1 ${TICKER} | tail -n${ANZAHL} | awk -v z=${ANZAHL} '{ab+=$1; bb+=$2; cb+=$3; db+=$4; eb+=$5; fb+=$6; gb+=$7; hb+=$8; ib+=$9; jb+=$10; kb+=$11; lb+=$12; mb+=$13; nb+=$14; ob+=$15; pb+=$16} END {print "number_of_processes_waiting_for_run_time="ab/z,"number_of_processes_in_uninterruptible_sleep="bb/z,"mem_swap_in_use="cb/z,"mem_free="db/z,"mem_inactive="eb/z,"mem_active="fb/z,"swap_read="gb/z,"swap_write="hb/z,"io_block_in="ib/z,"io_block_out="jb/z,"system_interrupts="kb/z,"system_context_switches_per_second="lb/z,"cpu_user_time="mb/z,"cpu_system_time="nb/z,"cpu_idle="ob/z,"cpu_waiting_for_io="pb/z}')"

RXBPS="$(RXB="$(cat /sys/class/net/eth*/statistics/rx_bytes | awk '{z=$1;s+=z}END{print s}')";sleep ${ANZAHL};echo "$(cat /sys/class/net/eth*/statistics/rx_bytes | awk '{z=$1;s+=z}END{print s}') $RXB" | awk -v z=${ANZAHL} '{print "byte_je_sekunde_empfangen="($1-$2)/z}')"
TXBPS="$(TXB="$(cat /sys/class/net/eth*/statistics/tx_bytes | awk '{z=$1;s+=z}END{print s}')";sleep ${ANZAHL};echo "$(cat /sys/class/net/eth*/statistics/tx_bytes | awk '{z=$1;s+=z}END{print s}') $TXB" | awk -v z=${ANZAHL} '{print "byte_je_sekunde_gesendet="($1-$2)/z}')"

PARAM=""
WERTE=""

for SPALTE in $(echo "hostname=$(hostname -f) ${VMSTAT} ${RXBPS} ${TXBPS} apache_processes=$(ps -ef | egrep '^www-data.*apache2 -k start' | wc -l) $(uptime | awk '{gsub(",","");print "load_average_1="$(NF-2),"load_average_5="$(NF-1),"load_average_15="$(NF)}')")
do
        PARAM+="$(echo ${SPALTE} | awk -F'=' '{print $1}'), "
        WERTE+="$(echo ${SPALTE} | awk -F'=' '{print "\""$2"\""}'), "
done

PARAREIHE="${PARAM} startzeit_fuer_die_messung"
WERTREIHE="${WERTE} \"${STARTMESSUNG}\""


### ab in die Datenbank mit den Daten
echo "INSERT INTO ${BELASTUNGSTAB} (${PARAREIHE}) VALUES (${WERTREIHE})" | mysql -h ${BELASTUNGSWAECHTER} -u${BELASTUNGSUSER} -p${BELASTUNGSPASSWD} ${BELASTUNGSDB}