2022-01-25 15:37:43 +01:00
|
|
|
package receivers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2022-01-27 17:43:00 +01:00
|
|
|
|
2022-10-10 11:53:11 +02:00
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/pkg/ccMetric"
|
2022-01-25 15:37:43 +01:00
|
|
|
)
|
|
|
|
|
2022-02-22 16:33:38 +01:00
|
|
|
var AvailableReceivers = map[string]func(name string, config json.RawMessage) (Receiver, error){
|
2022-04-19 12:05:03 +02:00
|
|
|
"nats": NewNatsReceiver,
|
|
|
|
"redfish": NewRedfishReceiver,
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type receiveManager struct {
|
|
|
|
inputs []Receiver
|
|
|
|
output chan lp.CCMetric
|
2022-02-21 12:45:08 +01:00
|
|
|
config []json.RawMessage
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReceiveManager interface {
|
|
|
|
Init(wg *sync.WaitGroup, receiverConfigFile string) error
|
2022-02-21 12:45:08 +01:00
|
|
|
AddInput(name string, rawConfig json.RawMessage) error
|
2022-01-25 15:37:43 +01:00
|
|
|
AddOutput(output chan lp.CCMetric)
|
|
|
|
Start()
|
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Init(wg *sync.WaitGroup, receiverConfigFile string) error {
|
2022-02-23 15:58:51 +01:00
|
|
|
// Initialize struct fields
|
2022-01-25 15:37:43 +01:00
|
|
|
rm.inputs = make([]Receiver, 0)
|
|
|
|
rm.output = nil
|
2022-02-21 12:45:08 +01:00
|
|
|
rm.config = make([]json.RawMessage, 0)
|
2022-02-23 15:58:51 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
configFile, err := os.Open(receiverConfigFile)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
jsonParser := json.NewDecoder(configFile)
|
2022-02-21 12:45:08 +01:00
|
|
|
var rawConfigs map[string]json.RawMessage
|
2022-01-25 15:37:43 +01:00
|
|
|
err = jsonParser.Decode(&rawConfigs)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-21 12:45:08 +01:00
|
|
|
for name, raw := range rawConfigs {
|
|
|
|
rm.AddInput(name, raw)
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
2022-02-23 15:58:51 +01:00
|
|
|
|
2022-01-25 15:37:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Start() {
|
2022-04-19 11:53:11 +02:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "START")
|
2022-01-25 15:37:43 +01:00
|
|
|
|
|
|
|
for _, r := range rm.inputs {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "START", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
r.Start()
|
|
|
|
}
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "STARTED")
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-21 12:45:08 +01:00
|
|
|
func (rm *receiveManager) AddInput(name string, rawConfig json.RawMessage) error {
|
|
|
|
var config defaultReceiverConfig
|
2022-01-25 15:37:43 +01:00
|
|
|
err := json.Unmarshal(rawConfig, &config)
|
|
|
|
if err != nil {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", config.Type, "JSON config error:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, found := AvailableReceivers[config.Type]; !found {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", config.Type, "unknown receiver:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-22 16:33:38 +01:00
|
|
|
r, err := AvailableReceivers[config.Type](name, rawConfig)
|
2022-01-25 15:37:43 +01:00
|
|
|
if err != nil {
|
2022-02-23 15:58:51 +01:00
|
|
|
cclog.ComponentError("ReceiveManager", "SKIP", name, "initialization failed:", err.Error())
|
2022-01-25 15:37:43 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
rm.inputs = append(rm.inputs, r)
|
2022-02-21 12:45:08 +01:00
|
|
|
rm.config = append(rm.config, rawConfig)
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "ADD RECEIVER", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) AddOutput(output chan lp.CCMetric) {
|
|
|
|
rm.output = output
|
|
|
|
for _, r := range rm.inputs {
|
|
|
|
r.SetSink(rm.output)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *receiveManager) Close() {
|
2022-04-19 11:53:11 +02:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "CLOSE")
|
|
|
|
|
|
|
|
// Close all receivers
|
2022-01-25 15:37:43 +01:00
|
|
|
for _, r := range rm.inputs {
|
2022-01-25 17:43:10 +01:00
|
|
|
cclog.ComponentDebug("ReceiveManager", "CLOSE", r.Name())
|
2022-01-25 15:37:43 +01:00
|
|
|
r.Close()
|
|
|
|
}
|
2022-04-19 11:53:11 +02:00
|
|
|
|
|
|
|
cclog.ComponentDebug("ReceiveManager", "DONE")
|
2022-01-25 15:37:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(wg *sync.WaitGroup, receiverConfigFile string) (ReceiveManager, error) {
|
2022-04-19 11:53:11 +02:00
|
|
|
r := new(receiveManager)
|
2022-01-25 15:37:43 +01:00
|
|
|
err := r.Init(wg, receiverConfigFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|