【问题标题】:Need alert when the system is going down系统出现故障时需要警报
【发布时间】:2017-05-11 12:01:46
【问题描述】:

当服务器出现故障时,我需要一个小警报(HTTP req 或任何)。我检查了许多应用程序,如 nagios、servercheck 等……所有这些应用程序都只监视远程服务器。我只有两台服务器要监控。因此,如果我的服务器 (10.172.65.124) 出现故障,它不能发送警报。我不想再维护一台服务器来监控这一点。我正在使用 rhel6 和 centos7。任何建议

【问题讨论】:

    标签: linux linux-kernel operating-system centos7 rhel6


    【解决方案1】:

    这是一个可以达到目的的 python 脚本。它使用 sendmail 发送您的电子邮件,这需要从启用了 sendmail 的 linux 服务器运行它。将 url 更改为指向您正在监视的 url。如果你运行这个脚本,它会检查 stackoverflow。

    这使用 urllib 来检查它在尝试加载您的 url 时收到的状态代码。如果它从 HTTP 请求中获得 200 以外的状态,则它预计该站点已关闭。

    要监控您的服务器,您应该在独立于您的虚拟主机的服务器或桌面上运行脚本,否则当您的服务器由于多种原因崩溃时,您不会收到警报。

    #Import time to allow you to sleep the script, urllib to load the site, subprocess will allow you to run a process on the machine outside of the script (in this instance it's send mail) 
    import time
    import urllib
    from email.mime.text import MIMEText
    from subprocess import Popen, PIPE
    
    #The url being monitored.
    url = "http://www.stackoverflow.com"
    
    #The contents of the email
    msg = MIMEText(url + " is not responding.  Please investigate.")
    msg["From"] = "me@youremail.com"
    msg["To"] = "me@youremail.com"
    msg["Subject"] = url + "is not responding"
    
    #This loops while the script is running.
    # It gets the status returned from the urllib call, if it's not 200 it will email the email contents above.  
    while True:
        status = urllib.urlopen(url).getcode()
    
        if status <> 200:
            #This is what sends the email.  If you don't have sendmail then update this. 
            p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
            p.communicate(msg.as_string())
        #The number of seconds the loop will pause for before checking again.  I set it to 60. 
        time.sleep(60)
    

    【讨论】:

    • 很好的答案。并且很好的解释。谢谢回复。但我需要从系统内部进行监控。但不在系统之外。我们可以这样做吗???
    • 没问题拉维。你能澄清一下你说你想从系统内部监控的意思吗?你的意思是你想让http服务告诉你它什么时候崩溃,或者它什么时候优雅地关闭了?我的一个建议是创建一个日志解析器来 grep error.log,它会告诉你服务发生了什么,但这又在系统之外。或者你可以做类似这里引用的事情:serverfault.com/questions/16243/…
    【解决方案2】:

    我建议创建简单的脚本来 ping 机器(它们可以互相监控),如果 ping 超时发送电子邮件。

    类似的东西

    #!/bin/bash
    SERVERIP=IP ADDRESS
    NOTIFYEMAIL=test@example.com
    
    ping -c 3 $SERVERIP > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      # Use your favorite mailer here:
      mailx -s "Server $SERVERIP is down" -t "$NOTIFYEMAIL" < /dev/null 
    fi
    

    【讨论】:

    • 我已经提到我不想从外部监控我的服务器。我们可以在服务器内部做任何事情吗?
    • 如果您遇到网络中断或机器崩溃,则外部是一个更好的选择,至少据我所知,机器不会告诉您它崩溃了。这就是为什么我说如果你只有 2 台服务器,你可以将它们设置为相互监控,这样如果一台服务器死了,另一台可以发送通知。
    • 在我的情况下,两个系统都连接到相同的交换机、相同的电源卡和相同的负载。如果一个最终下降,两个都会下降
    • 您要求的是操作系统监控自身,当网络连接出现问题或系统崩溃时向您发送电子邮件。那是不可能的,这就像让一个被困在荒岛上的死人给你发信息一样。您需要外部资源来监控此环境。有一种叫做 IPMI 的东西可以安装在主板上,它提供独立于操作系统的监控。但是主板需要支持它,而且如果网络出现故障,它将无法向您发送消息,停电也是如此。
    【解决方案3】:

    如上面给出的脚本,您可以配置普通的 bash 脚本来监控服务器 http 请求或任何其他服务请求,因此如果它没有得到回复,那么您将收到邮件。

    有一个用于监控网络服务的正常应用程序,每个用户的站点数量有限,您也可以使用它。

    http://uptimerobot.com/

    【讨论】:

      【解决方案4】:

      下面的脚本会检查接口的运行状态,如果您需要添加一些接口,请根据您的意愿进行警报

      #!/bin/bash
      while true
      do
              if [ $(cat /sys/class/net/eth0/operstate) != "up" ]; then
                      sleep 1
                      #send mail for logging
              fi
      done
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-16
        相关资源
        最近更新 更多