What is the commonly used Linux operating system monitoring script code?
This article is about what the commonly used Linux operating system monitoring script code has. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1. View the traffic of the host network card
The code is as follows:
#! / bin/bash # network # Mike.Xu while: Do time='date +% m "-"% d ""% k ":"% M 'day='date +% m "-"% d' rx_before='ifconfig eth0 | sed-n "8" p | awk'{print\ $2}'| cut-c7lily 'tx_before='ifconfig eth0 | sed-n "8" p | awk' {print\ $6}'| cut-c7waft 'sleep 2 rx_after='ifconfig eth0 | sed-n "8" p | awk' {print\ $2}'| cut-c7lam 'tx_after='ifconfig eth0 | sed-n "8" P | awk'{print\ $6}'| cut-c7Musi '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
The code is as follows:
#! / bin/sh # systemstat.sh # Mike.Xu 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 connect 80 "| wc-l`date"\ $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.
The code is as follows:
#! / bin/bash # monitor available disk space SPACE='df | sed-n'/\ /\ $/ p' | gawk'{print\ $5}'| sed's if% if [\ $SPACE-ge 90] then fty89@163.com fi
4. Monitor the usage of CPU and memory
The code is as follows:
#! / 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
The code is as follows:
#! / 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 & # DELAY 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 & 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.
Thank you for reading! This is the end of this article on "what are the commonly used Linux operating system monitoring script codes?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!