LoginSignup
10
10

More than 5 years have passed since last update.

サーバにsshログインしたらHipchatに通知させる

Last updated at Posted at 2014-08-19

経緯

仕事でHipChatを使用する場面が出てきたので、サーバーにsshログインしてきた通知をHipChatに通知・監視出来るようにしてみた。

  • GitHubで公開しています。 --> ssh_watch
  • ブログで詰まったとことかまとめてます。--> 鋭意製作中

環境構築

node.jsをインストール

epelリポジトリも用いてyumでインストール出来る。
2014/8現在では

  • epel repo -> v0.10.29
  • 公式の最新版(安定版) -> v0.10.30

なので十分に最新版として使用出来る。
インストールはsudo権限ないし、root権限にて行う。(sudo推奨)

cmd
$ sudo yum install nodejs npm --enablerepo=epel
確認
$ sudo yum list installed | grep nodejs.x86_64

これでnpmコマンドが使用出来るようになる。

npmを用いてhipchatterをインストール

npm: nodejs用のパッケージ管理ツール(RubyのGemのようなもの)

$ sudo mkdir /etc/nodejs/
$ cd mkdir /etc/nodejs/
$ sudo npm init
$ sudo npm install hipchatter --save

Hipchatterの概要は以下
https://github.com/charltoons/hipchatter

実行するJSONファイルの作成

作成すべきjsonファイルの形式は上記のREADME.mdに書いてあるので、それを参考に実際に書いていく

1. 自分のHipChatアカウントが閲覧可能なルームの値を取ってくる

/etc/nodejs/01_gets_rooms.js
var Hipchatter = require('hipchatter');
var hipchatter = new Hipchatter('user_API');

// this will list all of your rooms
    hipchatter.rooms(function(err, rooms){
        if(!err) console.log(rooms)
    });

user_APIとは

HipChatアカウントページ
-> Account settings
-> API access にて確認可能(生成可能)

  • 実行テスト
cmd
$ node 01_gets_rooms.js

2. 特定のルームのヒストリを取得する

/etc/nodejs/02_history_log.js
var Hipchatter = require('hipchatter');
var hipchatter = new Hipchatter('user_API');

    hipchatter.history('room_id', function(err, history){
        // print the last message
        var length = history.items.length;
        history.items.forEach(function(item){
          console.log(item)
        });
    });

room_idとは

先ほどルームの詳細を取得した際にidとして取得出来る値。ルーム固有のもの。

  • 実行テスト
cmd
$ node 02_history_log.js

3. 特定のルームにメッセージを送信する

/etc/nodejs/03_send_msg.js
var Hipchatter = require('hipchatter');
var hipchatter = new Hipchatter('rooms_api');

    hipchatter.notify('room_id',
    {
        message: 'Hello iganari !!',
        color  : 'green', 
        token  : 'rooms_api',
        notify : true
    }, function(err){
        if (err == null) console.log('Successfully notified the room.');
    });

rooms_apiとは

user_idと一緒でルームもapiを持っている。
こちらはルームの管理者権限が無いと作成・閲覧が出来ないので注意。

  • 実行テスト
cmd
$ node 03_send_msg.js

SSHログイン時に上記のファイルが実行されるように設定する

sshrcの設定

まずは下記を設定する。
ユーザがsshでログインしてきた場合、/etc/ssh/sshrcが読み込まれる仕組み。

node /etc/nodejs/03_send_msg.js

そして一度ログアウトし、HipChatを起動した状態で再度ログイン。
指定したルームに以下のような文言が出たら成功。
ss.png

実際に監視するために設定を追記していく

専用のルーム作成

room_name -> サーバー監視
Tokens_Label -> watcher
Token(= room message API)
上記を作成。要admin権限。
また
room_idは上記のコマンド等で確認しておく。

環境構築

cmd
$ sudo mkdir /etc/nodejs/ssh_watch

ファイル構成

iganari/ssh_watch
├── 01_gets_rooms.js
├── 02_history_log.js
├── 03_send_msg.js
├── README.md
├── installation.sh
├── run
│   ├── get_hostname.sh
│   ├── modify_hostname.sh
│   └── send_msg-ssh_login.js
└── ssh
    └── sshrc

スクリプト内容

/etc/nodejs/ssh_watch/get_hostname.sh
#!/bin/bash

HOSTNAME=`hostname`

sed -i "s/hostname/$HOSTNAME/g" /etc/nodejs/ssh_watch/send_msg-ssh_login.js
/etc/nodejs/ssh_watch/modify_hostname.sh
#!/bin/bash

HOSTNAME=`hostname`

sed -i "s/$HOSTNAME/hostname/g" /etc/nodejs/ssh_watch/send_msg-ssh_login.js
/etc/nodejs/ssh_watch/send_msg-ssh_login.js
var Hipchatter = require('hipchatter');
var hipchatter = new Hipchatter('rooms_api');

    hipchatter.notify('room_id',
    {
        message: "hostnameに"+process.argv[3]+''+process.argv[2]+'としてログインしてきたよ!',
        color: 'green',
        token: 'rooms_api',
        notify: true
    }, function(err){
        if (err == null) console.log('Successfully notified the room.');
    });

/etc/ssh/sshrc
sh /etc/nodejs/ssh_watch/get_hostname.sh
node /etc/nodejs/send_msg-ssh_login.js $USER $SSH_CLIENT
sh /etc/nodejs/ssh_watch/modify_hostname.sh

上記のsshdの環境変数は以下のよう

変数名 内容
$SSH_CLIENT 接続元IPアドレス 192.168.101.xxx
$SSH_TTY 接続元IPアドレス /dev/pts/1
$USER 接続元IPアドレス root, dev

権限変更をしておく

sedコマンドは対象のファイルを直接編集するのではなく一時的なファイルを作成し、リネームと削除をする。
なので、そのディレクトリに書き込み権限が無いといけない。

cmd
$ sudo chmod 777 /etc/nodejs/ssh_watch

sshdの設定

以下のシンボリックリンクを張る

cmd
ln -s /etc/nodejs/ssh_watch/ssh/sshrc /etc/ssh/sshrc 

実行結果

上記の設定が終了後、サーバに対してsshログインすると以下のようにHipChatに通知が来る

ss.png

これで完了です。
nodejsさえ入れられるのであれば、すんなりインストール出来ます!

参考URL

sshでLinuxマシンにログインしたらSlackで通知する
HipChatAPIのnode.jsラッパーHipChatter
付録 A. OpenSSH に関連するファイル

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10