cc-metric-collector/clusterdaemon.go

197 lines
4.6 KiB
Go
Raw Normal View History

2021-03-17 18:00:09 +01:00
package main
import (
2021-03-18 08:06:57 +01:00
"fmt"
"os"
"os/exec"
// "context"
"encoding/json"
"path/filepath"
"sort"
"strings"
"time"
2021-03-17 18:00:09 +01:00
)
type GlobalConfig struct {
2021-03-18 08:06:57 +01:00
Sink struct {
User string `json:"user"`
Password string `json:"password"`
} `json:"sink"`
Host string `json:"host"`
Port string `json:"port"`
Report struct {
Levels string `json:"levels"`
Interval int `json:"interval"`
} `json:"report"`
Schedule struct {
Core struct {
Frequency int `json:"frequency"`
Duration int `json:"duration"`
} `json:"core"`
Node struct {
Frequency int `json:"frequency"`
Duration int `json:"duration"`
} `json:"node"`
} `json:"schedule"`
Metrics []string `json:"metrics"`
CollectorPath string `json:"collector_path"`
2021-03-17 18:00:09 +01:00
}
type CollectorConfig struct {
2021-03-18 08:06:57 +01:00
Command string `json:"command"`
Args string `json:"arguments"`
2021-03-17 18:00:09 +01:00
}
2021-03-18 08:06:57 +01:00
func LoadGlobalConfiguration(file string, config *GlobalConfig) error {
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(config)
return err
2021-03-17 18:00:09 +01:00
}
2021-03-18 08:06:57 +01:00
func LoadCollectorConfiguration(file string, config *CollectorConfig) error {
configFile, err := os.Open(file)
defer configFile.Close()
if err != nil {
fmt.Println(err.Error())
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(config)
return err
2021-03-17 18:00:09 +01:00
}
func SortStringStringMap(input map[string]string) []string {
2021-03-18 08:06:57 +01:00
keys := make([]string, 0, len(input))
output := make([]string, len(input))
for k := range input {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
var s strings.Builder
fmt.Fprintf(&s, "%s=%s", k, string(input[k]))
output[i] = s.String()
}
return output
2021-03-17 18:00:09 +01:00
}
func SortStringInterfaceMap(input map[string]interface{}) []string {
2021-03-18 08:06:57 +01:00
keys := make([]string, 0, len(input))
output := make([]string, len(input))
for k := range input {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
var s strings.Builder
fmt.Fprintf(&s, "%s=%v", k, input[k])
output[i] = s.String()
}
return output
2021-03-17 18:00:09 +01:00
}
func CreatePoint(metricname string, tags map[string]string, fields map[string]interface{}, timestamp int64) string {
2021-03-18 08:06:57 +01:00
var s strings.Builder
taglist := SortStringStringMap(tags)
fieldlist := SortStringInterfaceMap(fields)
if len(taglist) > 0 {
fmt.Fprintf(&s, "%s,%s %s %d", metricname, strings.Join(taglist, ","), strings.Join(fieldlist, ","), timestamp)
} else {
fmt.Fprintf(&s, "%s %s %d", metricname, strings.Join(fieldlist, ","), timestamp)
}
return s.String()
2021-03-17 18:00:09 +01:00
}
func run_cmd(cmd string, cmd_opts string) string {
2021-03-18 08:06:57 +01:00
//ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
//defer cancel()
//command := exec.CommandContext(ctx, cmd, strings.Join(cmd_opts, " "))
command := exec.Command(cmd, cmd_opts)
command.Wait()
//select {
// case <-time.After(101 * time.Second):
2021-03-17 18:00:09 +01:00
// fmt.Println("overslept")
// case <-ctx.Done():
// fmt.Println(ctx.Err()) // prints "context deadline exceeded"
//}
2021-03-18 08:06:57 +01:00
stdout, err := command.Output()
2021-03-17 18:00:09 +01:00
2021-03-18 08:06:57 +01:00
if err != nil {
fmt.Println(err.Error())
return ""
}
return (string(stdout))
2021-03-17 18:00:09 +01:00
}
func GetSingleCollector(folders *[]string) filepath.WalkFunc {
2021-03-18 08:06:57 +01:00
return func(path string, info os.FileInfo, err error) error {
if err != nil {
panic(err)
}
if info.IsDir() {
configfile := filepath.Join(path, "config.json")
if _, err := os.Stat(configfile); err == nil {
*folders = append(*folders, path)
}
}
return nil
}
2021-03-17 18:00:09 +01:00
}
func GetCollectorFolders(root string, folders *[]string) error {
2021-03-18 08:06:57 +01:00
err := filepath.Walk(root, GetSingleCollector(folders))
if err != nil {
panic(err)
}
return err
2021-03-17 18:00:09 +01:00
}
func main() {
2021-03-18 08:06:57 +01:00
// fmt.Println("Hello")
// cmd_opts := []string{"la","le","lu"}
// cmd := "echo"
// s := run_cmd(cmd, cmd_opts)
// fmt.Println(s)
// tags := map[string]string {
// "host" : "broadep2",
// }
// fields := map[string]interface{} {
// "value" : float64(1.0),
// }
// fmt.Println(CreatePoint("flops_any", tags, fields, time.Now().UnixNano()))
var config GlobalConfig
LoadGlobalConfiguration("config.json", &config)
var folders []string
GetCollectorFolders(config.CollectorPath, &folders)
for _, path := range folders {
var col_config CollectorConfig
LoadCollectorConfiguration(filepath.Join(path, "config.json"), &col_config)
stdout := run_cmd(filepath.Join(path, col_config.Command), col_config.Args)
metrics := strings.Split(stdout, "\n")
for _, m := range metrics {
if len(m) > 0 {
t := strings.Fields(m)
if len(t) == 2 {
var s strings.Builder
fmt.Fprintf(&s, "%s %d", m, time.Now().UnixNano())
m = s.String()
}
fmt.Println("SEND", m)
}
}
}
2021-03-17 18:00:09 +01:00
}