func (d *Daemon) ping() error { for _, u := range d.Urls { if u == nil { return e.New(ErrUrlIsEmpty) } resolved, err := dns.ResolveUrl(u) if e.Contains(err, "invalid domain name") { resolved = u } else if err != nil { return e.Forward(err) } err = ping.Ping(resolved) if err != nil { d.status.LogAndEmail(status.Verbose, "", "Monitor ping failed for %v with error: %v", d.DaemonName, e.Trace(e.Forward(err))) return e.Push(err, ErrPingFailed) } } return nil }
func (a *Alive) Connect() error { if a.events == nil { a.events = make(chan event.Event) } a.closePing = make(chan bool) resolved, err := dns.ResolveUrl(a.Url) if e.Contains(err, "invalid domain name") { resolved = a.Url } else if err != nil { a.setConnected(false) return e.Forward(err) } for i := 0; i < a.Tries; i++ { a.conn, err = net.Dial(resolved.Scheme, resolved.Host) if e.Contains(err, definitions.NetErrorConnectionRefused) || e.Contains(err, definitions.NetErrorTimeOut) || e.Contains(err, definitions.ErrCheckIsAliveNoRoute) || e.Contains(err, definitions.ErrCheckIsAliveNoAnswerFromResolver) { time.Sleep(a.Wait) continue } else if err != nil { a.setConnected(false) return e.Forward(err) } break } if a.conn == nil { a.setConnected(false) if err != nil { return e.Push(e.New(err), definitions.ErrCheckIsAliveCantConnect) } return e.New(definitions.ErrCheckIsAliveCantConnect) } a.setConnected(true) dec := gob.NewDecoder(a.conn) enc := gob.NewEncoder(a.conn) go func() { defer func() { if a.conn != nil { a.conn.Close() } a.setConnected(false) }() for { select { case <-time.After(a.Wait): err := a.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) if err != nil { return } err = enc.Encode("ping") if err != nil { a.Statuses.Log(status.Verbose, "Alive: Ping encode error: %v", err) return } case <-a.closePing: return } } }() go func() { defer func() { if a.conn != nil { a.conn.Close() } a.setConnected(false) }() for { var typ string err := dec.Decode(&typ) if err != nil { a.Statuses.Log(status.Verbose, "Alive: Decode error: %v", err) return } t, err := types.GetType(typ) if err != nil { a.Statuses.Log(status.Verbose, "Alive: GetType error: %v", err) return } ptr := types.Make(reflect.PtrTo(t)) err = dec.DecodeValue(ptr) if err != nil { a.Statuses.Log(status.Verbose, "Alive: DecodeValue error: %v", err) return } val := reflect.Indirect(ptr) if val.IsValid() { a.events <- val.Interface().(event.Event) } } }() return nil }