`
C_J
  • 浏览: 127876 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Some My Shell on Unix in 2009

阅读更多
@Author:cjcj  cj.yangjun@gmail.com <c-j.iteye.com> 
 从数据库取表名称和order by条件,导出指定表数据,checksum后插入另一张表.
#! /usr/bin/ksh

################################################################################
#
#  Copyright (C) 2009
#
#  NAME: 
#		ck_dbpub.sh
#  
#  DESCRIPTION:  
#		Select and inject data about common paramenters from master server and 
#		slave server
#		
#
#  AUTHOR: 
#		cj.YangJun	
#
#  CHANGE LOG
#		2009-8-13
#
################################################################################
CKHEALTH_LOG_DIR=${CKHEALTH_LOG_DIR:-"./"}
CKHEALTH_WORK_DIR=${CKHEALTH_WORK_DIR:-"./"}
CONNECTSTRING="" 
# cfg file
DBPUB_TMP_CFG="ck_dbpub_tmp.cfg"

# sql file
DBPUB_TMP_SQL="ck_dbpub_sql.tmp"

# pub name
DBPUB_NAME="dbpub"

# split pre-name
DBPUB_TMP_ICS_DATA="ck_dbpub_ics_data_"
DBPUB_TMP_ISS_DATA="ck_dbpub_iss_data_"

DBPUB_TMP_ERR="ck_dbpub.err"



# get current conn
get_db_con(){
	CONNECTSTRING=`get_db_conn $1`
}






# get all table names which want to be checked from ckhealth_plugin_cfg table 
get_ctf_from_db(){

    if [ -r $DBPUB_TMP_CFG ];then
	rm -f $DBPUB_TMP_CFG
	fi
	ociuldr user=$CONNECTSTRING \
	query="select item_htag,item_ltag from ckhealth_plugin_cfg where plug_name='dbpub' order by item_htag,item_ltag" \
field="|" file=$DBPUB_TMP_CFG | awk '/error|Error|ERROR|failed|Failed|FAILED|not exist|ORA/' >> $DBPUB_TMP_ERR 


	while read line
	do
		LM_ERROR "$line"
	done <$DBPUB_TMP_ERR
	rm -f $DBPUB_TMP_ERR

	if [ ! -r $DBPUB_TMP_CFG ];then
		LM_ERROR "The DBPUB_CFG file is not exist!"
		exit -1
	fi

}









# get all table's check-sum and  bulid the sql-file used to insert data into ckhealth_data
build_sql(){

	SQLSTRING=""
	CKSUM=""
	TABLENAME=""
	ORDERBY=""
	ICSSQLSTR=""
	ISSSQLSTR=""
	DB=""
	if [ ! -r $1 ];then
 	 	 LM_ERROR "The DBPUB_CTG file is not exist!"
	 	 exit -1
	fi


	

# get every line from cft-file	
	while read ITEM
	do
# get the datebase name
		DB=`echo $ITEM | awk -F "|" '{print $1}'`

# get the table name
		TABLENAME=`echo $ITEM | awk -F "|" '{print $2}'`
		ORDERBY=`echo $ITEM | awk -F "|" '{print $3}'`
		ASTERISK='*'

# create the sql string
		SQLSTRING="select $ASTERISK from $TABLENAME"


# check the "order by" key
		if [ $ORDERBY ];then
				SQLSTRING=" order by $ORDERBY"
		fi



# avoid same table name between ics and iss datebase,so must split the file which used to be check-sum
		if [ -z $DB ] || [ -z $TABLENAME ];then
			LM_DEBUG "The datebase name or tablename is null"
			continue
		fi		
		if [ $DB = ics ];then
			echo "spool $DBPUB_TMP_ICS_DATA$TABLENAME.tmp;" >>ck_dbpub_ics_sql.tmp
			echo "$SQLSTRING;" >>ck_dbpub_ics_sql.tmp
			echo "spool off;" >>ck_dbpub_ics_sql.tmp
		elif [ $DB = iss ];then
			echo "spool $DBPUB_TMP_ISS_DATA$TABLENAME.tmp;" >>ck_dbpub_iss_sql.tmp
			echo "$SQLSTRING;" >>ck_dbpub_iss_sql.tmp
			echo "spool off;" >>ck_dbpub_iss_sql.tmp
		fi
	done<$1




# check the file 	
		if [ ! -r ck_dbpub_ics_sql.tmp ];then
			LM_DEBUG "the ics sql file is not built!!"
		fi
		if [ ! -r ck_dbpub_iss_sql.tmp ];then
			LM_DEBUG "the iss sql file is not bulit!!"
		fi



# build the files about ics and iss by sqlplus
sqlplus -S $CONNECTSTRING <<EOF
SET ECHO OFF;
SET FEEDBACK OFF;
SET HEADING OFF;
SET TERM OFF;
SET WRAP OFF;
@ck_dbpub_ics_sql.tmp;
@ck_dbpub_iss_sql.tmp;
exit;
EOF


# clear the split sql-file	
rm -f ck_dbpub_ics_sql.tmp
rm -f ck_dbpub_iss_sql.tmp

# get all file's checksum and bulid the second sql-file which used to insert into ckhealth_data 
	while read ITEM 
	do	
		DB=`echo $ITEM | awk -F "|" '{print $1}'`
		TABLENAME=`echo $ITEM | awk -F "|" '{print $2}'`

# check the split file is exsited and print the error table name and datebase name		
		if [ -z $DB ] || [ -z $TABLENAME ];then
			LM_DEBUG "The database name or table name is null"
			continue	
		fi	
		if [ $DB = ics ];then
			if [ -r $DBPUB_TMP_ICS_DATA$TABLENAME.tmp ];then
				CKSUM=`cksum $DBPUB_TMP_ICS_DATA$TABLENAME.tmp | awk '{print $1}'`
				rm -f $DBPUB_TMP_ICS_DATA$TABLENAME.tmp	
			else
				LM_DEBUG "The $TABLENAME data file is not exsited from $DB! Maybe the oracle is error!!"
				continue	
			fi
		elif [ $DB = iss ];then
			if [ -r $DBPUB_TMP_ISS_DATA$TABLENAME.tmp ];then
				CKSUM=`cksum $DBPUB_TMP_ISS_DATA$TABLENAME.tmp | awk '{print $1}'`
				rm -f $DBPUB_TMP_ISS_DATA$TABLENAME.tmp	
			else 
				LM_DEBUG "The $TABLENAME data file is not exsited from $DB! Maybe the oracle is error!!"
				continue
			fi
		else
			LM_DEBUG "The database name is not ics or iss!"
			continue	
		fi
# bulid the sql statement into a sql-file
		SQLSTRING="insert into ckhealth_data(plug_name, item_key, item_value) values('$DBPUB_NAME','$ITEM','$CKSUM');"
		echo $SQLSTRING >>$DBPUB_TMP_SQL
	done<$1

# delete cfg file
	rm -f $1
}












# excute the sql
excute_sql(){
	if [ ! -r $1 ];then
  		LM_ERROR "The sql file is not exist!"
	 	exit -1
	fi

sqlplus -S $2 <<EOF
	@$1;
	commit;
	exit;
EOF
# delete sql file	
	rm -f $1
}










main(){
	. ./ck_comm.sh
	
# check the connection!	
	if [ $? -eq 1 ];then
		LM_ERROR "Get db connection error!Gencipher fail!"
		exit -1
	fi

    cd $CKHEALTH_WORK_DIR

# check the work_dir	
	if [ $? -ne 0 ];then
		LM_ERROR "Can't cd WORK_DIR!"
		exit -1
	fi

# get the connection
	get_db_con ics

# get the ctf data	
	LM_INFO "Get ctf from database begin!!"
	get_ctf_from_db
	LM_INFO "Get ctf from database end!!"

# check the table data	
	LM_INFO "Build sql-file from cfg-file begin!!"
	build_sql $DBPUB_TMP_CFG
	LM_INFO "Build sql-file from cfg-file end!!"

# reget the connection,because the connection changed above!	
	LM_INFO "Insert ckhealth_data begin!!"
	excute_sql $DBPUB_TMP_SQL  $CONNECTSTRING | awk '/error|Error|ERROR|failed|Failed|FAILED|not exist|ORA/' >> $DBPUB_TMP_ERR
	while read line
		do
			LM_ERROR "$line"
		done<$DBPUB_TMP_ERR
	rm -f $DBPUB_TMP_ERR	
	LM_INFO "Insert ckhealth_data end!!"


}


main $@

 

 

定时查询数据库某些表count(*)定向到文件

 

 

################################################################################
#
#  Copyright (C) 2009
#
#  NAME: 
#               listenReject.sh
#  
#  DESCRIPTION:  
#               listen some table                
#
#  AUTHOR: 
#               cj.YangJun      
#
#  CHANGE LOG
#               
#
################################################################################


PRCLF="Log/rePRCLog.log"
CRPLF="Log/reCRPLog.log"
SRCLF="Log/reSRCLog.log"
CRSLF="Log/reCRSLog.log"


DBLOGIN="xxx"
DBPASSWD="xxx"
DBNAME="xxx"


# Get the sum of err from Datebase

doListen()
{
sqlplus -s $DBLOGIN/$DBPASSWD@$DBNAME <<EOF 
spool $PRCLF append;
select count(*) from ldst_log where inst_nm='recv' and file_nm like 'F%';
spool off;
spool $CRPLF append;
select count(*) from ldst_log where inst_nm='send' and file_nm like 'F%';
spool off;
spool $SRCLF append;
select count(*) from err_ciber_ric_cdr;
spool off;
spool $CRSLF append;
select count(*) from err_ciber_ioc_cdr;
spool off;
exit;
EOF
}

# Do it once per 30mins and record current time
main()
{
I=1
while [ 1 ];do
  date >>$PRCLF 
  date >>$CRPLF
  date >>$SRCLF
  date >>$CRSLF
  doListen  
  sleep 3600
done
}
main $@

 

 

分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    my view on some problems in china's higher education

    中国的高等教育在过去的几十年里取得了显著的发展,为国家的经济增长和社会进步做出了重大贡献。然而,快速发展的同时,也暴露出一些不容忽视的问题。以下是针对中国高等教育存在的三个主要问题的深入探讨及可能的...

    Unix shell programming in 24 hours.pdf

    ** In the context of Unix and shell programming, a command refers to an instruction or a set of instructions that are executed by the shell or another program. Commands can be simple, like `ls` for ...

    Advanced Programming in the UNIX Environment

    Describing the UNIX System in a strictly linear fashion, without any forward references to terms that haven't been described yet, is nearly impossible (and would probably be boring). This chapter ...

    csapp-shell-labcsapp-shell-labcsapp-shell-lab

    In this lab assignment, the primary objective is to deepen your understanding of process control and signaling by creating a simple Unix shell program that supports job control. The assignment is ...

    Unix shell教程

    Unix Shell教程主要介绍了如何在Linux环境下使用Bash脚本进行编程。Bash是bourne again shell的缩写,它是Unix和类Unix系统中最常用的Shell之一,因其免费和易用性而备受青睐。在本文中,作者重点讲解了创建和运行...

    Linux Shell Scripting Cookbook.epub

    It guides you on implementing some of the most common commands in Linux with recipes that handle any of the operations or properties related with files like searching and mining inside a file with ...

    shell高级编程实例(365例子)

    Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # -------------------------------------...

    Linux Shell Scripting Cookbook.mobi

    It guides you on implementing some of the most common commands in Linux with recipes that handle any of the operations or properties related with files like searching and mining inside a file with ...

    Some Solutions on QTP Common Issues

    Some Solutions on QTP Common Issues Some Solutions on QTP Common Issues Some Solutions on QTP Common Issues Some Solutions on QTP Common Issues

    Uniwin to develop C/C++ systems on remote Unix servers

    Uniwin is a ssh/sftp based toolset to develop C/C++ systems on remote Unix servers (including all Unix systems supporting ssh and sftp) based on Windows clients. Uniwin is especially optimized for ...

    shell编程电子书.docx

    1. Unix 命令:在 shell 脚本中可以使用任意 unix 命令,不过实际上最为常用的通常是那些文件和文字操作相关的命令。 2.Shell 命令:Shell 内置的一些命令,如 echo、read、expr 等。 3.流程控制命令:如 if、for、...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 ...

    GNU m4 is an implementation of the traditional Unix macro processor

    GNU m4 also has built-in functions for including files, running shell commands, doing arithmetic, etc. GNU m4 is a macro processor in the sense that it copies its input to the output expanding ...

    简单shell脚本编写

    bash 是一种 Unix Shell,它的前身是 Bourne Shell,是 Unix 系统的标准 Shell。bash 是一个功能强大、灵活的 Shell,它提供了许多高级特性,如 Job Control、Command History、Command Completion 等。 要编写一个...

    shell 基本语法

    Unix命令是Shell脚本的基础,常见的Unix命令包括`ls`、`echo`、`cat`等。 **示例代码:** ```sh #!/bin/sh # 使用echo命令打印信息 echo "This is a test." # 使用ls命令列出目录内容 ls -l ``` ##### 3.2 概念: ...

    shell基础12篇电子书shellchoki.chm

    Shell是Linux和Unix操作系统中的一个命令解释器,它作为内核与用户交互的接口,使得用户可以通过命令行与系统进行通信。Shell不仅执行单一的命令,还能通过编写脚本文件实现一系列命令的自动化执行。本文将深入讲解...

Global site tag (gtag.js) - Google Analytics