mirror of https://github.com/statping/statping
				
				
				
			@Southclaws TCP checker - email notifier update
							parent
							
								
									17668c0586
								
							
						
					
					
						commit
						edc2d9b7d0
					
				| 
						 | 
				
			
			@ -33,13 +33,8 @@ func CheckQueue(s *types.Service) {
 | 
			
		|||
			return
 | 
			
		||||
		default:
 | 
			
		||||
			ServiceCheck(s)
 | 
			
		||||
			if s.Interval < 1 {
 | 
			
		||||
				s.Interval = 1
 | 
			
		||||
			}
 | 
			
		||||
			msg := fmt.Sprintf("Service: %v | Online: %v | Latency: %0.0fms", s.Name, s.Online, (s.Latency * 1000))
 | 
			
		||||
			utils.Log(1, msg)
 | 
			
		||||
			time.Sleep(time.Duration(s.Interval) * time.Second)
 | 
			
		||||
		}
 | 
			
		||||
		time.Sleep(time.Duration(s.Interval) * time.Second)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -58,7 +53,39 @@ func DNSCheck(s *types.Service) (float64, error) {
 | 
			
		|||
	return subTime, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServiceTCPCheck(s *types.Service) *types.Service {
 | 
			
		||||
	t1 := time.Now()
 | 
			
		||||
	domain := fmt.Sprintf("%v", s.Domain)
 | 
			
		||||
	if s.Port != 0 {
 | 
			
		||||
		domain = fmt.Sprintf("%v:%v", s.Domain, s.Port)
 | 
			
		||||
	}
 | 
			
		||||
	conn, err := net.Dial("tcp", domain)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		RecordFailure(s, fmt.Sprintf("TCP Dial Error %v", err))
 | 
			
		||||
		return s
 | 
			
		||||
	}
 | 
			
		||||
	if err := conn.Close(); err != nil {
 | 
			
		||||
		RecordFailure(s, fmt.Sprintf("TCP Socket Close Error %v", err))
 | 
			
		||||
		return s
 | 
			
		||||
	}
 | 
			
		||||
	t2 := time.Now()
 | 
			
		||||
	s.Latency = t2.Sub(t1).Seconds()
 | 
			
		||||
	s.LastResponse = ""
 | 
			
		||||
	RecordSuccess(s)
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServiceCheck(s *types.Service) *types.Service {
 | 
			
		||||
	switch s.Type {
 | 
			
		||||
	case "http":
 | 
			
		||||
		ServiceHTTPCheck(s)
 | 
			
		||||
	case "tcp":
 | 
			
		||||
		ServiceTCPCheck(s)
 | 
			
		||||
	}
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServiceHTTPCheck(s *types.Service) *types.Service {
 | 
			
		||||
	dnsLookup, err := DNSCheck(s)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		RecordFailure(s, fmt.Sprintf("Could not get IP address for domain %v, %v", s.Domain, err))
 | 
			
		||||
| 
						 | 
				
			
			@ -113,7 +140,7 @@ func ServiceCheck(s *types.Service) *types.Service {
 | 
			
		|||
	s.LastResponse = string(contents)
 | 
			
		||||
	s.LastStatusCode = response.StatusCode
 | 
			
		||||
	s.Online = true
 | 
			
		||||
	RecordSuccess(s, response)
 | 
			
		||||
	RecordSuccess(s)
 | 
			
		||||
	return s
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -121,7 +148,7 @@ type HitData struct {
 | 
			
		|||
	Latency float64
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func RecordSuccess(s *types.Service, response *http.Response) {
 | 
			
		||||
func RecordSuccess(s *types.Service) {
 | 
			
		||||
	s.Online = true
 | 
			
		||||
	s.LastOnline = time.Now()
 | 
			
		||||
	data := HitData{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -66,6 +66,12 @@ func init() {
 | 
			
		|||
				Title:       "Outgoing Email Address",
 | 
			
		||||
				Placeholder: "Insert your Outgoing Email Address",
 | 
			
		||||
				DbField:     "Var1",
 | 
			
		||||
			}, {
 | 
			
		||||
				Id:          1,
 | 
			
		||||
				Type:        "email",
 | 
			
		||||
				Title:       "Send Alerts To",
 | 
			
		||||
				Placeholder: "Email Address",
 | 
			
		||||
				DbField:     "Var2",
 | 
			
		||||
			}},
 | 
			
		||||
		}}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -103,7 +109,7 @@ func (u *Email) Init() error {
 | 
			
		|||
func (u *Email) Test() error {
 | 
			
		||||
	if u.Enabled {
 | 
			
		||||
		email := &EmailOutgoing{
 | 
			
		||||
			To:       "info@socialeck.com",
 | 
			
		||||
			To:       emailer.Var2,
 | 
			
		||||
			Subject:  "Test Email",
 | 
			
		||||
			Template: "message.html",
 | 
			
		||||
			Data:     nil,
 | 
			
		||||
| 
						 | 
				
			
			@ -159,13 +165,11 @@ func (u *Email) Run() error {
 | 
			
		|||
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
 | 
			
		||||
func (u *Email) OnFailure(s *types.Service) error {
 | 
			
		||||
	if u.Enabled {
 | 
			
		||||
 | 
			
		||||
		msg := emailMessage{
 | 
			
		||||
			Service: s,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		email := &EmailOutgoing{
 | 
			
		||||
			To:       "info@socialeck.com",
 | 
			
		||||
			To:       emailer.Var2,
 | 
			
		||||
			Subject:  fmt.Sprintf("Service %v is Failing", s.Name),
 | 
			
		||||
			Template: "failure.html",
 | 
			
		||||
			Data:     msg,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue