通用shell脚本收集.docx
- 文档编号:11379646
- 上传时间:2023-02-28
- 格式:DOCX
- 页数:18
- 大小:20.44KB
通用shell脚本收集.docx
《通用shell脚本收集.docx》由会员分享,可在线阅读,更多相关《通用shell脚本收集.docx(18页珍藏版)》请在冰豆网上搜索。
通用shell脚本收集
常用shell脚本小工具
摘自,作者:
mxin
1.批量改名或拷贝文件
比如将start*.sh文件改为stop*.sh
拷贝
先查看
ls-lstart*.sh|awk'{m=$9;gsub(/start/,"stop",$9);print"cp"m""$9}'
再运行
ls-lstart*.sh|awk'{m=$9;gsub(/start/,"stop",$9);print"cp"m""$9}'|sh
改名
先查看
ls-lstart*.sh|awk'{m=$9;gsub(/start/,"stop",$9);print"mv"m""$9}'
再运行
ls-lstart*.sh|awk'{m=$9;gsub(/start/,"stop",$9);print"mv"m""$9}'|sh
2.改进grep2048字符限制的小工具
我们查找匹配文件时常常遇到grep报行长度超过2048这样的错误,我做了这个小工具,findtxt,可查找当前目录下的匹配文件,如果需要,把注释打开,可显示匹配内容。
希望对大家有所帮助。
forfilein`find.!
-typed`
do
line=`sed-n/$1/p$file`
if["$line"!
=""];then
echo$file
#echo$line
fi
done
3.查看文件使用者的小工具
对fuser的一点改进:
1.vishowuser建立一个文件,内容只有一句:
find$1-execfuser-u{}\;2>;&1|awk'{if($2!
=""print$1$2}'
2.chmod755showuser
3.cpshowuser/usr/bin
4.showuser.
showuser/usr
会把当前目录下的在使用的文件的文件名,使用者的id,name显示出来.
4.清理垃圾,可放在crontab里每天执行。
#rmlog.sh
find/tmp!
-name"*.X11*"-mtime+7-execrm-f{}\;
find/var/tmp-mtime+5-execrm-f{}\;
find/var/preserve/recycle-mtime+7-execrm-f{}\;
crontab
01***/home/scripts/rmlog.sh>/tmp/rmlog.log2>&1
5.强制关闭vg(包括umount所有相关文件系统)
#varyoffvg_force.sh
if[$#-le0];then
echo"nopara,example:
varyoff_vg.sherpapp_vg"
exit
fi
df-k|awk'{print$7}'|grep-vMounted>/tmp/fs_mounted.txt
foriin`lsvg-l$1|grep-vE"N/A|vg|MOUNT"|awk'{print$7}'`
do
if[`grep-c$i/tmp/fs_mounted.txt`-ge1];then
echofuser-kc$i
umount$i
fi
done
varyoffvg$1
6.kill_fs_user.sh
(停掉使用某文件系统的用户,自动判断该文件系统是否mount,避免kill掉其他用户)
if[$#-le0];then
echo"nopara,example:
kill_user.sh/applprod"
exit
fi
df-k|awk'{print$7}'|grep-vMounted>/tmp/du_.txt
if[`grep-c$1/tmp/du_.txt`-eq1];then
echofuser-kc$1
fi
7.mklvcopy做镜像
相当于mirrorrootvg,但当rootvg里有不想mirrror的lv或盘大小不一时比较有用。
##mkmirrorvg.sh
mklvcopy-khd52$1
mklvcopy-khd62$1
mklvcopy-khd82$1
mklvcopy-khd42$1
mklvcopy-khd9var2$1
mklvcopy-khd32$1
mklvcopy-khd12$1
mklvcopy-khd22$1
mklvcopy-khd10opt2$1
mklvcopy-klg_dumplv2$1
bosboot-ad$1
bootlist-mnormal$1
8.察看根目录各文件和子目录大小,去除文件系统统计
#du_.sh
df-k|awk'{print$7}'|grep-vMounted>/tmp/df_mounted.txt
cd/
foriin`ls-l|awk'{print$9}'|grep-v"\-i"`
do
if[`grep-c$i/tmp/df_mounted.txt`-eq0];then
du-sk$i
fi
done
9.防止文件系统下和根目录下rm-rf*误操作
#deny_rmall.sh
cd/;touch./-i;df-k|grep-vMounted|grep-vproc|grep-v"\/tmp"|awk'{print"cp\"./-i\""$7'}|sh
cp"/-i"/etc
cp"/-i"/dev
10.保存清理errpt,(当然可以改为其他目录)
#errclear.sh
errpt>/home/mxin/mon/log/errpt_`date+%Y%m%d`.log
errpt-a>>/home/mxin/mon/log/errpt_`date+%Y%m%d`.log
errclear0
11.起大量shell脚本
#start_procs.sh
catstart_procs.list|awk'{print"sh"$1".sh"}'|sh
#start_procs.list(可追加修改)
startprocessor
ProcProcessor
/home/scripts/startArocessor
p_mj_deal_cardevent
12.停大量进程
#stop_procs.sh
catprocs.list|awk'{print"stop_proc.sh"$1}'|sh
#stop_proc.sh
ps-ef|grep$1|grep-vgrep|awk'{print"kill-9"$2}'|sh
#stop_procs.list(可追加修改)
startprocessor
ProcProcessor
Jackrocessor
p_mj_deal_cardevent
13.收集系统信息
echo--------------------------------------`hostname`-------------------------------------------
prtconf
echo-----------lsvg;lsvg`lsvg-o`
echo"-----------lsvg-l";lsvg-l`lsvg-o`
echo"-----------lslvlv";lsvg-l`lsvg-o`|grep-v"LVNAME"|grep-vawk'{print"lslv"$1}'|sh
echo-----------df;df-k;lsfs
echo-------------netstat;netstat-in;netstat-i;netstat-r
echo-------------------ps;ps-efk;psgu
echo------------------lscfg;lscfg-vp
echo--------------lssrc;lssrc-a
echo------------lsslot;lsslot-cpci
echo-------------lspv;lspv
echo-------------lslpp;lslpp-l
echo-------------lsattr;lsdev-C|awk'{print"echo---"$1";lsattr-El"$1}'|sh
echo-------------prtconf-v;prtconf-v
echo-------------errpt;errpt;errpt-a
echo-------------major;ls-al/dev/*
echo-------------hostsfile;cat/etc/hosts
if[`ps-ef|grepcluster|grep-vgrep|wc-l`-ge1];then
echo-------------HAINFO
/usr/es/sbin/cluster/utilities/cltopinfo-c;/usr/es/sbin/cluster/utilities/cltopinfo-n;/usr/es/sbin/cluster/utilities/clshowres-n
`hostname`;/usr/es/sbin/cluster/utilities/cldisp
fi
14.将多级子目录的权限放开
#chmod_dir.sh
echoforexample:
:
chmod_dir.shrwx/home/mxin/mon
echo$1$2>/tmp/chmod_dir.txt
cat/tmp/chmod_dir.txt|awk-F/'{print"chmodo+x/"$2";chmodo+x/"$2"/"$3";chmodo+x/"$2"/"$3"/"$4";chmodo+x/"$2"/"$3"/"$4"/"$5";chmod-Ro+"$1"/"$2"/"$3"/"$4"/"$5"/"$6}'|sh
15.监控oracle是否有锁。
#mon_db_lock.sh
..profile
cd/home/mxin/mon
if[`date+%H%M`="0800"];then
echo0>warn_count
fi
warn_count=`catwarn_count`
sqlplus"/assysdba"< setfeedoff; setheadingoff; spool/tmp/db_lock.out1; @mon_db_lock.sql spooloff; exit EOF sleep13 sqlplus"/assysdba"< setfeedoff; setheadingoff; spool/tmp/db_lock.out2; @mon_db_lock.sql spooloff; exit EOF sleep27 sqlplus"/assysdba"< setfeedoff; setheadingoff; spool/tmp/db_lock.out3; @mon_db_lock.sql spooloff; exit EOF cat/tmp/db_lock.out1|grep-vSQL|grep[0-9]>/tmp/mon_db_lock.out1 cat/tmp/db_lock.out2|grep-vSQL|grep[0-9]>/tmp/mon_db_lock.out2 cat/tmp/db_lock.out3|grep-vSQL|grep[0-9]>/tmp/mon_db_lock.out3 cat/tmp/mon_db_lock.out1|grep-vSQL|grep[0-9]|awk'{print"grep\""$0"\"/tmp/mon_db_lock.out2"}'|sh>/tmp/db_lock1 if[`cat/tmp/db_lock1|wc-l`-gt0];then cat/tmp/mon_db_lock.out2|grep-vSQL|grep[0-9]|awk'{print"grep\""$0"\"/tmp/mon_db_lock.out3"}'|sh>/tmp/db_lock2 if[`cat/tmp/db_lock2|wc-l`-gt0];then cat/tmp/mon_db_lock.out3|grep-vSQL|grep[0-9]|awk'{print"grep\""$0"\"/tmp/mon_db_lock.out.old"}'|sh>/tmp/db_lock3 if[`cat/tmp/db_lock3|wc-l`-gt0];then cat/tmp/db_lock3|awk'{print"walldblock-------\""$0"\"! ! "}'|sh letwarn_count=$warn_count+1 fi fi fi cp/tmp/mon_db_lock.out3/tmp/mon_db_lock.out.old if[$warn_count-gt4];then beep.sh echo0>warn_count fi #mon_db_lock.sql setlinesize256 colobject_nameformata18 colobject_idformat99999999 colLocked_Modeformata15 colSERIAL#format9999999 colsession_idformat999999 coloracle_usernameformata15 colos_user_nameformata15 colprocessformat9999999 SELECTsubstr(b.object_name,1,1object_name,a.object_id, decode(a.locked_mode, 0,'None',/*MonLockequivalent*/ 1,'Null',/*N*/ 2,'Row-S(SS)',/*L*/ 3,'Row-X(SX)',/*R*/ 4,'Share',/*S*/ 5,'S/Row-X(SSX)',/*C*/ 6,'Exclusive', a.locked_mode)Locked_Mode,/*X*/ session_id,SERIAL#,oracle_username,os_user_name,a.process FROMv$LOCKED_OBJECTa,dba_OBJECTSb,v$sessionc WHEREa.object_id=b.object_idanda.session_id=c.sid / 16.监控oracle的表空间 #mon_ts_space.sh cd/home/oraprod sqlplus"/assysdba"< setfeedoff; setheadingoff; spool/tmp/mon_ts_space.out; @mon_ts_space.sql exit EOF cat/tmp/mon_ts_space.out|grep-vSQL|grep[0-9]|awk-fmon_ts_space.awk #mon_ts_space.sql select df.tablespace_name"Tablespace", (df.totalspace-fs.freespace)"UsedMB", fs.freespace"FreeMB", df.totalspace"TotalMB", round(100*(fs.freespace/df.totalspace))"Pct.Free" from dba_tablespacests, (selecttablespace_name, round(sum(bytes)/1048576)TotalSpace fromdba_data_files groupbytablespace_name)df, (selecttablespace_name, round(sum(bytes)/1048576)FreeSpace fromdba_free_space groupbytablespace_name)fs where ts.tablespace_name=fs.tablespace_name and df.tablespace_name=fs.tablespace_name(+) and round(100*(fs.freespace/df.totalspace))<20 anddf.tablespace_namenotin('APPS_UNDOTS1','TEMP'); 17.只清除所有当前使用该vg的用户 #kill_vg_user.sh if[$#-le0];then echo"nopara,example: kill_vg_user.sherpapp_vg" exit fi df-k|awk'{print$7}'|grep-vMounted>/tmp/fs_mounted.txt foriin`lsvg-l$1|grep-vE"N/A|vg|MOUNT"|awk'{print$7}'` do if[`grep-c$i/tmp/fs_mounted.txt`-ge1];then echofuser-kc$i fuser-kc$i fi done 18.每月月底执行的脚本 #month_lastday.sh ..profile TZ=TZ-24 echo`date+%d` if[`date+%d`="01"];then echo"ok.todayislastdayofthismonth.runit! " #insertyourshellscripts fi 19.每月1日执行的脚本 #month_firstdy.sh ..profile TZ=TZ+24 echo`date+%d` if[`date+%d`="02"];then echo"ok.todayisfirstdayofthismonth.runit! " #insertyourshellscripts fi 20.跟踪oracleexport结果的脚本(放在exp脚本最后) #exp_check.sh if[`tail/tmp/exp_dvlp.log|grep"success"|wc-l`-lt1];then echo"dbdvlpexportfail! ! ! "#报警 wall"dbdvlpexportfail! ! ! " fi 21.检查系统的进程 a.check_proc.sh: #check_proc.sh catcheck_proc.list|awk-F","{'print"Check_proc.sh"$1"\""$2"\""""$3'}|sh b.check_proc.list: (可修改) telnet,telnetd-a,2 xcom,xcommanager.py,1 c.Check_proc.sh: #Check_proc.sh exportLANG=en_US count=`ps-ef|grep"$2"|grep-vgrep|wc-l` if[$count-lt$3];then echo$1hasnotbestartedall,thenumberis$count/$3! fi 运行示例: [test3][root][/home/mxin]>ps-ef UIDPIDPPIDCSTIMETTYTIMECMD root100Aug10-0: 11/etc/init root7791010Aug10-5: 53/usr/sbin/syncd60 root10247010Aug10-0: 00/usr/ccs/bin/shlap64 root10657210Aug10-0: 00/usr/lib/errdemon root13936610Aug10-0: 00/usr/sbin/srcmstr root1640681393660Aug10-0: 00/usr/sbin/snmpd root1844661393660Aug10-4: 38/usr/sbin/aixmibd root24600210Aug10vty08: 50-ksh root4096121393660Aug10-0: 05sendmail: acceptingconnections root4178301393660Aug10-0: 00/usr/sbin/syslogd root42189810Aug10-0: 00/usr/sbin/uprintfd root442602782494016: 19: 29-0: 00telnetd-a root446688442602016: 19: 29pts/10: 00-ksh root4669767824940Sep07-0: 00ftpd root4711081393660Au
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 通用 shell 脚本 收集