利用DCLI命令实现跨机器检查

dcli介绍

     dcli 为Oracle Exadata Machine中提供的管理cell的工具,全名 Distributed Command Line Interface,在Exadata, Exalogic, Exalytics等系列一体机都自带这个工具, 该工具是一套python脚本,可以用文本工具直接查看编辑, 在当前的IT管理中批量管理几百台机器已不是什么稀奇的事, 所以在日常一些批量共性的常规检查和运维就需要一种维护工具自动实现或者叫自动化运维工具, 当前较流行的有puppet和ansible 产品,其中ansible是个很不错的工具,因为被监控端无需安装客户端,后期有时间再介绍。 同样dcli也是一个基于SSH协议,而且更佳轻量级,控制端一个dcli(python 脚本)和python环境 2.3以上,受控端不要任何客户端。 其实dcli可以很方便的移植到非Exadata机器上使用。
     dcli部署到其它非Exadata需要修改脚本中指定的默认用户celladmin为受控端一个共用的用户如oracle DB环境的oracle。如果有一点开发语言功底建议从头阅读一篇dcli脚本。

  • 1 dcli 脚本可以自已配置ssh互信,后期无密码执行
  • 2 dcli脚本也可以不配置SSH,使用 -l 选项指定用户名,交互输出密码
  • 3 dcli脚本可以远程执行系统自带的命令、文件目录操作、远程执行自定议shell
  • 4 控制端要有python环境 2.3以上,受控端可无。
  • 5 dcli可以使用dsa(非对称加密) 生成ssh key, 并把公钥通过SSH传递并追加到~/.ssh/authorized_keys中,实现ssh认证。
  • 6 可以自己根据需求修改dcli脚本, 该脚本的版权是oracle所有
    以上内容参考https://www.anbob.com/archives/2843.html

dcli脚本介绍

     dcli本身为py脚本,通过pyinstaller打包为Linux可以执行文件,放置在/usr/local/bin目录下,既可以实现随时调用。以下是dcli的简单使用说明

[root@19db1 ~]# dcli 
Error: No command specified.
Usage: dcli [options] [command]

Options:
  --version            show program's version number and exit
  --batchsize=MAXTHDS  limit the number of target cells on which to run the
                       command or file copy in parallel
  -c CELLS             comma-separated list of cells
  -d DESTFILE          destination directory or file
  -f FILE              files to be copied
  -g GROUPFILE         file containing list of cells
  -h, --help           show help message and exit
  --hidestderr         hide stderr for remotely executed commands in ssh
  -k                   push ssh key to cell's authorized_keys file
  -l USERID            user to login as on remote cells (default: celladmin)
  --maxlines=MAXLINES  limit output lines from a cell when in parallel
                       execution over multiple cells (default: 100000)
  -n                   abbreviate non-error output
  -r REGEXP            abbreviate output lines matching a regular expression
  -s SSHOPTIONS        string of options passed through to ssh
  --scp=SCPOPTIONS     string of options passed through to scp if different
                       from sshoptions
  --serial             serialize execution over the cells
  --showbanner         show banner of the remote node in ssh
  -t                   list target cells
  --unkey              drop keys from target cells' authorized_keys file
  -v                   print extra messages to stdout
  --vmstat=VMSTATOPS   vmstat command options
  -x EXECFILE          file to be copied and executed
[root@19db1 ~]# dcli  --version
version 1.3
[root@19db1 ~]# which dcli
/usr/local/bin/dcli
[root@19db1 host]# dcli -k -g /root/crmtest/host/host_all -l root <=====配置机器之间免密
root@19db1's password:
root@19db2's password:
root@zsdb's password:
19db1: ssh key added
19db2: ssh key added
zsdb: ssh key added
[oracle@19db1 ~]$ dcli -g /home/oracle/crmtest/host/host_all -l oracle hostname    <======执行简单系统命令
19db1-192.168.20.126: 19db1
19db2-192.168.20.127: 19db2
zsdb-192.168.20.43: zsdb
[oracle@19db1 ~]$ dcli -g /home/oracle/crmtest/host/host_all -l oracle -x /tmp/test.sh <======执行shell脚本
19db1-192.168.20.126: total        used        free      shared  buff/cache   available
19db1-192.168.20.126: Mem:          16027        1837       12987          43        1203       13799
19db1-192.168.20.126: Swap:         16383           0       16383
19db2-192.168.20.127: total        used        free      shared  buff/cache   available
19db2-192.168.20.127: Mem:          16027         219       15603          19         204       15510
19db2-192.168.20.127: Swap:         16383           0       16383
zsdb-192.168.20.43: total        used        free      shared  buff/cache   available
zsdb-192.168.20.43: Mem:          11853        1348         291         978       10212        9119
zsdb-192.168.20.43: Swap:          5119           2        5117
[oracle@19db1 ~]$ dcli -g /home/oracle/crmtest/host/host_all -l oracle -f p2020.ora -d /tmp  <=====传输文件
[oracle@19db1 ~]$ cd /tmp
[oracle@19db1 tmp]$ ll /tmp/p2020.ora 
-rw-r--r-- 1 oracle oinstall 1494 Jan 19 16:22 /tmp/p2020.ora

dcli实现跨机器检查

dcli放置到对应目录

[root@19db1 ~]# cd /usr/local/bin/
[root@19db1 bin]# ll dcli 
-rwxrwxrwx 1 root root 48055 Jan 15 10:46 dcli

准备hosts文件

[oracle@19db1 crmtest]$ cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.126 19db1-192.168.20.126 19db1
192.168.20.127 19db2-192.168.20.127 19db2
192.168.20.43 zsdb-192.168.20.43 zsdb
192.168.20.126 19db1
[oracle@19db1 crmtest]$ more /home/oracle/crmtest/host/*
::::::::::::::
/home/oracle/crmtest/host/host_19db1
::::::::::::::
19db1-192.168.20.126
::::::::::::::
/home/oracle/crmtest/host/host_19db2
::::::::::::::
19db2-192.168.20.127
::::::::::::::
/home/oracle/crmtest/host/host_19dball
::::::::::::::
19db1-192.168.20.126
19db2-192.168.20.127
::::::::::::::
/home/oracle/crmtest/host/host_all
::::::::::::::
19db1-192.168.20.126
19db2-192.168.20.127
zsdb-192.168.20.43
::::::::::::::
/home/oracle/crmtest/host/host_zsdb43
::::::::::::::
zsdb-192.168.20.43

配置主机之间免密

--生成公钥
[oracle@19db1 .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/oracle/.ssh/id_rsa.
Your public key has been saved in /home/oracle/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:UTJfE6r9Miy8yN/UUJHVw7lPbWaWEHeRb9JqUipme+I oracle@19db1
The key's randomart image is:
+---[RSA 2048]----+
|        o . ==++=|
|         = o.+.=o|
|        . o . ..*|
|         + .  oo@|
|        S o  o X.|
|       . .++o o .|
|        oo=ooo   |
|     . . =oo.    |
|      o.o.Eo     |
+----[SHA256]-----+
--传输公钥至所有机器
[root@19db1 host]# dcli -k -g /home/oracle/crmtest/host/host_all -l oracle

整理主机检查脚本

[oracle@19db1 crmtest]$ more crmcheck.sh 
#!/bin/bash
pathpwd=$(cd `dirname $0`;pwd)
cd $pathpwd
tar -zcf `date -d "-1 day" "+%Y%m%d-%H%M%S"`.tgz log* --remove-files
## log format args
log_prefix="log_crmcheck_"
date_str="`date +%Y%m%d`"
log_name=$pathpwd/$log_prefix$date_str".log"
log_name_final=$pathpwd/$log_prefix$date_str"_final.log"
anynowtime="date +'%Y-%m-%d %H:%M:%S'"
## check type program
check_pro=""
## dcli hosts
hosts_tomcat_single="host_19db1 host_19db2"
hosts_tomcat_all="host_19dball"
hosts_mservice_single="host_19db1 host_19db2 host_zsdb43"
hosts_mservice_all="host_all"
## check process args
tomcat_filter="LiveBOS_Tomcat9_Kirin_WH"
mservice_filter="ams-server-esa-retailassessment-1.0.4-SNAPSHOT.jar ams-server-zy-1.0.1.jar cim-webapp-base-1.0.1.jar ams-server-co
ntinuous-service-1.0.1.jar ams-server-cusgroup-base-1.0.1.jar ams-server-fuwu-product-1.0.1.jar ams-server-marketing-activity-1.0.1
.jar ams-server-marketing-management-1.0.1.jar ams-server-product-adaptation-1.0.1.jar ams-server-product-financial-1.0.1.jar ams-s
erver-report-center-1.0.1.jar ams-server-rest-rpc-0.0.1-SNAPSHOT.jar ams-server-salary-assessment-1.0.1.jar ams-server-staff-relati
on-1.0.1.jar ams-server-task-center-1.0.1.jar ams-server-wm-research-0.1.jar ams-server-wm-portfolio-0.1.jar ams-server-wm-informat
ion-1.0.1.jar ams-server-wm-base-1.0.1.jar ams-server-train-exam-1.0.1.jar ams-server-mot-base-1.0.1.jar ams-server-customer-senior
-1.0.1.jar ams-server-esa-organizationassessment-1.0.4-SNAPSHOT.jar engineweb-1.0-SNAPSHOT.jar webapp-base-1.0.1.jar ams-server-cus
tomer-base-1.0.1.jar ams-server-common-base-1.0.1.jar"
## date format
NOW="echo [\`$anynowtime\`]"

function recho
{
    echo "`eval $NOW` $@"
}
function errorecho
{
    echo -e "`eval $NOW`\033[1m $@ \033[0m"
}
function readecho
{
  while read line
    do
      echo "`eval $NOW` $line"
    done
}
function resultcheck
{
   var1=$@
   var_hostname=`echo $var1|awk '{print $1}'`
   var_result=`echo $var1|awk '{print $3}'`
   if [ $var_result -eq "1" ]; then
     recho $var_hostname $check_pro" "$v_filter "is OK" |tee -a $log_name_final
   else
     errorecho $var_hostname $check_pro" "$v_filter "is not running!!" |tee -a $log_name_final
   fi
}
function writelog
{
  while read line
    do
      echo "`eval $NOW` $line" >>$log_name
      resultcheck $line
    done
}
function headtitle
{
    recho "--------------------------------------------"
    recho "---------  $1 check $2"
    recho "--------------------------------------------"
}
function check1
{
    dcli -g $pathpwd/host/host_all -l root sh /tmp/test.sh |readecho
}
function check
{
    #v_pro=$1
    v_host=$2
    v_filter=$3
    ##headtitle $v_pro "start"|tee -a $log_name $log_name_final
    dcli -g $pathpwd/host/$v_host -l oracle "ps -ef|grep $v_filter|grep -vE 'grep|xargs'|wc -l|xargs echo $v_filter"|writelog
    ##headtitle $v_pro "end" |tee -a $log_name $log_name_final
}
function execcheck
{
    check_pro="Tomcat"
    for tomcat in $hosts_tomcat_all
    do
     headtitle $check_pro "start"|tee -a $log_name $log_name_final
     check $check_pro $tomcat $tomcat_filter
     headtitle $check_pro "end" |tee -a $log_name $log_name_final
    done
    check_pro="MService"
    for mservice in $hosts_mservice_single
    do
     headtitle $check_pro "start"|tee -a $log_name $log_name_final
     for filter in $mservice_filter
     do
       check $check_pro $mservice $filter
     done
     headtitle $check_pro "end" |tee -a $log_name $log_name_final
    done
}

execcheck

执行效果

[oracle@19db1 crmtest]$ more log_crmcheck_20210118_final.log 
[2021-01-18 15:37:12] --------------------------------------------
[2021-01-18 15:37:12] ---------  Tomcat check start
[2021-01-18 15:37:12] --------------------------------------------
[2021-01-18 15:37:13] 19db1-192.168.20.126: Tomcat LiveBOS_Tomcat9_Kirin_WH is not running!! 
[2021-01-18 15:37:13] 19db2-192.168.20.127: Tomcat LiveBOS_Tomcat9_Kirin_WH is not running!! 
[2021-01-18 15:37:13] --------------------------------------------
[2021-01-18 15:37:13] ---------  Tomcat check end
[2021-01-18 15:37:13] --------------------------------------------
[2021-01-18 15:37:13] --------------------------------------------
[2021-01-18 15:37:13] ---------  MService check start
[2021-01-18 15:37:13] --------------------------------------------
[2021-01-18 15:37:13] 19db1-192.168.20.126: MService ams-server-esa-retailassessment-1.0.4-SNAPSHOT.jar is not running!! 
[2021-01-18 15:37:13] 19db1-192.168.20.126: MService ams-server-zy-1.0.1.jar is not running!! 
[2021-01-18 15:37:13] 19db1-192.168.20.126: MService cim-webapp-base-1.0.1.jar is not running!! 
[2021-01-18 15:37:14] 19db1-192.168.20.126: MService ams-server-continuous-service-1.0.1.jar is not running!! 
[2021-01-18 15:37:14] 19db1-192.168.20.126: MService ams-server-cusgroup-base-1.0.1.jar is not running!! 
[2021-01-18 15:37:14] 19db1-192.168.20.126: MService ams-server-fuwu-product-1.0.1.jar is not running!! 
[2021-01-18 15:37:14] 19db1-192.168.20.126: MService ams-server-marketing-activity-1.0.1.jar is not running!! 
[2021-01-18 15:37:15] 19db1-192.168.20.126: MService ams-server-marketing-management-1.0.1.jar is not running!! 
[2021-01-18 15:37:15] 19db1-192.168.20.126: MService ams-server-product-adaptation-1.0.1.jar is not running!! 
[2021-01-18 15:37:15] 19db1-192.168.20.126: MService ams-server-product-financial-1.0.1.jar is not running!! 
[2021-01-18 15:37:16] 19db1-192.168.20.126: MService ams-server-report-center-1.0.1.jar is not running!! 
[2021-01-18 15:37:16] 19db1-192.168.20.126: MService ams-server-rest-rpc-0.0.1-SNAPSHOT.jar is not running!! 
[2021-01-18 15:37:16] 19db1-192.168.20.126: MService ams-server-salary-assessment-1.0.1.jar is not running!! 
[2021-01-18 15:37:16] 19db1-192.168.20.126: MService ams-server-staff-relation-1.0.1.jar is not running!! 
[2021-01-18 15:37:17] 19db1-192.168.20.126: MService ams-server-task-center-1.0.1.jar is not running!! 

posted on 2021-01-20 13:46  空白葛  阅读(491)  评论(0编辑  收藏  举报

导航