1. 程式人生 > >監控守護腳本

監控守護腳本

bool lee utf-8 n) message sprint 失敗 mman 發送郵件

監控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,請檢查後重試。\n",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("啟動服務失敗!守護程序退出。\n\n") } //檢測某個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\ttime: %s\n",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\ttime: %s\n",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\ttime: %s\n","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\terror: %s)\n", 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\terror: %s)\n", cmd_start, err.Error()) return false } fmt.Printf("啟動服務 %s 成功,time: %s\n\n",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\n",time.Now().Format("2006-01-02 15:04:05")) } }() m := gomail.NewMessage() m.SetAddressHeader("From", "[email protected]", "[email protected]") // 發件人 m.SetHeader("To", // 收件人 m.FormatAddress("[email protected]", "輕典"), ) m.SetHeader("Subject", "服務器故障") // 主題 m.SetBody("text/html", content) // 正文 d := gomail.NewDialer("smtp.163.com", 25, "[email protected]", "123456") // 發送郵件服務器、端口、發件人賬號、發件人密碼 if err := d.DialAndSend(m); err != nil { fmt.Printf("發送郵件失敗,time: %s\n",time.Now().Format("2006-01-02 15:04:05")) } else { fmt.Printf("發送郵件成功,time: %s\n",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 } } }

監控守護腳本