• 监控守护脚本


    监控HTTP服务程序脚本:

    package main
    
    import (
        "strings"
        "io/ioutil"
        "net/http"
        "fmt"
        "time"
        "os/exec"
        "path/filepath"
        "gopkg.in/gomail.v2"
        "os"
    )
    
    func main() {
    
        error_count := 0
        url := "http://127.0.0.1:10240/watch_api"
        server_path := "/service/online/game_service"
    
        //检测文件路径是否正确
        if false == file_exist(server_path) {
            fmt.Printf("文件路径不存在: %s,请检查后重试。
    ",server_path)
            return
        }
    
        for {
            time.Sleep(5 * time.Second)
    
            if false == check(url,"Time") {
                error_count += 1
            } else {
                error_count = 0
            }
            if error_count >= 3 {
                //启动服务
                if false == start_server(server_path) {
                    send_mail(fmt.Sprintf("启动服务失败!!!请尽快查看!!!(%s)",server_path))
                    break
                } else {
                    send_mail(fmt.Sprintf("启动服务成功!(%s)",server_path))
                    time.Sleep(30 * time.Second)
                    error_count = 0
                }
            }
        }
        fmt.Printf("启动服务失败!守护程序退出。
    
    ")
    }
    
    //检测某个HTTP链接请求,返回值是否包括指定字符串
    func check(url string, ret_contain string) bool {
        res, err := http.Post(url, "application/json;charset=utf-8", strings.NewReader(""))
    
        if err != nil {
            fmt.Printf("error: %s	time: %s
    ",err.Error(),time.Now().Format("2006-01-02 15:04:05"))
            return false
        }
    
        result, err := ioutil.ReadAll(res.Body)
        defer res.Body.Close()
        if err != nil {
            fmt.Printf("error: %s	time: %s
    ",err.Error(),time.Now().Format("2006-01-02 15:04:05"))
            return false
        }
    
        if false == strings.Contains(string(result),ret_contain)  {
            fmt.Printf("error: %s	time: %s
    ","Dont Contain `Time`: " + string(result),time.Now().Format("2006-01-02 15:04:05"))
            return false
        }
        return true
    }
    
    //启动服务
    func start_server(server_path string) bool {
        filename := filepath.Base(server_path)
    
        cmd_kill := fmt.Sprintf("killall -9 %s", filename)
        _, err := exec.Command("sh", "-c", cmd_kill).Output()
        if err != nil {
            fmt.Printf("可忽略的命令失败! (cmd: %s	error: %s)
    ", cmd_kill, err.Error())
            //return false
        }
    
        cmd_start := fmt.Sprintf("nohup %s 1>/dev/null 2>/dev/null  &", server_path)
        _, err = exec.Command("sh", "-c", cmd_start).Output()
        if err != nil {
            fmt.Printf("cmd.Output Error! (cmd: %s	error: %s)
    ", cmd_start, err.Error())
            return false
        }
        fmt.Printf("启动服务 %s 成功,time: %s
    
    ",server_path,time.Now().Format("2006-01-02 15:04:05"))
        return true
    }
    
    func send_mail(content string) {
        defer func() {
            if err := recover(); err != nil {
                fmt.Printf("发送邮件异常,time: %s
    ",time.Now().Format("2006-01-02 15:04:05"))
            }
        }()
    
        m := gomail.NewMessage()
        m.SetAddressHeader("From", "123456789@163.com", "123456789@163.com")  // 发件人
        m.SetHeader("To",  // 收件人
            m.FormatAddress("123456789@qq.com", "轻典"),
        )
        m.SetHeader("Subject", "服务器故障")  // 主题
        m.SetBody("text/html", content)  // 正文
    
        d := gomail.NewDialer("smtp.163.com", 25, "123456789@163.com", "123456")  // 发送邮件服务器、端口、发件人账号、发件人密码
        if err := d.DialAndSend(m); err != nil {
            fmt.Printf("发送邮件失败,time: %s
    ",time.Now().Format("2006-01-02 15:04:05"))
        } else {
            fmt.Printf("发送邮件成功,time: %s
    ",time.Now().Format("2006-01-02 15:04:05"))
        }
    }
    
    func file_exist(path string) bool {
        if stat, err := os.Stat(path); err != nil {
            if os.IsNotExist(err) {
                return false
            } else {
                return false
            }
        } else {
            if stat.IsDir() {
                return false
            } else {
                return true
            }
        }
    }
  • 相关阅读:
    线性表的各种基本操作
    malloc&&free的系统运行机制及其源代码的理解
    剪枝的定义&&hdu1010
    hdu 1045
    hdu2094 stl之set的应用
    关联式容器的总结
    STL之map容器的详解
    2018-2019 ACM-ICPC 焦作赛区 部分题解
    2018-2019 ACM-ICPC 沈阳赛区 K. Let the Flames Begin
    2018-2019 ACM-ICPC 徐州区域赛 部分题解
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/7613345.html
Copyright © 2020-2023  润新知