mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-26 15:29:04 +01:00
Merge pull request #1 from ClusterCockpit/dev-jan
Go Modules and further changes
This commit is contained in:
commit
63cdc060c7
@ -4,15 +4,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
// "context"
|
|
||||||
|
// "context"
|
||||||
|
"encoding/json"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"sort"
|
|
||||||
"path/filepath"
|
|
||||||
"encoding/json"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type GlobalConfig struct {
|
type GlobalConfig struct {
|
||||||
Sink struct {
|
Sink struct {
|
||||||
User string `json:"user"`
|
User string `json:"user"`
|
||||||
@ -34,7 +34,7 @@ type GlobalConfig struct {
|
|||||||
Duration int `json:"duration"`
|
Duration int `json:"duration"`
|
||||||
} `json:"node"`
|
} `json:"node"`
|
||||||
} `json:"schedule"`
|
} `json:"schedule"`
|
||||||
Metrics string `json:"metrics"`
|
Metrics []string `json:"metrics"`
|
||||||
CollectorPath string `json:"collector_path"`
|
CollectorPath string `json:"collector_path"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ type CollectorConfig struct {
|
|||||||
Args string `json:"arguments"`
|
Args string `json:"arguments"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadGlobalConfiguration(file string, config* GlobalConfig) error {
|
func LoadGlobalConfiguration(file string, config *GlobalConfig) error {
|
||||||
configFile, err := os.Open(file)
|
configFile, err := os.Open(file)
|
||||||
defer configFile.Close()
|
defer configFile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -54,7 +54,7 @@ func LoadGlobalConfiguration(file string, config* GlobalConfig) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadCollectorConfiguration(file string, config* CollectorConfig) error {
|
func LoadCollectorConfiguration(file string, config *CollectorConfig) error {
|
||||||
configFile, err := os.Open(file)
|
configFile, err := os.Open(file)
|
||||||
defer configFile.Close()
|
defer configFile.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -102,7 +102,7 @@ func CreatePoint(metricname string, tags map[string]string, fields map[string]in
|
|||||||
taglist := SortStringStringMap(tags)
|
taglist := SortStringStringMap(tags)
|
||||||
fieldlist := SortStringInterfaceMap(fields)
|
fieldlist := SortStringInterfaceMap(fields)
|
||||||
|
|
||||||
if (len(taglist) > 0) {
|
if len(taglist) > 0 {
|
||||||
fmt.Fprintf(&s, "%s,%s %s %d", metricname, strings.Join(taglist, ","), strings.Join(fieldlist, ","), timestamp)
|
fmt.Fprintf(&s, "%s,%s %s %d", metricname, strings.Join(taglist, ","), strings.Join(fieldlist, ","), timestamp)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(&s, "%s %s %d", metricname, strings.Join(fieldlist, ","), timestamp)
|
fmt.Fprintf(&s, "%s %s %d", metricname, strings.Join(fieldlist, ","), timestamp)
|
||||||
@ -137,7 +137,7 @@ func GetSingleCollector(folders *[]string) filepath.WalkFunc {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if (info.IsDir()) {
|
if info.IsDir() {
|
||||||
configfile := filepath.Join(path, "config.json")
|
configfile := filepath.Join(path, "config.json")
|
||||||
if _, err := os.Stat(configfile); err == nil {
|
if _, err := os.Stat(configfile); err == nil {
|
||||||
*folders = append(*folders, path)
|
*folders = append(*folders, path)
|
||||||
@ -157,30 +157,29 @@ func GetCollectorFolders(root string, folders *[]string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// fmt.Println("Hello")
|
// fmt.Println("Hello")
|
||||||
// cmd_opts := []string{"la","le","lu"}
|
// cmd_opts := []string{"la","le","lu"}
|
||||||
// cmd := "echo"
|
// cmd := "echo"
|
||||||
// s := run_cmd(cmd, cmd_opts)
|
// s := run_cmd(cmd, cmd_opts)
|
||||||
// fmt.Println(s)
|
// fmt.Println(s)
|
||||||
// tags := map[string]string {
|
// tags := map[string]string {
|
||||||
// "host" : "broadep2",
|
// "host" : "broadep2",
|
||||||
// }
|
// }
|
||||||
// fields := map[string]interface{} {
|
// fields := map[string]interface{} {
|
||||||
// "value" : float64(1.0),
|
// "value" : float64(1.0),
|
||||||
// }
|
// }
|
||||||
// fmt.Println(CreatePoint("flops_any", tags, fields, time.Now().UnixNano()))
|
// fmt.Println(CreatePoint("flops_any", tags, fields, time.Now().UnixNano()))
|
||||||
var config GlobalConfig
|
var config GlobalConfig
|
||||||
LoadGlobalConfiguration("config.json", &config)
|
LoadGlobalConfiguration("config.json", &config)
|
||||||
// fmt.Println(config)
|
|
||||||
var folders []string
|
var folders []string
|
||||||
GetCollectorFolders(config.CollectorPath, &folders)
|
GetCollectorFolders(config.CollectorPath, &folders)
|
||||||
// fmt.Println(folders)
|
|
||||||
for _, path := range folders {
|
for _, path := range folders {
|
||||||
var col_config CollectorConfig
|
var col_config CollectorConfig
|
||||||
configfile := filepath.Join(path, "config.json")
|
LoadCollectorConfiguration(filepath.Join(path, "config.json"), &col_config)
|
||||||
LoadCollectorConfiguration(configfile, &col_config)
|
stdout := run_cmd(filepath.Join(path, col_config.Command), col_config.Args)
|
||||||
cmd := filepath.Join(path, col_config.Command)
|
|
||||||
stdout := run_cmd(cmd, col_config.Args)
|
|
||||||
metrics := strings.Split(stdout, "\n")
|
metrics := strings.Split(stdout, "\n")
|
||||||
for _, m := range metrics {
|
for _, m := range metrics {
|
||||||
if len(m) > 0 {
|
if len(m) > 0 {
|
||||||
@ -193,7 +192,5 @@ func main() {
|
|||||||
fmt.Println("SEND", m)
|
fmt.Println("SEND", m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
64
collectors/likwid.go
Normal file
64
collectors/likwid.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package collectors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
protocol "github.com/influxdata/line-protocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
type LikwidCollector struct {
|
||||||
|
name string
|
||||||
|
tags []*protocol.Tag
|
||||||
|
fields []*protocol.Field
|
||||||
|
t time.Time
|
||||||
|
encoder *protocol.Encoder
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LikwidCollector) Name() string {
|
||||||
|
return c.name
|
||||||
|
}
|
||||||
|
func (c *LikwidCollector) TagList() []*protocol.Tag {
|
||||||
|
return c.tags
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LikwidCollector) FieldList() []*protocol.Field {
|
||||||
|
return c.fields
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LikwidCollector) Time() time.Time {
|
||||||
|
return c.t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LikwidCollector) New() {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
c.encoder = protocol.NewEncoder(buf)
|
||||||
|
c.encoder.SetMaxLineBytes(1024)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *LikwidCollector) Start(
|
||||||
|
level string,
|
||||||
|
frequency time.Duration,
|
||||||
|
duration int) {
|
||||||
|
ticker := time.NewTicker(frequency * time.Second)
|
||||||
|
done := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case t := <-ticker.C:
|
||||||
|
fmt.Println("Tick at", t)
|
||||||
|
|
||||||
|
c.encoder.Encode(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(1600 * time.Second)
|
||||||
|
ticker.Stop()
|
||||||
|
done <- true
|
||||||
|
fmt.Println("Ticker stopped")
|
||||||
|
}
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module github.com/ClusterCockpit/cc-metric-collector
|
||||||
|
|
||||||
|
go 1.16
|
||||||
|
|
||||||
|
require github.com/influxdata/line-protocol v0.0.0-20210311194329-9aa0e372d097
|
Loading…
Reference in New Issue
Block a user