Skip to content

Commit

Permalink
core: Fix persistence data not being loaded and stored
Browse files Browse the repository at this point in the history
  • Loading branch information
ppmathis committed Apr 29, 2019
1 parent abd7ecf commit 98a3681
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
15 changes: 8 additions & 7 deletions mod-system/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type interfacePlugin struct {
}

type interfaceResource struct {
nagocheck.Resource
nagocheck.Resource `json:"-"`

linkState string
linkSpeed int
Expand Down Expand Up @@ -71,7 +71,7 @@ func (p *interfacePlugin) DefineFlags(kp nagocheck.KingpinNode) {
}

func (p *interfacePlugin) DefineCheck() nagopher.Check {
deltaRange := nagopher.NewBounds(nagopher.LowerBound(0))
deltaRange := nagopher.NewBounds(nagopher.LowerBound(math.Inf(-1)), nagopher.UpperBound(0))
resource := newInterfaceResource(p)

check := nagopher.NewCheck("interface", newInterfaceSummarizer(p))
Expand All @@ -88,11 +88,12 @@ func (p *interfacePlugin) DefineCheck() nagopher.Check {
}

func newInterfaceResource(plugin *interfacePlugin) *interfaceResource {
return &interfaceResource{
Resource: nagocheck.NewResource(plugin,
nagocheck.ResourcePersistence(plugin.InterfaceName),
),
}
resource := &interfaceResource{}
resource.Resource = nagocheck.NewResource(plugin,
nagocheck.ResourcePersistence(plugin.InterfaceName, &resource),
)

return resource
}

func (r *interfaceResource) Probe(warnings nagopher.WarningCollection) (metrics []nagopher.Metric, _ error) {
Expand Down
40 changes: 22 additions & 18 deletions nagocheck/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/snapserv/nagopher"
"io/ioutil"
"os"
"strings"
"syscall"
)

Expand All @@ -38,13 +39,16 @@ type Resource interface {
type ResourceOpt func(*baseResource)

type baseResource struct {
nagopher.Resource
plugin Plugin
nagopher.Resource `json:"-"`
plugin Plugin

persistenceKey string
persistenceKey string
persistenceStore interface{}
}

const shmOpenFlags = os.O_CREATE | os.O_RDONLY | syscall.O_DSYNC | syscall.O_RSYNC
const shmOpenFlags = os.O_CREATE | syscall.O_DSYNC | syscall.O_RSYNC
const shmReadFlags = shmOpenFlags | os.O_RDONLY
const shmWriteFlags = shmOpenFlags | os.O_WRONLY | os.O_TRUNC
const shmDefaultMode = 0600

// NewResource instantiates baseResource with the given functional options
Expand All @@ -62,27 +66,27 @@ func NewResource(plugin Plugin, options ...ResourceOpt) Resource {
}

// ResourcePersistence is a functional option for NewResource(), which enables resource persistence with the given key
func ResourcePersistence(uniqueKey string) ResourceOpt {
func ResourcePersistence(uniqueKey string, dataStore interface{}) ResourceOpt {
return func(r *baseResource) {
r.persistenceKey = r.Plugin().Name() + uniqueKey
r.persistenceKey = strings.ToLower(".nagocheck-" + r.Plugin().Name() + "-" + uniqueKey)
r.persistenceStore = dataStore
}
}

func (r baseResource) Probe(warnings nagopher.WarningCollection) ([]nagopher.Metric, error) {
func (r baseResource) Setup(warnings nagopher.WarningCollection) error {
if err := r.loadPersistentData(); err != nil {
return []nagopher.Metric{}, fmt.Errorf("nagopher: unable to load persistent data: %s", err.Error())
return fmt.Errorf("unable to load persistent data: %s", err.Error())
}

metrics, err := r.Resource.Probe(warnings)
if err != nil {
return metrics, err
}
return nil
}

func (r baseResource) Teardown(warnings nagopher.WarningCollection) error {
if err := r.storePersistentData(); err != nil {
return []nagopher.Metric{}, fmt.Errorf("nagopher: unable to store persistent data: %s", err.Error())
return fmt.Errorf("unable to store persistent data: %s", err.Error())
}

return metrics, err
return nil
}

func (r *baseResource) loadPersistentData() (rerr error) {
Expand All @@ -92,7 +96,7 @@ func (r *baseResource) loadPersistentData() (rerr error) {
}

// Attempt to open or create file using SHM
file, err := shm.Open(r.persistenceKey, shmOpenFlags, shmDefaultMode)
file, err := shm.Open(r.persistenceKey, shmReadFlags, shmDefaultMode)
if err != nil {
return err
}
Expand All @@ -113,7 +117,7 @@ func (r *baseResource) loadPersistentData() (rerr error) {

// Attempt to unmarshal contents as JSON into target
if len(jsonData) > 0 {
if err := json.Unmarshal(jsonData, r); err != nil {
if err := json.Unmarshal(jsonData, r.persistenceStore); err != nil {
return err
}
}
Expand All @@ -128,13 +132,13 @@ func (r baseResource) storePersistentData() (rerr error) {
}

// Attempt to marshal source into JSON
jsonData, err := json.Marshal(r)
jsonData, err := json.Marshal(r.persistenceStore)
if err != nil {
return err
}

// Attempt to open or create file using SHM
file, err := shm.Open(r.persistenceKey, shmOpenFlags, shmDefaultMode)
file, err := shm.Open(r.persistenceKey, shmWriteFlags, shmDefaultMode)
if err != nil {
return err
}
Expand Down

0 comments on commit 98a3681

Please sign in to comment.