github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/nagioswatcher/builtin_choria.go (about)

     1  // Copyright (c) 2020-2023, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package nagioswatcher
     6  
     7  import (
     8  	"fmt"
     9  	"math"
    10  	"time"
    11  
    12  	"github.com/choria-io/go-choria/statistics"
    13  )
    14  
    15  func (w *Watcher) watchUsingChoria() (state State, output string, err error) {
    16  	f, freq := w.machine.ChoriaStatusFile()
    17  	if f == "" || freq == 0 {
    18  		return UNKNOWN, "Status file not configured", nil
    19  	}
    20  
    21  	status, err := statistics.LoadInstanceStatus(f)
    22  	if err != nil {
    23  		return CRITICAL, fmt.Sprintf("Status file error: %s", err), nil
    24  	}
    25  
    26  	ce := math.MaxInt
    27  	te := math.MaxInt
    28  
    29  	if !status.CertificateExpires.IsZero() {
    30  		ce = int(time.Until(status.CertificateExpires).Seconds())
    31  	}
    32  	if !status.TokenExpires.IsZero() {
    33  		te = int(time.Until(status.TokenExpires).Seconds())
    34  	}
    35  
    36  	perfData := fmt.Sprintf("uptime=%d;; filtered_msgs=%d;; invalid_msgs=%d;; passed_msgs=%d;; replies_msgs=%d;; total_msgs=%d;; ttlexpired_msgs=%d;; last_msg=%d;; cert_expire_seconds=%d;; token_expire_seconds=%d;; events=%d;;", status.Uptime, int(status.Stats.Filtered), int(status.Stats.Invalid), int(status.Stats.Passed), int(status.Stats.Replies), int(status.Stats.Total), int(status.Stats.TTLExpired), status.LastMessage, ce, te, int(status.Stats.Events))
    37  
    38  	err = status.CheckFileAge(time.Duration(3*freq) * time.Second)
    39  	if err != nil {
    40  		return CRITICAL, fmt.Sprintf("CRITICAL: %s|%s", err, perfData), nil
    41  	}
    42  
    43  	if w.properties.CertExpiry > 0 {
    44  		err = status.CheckCertValidity(w.properties.CertExpiry)
    45  		if err != nil {
    46  			return CRITICAL, fmt.Sprintf("CRITICAL: %s|%s", err, perfData), nil
    47  		}
    48  	}
    49  
    50  	if w.properties.TokenExpiry > 0 {
    51  		err = status.CheckTokenValidity(w.properties.TokenExpiry)
    52  		if err != nil {
    53  			return CRITICAL, fmt.Sprintf("CRITICAL: %s|%s", err, perfData), nil
    54  		}
    55  	}
    56  
    57  	err = status.CheckLastMessage(w.properties.LastMessage)
    58  	if err != nil {
    59  		return CRITICAL, fmt.Sprintf("CRITICAL: %s|%v", err, perfData), nil
    60  	}
    61  
    62  	err = status.CheckConnection()
    63  	if err != nil {
    64  		return CRITICAL, fmt.Sprintf("CRITICAL: Not connected to any server|%v", perfData), nil
    65  	}
    66  
    67  	return OK, fmt.Sprintf("OK: %s|%v", f, perfData), nil
    68  }