《linux shell 编程一》由会员分享,可在线阅读,更多相关《linux shell 编程一(8页珍藏版)》请在金锄头文库上搜索。
1、Linux Shell 编程一一 变量,数组二 if, while, until, for, case,三 function四 test, , (), , , $()五 |, , stdin stdout stderr六正则脚本示例#!/bin/bash# =# RAID health plugin for Nagios# Written by : Roland# Release : 1.0.0# Creation date : 2010-06-10# Revision date : 2010-06-10# Description : Nagios plugin (script) to che
2、ck RAID health .# This script has been designed and written on Linux System.# USAGE : ./check_raid_health.sh -d (raid num)# Exemple: ./check_raid_stats.sh# ./check_raid_stats.sh -d raid1# HISTORY :# Release | Date | Authors | Description# -+-+-+-# 1.0.0 | 2010-06-10 | Roland | Create the script# -#
3、NOTICE:#-# You should have the root Permissions ,You can use sudo to realize .# - # Nagios return codes# 定义 nagios 返回的状态变量STATE_OK=0STATE_WARNING=1STATE_CRITICAL=2STATE_UNKNOWN=3 # Paths to commands used in this script. These may have to be modified to match your system setup.# 定义关键的核心命令 MegaCli 路径。
4、#MEAGCLI=/root/MegaCli -AdpAllInfo -aALL |egrep Degrade|Failed Disks|Offline|egrep -v No|Yes# Plugin parameters value if not define# 定义默认的检测 RAIDCHECK_RAID=raid1 # Plugin variable description# 插件定义此插件脚本的描述信息PROGNAME=$(basename $0)RELEASE=Revision 1.0.0AUTHOR=(c) 2010 Roland (R)# Functions plugin usa
5、ge# 插件的使用方法函数print_release() echo $RELEASE $AUTHOR print_usage() echo echo $PROGNAME $RELEASE - RAID health check script for Nagiosecho echo Usage: check_raid_health.sh -d raid1echo echo -d the RAID (raid num) echo -v check the versionecho -h Show this pageecho echo Usage: $PROGNAMEecho Usage: $PROG
6、NAME -helpecho exit 0 print_help() print_usageecho echo This plugin will check disk health echo exit 0 # Parse parameters# 传递参数while $# -gt 0 docase $1 in-h | -help)print_helpexit $STATE_OK;-v | -version)print_releaseexit $STATE_OK;-d | -disk)shiftCHECK_RAID=$1# 判断磁盘是否存在if $1 = thenecho Please speci
7、fy RAID number print_usageexit $STATE_UNKNOWNfi;*) echo Unknown argument: $1print_usageexit $STATE_UNKNOWN;esacshiftdone# 根据不同的操作系统进行不同的操作,暂时只支持 Linuxcase uname inLinux )# 以下是脚本的核心程序/root/shell/MegaCli -AdpAllInfo -aALL |egrep Degrade|Failed Disks|Offline|egrep -v No|Yes statusrm -f status.tmpnum=ca
8、t status | wc -lif $CHECK_RAID = raid1 & $num -ge 3 thencat status | head -n3 status.tmpfiif $CHECK_RAID = raid2 & $num -ge 6 thencat status | head -n6 | tail -n3 status.tmpfiif $CHECK_RAID = raid3 & $num -ge 9 thencat status | head -n9 | tail -n3 status.tmpfiif ! -f status.tmp thenecho UNKNOWN - $C
9、HECK_RAID status is not existexit $STATE_UNKNOWNfi deg=cat status.tmp | grep Degraded | awk -F : print $NF | cut -c2off=cat status.tmp | grep Offline | awk -F : print $NF | cut -c2fd=cat status.tmp | grep Failed Disks | awk -F : print $NF | cut -c2if $deg = | $off = | $fd = thenecho UNKNOWN - $CHECK
10、_RAID status is cat status.tmp | xargsexit $STATE_UNKNOWNfiif $deg = 0 & $off = 0 & $fd = 0 thenecho OK - $CHECK_RAID status is cat status.tmp | xargsexit $STATE_OKelseif $fd != 0 thenecho CRITICAL - $CHECK_RAID status is cat status.tmp | xargsexit $STATE_CRITICALfiif $off != 0 thenecho WARNING - $C
11、HECK_RAID status is cat status.tmp | xargsexit $STATE_WARNINGfiif $deg != 0 thenecho WARNING - $CHECK_RAID status is cat status.tmp | xargsexit $STATE_WARNINGfifi;*) echo UNKNOWN: uname not yet supported by this plugin. Coming soon !exit $STATE_UNKNOWN;esac测试的标志 代表意义 1. 关于文件/目录类型检查(存在与否),如 test -e f
12、ilename -e 该档名是否存在?(常用) -f 该档名是否为档案(file)?(常用) -d 该文件名是否为目录(directory)?(常用) -b 该文件名是否为一个 block device 装置? -c 该文件名是否为一个 character device 装置? -S 该档名是否为一个 Socket 档案? -p 该档名是否为一个 FIFO (pipe) 档案? -L 该档名是否为一个连结档? 2. 关于权限检查,如 test -r filename -r 侦测该文件名是否具有可读的属性? -w 侦测该档名是否具有可写的属性? -x 侦测该档名是否具有可执行的属性? -u 侦测该文件名是否具有SUID的属性? -g 侦测该文件名是否具有SGID的属性? -k 侦测该文件名是否具有Sticky bit的属性? -s 侦测该档名是否为非空白档案? 3. 两个文件之间的比较,如: test file1 -nt file2 -nt (newer than)判断 file1 是否比 file2 新 -ot (older than)判断 file1 是否比 file2 旧 -ef 判断 file2 与 file2