几个常用的Linux操作系统监控脚本.docx
《几个常用的Linux操作系统监控脚本.docx》由会员分享,可在线阅读,更多相关《几个常用的Linux操作系统监控脚本.docx(14页珍藏版)》请在冰豆网上搜索。
几个常用的Linux操作系统监控脚本
几个常用的Linux操作系统监控脚本:
1、查看主机网卡流量
#!
/bin/bash#network#Mike.Xuwhile:
;dotime='date+%m"-"%d""%k":
"%M'day='date+%m"-"%d'rx_before='ifconfigeth0|sed-n"8"p|awk'{print\$2}'|cut-c7-'tx_before='ifconfigeth0|sed-n"8"p|awk'{print\$6}'|cut-c7-'sleep2rx_after='ifconfigeth0|sed-n"8"p|awk'{print\$2}'|cut-c7-'tx_after='ifconfigeth0|sed-n"8"p|awk'{print\$6}'|cut-c7-'rx_result=\$[(rx_after-rx_before)/256]tx_result=\$[(tx_after-tx_before)/256]echo"\$timeNow_In_Speed:
"\$rx_result"kbpsNow_OUt_Speed:
"\$tx_result"kbps"sleep2done
2、系统状况监控
#!
/bin/sh#systemstat.sh#Mike.XuIP=192.168.1.227top-n2|grep"Cpu"》./temp/cpu.txtfree-m|grep"Mem"》./temp/mem.txtdf-k|grep"sda1"》./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":
"%M`connect=`netstat-na|grep"219.238.148.30:
80"|wc-l`echo"\$time\$connect"》./temp/connect_count.txt
3、监控主机的磁盘空间,当使用空间超过90%就通过发mail来发警告
#!
/bin/bash#monitoravailablediskspaceSPACE='df|sed-n'/\/\$/p'|gawk'{print\$5}'|sed's/%//'if[\$SPACE-ge90]thenfty89@fi
4、监控CPU和内存的使用情况
#!
/bin/bash#scripttocapturesystemstatisticsOUTFILE=/home/xu/capstats.csv
DATE='date+%m/%d/%Y'
TIME='date+%k:
%m:
%s'
TIMEOUT='uptime'
VMOUT='vmstat12'
USERS='echo\$TIMEOUT|gawk'{print\$4}''
LOAD='echo\$TIMEOUT|gawk'{print\$9}'|sed"s/,//''
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、全方位监控主机
#!
/bin/bash#check_xu.sh#0****/home/check_xu.shDAT="`date+%Y%m%d`"HOUR="`date+%H`"DIR="/home/oslog/host_\${DAT}/\${HOUR}"DELAY=60COUNT=60#whethertheresponsibledirectoryexistif!
test-d\${DIR}then/bin/mkdir-p\${DIR}fi#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/bin/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的统计数据。
如果某个时间段产生问题了,就可以去看对应的日志信息,看看当时的主机性能如何。
生产服务器LNMP全自动安装脚本
使用方法:
将nginx_php代码保存一个文件,例如nginx_php。
上传到服务器上执行。
此脚本在CentOS5.6i386/x86_64环境下通过。
bashnginx_php出现提示,如下列图所示。
根据大家自己的需要选择数字中任意一个,开始全自动安装LNMP的世界中去吧。
如果之前安装过nginx或者php的话,脚本会自动检测它们是不是安装过。
例如
就会提示nginx或者php已经安装过了。
nginx_php源码
#!
/bin/bash
#author:
coralzdpoweredbywww.freebsdsystem.org
#writtenbycoralzd2011.09.30
#version0.1.5build20110930
nginx_dir="/usr/local/nginx"
php52_dir="/usr/local/php52"
mysql_dir="/usr/local/mysql"
functioncheck_init()#检测初始化工作有没有执行
{
if[`rpm-qa|grepre2c`=="re2c-0.12.1-2.el5.x86_64"];then
echo"initalreadyexec!
"
exit0
fi
}
functioncheck_nginx()#检测nginx有没有安装
{
if[-d/usr/local/nginx/];then
echo"nginxalreadyinstalled!
"
exit1
fi
}
functioncheck_php52()#检测php有没有安装
{
if[-d/usr/local/php52];then
echo"php5.2.17alreadyinstalled!
"
exit1
fi
}
functioncheck_mysql()#检测mysql有没有安装
{
if[-d/usr/local/mysql/];then
echo-n"mysqlalreadyinstalled!
"
exit1
fi
}
functioninit()#系统初始化工作
{
yum-yinstallwgetgccgcc-c++autoconfbisonflexre2clibmhashlibjpeglibjpeg-devellibpnglibpng-devel\
freetypefreetype-develgdgd-devellibxml2libxml2-develzlibzlib-develglibcglibc-develglib2glib2-devel\
bzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5-devellibidnlibidn-developensslopenssl-devel
echo-n"startingdownloadnginx_php..."
cat>list<<"EOF"&&
nginx-1.0.1.tar.gz
php-5.2.17.tar.gz
php-5.2.17-fpm-0.5.14.diff.gz
libiconv-1.13.1.tar.gz
libmcrypt-2.5.8.tar.gz
mcrypt-2.6.8.tar.gz
memcache-2.2.5.tgz
mhash-0.9.9.9.tar.gz
mysql-5.1.58.tar.gz
suhosin-patch-5.2.17-0.9.7.patch
pcre-8.10.tar.gz
autoconf-2.13.tar.gz
eaccelerator-0.9.5.3.tar.bz2
PDO_MYSQL-1.0.2.tgz
libunwind-0.99.tar.gz
ImageMagick.tar.gz
imagick-2.3.0.tgz
google-perftools-1.6.tar.gz
fcgi.conf
php.ini
php-fpm
nginxd
f
nginx.conf
php-fpm.conf
EOF
mkdirpackages
foriin`catlist`
do
if[-spackages/$i];then
echo"$i[found]"
else
echo"Error:
$inotfound!
!
!
downloadnow......"
wgethttp:
//www.freebsdsystem.org/linux/nginx-php/$i-Ppackages/
fi
done
mvpackages/nginxd/etc/init.d/
mvpackages/php-fpm/etc/init.d/
chmod755/etc/init.d/*
chkconfig--addnginxd
chkconfig--addphp-fpm
echo"createtheeacceleratordirectoryto/data0/cache"
mkdir/data0/cache
echo"createthemysqldatadirectoryto/data0/mysql/data"
mkdir-p/data0/mysql/data
echo"PATH=/usr/local/mysql/bin/:
$PATH">>/etc/profile
source/etc/profile
groupadd-g80www&&useraddwww-s/sbin/nologin-gwww-u80
groupadd-g3306mysql&&useraddmysql-s/sbin/nologin-gmysql-u3306
echo"wwwandmysqluser&&groupcreate!
"
cdpackages/
/bin/rm-rflist
echo-e"Allofinstalledsucussful!
"
}
functionis_version()#检测系统是64位的还是32位的?
{
if[`uname-m`=="x86_64"];then
tarzxflibunwind-0.99.tar.gz
tarzxvflibunwind-0.99.tar.gz
cdlibunwind-0.99/
CFLAGS=-fPIC./configure
makeCFLAGS=-fPIC
makeCFLAGS=-fPICinstall
cd../
else
echo"yoursystemis32bit,notinstalllibunwindlib!
"
fi
}
functionins_nginx()#安装nginx
{
check_nginx
cdpackages/
is_version
tarzxfgoogle-perftools-1.6.tar.gz
cdgoogle-perftools*
./configure
make
makeinstall
cd..
tarzxfpcre-8.10.tar.gz
cdpcre-*
./configure
make
makeinstall
cd..
tarzxfnginx-1.0.1.tar.gz
cdnginx-1.0.1
./configure--prefix=${nginx_dir}--user=www--group=www--with-http_stub_status_module
make&&makeinstall
cd..
rm-rf/usr/local/nginx/conf/nginx.conf
echo"/usr/local/lib">/etc/ld.so.conf.d/usr_local_lib.conf
cpnginx.conf/usr/local/nginx/conf/
cpfcgi.conf/usr/local/nginx/conf/
echo"nginxinstalledsucussfully!
"
}
functionins_mysqlserver()#安装mysql服务端
{
cdpackages/
tarzxfmysql-5.1.58.tar.gz
cdmysql-5.1.58
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=nocona-O2-pipe"
CXXFLAGS="${CFLAGS}"
./configure--prefix=/usr/local/mysql--enable-assembler--with-server-suffix=-DZWWW--enable-thread-safe-client--enable-local-infile--enable-thread-safe-client--with-big-tables--with-charset=utf8--with-client-ldflags=-all-static--with-collation=utf8_general_ci--with-extra-charsets=all--with-mysqld-ldflags=-all-static--with-mysqld-ldflags=-ltcmalloc--with-mysqld-user=mysql--with-plugins=partition,myisammrg--with-pthread--with-unix-socket-path=/tmp/mysql.sock--without-ndb-debug
make&&makeinstall
cpsupport-*/mysql.server/etc/init.d/mysqld
cpf/etc/
chmod744/etc/init.d/mysqld
cd/usr/local/mysql
chown-Rmysql:
mysql.
rm-rfsql-benchmysql-test
mkdir-p/data0/mysql/relaylog/
mkdir-p/data0/mysql/binlog/
chown-Rmysql.mysql/data0/mysql
/usr/local/mysql/bin/mysql_install_db--user=mysql--basedir=/usr/local/mysql--datadir=/data0/mysql/data
echo"mysqlserver5.1.58installedsuccessfully!
"
}
functionins_mysqlclient()#安装mysql客户端
{
cdpackages/
tarzxfmysql-5.1.58.tar.gz
cdmysql-5.1.58
CHOST="x86_64-pc-linux-gnu"
CFLAGS="-march=nocona-O2-pipe"
CXXFLAGS="${CFLAGS}"
./configure"--prefix=${mysql_dir}""--with-mysqld-user=mysql""--without-debug""--with-charset=utf8""--with-extra-charsets=all""--with-pthread""--with-big-tables""--enable-thread-safe-client""--enable-assembler""--with-readline""--with-ssl""--enable-local-infile""--without-server"
make&&makeinstall
cd/usr/local/mysql
chown-Rmysql:
mysql.
rm-rfsql-benchmysql-test
echo"mysqlclient5.1.58installedsuccessfully!
"
}
functionins_php52()#安装php
{
check_php52
cdpackages/
tarzxflibiconv-1.13.1.tar.gz
cdlibiconv-1.13.1/
./configure--prefix=/usr/local
make
makeinstall
cd../
tarzxfautoconf-2.13.tar.gz
cdautoconf-2.13
./configuire--prefix=/usr
make&&makeinstall
cd..
tarzxflibmcrypt-2.5.8.tar.gz
cdlibmcrypt-2.5.8/
./configure
make
makeinstall
/sbin/ldconfig
cdlibltdl/
./configure--enable-ltdl-install
make
makeinstall
cd../../
tarzxfmhash-0.9.9.9.tar.gz
cdmhash-0.9.9.9/
./configure
make
makeinstall
cd../
ln-s/usr/local/lib/libmcrypt.la/usr/lib/libmcrypt.la
ln-s/usr/local/lib/libmcrypt.so/usr/lib/libmcrypt.so
ln-s/usr/local/lib/libmcrypt.so.4/usr/lib/libmcrypt.so.4
ln-s/usr/local/lib/libmcrypt.so.4.4.8/usr/lib/libmcrypt.so.4.4.8
ln-s/usr/local/lib/libmhash.a/usr/lib/libmhash.a
ln-s/usr/local/lib/libmhash.la/usr/lib/libmhash.la
ln-s/usr/local/lib/libmhash.so/usr/lib/libmhash.so
ln-s/usr/local/lib/libmhash.so.2/usr/lib/libmhash.so.2
ln-s/usr/local/lib/libmhash.so.2.0.1/usr/lib/libmhash.so.2.0.1
ln-s/usr/local/bin/libmcrypt-config/usr/bin/libmcrypt-config
tarzxfmcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8/
/sbin/ldconfig
./configure
make
makeinstall
cd../
tarzxfphp-5.2.17.tar.gz
gzip-cdphp-5.2.17-fpm-0.5.14.diff.gz|patch-dphp-5.2.17-p1
cdphp-5.2.17/
patch-p1-i../suhosin-patch-5.2.17-0.9.7.patch
./buildconf--force
./configure--prefix=${php52_dir}--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-config-file-path=${php52_dir}/etc--with-ic