Tag: Shell



17 八 09

前言:今天df看到磁盘快满了,通过查看发现很多都是隐藏文件的,但是通过du又查不到隐藏目录的大小 不知道 哪些目录太大?怎么查看方便呢?

所以写了个脚本进行统计

脚本的功能,打印出指定目录下的一级目录所有隐藏目录的大小并且按M进行排序

#!/bin/bash
#Author:Ajian
#Date   : 2009-08-17
#website:www.ohlinux.com
for i in `find $DOCUMENT -maxdepth 1 -name ".*"  -type d -print `
do
  du -sm $i
done | sort -n | awk '{print $1"M"," ",$2}'

后来我又减化了下到一个命令

find /root -maxdepth 1 -name ".*"  -type d -print | xargs du -sm | sort -n | awk '{print $1"M",$2}'

解説:
1、查找隐藏目录 使用find /path -name “.*” -type d -print
2、查找一级的隐藏目录添加上 -maxdepth 1
3、对目录进行以M兆的统计 du -sm
4、对大小进行排序 默认以第一列 sort -nr (r是反向排序)
5、awk 在这里进行修饰了下因为du -sm 是没有显示单位的 用du -sh 有单位但增加了排序的麻烦
6、脚本和命令的书写区别 因为脚本用的for 语句是分条读入的所以如果把sort -n | awk ‘{print $1″M”,” “,$2}’ 放到 du -sm $i 之后不会对顺序产生变化,因为每次就对一条进行排序肯定最后的结果是没有影响的 而命令是用管道的方式 每次都是一批处理完后交给后面进行 注意脚本和命令的区别。

结果:

13M   /root/.opera
13M   /root/.svnqt
15M   /root/.cpan
16M   /root/.mplayer
78M   /root/.tencent
100M   /root/.config
116M   /root/.VirtualBox
158M   /root/.icons
160M   /root/.google
218M   /root/.kde4
258M   /root/.mozilla
517M   /root/.thumbnails
650M   /root/.beagle






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