Five common Linux monitoring script code _linux Shell

Source: Internet
Author: User
Tags memory usage

1, view the host network card traffic

Copy Code code as follows:

#!/bin/bash
#network
#Mike. Xu
While:; do
  &nbs p;   time= ' Date +%m '-'%d ' '%k ': '%m '
      day= ' date +%m '-'%d '
       rx_before= ' ifconfig eth0|sed-n ' 8 ' P|awk ' {print $} ' |cut-c7-'
      Tx_before= ' ifconfig eth0|sed-n ' 8 ' P|awk ' {print $} ' |cut-c7-'
      sleep 2
 &NB sp;    rx_after= ' ifconfig eth0|sed-n ' 8 ' P|awk ' {print $} ' |cut-c7-'
       tx_after= ' ifconfig eth0|sed-n ' 8 ' P|awk ' {print $} ' |cut-c7-'
      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"
       2
Done

2. System condition Monitoring

Copy Code code 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:80" | Wc-l '
echo "$time $connect" >>/temp/connect_count.txt

3, monitoring the host's disk space, when the use of more than 90% in space by sending mail to send a warning

Copy Code code as follows:

#!/bin/bash
#monitor Available disk space
Space= ' DF | Sed-n '/\/$/P ' | Gawk ' {print $} ' | Sed ' s/%//'
If [$SPACE-ge 90]
Then
Jbxue123@163.com
Fi

4. Monitor CPU and Memory usage

Copy Code code as follows:

#!/bin/bash
#script to capture system statistics
Outfile=/home/xu/capstats.csv
Date= ' Date +%m/%d/%y '
Time= ' Date +%k:%m:%s '
timeout= ' uptime '
Vmout= ' Vmstat 1 2 '
Users= ' echo $TIMEOUT | Gawk ' {print $} '
Load= ' echo $TIMEOUT | Gawk ' {print $} ' | Sed "s/,//"
Free= ' echo $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' | Gawk ' {print $} '
Idle= ' echo $VMOUT | Sed-n '/[0-9]/p ' | Sed-n ' 2p ' |gawk ' {print $} '
echo "$DATE, $TIME, $USERS, $LOAD, $FREE, $IDLE" >> $OUTFILE

5, the Omni-directional monitoring host

Copy Code code 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 &
# 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 &

Put it in the crontab. Automatic execution per hour:
0 * * * */home/check_xu.sh

This will generate hourly CPU, memory, network, IO statistics in the/HOME/OSLOG/HOST_YYYYMMDD/HH directory.
If a time period problem arises, you can look at the corresponding log information, to see how the host performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.