Tag: 程序



15 四 09

有时就算你有网卡监控的工作如cacti nagios这些长时间的监控工具,但有时为了解决临时的瞬时的一些检测任务,就需要直接查看到网卡的流量,还可以变化成自己喜欢的格式,只要你愿意去改脚本。这个脚本的单位跟cacti展示的是一样的。

#!/bin/bash                                                                                        
 
# -------------------------------------------------------------------------------                  
# Filename:    netflood.sh                                                                         
# Revision:    1.2                                                                                 
# Date:        2007/08/30                                                                          
# Author:      Ajian                                                                               
# Email:       ajian521#gmail.com                                                                  
# Website:     www.ohlinux.com                                                                     
# Description: LAN traffic monitor                                                                 
# -------------------------------------------------------------------------------                  
# Copyright:   2009 (c) Ajian                                                                      
# License:     GPL                                                                                 
#                                                                                                  
# This program is free software; you can redistribute it and/or                                    
# modify it under the terms of the GNU General Public License                                      
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# -------------------------------------------------------------------------------
#Version 1.1 2007/08/30
#create the script
#Version 1.2 2008/01/10
#modify the script and add the variable.
NIC=eth1
while : ; do
        time=`date +%m"-"%d" "%k":"%M`
        day=`date +%m"-"%d`
        rx_before=`ifconfig $NIC|sed -n "8"p|awk '{print $2}'|cut -c7-`
        tx_before=`ifconfig $NIC|sed -n "8"p|awk '{print $6}'|cut -c7-`
        sleep 2
        rx_after=`ifconfig $NIC|sed -n "8"p|awk '{print $2}'|cut -c7-`
        tx_after=`ifconfig $NIC|sed -n "8"p|awk '{print $6}'|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"
        sleep 2
done






27 三 09

今天正巧碰上看到两个一样的SHELL脚本 一个是英文原版的 一个是加了中文注释的,把采集的信息用wiki的格式输出。 感觉这个脚本挺清楚的,可以把中间的一些东西抽出来加以利用。

#!/bin/bash                        
 
#make sure tools work as expected
LANG=C                           
 
#this specifies which Headline level is used
HL='===='                                   
 
CPU=`cat /proc/cpuinfo  | grep 'model name' | awk -F\: '{print $2}'|uniq|sed -e 's/ //'`
MHz=`cat /proc/cpuinfo | grep 'cpu MHz' | awk -F\: '{print $2}'|uniq|sed -e 's/ //'`    
CPUCOUNT=`cat /proc/cpuinfo|grep "physical id"|uniq|wc -l`                              
RAM=`cat /proc/meminfo | grep MemTotal | awk -F\: '{print $2}' | awk -F\  '{print $1 " " $2}'`
SWAP=`cat /proc/meminfo | grep SwapTotal | awk -F\: '{print $2}' | awk -F\  '{print $1 " " $2}'`
SYSTEM=`uname -sr`                                                                              
HOSTNAME=`(hostname -f || hostname) 2>/dev/null`                                                
 
# look for known Distributions
if [ -e /etc/debian_version ]; then
OS="Debian `cat /etc/debian_version`"
elif [ -e /etc/redhat-release ]; then
OS=`cat /etc/redhat-release`         
elif [ -e /etc/SuSE-release ]; then  
OS=`cat /etc/SuSE-release |head -n1` 
elif [ -e /etc/gentoo-release ]; then
OS=`< /etc/gentoo-release`           
else                                 
OS='unknown'                         
fi                                   
 
echo "
$HL General $HL
 
^ Hostname | $HOSTNAME |
^ CPU      | $CPU      |
^ MHz      | $MHz      |
^ # CPU    | $CPUCOUNT |
^ RAM      | $RAM      |
^ Swap     | $SWAP     |
^ System   | $SYSTEM   |
^ OS       | $OS       |
"                       
 
echo -e "$HL Network $HL\n"
for DEV in `/sbin/ifconfig -a |grep '^\w'|awk '!/lo/{print $1}'`
do                                                              
IP=`/sbin/ifconfig $DEV |awk -F\: '/inet / {print $2}'|awk '{print $1}'`
echo "^ $DEV | $IP |"                                                   
done                                                                    
echo                                                                    
 
echo -e "$HL PCI $HL\n"
lspci |sed 's/^/  /'   
echo                   
 
echo -e "$HL Filesystems $HL\n"
df -hPT -x tmpfs | awk '{print "| " $1 " | " $2 " | " $3 " | " $7 " |"}'
echo                                                                    
 
echo -e "$HL IDE devices $HL\n"
 
for DEV in `ls -1d /proc/ide/hd* |sed 's/.*\///'`
do                                               
MODEL=`cat /proc/ide/$DEV/model`                 
if [ -e /proc/ide/$DEV/capacity ]; then          
SIZE=`cat /proc/ide/$DEV/capacity`
SIZE=`expr $SIZE / 2097152`
else
if [ -e /sys/block/$DEV/size ]; then
SIZE=`cat /sys/block/$DEV/size`
SIZE=`expr $SIZE / 2097152`
else
SIZE='(unknown)'
fi
fi
 
echo "| /dev/$DEV | $MODEL | $SIZE GB |"
done
 
if [ "$(ls -1d /sys/block/sd* 2> /dev/null)" ]; then
echo -e "$HL SCSI devices $HL\n"
for DEV in `ls -1d /sys/block/sd* |sed 's/.*\///'`
do
MODEL=`cat /sys/block/$DEV/device/model`
SIZE=`cat /sys/block/$DEV/size`
SIZE=`expr $SIZE / 2097152`
 
echo "| /dev/$DEV | $MODEL | $SIZE GB |"
done
echo
fi
#!/bin/bash
 
# 为了使脚本正确运行,将语系设置为通用 C
LANG=C
 
# 指定不同输出部分以 H2 输出
HL='===='
 
# 将从 /proc/cpuinfo 获得的 CPU 型号信息存于变量 CPU 中
CPU=`cat /proc/cpuinfo  | grep 'model name' | awk -F\: '{print $2}'|uniq|sed -e 's/ //'`
# 将从 /proc/cpuinfo 获得的 CPU 频率信息存于变量 MHz 中
MHz=`cat /proc/cpuinfo | grep 'cpu MHz' | awk -F\: '{print $2}'|uniq|sed -e 's/ //'`
# 将从 /proc/cpuinfo 获得的 CPU 个数信息存于变量 CPUCOUNT 中
CPUCOUNT=`cat /proc/cpuinfo|grep "physical id"|uniq|wc -l`
# 将从 /proc/cpuinfo 获得的 CPU 核数信息存于变量 CPUKENELCOUNT 中
CPUKENELCOUNT=`cat /proc/cpuinfo|grep "processor"|uniq|wc -l`
# 将从 /proc/meminfo 获得的物理内存数量存于变量 RAM 中
RAM=`cat /proc/meminfo | grep MemTotal | awk -F\: '{print $2}' | awk -F\  '{print $1 " " $2}'`
# 将从 /proc/meminfo 获得的虚拟内存数量存于变量 SWAP 中
SWAP=`cat /proc/meminfo | grep SwapTotal | awk -F\: '{print $2}' | awk -F\  '{print $1 " " $2}'`
# 将操作系统类型信息存于变量 SYSTEM 中
SYSTEM=`uname -sr`
# 将计算机名存于变量 HOSTNAME 中
HOSTNAME=`(hostname -f || hostname) 2>/dev/null`
 
# 将操作系统发型版信息存于变量 OS 中
if [ -e /etc/debian_version ]; then
  OS="Debian `cat /etc/debian_version`"
elif [ -e /etc/redhat-release ]; then
  OS=`cat /etc/redhat-release`
elif [ -e /etc/SuSE-release ]; then
  OS=`cat /etc/SuSE-release |head -n1`
elif [ -e /etc/gentoo-release ]; then
  OS=`< /etc/gentoo-release`
else
  OS='unknown'
fi
 
# 打印常规信息
echo "
 
# 打印常规信息标题
$HL General $HL
 
# 以表格形式打印上面收集的信息变量
^ Hostname | $HOSTNAME |
^ CPU      | $CPU      |
^ MHz      | $MHz      |
^ # CPU    | $CPUCOUNT |
^ # CPUKENEL | $CPUKENELCOUNT |
^ RAM      | $RAM      |
^ Swap     | $SWAP     |
^ System   | $SYSTEM   |
^ OS       | $OS       |
"
 
# 打印网络信息标题
echo -e "$HL Network $HL\n"
# 以 for 循环用表格形式打印各个网络接口及 IP
for DEV in `/sbin/ifconfig -a |grep '^\w'|awk '!/lo/{print $1}'`
do
  IP=`/sbin/ifconfig $DEV |awk -F\: '/inet / {print $2}'|awk '{print $1}'`
  echo "^ $DEV | $IP |"
done
# 打印空行
echo
 
# 打印PCI设备信息标题
echo -e "$HL PCI $HL\n"
# 将 lspci 命令输出的每一行前加两个空格,即以CODE方式显示 lspci 命令的输出
lspci |sed 's/^/  /'
# 打印空行
echo
 
# 打印文件系统信息标题
echo -e "$HL Filesystems $HL\n"
# 以表格方式打印除了 tmpfs 之外所有文件系统的信息
# (包括: Filesystem、Type、Size、Mounted 四项信息)
df -hPT -x tmpfs | awk '{print "| " $1 " | " $2 " | " $3 " | " $7 " |"}'
echo
 
# 打印IDE设备信息标题
echo -e "$HL IDE devices $HL\n"
 
# 以 for 循环用表格形式输出每一个 IDE 设备的信息
for DEV in `ls -1d /proc/ide/hd* |sed 's/.*\///'`
do
  MODEL=`cat /proc/ide/$DEV/model`
  # 由于不同的Linux发布系统记录设备大小的文件不同
  # 因此使用下面的 if 嵌套语句进行判断
  if [ -e /proc/ide/$DEV/capacity ]; then
    SIZE=`cat /proc/ide/$DEV/capacity`
    SIZE=`expr $SIZE / 2097152`
  else
    if [ -e /sys/block/$DEV/size ]; then
      SIZE=`cat /sys/block/$DEV/size`
      SIZE=`expr $SIZE / 2097152`
    else
      SIZE='(unknown)'
    fi
  fi
 
  echo "| /dev/$DEV | $MODEL | $SIZE GB |"
done
 
# 当  /sys/block/sd* 存在时
if [ "$(ls -1d /sys/block/sd* 2> /dev/null)" ]; then
 # 打印SCSI设备信息标题
  echo -e "$HL SCSI devices $HL\n"
  # for 循环用表格形式输出每一个 SCSI 设备的信息
  for DEV in `ls -1d /sys/block/sd* |sed 's/.*\///'`
  do
    MODEL=`cat /sys/block/$DEV/device/model`
    SIZE=`cat /sys/block/$DEV/size`
    SIZE=`expr $SIZE / 2097152`
 
    echo "| /dev/$DEV | $MODEL | $SIZE GB |"
  done
  echo
fi






19 三 09

前言:我们常常需要收集服务器的信息,硬件的来补充硬件表,服务器的流量、连接数来监控服务器状态等都需要收集,而且还会想把收集过来的信息通过邮件或者excel表显示出来,等等。计划写了个比较强的综合性的服务器收集程序,现在下面先展示的是一个硬件信息收集的程序
这个程序的特点:适应slackware和centos redhat系列的系统,还有需要安装dmidecode 这个可以统计到空闲的PCI槽 物理内存的数量和大小 还可以统计CPU的物理个数和类型等。最后可以结合转excel脚本转换成excel文件。最后就可以形成了一张表。

 
#!/bin/bash
 
# -------------------------------------------------------------------------------
# Filename:    system_info.sh
# Revision:    1.1
# Date:        2008/12/30
# Author:      Ajian
# Email:       ajian521#gmail.com
# Website:     www.ohlinux.com
# Description: get the infomation of the system ,eg: memory,cpu ,etc
# Notes:       can check one or many sites
# -------------------------------------------------------------------------------
# Copyright:   2009 (c) Ajian
# License:     GPL
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# -------------------------------------------------------------------------------
#Version 1.1
#get the infomation about , hostname OS CPU CPU_num CPU_Phy  Mem_inuse Mem_Phy disk disk_inuse
#
Hostname=`hostname | cut -d. -f1`
#echo $Hostname
if [ ! -f /etc/slackware-version ]
then
OS=`cat /etc/issue | head -1`
else
OS=`cat /etc/slackware-version | head -1`
fi
 
#echo $OS
CPU=`cat /proc/cpuinfo | grep "model name" | head -1 | cut -d: -f2`
#echo $CPU
CPU_Phy=`cat /proc/cpuinfo | grep phy | sort | uniq | wc -l`
#echo $CPU_Phy
CPU_num=`cat /proc/cpuinfo | grep ^process | wc -l`
#echo $CPU_num
CPU_core=$(( $CPU_num / $CPU_Phy ))
#echo $CPU_core
 
Mem_inuse=`free -m | grep Mem | awk '{print $2}'`
Mem_Phy=`dmidecode -t 17 | grep "Size" | cut -d: -f2 | sort | uniq -c | awk '{printf "%s ",$0}'`
#echo $Mem_inuse
#echo $Mem_Phy
Disk=`fdisk -l | grep Disk | grep GB | cut -d, -f1 | cut -d/ -f3 | awk '{printf "%s  ",$0}'`
#echo $Disk
Disk_inuse=`df -h | grep ^/dev | awk '{print $1,$6,$2}' |  awk '{printf "%s  ",$0}'`
#echo $Disk_inuse
 
echo -e "$Hostname\t$OS\t$CPU\t$CPU_Phy*$CPU_core\t$Mem_inuse MB\t$Mem_Phy\t$Disk\t$Disk_inuse" > ; /tmp/systeminfo.txt






7 三 09

前言:自从我们的网站用了nginx+fastcgi后,出了不少的问题,尤其在那一段时间内。出现的问题头疼之一的就是502,nginx并没有死,但fastcgi却死掉了,所以单单查看80端口是不够的,

另外我们的系统都是集群的,某一个WEB出现问题也是不好发现的(在没有加监控之前),所以当时好几个同事都写了相同功能的程序,我对SHELL熟悉,写了shell脚本,有同事是写PHP的 利用curl写了PHP的程序,还有利用shell结合curl写的,而我觉得curl要必须安装才有,所以我写了一个wget 的 利用最基本的系统命令就可以完成。

#!/bin/sh                                          
 
# -------------------------------------------------------------------------------
# Filename:    check_web.sh                                                      
# Revision:    1.2                                                               
# Date:        2008/12/15                                                        
# Author:      Ajian                                                             
# Email:       ajian521#gmail.com                                                
# Website:     www.ohlinux.com                                                   
# Description: check the web is right or wrong ,eg: nginx+fastcgi                       
# Notes:       can check one or many sites                                    
# -------------------------------------------------------------------------------
# Copyright:   2009 (c) Ajian                                                    
# License:     GPL                                                               
#                                                                                
# This program is free software; you can redistribute it and/or                  
# modify it under the terms of the GNU General Public License                    
# as published by the Free Software Foundation; either version 2                 
# of the License, or (at your option) any later version.                         
#                                                                                
# This program is distributed in the hope that it will be useful,                
# but WITHOUT ANY WARRANTY; without even the implied warranty                    
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                
# GNU General Public License for more details.                                   
#                                                                                
# -------------------------------------------------------------------------------
#Version 1.1                                                                     
#Can check the web is working right or wrong                                     
#Version 1.2                                                                     
#Add the color of the dscription and print more information                      
 
HOSTLIST=hosts.list
OUTFILE=/tmp/check_log.txt
 
#function: shellUsage()
#shellUsage: How to use the shell script.
#return: 0                               
shellUsage()                             
{                                        
#shellUsage                              
        echo "Usage :`basename $0` [-f HostFile]||[-h domain_or_IP]"
        echo "try `basename $0` -help for more info"                
        exit 1                                                      
}                                                                   
 
 
while getopts f:h:o: OPTION ; do
   case $OPTION  in             
        f)HOSTLIST="$OPTARG"       
                if [ ! -f $HOSTLIST ]; then
                        echo "$HOSTLIST can not find out!"
                        shellUsage                        
                fi                                        
                ;;                                        
        o)OUTFILE="$OPTARG"                               
                ;;                                        
 
        h)Host="$OPTARG"
                ;;
        *)shellUsage
                ;;
        esac
done
 
#Function checkHttp()
checkHttp()
{
        wget --server-response --spider  http://$Host -o $OUTFILE
        echo  "\033[44;37;5m Host: \033[0m \033[32;49;1m  $Host \033[39;49;0m"
        echo "==================================="
        grep '^[ ]' $OUTFILE
        echo "==================================="
}
#Function checkMain()
checkMain()
{
    for Host in `cat "$HOSTLIST"`
        do
                checkHttp
        done
}
 
if [ "$Host" = "" ];    then
                checkMain
        else
                checkHttp
fi

附上小刘的PHP脚本,后来他还加入了gtalk的消息发送

<?php                                                  
$cmd = "cat /etc/hosts | grep -v 'pma.' | grep -v 'svn.' | grep 'imobile.com.cn'";
$out = shell_exec($cmd);                                                          
 
$hosts = explode("\n", $out);
$cnt = count($hosts) - 1;    
for($i = 0; $i < $cnt; $i++) 
{                            
list($ip, $host) = explode(" ", $hosts[$i]);
if (test_server_status($host))
{
echo "OK -- {$ip} {$host}";
}
else
{
echo "ERROR -- {$ip} {$host}";
}
echo "\n";
}
 
function test_server_status($host)
{
$ptn0 = 'HTTP/1.0 200 OK';
$ptn1 = 'HTTP/1.1 200 OK';
 
$cmd = '/usr/bin/curl -I ' . $host;
$out = shell_exec($cmd);
if (strpos($out, $ptn0) !== false)
{
return true;
}
if (strpos($out, $ptn1) !== false)
{
return true;
}
return false;
}
?>