Skip to content

Instantly share code, notes, and snippets.

@okochang
Created September 19, 2011 15:13
Show Gist options
  • Save okochang/1226724 to your computer and use it in GitHub Desktop.
Save okochang/1226724 to your computer and use it in GitHub Desktop.
Login check and post error message to yammer.com
# -*- encoding: UTF-8 -*-
# このスクリプトはあるURLでログインして失敗したら警告メールをyammerに投稿するスクリプトです
# スクリプトを実行するには以下のライブラリが必要となりますので、事前にインストールして下さい
# gem install httpclient
# gem install tmail
# gem install tlsmail
require 'rubygems'
require 'httpclient'
require 'tmail'
require 'tlsmail'
## 接続先設定
accessurl = 'https://www.example.com/login'
userid = 'yourid'
psword = 'yourpassword'
postdata = "userID=#{userid}&password=#{psword}"
## メール設定
maildomain = 'yourdomain.com'
mailuser = 'user@yourdomain.com'
mailpass = 'mailpassword'
mail = TMail::Mail.new
mail.to = 'yourdomain.com@yammer.com'
mail.from = 'hoge@yourdomain.com'
mail.subject = '#alertmail'
mail.date = Time.now
mail.mime_version = '1.0'
mail.set_content_type 'text/plain', {"charset"=>"iso-2022-jp"}
mail.body = "#{Time.now} Login_failed_check_for_your_Site"
## タイムアウトを指定して監視対象ページにログインをする
begin
client = HTTPClient.new
client.connect_timeout = 60
client.send_timeout = 60
client.receive_timeout = 60
puts c.post_content(accessurl, postdata)
## 失敗だった場合はyammerに警告文を投稿する
rescue
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)
Net::SMTP.start('smtp.gmail.com', 587, maildomain, mailuser, mailpass, 'plain') do |smtp|
smtp.sendmail(mail.encoded, mail.from, mail.to)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment