Linuxhttps.blog.chinaunix.net
viviecho
全部博文(279)
2013年(1)
2012年(39)
2011年(35)
2009年(29)
2008年(131)
2007年(44)
marsgray
abc123zx
lionking
cynthia
格伯纳
Bsolar
浪花小雨
danxingd
shibaola
快乐的小
小石匠_
linxuegu
分类:
2009-02-16 16:05:39
在内网监控系统性能的工具很多,但是针对外网服务器,则这些工具有点不太实用,因为它们或多或少需要我们开启一些不必要服务,如web(本身通过web检测外网服务器性能就不安全),针对服务器而言,服务越少就越安全!因此,针对公司这种特殊的网络环境,我写了这个脚本,当然有多可以实现这种功能的方法,我也肯定你能写出比我这脚本更好的程序来(如果确实如此,希望你能与我们共享,脚本本身很简单,在这我只是将脚本代码放在上面,见如下: 主要功能,检测系统的磁盘空间使状况,cpu负载,mem,swap使用状况,mysql与apache占用资源情况及apache的并发连接数量,并且当cpu或磁盘空间使用率达到一定百份比时,自动将邮件发送到管理邮箱 #!/bin/bash #systeminfo ################################################################ #/*定义环境变量*/ ############################################################### source /etc/profile ADMIN="zhenghua.liu@ux168.com" DATE=`date '+%m-%d-%Y %H:%M'` SOURCINGMAP="/home/script/sourcingmap" SYSTEMINFO="/home/script/systeminfo" #######检测磁盘,mysql与apache详细信息 df -Th|egrep "(^/dev/sda3|^Filesystem)">$SYSTEMINFO top -u mysql -b -n 1 >>$SYSTEMINFO netstat -n|grep "^tcp"|grep "ffff"|egrep "(80|443)"|sort >>$SYSTEMINFO ################################################################ ###从日志信息中获取相关信息给变量符值 UP=`grep "^top" $SYSTEMINFO|awk '{print$5,$6,$7}'` CPU_LOAD_AVERAGE_DETAILS=`grep "^top" $SYSTEMINFO|awk -F',' '{print $4,$5,$6}'` PROCESSLIST_DETAILS=`grep "^Tasks" $SYSTEMINFO` CPU_DETAILS=`grep "^Cpu" $SYSTEMINFO` MEM_DETAILS=`grep "^Mem" $SYSTEMINFO` SWAP_DETAILS=`grep "^Swap" $SYSTEMINFO` MYSQL_CPU=`grep "mysql" $SYSTEMINFO|awk '{print $9}'` ##如果mysql cpu负载达到100,则将mysql进程信息输出到指定日志文件 if [ "$MYSQL_CPU" -ge "100" ];then /export/script/mysql_status.sh fi ################################################################## ###从日志信息中获取相关信息给变量符值 MYSQL_MEM=`grep "mysql" $SYSTEMINFO|awk '{print $10}'` HTTPD_CONNECTS=`ps -elf |grep httpd|wc -l` HTTPD_CONNECTS_DETAILS_SORT=`egrep "(80|443)" $SYSTEMINFO` HTTPD_CONNECTS_TOTAL_SORT=`/export/script/httpd_port.sh|awk '{print $1,$2"\n"$3,$4"\n"$5,$6"\n"$7,$8"\n"$9,$10}'` DISK_DETAILS=`egrep "(^/dev/sda3|^Filesystem)" $SYSTEMINFO` ################################################################ #计算磁盘空间巳被使空间百分比数及一分钟内cpu的负载值 PERCENT_DISK=`egrep "(^/dev/sda3)" $SYSTEMINFO|awk '{print $6*1}'` CPU_LOAD_ONE_MIN=`grep "^top" $SYSTEMINFO|awk -F',' '{print $4}'|awk -F':' '{print $2*100}'` #echo $PERCENT_DISK #echo $CPU_LOAD_ONE_MIN ################################################################ #将获得的系统变量信息导入到指定的日志文中 echo "System Details Report From Sourcingmap" >$SOURCINGMAP echo "Now is $DATE">>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Time of Sourcingmap has been working:$UP" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Cpu_Load_Average_Details of Sourcingmap are:$CPU_LOAD_AVERAGE_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo -e "Disk Details of Sourcingmap are: \n $DISK_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Processlist Details of Sourcingmap are:$PROCESSLIST_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Cpu Load Details are: $CPU_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Mem Details of Sourcingmap are :$MEM_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Swap Details of Sourcingmap are :$SWAP_DETAILS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Cpu Load For Mysql is: $MYSQL_CPU" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Mem Load For Mysql is: $MYSQL_MEM" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo "Apache's process is: $HTTPD_CONNECTS" >>$SOURCINGMAP echo "">>$SOURCINGMAP echo -e "Detail of Sourcingmap for Httpd :\n $HTTPD_CONNECTS_DETAILS_SORT" >>$SOURCINGMAP echo "">> $SOURCINGMAP echo -e "Connect Sort of Sourcingmap for Httpd :\n $HTTPD_CONNECTS_TOTAL_SORT" >>$SOURCINGMAP #如果满足指定条件,则系统信息将发送到指邮箱 if [ "$PERCENT_DISK" -ge "70" -o "$CPU_LOAD_ONE_MIN" -ge "180" -o "$MYSQL_CPU" -ge "100" ] then mail $ADMIN -s "System Report For Sourcingmap(Cpu or Disk exceeded)" < $SOURCINGMAP mail zhanghuiyun0978@gmail.com -s "System Report For Sourcingmap(Cpu or Disk exceeded)" <$SOURCINGMAP mail mingfeng1207@gmail.com -s "System Report For Sourcingmap(Cpu or Disk exceeded)" <$SOURCINGMAP fi 将脚本放入cron中,并且在/etc/aliases中设置管理邮箱即可 httpd_port.sh中的内容如下: #/bin/bash netstat -n|awk '/^tcp/ {++S[$NF]} END {for(a in S) print a,S[a]}' mysql_status.sh内容如下 : #!/bin/sh DATE=`date '+%Y%m%d%H%M'` STATUS_LOGS="/home/script/sourcingmap" IP="localhost" if [ ! -f $STATUS_LOGS ];then /bin/touch $STATUS_LOGS fi for i in $IP do HOSTNAME=$i echo "================ mysql status analize for $HOSTNAME date:$DATE====================">>$STATUS_LOGS echo "========================== Status For $HOSTNAME ==================================">>$STATUS_LOGS mysqladmin processlist >>$STATUS_LOGS echo "" >> $STATUS_LOGS /usr/sbin/mysqlreport --host $IP >>$STATUS_LOGS done
上一篇:df del log
下一篇:mysql 游标使用
登录 注册