『WEB+DB PRESS Vol.75』の『継続的Webサービス改善ガイド』は良かったですね。
その記事に、「毎日、アクセスログを解析した結果をGoogle DocsのSpreadSheetにまとめている」ということが書かれていて、サンプルがgistに公開されていました。
puboo-performance-index.rb
へー、SpreadSheetに自動でいろいろまとめるの、なかなか便利そう!ということで自分でもちょっとやってみました。
まずは試し
rubyでSpreadSheetを操作するのはgoogle_driveを使うとすごく簡単でした。
#!/home/mikeda/.rbenv/shims/ruby # -*- coding: utf-8 -*- require 'google_drive' session = GoogleDrive.login("<user>@gmail.com", "<password>") #ファイルのリストを表示 #session.files.each do |file| # p file.title #end #テスト用のスプレッドシート、ワークシートを開く。なければ作る ss_title = 'test_ss' ws_title = 'test_ws' ss = session.spreadsheet_by_title(ss_title) || session.create_spreadsheet(ss_title) ws = ss.worksheet_by_title(ws_title) || ss.add_worksheet(ws_title) #A1の値を更新する ws[1,1] = "aaa" ws.save
NagiosのアラートをSpreadSheetに!
というわけで、今回はNagiosのアラートを自動でSpreadSheetに書き込むようにしてみます。
※『定例会議で障害情報を報告しろ』とかってけっこう言われますよね!
まずこういうスクリプトを作って
#!/usr/local/bin/ruby # -*- coding: utf-8 -*- require 'google_drive' # Google Docsのアカウント USER = '<user>@gmail.com' PASSWORD = '<password>' host, state, type, desc, info, timestamp = ARGV time = Time.at(timestamp.to_i) headers = %w(時間 Host State Type Desc Info 担当者 対応詳細) ss_title = 'nagios_alert' ws_title = time.strftime("%Y-%m") session = GoogleDrive.login(USER, PASSWORD) ss = session.spreadsheet_by_title(ss_title) || session.create_spreadsheet(ss_title) ws = ss.worksheet_by_title(ws_title) unless ws ws = ss.add_worksheet(ws_title) headers.each.with_index do |header, i| ws[1, i+1] = header end end num_rows = ws.num_rows ws[num_rows + 1, 1] = time.strftime("%Y/%m/%d %H:%M:%S") ws[num_rows + 1, 2] = host ws[num_rows + 1, 3] = state ws[num_rows + 1, 4] = type ws[num_rows + 1, 5] = desc ws[num_rows + 1, 6] = info ws.save
ちょいちょいとNagiosのコンフィグをいじります
define command{ command_name notify-host-by-spreadsheet command_line /etc/nagios/helper/notify-by-spreadsheet.rb "$HOSTALIAS$" "HOST DOWN" "$NOTIFICATIONTYPE$" "-" "-" "$TIMET$" } define command{ command_name notify-service-by-spreadsheet command_line /etc/nagios/helper/notify-by-spreadsheet.rb "$HOSTALIAS$" "$SERVICESTATE$" "$NOTIFICATIONTYPE$" "$SERVICEDESC$" "$SERVICEOUTPUT$" "$TIMET$" } define contact{ contact_name spreadsheet use generic-contact host_notification_commands notify-host-by-spreadsheet service_notification_commands notify-service-by-spreadsheet }
書き込まれた!(枠線とか色はちょっと手でいじりました。)
まとめ
アプリとSpreadSheetを連携させると、各種レポートの作成がいろいろ楽にできそうですねー