基于go语言的agent怎么用

2024-03-14,

这篇文章给大家介绍基于go语言的agent怎么用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一 介绍
     在构建数据库自动化运维系统的时候,数据库服务器上必须要有一个agent来执行web服务器端发起的命令,我们研究了好几种技术Celery,Redis Queue 或者基于socket实现,当然还有自己写,因为之前有同事已经完成了一个agent---servant,在和同事沟通之后,我们决定复用servant,不用重复造轮子。servant是一款基于go语言编写的,通过http协议调用,提供权限认证和远程调用,支持异步执行命令的agent ,满足我们目前数据库备份任务,定时收集数据库元数据信息,定时校验备份的有效性的任务需求。本文是一篇how to 文档,相对比较详细的介绍如何安装和使用servant,希望对读者朋友有所帮助。

二安装
2.1 软件准备
因为该agent是基于go语言编写的,所以要安装 go语言包

  1. yum install -y go

  2. cd /opt/

  3. git clone https://github.com/xiezhenye/servant.git

  4. cd /opt/servant

  5. 方式一 make rpm

  6. 方式二 make

2.2 目录结构
编译之后查看主要的目录结构
bin           # 编译的二进制文件 
conf         # 配置文件目录
example   #
README.md  
scripts     #servantctl 执行文件 用于启停 查看状态等
src          #源代码文件
维护servant的操作命令 
/opt/servant/scripts/servantctl (start|stop|restart|status|help)
启动的时候遇到报错请到/data/logs/servant/servant.log 查看log的信息哪里有错

2.3 配置文件详解
默认在/opt/servant/conf里面有配置文件 servant.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <config>

  3.     <server>

  4.         <listen>:2465</listen>  #监听的端口

  5.         <auth enabled="true">   #调用的时候是否启用 权限 验证,生产环境建议开启

  6.             <maxTimeDelta>30</maxTimeDelta> # 启动权限验证的时候 超时时间,超过30s 则认为该调用无效

  7.         </auth>

  8.         <log>/data/logs/servant/servant.log</log> # 日志目录log ,这是有赞标准的日志目录,其他朋友在自己环境需要适当调整

  9.     </server>

  10.     <!-- ...... -->

  11. </config

example 的配置文件,使用的时候需要根据实际情况进行调整

  1. <?xml version="1.0" encoding="utf-8" ?>

  2. <config>

  3.     <server> ##server和/opt/servant/conf/servant.xml 配置是一样的。

  4.         <listen>:2465</listen>

  5.         <auth enabled="0">

  6.             <maxTimeDelta>300</maxTimeDelta>

  7.         </auth>

  8.         <log>servant.log</log>

  9.     </server>

  10. #commands 定义了一个可执行的命令组,其中包含了多个command,其中

  11. lang 可以是exec 或者bash

  12. id   是每一组command的标示,runas标示以什么样的用户执行。

  13. background="true" 标示以后台方式执行,并且servant 立即返回

  14.     <commands id="db1">

  15.         <command id="foo" runas="mysql" lang="bash">

  16.             <code>echo "hello world $(whoami)"</code>

  17.         </command>

  18.         <command id="grep" lang="exec">

  19.             <code>grep hello</code>

  20.         </command>

  21.         <command id="sleep" timeout="5" lang="exec">

  22.             <code> sleep ${t}</code>

  23.         </command>

  24.     </commands>

  25. # daemon

  26.     <daemon id="daemon1" retries="10" lang="bash">

  27.         <code>sleep 10000</code>

  28.     </daemon>

  29. # 定时器 ,定期执行某一个命令

  30. tick 执行命令的间隔

  31. deadline 命令执行的最长时间,如果为5s 则命令最长执行5s ,超过5s会被kill掉?

  32.     <timer id="xx" tick="5" deadline="5" lang="bash">

  33.         <code>

  34.         <![CDATA[

  35.              date >>/tmp/timer.log

  36.         ]]>

  37.         </code>

  38.     </timer>

  39. #文件操作类,和commands类似,可以配置多个操作文件的命令,主要包含 获取文件内容,创建文件,删除文件,读取指定字节范围

  40. root 表示有权限访问指定的目录,例子中是访问 /tmp/ 目录下的文件。

  41.     <files id="db1">

  42.         <dir id="binlog1">

  43.             <root>/tmp/</root>

  44.             <allow>get</allow>

  45.             <allow>head</allow>

  46.             <allow>post</allow>

  47.             <allow>delete</allow>

  48.             <allow>put</allow>

  49.             <pattern>log-bin\.\d+</pattern> #正则表达式

  50.         </dir>

  51.     </files>

  52. #这个比较少用 访问数据库

  53.     <database id="mysql" driver="mysql" dsn="root:@tcp(127.0.0.1:3306)/test">

  54.         <query id="select_1">select 1;</query>

  55.     </database>

  56. #

  57.     <vars id="vars">

  58.         <var id="foo">

  59.             <value>bar</value>

  60.         </var>

  61.         <var id="hello" expand="true">

  62.             <value>${world}</value>

  63.         </var>

  64.     </vars>

  65. # 配合auth=true的时候一起使用,访问的时候 必须使用和配置文件中指定的user ,否则不能调用servant

  66.     <user id="user1">

  67.         <key>someKey</key>

  68.         <host>192.168.1.0/24</host> #指定允许访问servant 的ip源地址。通常建议使用本地调用,更加安全。

  69.         <files id="db1" />

  70.         <commands id="db1" />

  71.     </user>

  72. </config>

以上针对常用的配置做了解释,更加详细的解释可以参考 servant的readme.md
2.4 具体的测试用例   为了测试方便,先去掉权限认证。
comand 支持get 和post 两种方式调用

  1. [root@rac4 22:38:05 /opt/servant/conf/extra]

  2. # curl http://127.0.0.1:2465/commands/db1/foo

  3. hello world mysql

  4. [root@rac4 22:40:07 /opt/servant/conf/extra]

  5. # echo "hello world" | curl -XPOST http://127.0.0.1:2465/commands/db1/grep -d @-

  6. hello world

  7. [root@rac4 22:40:08 /opt/servant/conf/extra]

  8. # echo "hxxello world" | curl -XPOST http://127.0.0.1:2465/commands/db1/grep -d @-

文件类型操作
获取文件内容

  1. [root@rac4 22:38:00 /opt/servant/conf/extra]

  2. # curl http://127.0.0.1:2465/files/db1/test/yz.log

  3. youzan ,nihao ,yangyi dba

创建文件

  1. [root@rac4 22:41:56 /opt/servant/conf/extra]

  2. # curl -XPOST http://127.0.0.1:2465/files/db1/test/54.txt -d "hello world "

  3. 验证上面的写入情况

  4. [root@rac4 22:42:03 /opt/servant/conf/extra]

  5. # curl  http://127.0.0.1:2465/files/db1/test/54.txt

  6. hello world

更新文件内容

  1. [root@rac4 22:45:13 /opt/servant/conf/extra]

  2. # curl -XPUT http://127.0.0.1:2465/files/db1/test/54.txt -d "yangyi dba"

  3. [root@rac4 22:45:26 /opt/servant/conf/extra]

  4. # curl  http://127.0.0.1:2465/files/db1/test/54.txt

  5. yangyi dba

开启权限验证生产环境下从安全的角度考虑建议开启权限验证
修改配置文件 启用auth 为true 和设置user 配置

  1. [root@rac4 22:16:50 /opt/servant/conf]

  2. # uri='/commands/db1/foo'

  3. # ts=$(date +%s)

  4. # key=someKey

  5. # curl -H "Authorization: ${user} ${ts} $(echo -n "${user}${key}${ts}GET${uri}"|sha1sum|cut -f1 -d' ')" "http://127.0.0.1:2465${uri}"

  6. [root@rac4 22:30:30 /opt/servant/conf]

log报错 执行失败,因为ts 的实际时间是22:16:50,执行的实际时间是22:30:30 超时时间是30s,故调用失败

  1. 2017/05/05 22:30:29 INFO (6) [commands] + 127.0.0.1:42798 GET /commands/db1/foo

  2. 2017/05/05 22:30:30 WARN (6) [commands] - auth failed: timestamp delta too large

重新设置时间 ts  再次执行 成功。

  1. [root@rac4 22:30:58 /opt/servant/conf]

  2. # ts=$(date +%s)

  3. [root@rac4 22:31:02 /opt/servant/conf]

  4. # curl -H "Authorization: ${user} ${ts} $(echo -n "${user}${key}${ts}GET${uri}"|sha1sum|cut -f1 -d' ')" "http://127.0.0.1:2465${uri}"

  5. hello world mysql

  6. 日志输出

  7. 2017/05/05 22:31:05 INFO (7) [commands] + 127.0.0.1:42808 GET /commands/db1/foo

  8. 2017/05/05 22:31:05 INFO (7) [commands] command: [bash -c echo "hello world $(whoami)"]

  9. 2017/05/05 22:31:05 INFO (7) [commands] process started. pid: 27706

  10. 2017/05/05 22:31:05 INFO (7) [commands] - execution done

2.5 安装过程中遇到的问题
1 安装的时候 需要创建 
mkdir -p /opt/servant/conf/extra
2 认证权限问题
因为默认的/opt/servant/conf/servant.xml 的auth =true ,需要改为false。
不然使用curl 执行命令
curl http://127.0.0.1:2465/commands/db1/foo 
日志里面报错
2017/05/05 21:52:30 INFO (3) [commands] + 127.0.0.1:41988 GET /commands/db1/foo
2017/05/05 21:52:31 WARN (3) [commands] - auth failed: bad auth header

关于基于go语言的agent怎么用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

《基于go语言的agent怎么用.doc》

下载本文的Word格式文档,以方便收藏与打印。