Add documentation

This commit is contained in:
Holger Obermaier 2022-01-26 12:31:04 +01:00
parent 3d073080f8
commit c193b80083

View File

@ -6,26 +6,27 @@ import (
"sync" "sync"
"time" "time"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric" lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker" mct "github.com/ClusterCockpit/cc-metric-collector/internal/multiChanTicker"
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
) )
// Map of all available metric collectors
var AvailableCollectors = map[string]MetricCollector{ var AvailableCollectors = map[string]MetricCollector{
"likwid": &LikwidCollector{}, "likwid": new(LikwidCollector),
"loadavg": &LoadavgCollector{}, "loadavg": new(LoadavgCollector),
"memstat": &MemstatCollector{}, "memstat": new(MemstatCollector),
"netstat": &NetstatCollector{}, "netstat": new(NetstatCollector),
"ibstat": &InfinibandCollector{}, "ibstat": new(InfinibandCollector),
"lustrestat": &LustreCollector{}, "lustrestat": new(LustreCollector),
"cpustat": &CpustatCollector{}, "cpustat": new(CpustatCollector),
"topprocs": &TopProcsCollector{}, "topprocs": new(TopProcsCollector),
"nvidia": &NvidiaCollector{}, "nvidia": new(NvidiaCollector),
"customcmd": &CustomCmdCollector{}, "customcmd": new(CustomCmdCollector),
"diskstat": &DiskstatCollector{}, "diskstat": new(DiskstatCollector),
"tempstat": &TempCollector{}, "tempstat": new(TempCollector),
"ipmistat": &IpmiCollector{}, "ipmistat": new(IpmiCollector),
"gpfs": new(GpfsCollector), "gpfs": new(GpfsCollector),
"cpufreq": new(CPUFreqCollector), "cpufreq": new(CPUFreqCollector),
"cpufreq_cpuinfo": new(CPUFreqCpuInfoCollector), "cpufreq_cpuinfo": new(CPUFreqCpuInfoCollector),
@ -34,14 +35,15 @@ var AvailableCollectors = map[string]MetricCollector{
type collectorManager struct { type collectorManager struct {
collectors []MetricCollector collectors []MetricCollector
output chan lp.CCMetric output chan lp.CCMetric // List of all output channels
done chan bool done chan bool // channel to finish / stop metric collector manager
ticker mct.MultiChanTicker ticker mct.MultiChanTicker
duration time.Duration duration time.Duration
wg *sync.WaitGroup wg *sync.WaitGroup
config map[string]json.RawMessage config map[string]json.RawMessage
} }
// Metric collector access functions
type CollectorManager interface { type CollectorManager interface {
Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error
AddOutput(output chan lp.CCMetric) AddOutput(output chan lp.CCMetric)
@ -49,6 +51,13 @@ type CollectorManager interface {
Close() Close()
} }
// Init initializes a new metric collector manager by setting up:
// * output channels
// * done channel
// * wait group synchronization (from variable wg)
// * ticker (from variable ticker)
// * configuration (read from config file in variable collectConfigFile)
// Initialization is done for all configured collectors
func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error { func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) error {
cm.collectors = make([]MetricCollector, 0) cm.collectors = make([]MetricCollector, 0)
cm.output = nil cm.output = nil
@ -56,6 +65,8 @@ func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Durat
cm.wg = wg cm.wg = wg
cm.ticker = ticker cm.ticker = ticker
cm.duration = duration cm.duration = duration
// Read collector config file
configFile, err := os.Open(collectConfigFile) configFile, err := os.Open(collectConfigFile)
if err != nil { if err != nil {
cclog.Error(err.Error()) cclog.Error(err.Error())
@ -68,6 +79,8 @@ func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Durat
cclog.Error(err.Error()) cclog.Error(err.Error())
return err return err
} }
// Initialize configured collectors
for k, cfg := range cm.config { for k, cfg := range cm.config {
if _, found := AvailableCollectors[k]; !found { if _, found := AvailableCollectors[k]; !found {
cclog.ComponentError("CollectorManager", "SKIP unknown collector", k) cclog.ComponentError("CollectorManager", "SKIP unknown collector", k)
@ -86,6 +99,7 @@ func (cm *collectorManager) Init(ticker mct.MultiChanTicker, duration time.Durat
return nil return nil
} }
// Start starts the metric collector manager
func (cm *collectorManager) Start() { func (cm *collectorManager) Start() {
cm.wg.Add(1) cm.wg.Add(1)
tick := make(chan time.Time) tick := make(chan time.Time)
@ -123,15 +137,18 @@ func (cm *collectorManager) Start() {
cclog.ComponentDebug("CollectorManager", "STARTED") cclog.ComponentDebug("CollectorManager", "STARTED")
} }
// AddOutput adds the output channel to the metric collector manager
func (cm *collectorManager) AddOutput(output chan lp.CCMetric) { func (cm *collectorManager) AddOutput(output chan lp.CCMetric) {
cm.output = output cm.output = output
} }
// Close finishes / stops the metric collector manager
func (cm *collectorManager) Close() { func (cm *collectorManager) Close() {
cm.done <- true cm.done <- true
cclog.ComponentDebug("CollectorManager", "CLOSE") cclog.ComponentDebug("CollectorManager", "CLOSE")
} }
// New creates a new initialized metric collector manager
func New(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) { func New(ticker mct.MultiChanTicker, duration time.Duration, wg *sync.WaitGroup, collectConfigFile string) (CollectorManager, error) {
cm := &collectorManager{} cm := &collectorManager{}
err := cm.Init(ticker, duration, wg, collectConfigFile) err := cm.Init(ticker, duration, wg, collectConfigFile)