Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "net"
- "os/exec"
- "time"
- )
- const (
- target = "192.168.1.50:8080" // IP:porta da controllare
- serviceName = "mioservizio" // servizio systemd
- checkInterval = 5 * time.Second
- maxFailures = 3
- timeout = 2 * time.Second
- )
- func checkTCP(addr string, timeout time.Duration) bool {
- conn, err := net.DialTimeout("tcp", addr, timeout)
- if err != nil {
- return false
- }
- conn.Close()
- return true
- }
- func systemctl(action string) {
- cmd := exec.Command("systemctl", action, serviceName)
- cmd.Run()
- }
- func main() {
- failCount := 0
- serviceDown := false
- for {
- ok := checkTCP(target, timeout)
- if ok {
- failCount = 0
- if serviceDown {
- systemctl("start")
- serviceDown = false
- }
- } else {
- failCount++
- if failCount >= maxFailures && !serviceDown {
- systemctl("stop")
- serviceDown = true
- }
- }
- time.Sleep(checkInterval)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment