Skip to content

Commit

Permalink
Merge pull request #138 from mehdy/fix-135
Browse files Browse the repository at this point in the history
fix: send signal for before getting script
  • Loading branch information
clwluvw authored Aug 3, 2023
2 parents 00d00e2 + 2dc8d12 commit 558171b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 36 deletions.
5 changes: 5 additions & 0 deletions internal/collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
)

type Collector interface {
Refresh() error
ScriptVrrps() ([]VRRPScript, error)
DataVrrps() (map[string]*VRRPData, error)
StatsVrrps() (map[string]*VRRPStats, error)
Expand Down Expand Up @@ -218,6 +219,10 @@ func (k *KeepalivedCollector) getKeepalivedStats() (*KeepalivedStats, error) {

var err error

if err := k.collector.Refresh(); err != nil {
return nil, err
}

if k.useJSON {
stats.VRRPs, err = k.collector.JSONVrrps()
if err != nil {
Expand Down
44 changes: 26 additions & 18 deletions internal/types/container/keepalived_container_collector_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ func NewKeepalivedContainerCollectorHost(useJSON bool, containerName, containerT
return k
}

func (k *KeepalivedContainerCollectorHost) Refresh() error {
if k.useJSON {
if err := k.signal(k.SIGJSON); err != nil {
logrus.WithError(err).Error("Failed to send JSON signal to keepalived")

return err
}

return nil
}

if err := k.signal(k.SIGSTATS); err != nil {
logrus.WithError(err).Error("Failed to send STATS signal to keepalived")

return err
}

if err := k.signal(k.SIGDATA); err != nil {
logrus.WithError(err).Error("Failed to send DATA signal to keepalived")

return err
}

return nil
}

func (k *KeepalivedContainerCollectorHost) initPaths(containerTmpDir string) {
k.jsonPath = filepath.Join(containerTmpDir, "keepalived.json")
k.statsPath = filepath.Join(containerTmpDir, "keepalived.stats")
Expand Down Expand Up @@ -121,12 +147,6 @@ func (k *KeepalivedContainerCollectorHost) signal(signal syscall.Signal) error {

// JSONVrrps send SIGJSON and parse the data to the list of collector.VRRP struct.
func (k *KeepalivedContainerCollectorHost) JSONVrrps() ([]collector.VRRP, error) {
if err := k.signal(k.SIGJSON); err != nil {
logrus.WithError(err).Error("Failed to send JSON signal to keepalived")

return nil, err
}

f, err := os.Open(k.jsonPath)
if err != nil {
logrus.WithError(err).WithField("path", k.jsonPath).Error("Failed to open keepalived.json")
Expand All @@ -140,12 +160,6 @@ func (k *KeepalivedContainerCollectorHost) JSONVrrps() ([]collector.VRRP, error)

// StatsVrrps send SIGSTATS and parse the stats.
func (k *KeepalivedContainerCollectorHost) StatsVrrps() (map[string]*collector.VRRPStats, error) {
if err := k.signal(k.SIGSTATS); err != nil {
logrus.WithError(err).Error("Failed to send STATS signal to keepalived")

return nil, err
}

f, err := os.Open(k.statsPath)
if err != nil {
logrus.WithError(err).WithField("path", k.statsPath).Error("Failed to open keepalived.stats")
Expand All @@ -159,12 +173,6 @@ func (k *KeepalivedContainerCollectorHost) StatsVrrps() (map[string]*collector.V

// DataVrrps send SIGDATA ans parse the data.
func (k *KeepalivedContainerCollectorHost) DataVrrps() (map[string]*collector.VRRPData, error) {
if err := k.signal(k.SIGDATA); err != nil {
logrus.WithError(err).Error("Failed to send DATA signal to keepalived")

return nil, err
}

f, err := os.Open(k.dataPath)
if err != nil {
logrus.WithError(err).WithField("path", k.dataPath).Error("Failed to open keepalived.data")
Expand Down
44 changes: 26 additions & 18 deletions internal/types/host/keepalived_host_collector_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ func NewKeepalivedHostCollectorHost(useJSON bool, pidPath string) *KeepalivedHos
return k
}

func (k *KeepalivedHostCollectorHost) Refresh() error {
if k.useJSON {
if err := k.signal(k.SIGJSON); err != nil {
logrus.WithError(err).Error("Failed to send JSON signal to keepalived")

return err
}

return nil
}

if err := k.signal(k.SIGSTATS); err != nil {
logrus.WithError(err).Error("Failed to send STATS signal to keepalived")

return err
}

if err := k.signal(k.SIGDATA); err != nil {
logrus.WithError(err).Error("Failed to send DATA signal to keepalived")

return err
}

return nil
}

func (k *KeepalivedHostCollectorHost) initSignals() {
if k.useJSON {
k.SIGJSON = k.sigNum("JSON")
Expand Down Expand Up @@ -123,12 +149,6 @@ func (k *KeepalivedHostCollectorHost) sigNum(sigString string) syscall.Signal {
}

func (k *KeepalivedHostCollectorHost) JSONVrrps() ([]collector.VRRP, error) {
if err := k.signal(k.SIGJSON); err != nil {
logrus.WithError(err).Error("Failed to send JSON signal to keepalived")

return nil, err
}

const fileName = "/tmp/keepalived.json"

f, err := os.Open(fileName)
Expand All @@ -143,12 +163,6 @@ func (k *KeepalivedHostCollectorHost) JSONVrrps() ([]collector.VRRP, error) {
}

func (k *KeepalivedHostCollectorHost) StatsVrrps() (map[string]*collector.VRRPStats, error) {
if err := k.signal(k.SIGSTATS); err != nil {
logrus.WithError(err).Error("Failed to send STATS signal to keepalived")

return nil, err
}

const fileName = "/tmp/keepalived.stats"

f, err := os.Open(fileName)
Expand All @@ -163,12 +177,6 @@ func (k *KeepalivedHostCollectorHost) StatsVrrps() (map[string]*collector.VRRPSt
}

func (k *KeepalivedHostCollectorHost) DataVrrps() (map[string]*collector.VRRPData, error) {
if err := k.signal(k.SIGDATA); err != nil {
logrus.WithError(err).Error("Failed to send DATA signal to keepalived")

return nil, err
}

const fileName = "/tmp/keepalived.data"

f, err := os.Open(fileName)
Expand Down

0 comments on commit 558171b

Please sign in to comment.