《几个常用的Linux监控脚本.doc》由会员分享,可在线阅读,更多相关《几个常用的Linux监控脚本.doc(2页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、最近时不时有互联网的朋友问我关于服务器监控方面的问题,问常用的服务器监控除了用开源软件,比如:cacti,nagios监控外是否可以自己写shell脚本呢?根据自己的需求写出的shell脚本更能满足需求,更能细化主机监控的全面性。下面是我常用的几个主机监控的脚本,大家可以根据自己的情况再进行修改,希望能给大家一点帮助。1、查看主机网卡流量#!/bin/bash#network#Mike.Xuwhile:dotime=date+%m-%d%k:%Mday=date+%m-%drx_before=ifconfigeth0|sed-n8p|awkprint$2|cut-c7-tx_before=if
2、configeth0|sed-n8p|awkprint$6|cut-c7-sleep2rx_after=ifconfigeth0|sed-n8p|awkprint$2|cut-c7-tx_after=ifconfigeth0|sed-n8p|awkprint$6|cut-c7-rx_result=$(rx_after-rx_before)/256tx_result=$(tx_after-tx_before)/256echo$timeNow_In_Speed:$rx_resultkbpsNow_OUt_Speed:$tx_resultkbpssleep2done2、系统状况监控#!/bin/sh
3、#systemstat.sh#Mike.XuIP=192.168.1.227top-n2|grepCpu./temp/cpu.txtfree-m|grepMem./temp/mem.txtdf-k|grepsda1./temp/drive_sda1.txt#df-k|grepsda2./temp/drive_sda2.txtdf-k|grep/mnt/storage_0./temp/mnt_storage_0.txtdf-k|grep/mnt/storage_pic./temp/mnt_storage_pic.txttime=date+%m.%d%k:%Mconnect=netstat-na|
4、grep219.238.148.30:80|wc-lecho$time$connect./temp/connect_count.txt3、监控主机的磁盘空间,当使用空间超过90就通过发mail来发警告#!/bin/bash#monitoravailablediskspaceSPACE=df|sed-n/$/p|gawkprint$5|seds/%/if$SPACE-ge90thenfty89fi4、监控CPU和内存的使用情况#!/bin/bash#scripttocapturesystemstatisticsOUTFILE=/home/xu/capstats.csvDATE=date+%m/%
5、d/%YTIME=date+%k:%m:%sTIMEOUT=uptimeVMOUT=vmstat12USERS=echo$TIMEOUT|gawkprint$4LOAD=echo$TIMEOUT|gawkprint$9|seds/,/FREE=echo$VMOUT|sed-n/0-9/p|sed-n2p|gawkprint$4IDLE=echo$VMOUT|sed-n/0-9/p|sed-n2p|gawkprint$15echo$DATE,$TIME,$USERS,$LOAD,$FREE,$IDLE$OUTFILE5、全方位监控主机#!/bin/bash#check_xu.sh#0*/home
6、/check_xu.shDAT=date+%Y%m%dHOUR=date+%HDIR=/home/oslog/host_$DAT/$HOURDELAY=60COUNT=60#whethertheresponsibledirectoryexistif!test-d$DIRthen/bin/mkdir-p$DIRfi#generalcheckexportTERM=linux/usr/bin/top-b-d$DELAY-n$COUNT$DIR/top_$DAT.log2&1cpucheck/usr/bin/sar-u$DELAY$COUNT$DIR/cpu_$DAT.log2&1/usr/b
7、in/mpstat-P0$DELAY$COUNT$DIR/cpu_0_$DAT.log2&1/usr/bin/mpstat-P1$DELAY$COUNT$DIR/cpu_1_$DAT.log2&1memorycheck/usr/bin/vmstat$DELAY$COUNT$DIR/vmstat_$DAT.log2&1I/Ocheck/usr/bin/iostat$DELAY$COUNT$DIR/iostat_$DAT.log2&1networkcheck/usr/bin/sar-nDEV$DELAY$COUNT$DIR/net_$DAT.log2&1/usr/bin/sar-nEDEV$DELAY$COUNT$DIR/net_edev_$DAT.log2&1&放在crontab里每小时自动执行:0*/home/check_xu.sh这样会在/home/oslog/host_yyyymmdd/hh目录下生成各小时cpu、内存、网络,IO的统计数据。如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。
限制150内