Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Shell monitoring host

Shulou Source: shulou.com Published: 2022-06-01 03:06:55 09月29日 Update

1. View the traffic of the host network card

#! / bin/bash

# network

While:; do

Time='date +% m "-"% d ""% k ":"% M'

Day='date +% m "-"% d'

Rx_before='ifconfig eth0 | sed-n "8" p | awk'{print $2}'| cut-c7muri'

Tx_before='ifconfig eth0 | sed-n "8" p | awk'{print $6}'| cut-c7muri'

Sleep 2

Rx_after='ifconfig eth0 | sed-n "8" p | awk'{print $2}'| cut-c7muri'

Tx_after='ifconfig eth0 | sed-n "8" p | awk'{print $6}'| cut-c7muri'

Rx_result=$ [(rx_after-rx_before) / 256]

Tx_result=$ [(tx_after-tx_before) / 256]

Echo "$time Now_In_Speed:" $rx_result "kbps Now_OUt_Speed:" $tx_result "kbps"

Sleep 2

Done

2. System condition monitoring

#! / bin/sh

# systemstat.sh

IP=192.168.1.227

Top-n 2 | grep "Cpu" > >. / temp/cpu.txt

Free-m | grep "Mem" > >. / temp/mem.txt

Df-k | grep "sda1" > >. / temp/drive_sda1.txt

# df-k | grep sda2 > >. / temp/drive_sda2.txt

Df-k | grep "/ mnt/storage_0" > >. / temp/mnt_storage_0.txt

Df-k | grep "/ mnt/storage_pic" > >. / temp/mnt_storage_pic.txt

Time= `date +% m "."% d ""% k ":"% M`

Connect= `netstat-na | grep "219.238.148.30 grep 80" | wc-l`

Echo "$time $connect" > >. / temp/connect_count.txt

3. Monitor the disk space of the host. When the disk space exceeds 90%, send a warning by sending mail.

#! / bin/bash

# monitor available disk space

SPACE='df | sed-n'/ / $/ p'| gawk'{print $5}'| sed's Universe% Universe'

If [$SPACE-ge 90]

Then

Fty89@163.com

Fi

4. Monitor the usage of CPU and memory

#! / bin/bash

# script to capture system statistics

OUTFILE=/home/xu/capstats.csv

DATE='date +% m _% Y'

TIME='date +% k _ v% m _ v% s'

TIMEOUT='uptime'

VMOUT='vmstat 1 2'

USERS='echo $TIMEOUT | gawk'{print $4}'

LOAD='echo $TIMEOUT | gawk'{print $9}'| sed "_

FREE='echo $VMOUT | sed-n'/ [0-9] / p' | sed-n '2p' | gawk' {print $4}'

IDLE='echo $VMOUT | sed-n'/ [0-9] / p' | sed-n '2p' | gawk' {print $15}'

Echo "$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE" > > $OUTFILE

5. Omni-directional monitoring of the host

#! / bin/bash

# check_xu.sh

# 0 * / home/check_xu.sh

DAT= "`date +% Y% m% d`"

HOUR= "`date +% H`"

DIR= "/ home/oslog/host_$ {DAT} / ${HOUR}"

DELAY=60

COUNT=60

# whether the responsible directory exist

If! Test-d ${DIR}

Then

/ bin/mkdir-p ${DIR}

Fi

# general check

Export TERM=linux

/ usr/bin/top-b-d ${DELAY}-n ${COUNT} > ${DIR} / top_$ {DAT} .log 2 > & 1 &

# cpu check

/ usr/bin/sar-u ${DELAY} ${COUNT} > ${DIR} / cpu_$ {DAT} .log 2 > & 1 &

# / usr/bin/mpstat-P 0 ${DELAY} ${COUNT} > ${DIR} / cpu_0_$ {DAT} .log 2 > & 1 &

# / usr/bin/mpstat-P 1 ${DELAY} ${COUNT} > ${DIR} / cpu_1_$ {DAT} .log 2 > & 1 &

# memory check

/ usr/bin/vmstat ${DELAY} ${COUNT} > ${DIR} / vmstat_$ {DAT} .log 2 > & 1 &

# I/O check

/ usr/bin/iostat ${DELAY} ${COUNT} > ${DIR} / iostat_$ {DAT} .log 2 > & 1 &

# network check

/ usr/bin/sar-n DEV ${DELAY} ${COUNT} > ${DIR} / net_$ {DAT} .log 2 > & 1 &

# / usr/bin/sar-n EDEV ${DELAY} ${COUNT} > ${DIR} / net_edev_$ {DAT} .log 2 > & 1 &

Automatically executed hourly in crontab:

0 * / home/check_xu.sh

This will generate hourly cpu, memory, network, and IO statistics in the / home/oslog/host_yyyymmdd/hh directory.

If there is a problem in a certain period of time, you can look at the corresponding log information to see what the performance of the host is at that time.

Tags: Host monitoring memory hours space information omni-directional performance situation data log time time period traffic status directory disk system statistics network card Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Xiaomi MariaDB Shulou Technology MySQL