shitou's blog 

  • Home
  • About
  • Feed
  • 十人族
  • MIX BOY
  • 塌客

Category "system manage"

21
May

服务器监控小脚本

监控服务器存活的脚本,用ruby写的,需要安装有action_mailer gem,

功能:

检测主机的80端口是否可用

失败次数记录,超过指定次数判定主机Down掉

发送通知邮件到指定邮箱


在前面的日志中有说明怎样连接gmail来发送邮件的


require 'rubygems'
require 'action_mailer'
require 'ping'
require 'fileutils'
require 'tlsmail' #need install tlsmail gem to support TLS connect
Net::SMTP.enable_tls(OpenSSL::SSL::VERIFY_NONE)

#发送通知from账户设置
ActionMailer::Base.smtp_settings = {
  :address => 'smtp.gmail.com',
  :port => '25',
  :domain => 'gmail.com',
  :user_name => 'username',
  :password => 'passwd',
  :authentication => :login
}

#recp: 收到通知的邮箱地址,可以填多个地址,逗号分隔
#sub: 邮件主题
#bo: 邮件内容
class SimpleMailer < ActionMailer::Base
  def simple_message(recp, sub, bo)
    from 'sender@gmail.com'
    recipients recp
    subject sub
    body bo
  end
end

class ServerMonitor
  def initialize
    #config
    @hit = 5  #最大失败次数,达到后才会发送邮件,避免误报
    @tmp = "/tmp/server_monitor_tmp"  #计数器地址
    @recp = 'mytake6@gmail.com, std8545@yahoo.com.cn'  #收信的邮箱地址
    @host = ['www.ccok.me', 'www.google.com']  #需要监控的主机
    
    #do not touch these
    @hosts = Struct.new(:host, :status)
    @servers, @msg = [], []
    @host.each { |h| @servers << @hosts.new(h, true) }
    
    FileUtils.mkdir(@tmp) unless File.exists? @tmp
  end
  
  def run
    @servers.each do |s| 
      num = Ping.pingecho(s.host, 5, 80) ? 0 : rf(s.host) + 1
      File.open(dest(s.host), 'w') { |f| f.puts num }
    end

    @servers.each do |e|
      if rf(e.host) == @hit
        e.status = false
        File.open(dest(e.host), 'w') { |f| f.puts 0 }
      end
    end
    
    send_mail
  end
  
  def rf file
    return 0 unless File.exist?(dest(file))
    File.open(dest(file), 'r').readlines.to_s.chomp.to_i
  end
  
  def dest host
    "#{@tmp}/#{host}"
  end
  
  def send_mail
    @servers.each { |m| @msg << m.host unless m.status }
    SimpleMailer.deliver_simple_message @recp, 
        "server monitor", 
        @msg.join(', ') + " is Down!!! " unless @msg.empty?
  end
end

#go
ServerMonitor.new.run


可以放到crontab中每2分钟运行一次


Tags: 监控,ruby,linux

2009-05-21 14:20:44, 16623 reviews, comment

send to mailbox

Your email:

Related Posts

Soft Raid and LVM

[转载]Using temporary files in Ruby - Tempfile.new

[转载]5个有用的ruby gems

关于yield的使用

进程和线程

ruby写的一个随机发扑克类

Ruby和Erlang的交互

json_formatter

innotop / mysqlreport / mtop / DBD::mysql

监控mysql的性能

mysqlreport详解

MooseFS vs. NFS

TOP

Tags

json Impactjs Canvas fun gen_server superfly PS3 Webgame HTML5 jquery SSH tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • HTML5[3]
  • iPhone[17]
  • Erlang[11]
  • google[13]
  • 生活[45]
  • 音乐[13]
  • 电影[11]
  • linux[29]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[9]
  • ruby[20]
  • ruby on rails[28]
  • 开源[4]

Episode

  • MongoDB
  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache


Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • Ruby遍历MemCached的key
  • 服务器监控小脚本
  • MySQL Innodb备份
  • [转载]5个有用的ruby gems

Recommended Posts

  • Tri-survive - HTML5 Game
  • Cut the rope - HTML5版
  • json_formatter
  • 在gen_server中spawn新的进程
  • Superfly - Wildflowers
  • 继续凸墙 for Mac OS
  • MongoDB Beijing 2011
  • MongoDB删除map_reduce生成的tmp collection
  • Mai Kuraki - Future Kiss
  • Erlang OOP
  • 用SSH tunnel凸墙
  • Google的语法高亮工具包
  • 大量数据的批量操作
  • 再次被和谐-_-
  • 十人族: 上线了

Friends' blogs

  • levy
  • sphance
  • andreas
  • yangkunlun
  • {:dev=>:wxianfeng}
  • bheye
  • joeydarko

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 317294
  • 今天访问: 102
  • 日志: 213
  • 评论: 4967
  • 音乐: 9
  • 用户: 1766


少女,不点下广告吗!

 

all by shitou

blog comments powered by Disqus

close