首页 » ORACLE 9i-23c, 系统相关 » 自动化运维工具之: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所有
下面我们修改dcli,根据个人需求进行定制,来管理oracle主机。
一,定制
上传dcli脚本到装python 2.3+环境的linux上
1、为了安全和不破坏现有的ssh配置,可以修改dcli默认ssh key位置,并创建ssh key
 
    mkdir -p /home/oracle/tools/.s
    chmod 700 /home/oracle/tools/.s
    ssh-kengen -t dsa -f /home/oracle/tools/.s/dsa
    $ ls -la
    drwx------ 2 oracle oinstall 4096 Mar 21 10:22 .s

   dcli修改,实际行号可能有不同

    line 120:
    SSHSUBDIR="/home/oracle/tools/.s"
    SSHDSAFILE="dsa.pub"
    line 885:
    parser.add_option("-s",
    help="string of options passed through to ssh",
    action="store", type="string", dest="sshOptions",default="-i /home/oracle/tools/.s/dsa")
 
    2、修改默认ssh 登录用户,如oracle,dcli默认登录用户为celladmin

    line 872:
    parser.add_option("-l", default="oracle",
    help="user to login as on remote cells (default: oracle) ",
    action="store", type="string", dest="userID")

    3、增加命令过滤,限制rm,shutdown,stop,halt,alias,尽量减少误操作

    line 172:
    def buildCommand( args, verbose ):
    """
    Build a command string to be sent to all hosts.
    Command arguments can be used to build the command to
    be sent to hosts.
    """
    command = ""
    command2= 'echo not allow execute this command!'
    if args:
    for word in args:
    command += " " + word;
    if command.find("rm ")>=0:
    return command2
    elif command.find("shutdown")>=0:
    return command2
    elif command.find("stop")>=0:
    return command2
    elif command.find("halt")>=0:
    return command2
    elif command.find("alias")>=0:
    return command2
    return command

二, 测试

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

Options:
  --version            show program's version number and exit
  -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
  -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
  -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@localhost ~]#  dcli --version
version 0.9.2

常用选项如-c 指定目标主机IP或机器名, -k 传送pub sshkey部署到受控主机 -l 指定目标主机存在的用户,以该用户身份执行
-g 指定一个配置了IP列表的文件,用于多机器执行。 -f 指定文件传送到受控主机 -x 执行自定义shell名 -d 传送或远程执行shell时文件存放在目标主机的目录。
 
1,创建ssh 免密码登录
$ ssh-keygen -t dsa

2,把pub key 传到远程主机
$dcli -c db1 -k

3,  查看主机名
$ ./dcli -c db1 "hostname"
db1: db1

4, 查看一组主机
$ vi database
db1
db2
$ ./dcli -g database "hostname"
db1: db1
db2: db2

5, 以root用户查看目标文件
$ ./dcli -c db2 -l root "tail -100 ~/test.txt" 
root@db2's password: 
db2: d
db2: dafdsfasd

6, 传文件到一组主机,把test2传到 database主机组的所有主机上
[test2@db1 ~]$ ./dcli -g database -f test2.txt -d /home/test2/

7, 远程执行shell

将shell传到远程主机并执行
$ ./test.sh
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
test2     1990  0.0  0.0 106144  1144 pts/0    S+   16:53   0:00 /bin/bash ./test.sh
$ ./dcli -g database -x /home/test2/test.sh 

8, 远程执行vmstat
./dcli -g database --vmstat=1
         procs -----------memory---------- ---swap-- -----io----  --system--  -----cpu----- 
16:55:33: r  b   swpd    free   buff  cache   si   so    bi    bo    in    cs us sy id wa st 
     db1: 0  0      0 1371008  17968  49728    0    0    22     6    21    37  0  0 99  0  0 
     db2: 0  0      0 1387304  12716  44392    0    0    20     3    17    19  0  0 95  4  0 
 Minimum: 0  0      0 1371008  12716  44392    0    0    20     3    17    19  0  0 95  0  0 
 Maximum: 0  0      0 1387304  17968  49728    0    0    22     6    21    37  0  0 99  4  0 
 Average: 0  0      0 1379156  15342  47060    0    0    21     4    19    28  0  0 97  2  0 
         procs -----------memory---------- ---swap-- -----io----  --system--  -----cpu----- 
16:55:34: r  b   swpd    free   buff  cache   si   so    bi    bo    in    cs us sy  id wa st 
     db1: 0  0      0 1371744  17976  49772    0    0     0     0    61    89  0  0 100  0  0 
     db2: 0  0      0 1387308  12724  44412    0    0     0     0    17    18  0  1  99  0  0 
 Minimum: 0  0      0 1371744  12724  44412    0    0     0     0    17    18  0  0  99  0  0 
 Maximum: 0  0      0 1387308  17976  49772    0    0     0     0    61    89  0  1 100  0  0 
 Average: 0  0      0 1379526  15350  47092    0    0     0     0    39    53  0  0  99  0  0 

更多功能自己挖掘。

打赏

,

对不起,这篇文章暂时关闭评论。