bearychat+hubot chatops运维机器人

1.安装nodejs

yum安装:
[root@node1 ~]# yum install -y epel-release
[root@node1 ~]# yum install -y npm
[root@node1 ~]# npm -version
3.10.8
[root@node1 ~]# node -v
v6.9.1


二进制包安装最新版:
官网下载地址:https://nodejs.org/en/download/

[root@node1 ~]# tar xf node-v6.9.5-linux-x64.tar.xz
[root@node1 ~]# mv node-v6.9.5-linux-x64 /usr/local/nodejs
[root@node1 ~]# vim /etc/profile
export NODE_HOME=/usr/local/nodejs
export PATH=$NODE_HOME/bin:$PATH
[root@node1 ~]# source /etc/profile
[root@node1 ~]# npm -version
3.10.10
[root@node1 ~]# node -v
v6.9.5

2. 安装hubot

[root@node1 ~]# npm install -g hubot coffee-script yo generator-hubot
[root@node1 ~]# mkdir myhubot && cd myhubot
[root@node1 myhubot]# yo hubot  (提示没有权限)
/usr/local/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:90
                    throw err0;
                    ^

Error: EACCES: permission denied, mkdir '/root/.config'
    at Error (native)
    at Object.fs.mkdirSync (fs.js:922:18)
    at sync (/usr/local/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:71:13)
    at Function.sync (/usr/local/nodejs/lib/node_modules/yo/node_modules/mkdirp/index.js:77:24)
    at Object.get (/usr/local/nodejs/lib/node_modules/yo/node_modules/configstore/index.js:38:13)
    at Object.Configstore (/usr/local/nodejs/lib/node_modules/yo/node_modules/configstore/index.js:27:44)
    at new Insight (/usr/local/nodejs/lib/node_modules/yo/node_modules/insight/lib/index.js:37:34)
    at Object. (/usr/local/nodejs/lib/node_modules/yo/lib/cli.js:172:11)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)

[root@node1 myhubot]# chmod g+w /root   (修改root目录权限)
[root@node1 myhubot]# yo hubot
? ==========================================================================
We're constantly looking for ways to make yo better! 
May we anonymously report usage statistics to improve the tool over time? 
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes
                     _____________________________  
                    /                             \ 
   //\              |      Extracting input for    |
  ////\    _____    |   self-replication process   |
 //////\  /_____\   \                             / 
 ======= |[^_/\_]|   /----------------------------  
  |   | _|___@@__|__                                
  +===+/  ///     \_\                               
   | |_\ /// HUBOT/\\                             
   |___/\//      /  \\                            
         \      /   +---+                            
          \____/    |   |                            
           | //|    +===+                            
            \//      |xx|                            

? Owner XXX 
? Bot name myhubot
? Description A simple helpful robot for your Company
? Bot adapter campfire
   create bin/hubot
   create bin/hubot.cmd
   create Procfile
   create README.md
   create external-scripts.json
   create hubot-scripts.json
   create .gitignore
   create package.json
   create scripts/example.coffee
   create .editorconfig
                     _____________________________  
 _____              /                             \ 
 \    \             |   Self-replication process   |
 |    |    _____    |          complete...         |
 |__\\|   /_____\   \     Good luck with that.    / 
   |//+  |[^_/\_]|   /----------------------------  
  |   | _|___@@__|__                                
  +===+/  ///     \_\                               
   | |_\ /// HUBOT/\\                             
   |___/\//      /  \\                            
         \      /   +---+                            
          \____/    |   |                            
           | //|    +===+                            
            \//      |xx|                            

[email protected] /root/myhubot
├─┬ [email protected] 
│ ├── [email protected] 
│ ├─┬ [email protected] 
│ │ ├── [email protected] 
│ │ ├── [email protected] 
│ │ ├─┬ [email protected] 
│ │ │ └── [email protected] 
│ │
│ │ ├─┬ [email protected] 
......
[root@node1 myhubot]# chmod g-w /root (将root目录权限恢复原状)
[root@node1 myhubot]# npm install hubot-bearychat --save
myhubot@0.0.0 /root/myhubot
└─┬ hubot-bearychat@0.3.1 
  ├─┬ bearychat@0.1.0 
  │ ├── babel-plugin-add-module-exports@0.2.1 
  │ ├─┬ isomorphic-fetch@2.2.1 
  │ │ ├─┬ node-fetch@1.6.3 
  │ │ │ ├─┬ encoding@0.1.12 
  │ │ │ │ └── iconv-lite@0.4.15 
  │ │ │ └── is-stream@1.1.0 
  │ │ └── whatwg-fetch@2.0.2 
  │ └── urijs@1.18.5 
  └─┬ ws@1.1.1 
    ├── options@0.0.6 
    └── ultron@1.0.2

安装shell 命令脚本支持模块:

[root@node1 myhubot]# npm install hubot-script-shellcmd
myhubot@0.0.0 /root/myhubot
└── hubot-script-shellcmd@0.0.16
[root@node1 myhubot]# cp -R node_modules/hubot-script-shellcmd/bash ./

修改一下external-scripts.json,添加模块:hubot-script-shellcmd
[root@node1 myhubot]# cat external-scripts.json 
[
  "hubot-diagnostics",
  "hubot-help",
  "hubot-heroku-keepalive",
  "hubot-google-images",
  "hubot-google-translate",
  "hubot-pugme",
  "hubot-maps",
  "hubot-redis-brain",
  "hubot-rules",
  "hubot-shipit",
  "hubot-script-shellcmd"  //新增部分
]
[root@node1 myhubot]# cd bash/handlers/
[root@node1 handlers]# ls
helloworld  update

新增脚本
[root@node1 handlers]# cat disk 
#!/bin/bash
df -h
[root@node1 handlers]# chmod +x disk 

3. 注册bearychat添加机器人

  • https://www.bearychat.com 注册并登陆
    bearychat+hubot chatops运维机器人_第1张图片
    bearychat+hubot chatops运维机器人_第2张图片
    bearychat+hubot chatops运维机器人_第3张图片

注意:

  • 复制Hubot Token 这个很重要
  • Hubot URL 端口可以随意指定【export EXPRESS_PORT=9090】

下载bearychat客户端或者直接使用网页版都可以

配置hubot并启动

此处的token就是第三步添加机器人时显示的token多个使用逗号分隔
[root@node1 myhubot]# export HUBOT_BEARYCHAT_TOKENS=bd00e55956a3759886c21c1ac1fd17dd
[root@node1 myhubot]# export HUBOT_BEARYCHAT_MODE=rtm
[root@node1 myhubot]# export EXPRESS_PORT=9090
[root@node1 myhubot]# nohup ./bin/hubot -a bearychat 2&1> hubot.log
[root@node1 myhubot]# netstat -tnlp|grep 9090
tcp        0      0 0.0.0.0:9090            0.0.0.0:*               LISTEN      54886/node 

为了以后启动方便可以直接将变量写人文件:
[root@node1 myhubot]# cat /etc/profile.d/myhubot.sh 
export HUBOT_BEARYCHAT_TOKENS=bd00e5595121249886c21c1ac1fd17dd
export HUBOT_BEARYCHAT_MODE=rtm
export EXPRESS_PORT=9090
[root@node1 myhubot]# source /etc/profile 
这样以后就无需手动再次export

5. 客户端登录并测试

bearychat+hubot chatops运维机器人_第4张图片

6. 机器人脚本示例

[root@node1 ~]# cd myhubot/scripts
[root@node1 scripts]# vim example.coffee   (编辑并修改) 
....
#   Uncomment the ones you want to try and experiment with.
#
#   These are from the scripting documentation: https://github.com/github/hubot/blob/master/docs/scripting.md

module.exports = (robot) ->

   robot.hear /jevic/i, (res) ->
     res.send "https://jevic.github.io"

#   robot.respond /open the (.*) doors/i, (res) ->
....

bearychat+hubot chatops运维机器人_第5张图片

  • 重启hubot
    [root@node1 ~]# nohup ./bin/hubot -a bearychat 2&1>> hubot.log &

测试

bearychat+hubot chatops运维机器人_第6张图片

  • 参考链接
  • 用slack和hubot搭建你自己的运维机器人
  • hubot-bearychat
  • https://hubot.github.com

你可能感兴趣的:(Shell,LinuxBasic)