mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-12-25 23:19:06 +01:00
7840de7b82
* Add cpu_used (all-cpu_idle) to CpustatCollector * Update cc-metric-collector.init * Allow selection of timestamp precision in HttpSink * Add comment about precision requirement for cc-metric-store * Fix for API changes in gofish@v0.15.0 * Update requirements to latest version * Read sensors through redfish * Update golang toolchain to 1.21 * Remove stray error check * Update main config in configuration.md * Update Release action to use golang 1.22 stable release, no golang RPMs anymore * Update runonce action to use golang 1.22 stable release, no golang RPMs anymore * Update README.md Use right JSON type in configuration * Update sink's README * Test whether ipmitool or ipmi-sensors can be executed without errors * Little fixes to the prometheus sink (#115) * Add uint64 to float64 cast option * Add prometheus sink to the list of available sinks * Add aggregated counters by gpu for nvlink errors --------- Co-authored-by: Michael Schwarz <schwarz@uni-paderborn.de> * Ccmessage migration (#119) * Add cpu_used (all-cpu_idle) to CpustatCollector * Update cc-metric-collector.init * Allow selection of timestamp precision in HttpSink * Add comment about precision requirement for cc-metric-store * Fix for API changes in gofish@v0.15.0 * Update requirements to latest version * Read sensors through redfish * Update golang toolchain to 1.21 * Remove stray error check * Update main config in configuration.md * Update Release action to use golang 1.22 stable release, no golang RPMs anymore * Update runonce action to use golang 1.22 stable release, no golang RPMs anymore * Switch to CCMessage for all files. --------- Co-authored-by: Holger Obermaier <Holger.Obermaier@kit.edu> Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Switch to ccmessage also for latest additions in nvidiaMetric * New Message processor (#118) * Add cpu_used (all-cpu_idle) to CpustatCollector * Update cc-metric-collector.init * Allow selection of timestamp precision in HttpSink * Add comment about precision requirement for cc-metric-store * Fix for API changes in gofish@v0.15.0 * Update requirements to latest version * Read sensors through redfish * Update golang toolchain to 1.21 * Remove stray error check * Update main config in configuration.md * Update Release action to use golang 1.22 stable release, no golang RPMs anymore * Update runonce action to use golang 1.22 stable release, no golang RPMs anymore * New message processor to check whether a message should be dropped or manipulate it in flight * Create a copy of message before manipulation --------- Co-authored-by: Holger Obermaier <Holger.Obermaier@kit.edu> Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Update collector's Makefile and go.mod/sum files * Use message processor in router, all sinks and all receivers * Add support for credential file (NKEY) to NATS sink and receiver * Fix JSON keys in message processor configuration * Update docs for message processor, router and the default router config file * Add link to expr syntax and fix regex matching docs * Update sample collectors * Minor style change in collector manager * Some helpers for ccTopology * LIKWID collector: write log owner change only once * Fix for metrics without units and reduce debugging messages for messageProcessor * Use shorted hostname for hostname added by router * Define default port for NATS * CPUstat collector: only add unit for applicable metrics * Add precision option to all sinks using Influx's encoder * Add message processor to all sink documentation * Add units to documentation of cpustat collector --------- Co-authored-by: Holger Obermaier <Holger.Obermaier@kit.edu> Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> Co-authored-by: oscarminus <me@oscarminus.de> Co-authored-by: Michael Schwarz <schwarz@uni-paderborn.de>
243 lines
6.1 KiB
Go
243 lines
6.1 KiB
Go
package receivers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
lp "github.com/ClusterCockpit/cc-energy-manager/pkg/cc-message"
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/pkg/ccLogger"
|
|
mp "github.com/ClusterCockpit/cc-metric-collector/pkg/messageProcessor"
|
|
influx "github.com/influxdata/line-protocol/v2/lineprotocol"
|
|
)
|
|
|
|
const HTTP_RECEIVER_PORT = "8080"
|
|
|
|
type HttpReceiverConfig struct {
|
|
defaultReceiverConfig
|
|
Addr string `json:"address"`
|
|
Port string `json:"port"`
|
|
Path string `json:"path"`
|
|
|
|
// Maximum amount of time to wait for the next request when keep-alives are enabled
|
|
// should be larger than the measurement interval to keep the connection open
|
|
IdleTimeout string `json:"idle_timeout"`
|
|
idleTimeout time.Duration
|
|
|
|
// Controls whether HTTP keep-alives are enabled. By default, keep-alives are enabled
|
|
KeepAlivesEnabled bool `json:"keep_alives_enabled"`
|
|
|
|
// Basic authentication
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
useBasicAuth bool
|
|
}
|
|
|
|
type HttpReceiver struct {
|
|
receiver
|
|
//meta map[string]string
|
|
config HttpReceiverConfig
|
|
server *http.Server
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
|
|
r.name = fmt.Sprintf("HttpReceiver(%s)", name)
|
|
|
|
// Set default values
|
|
r.config.Port = HTTP_RECEIVER_PORT
|
|
r.config.KeepAlivesEnabled = true
|
|
// should be larger than the measurement interval to keep the connection open
|
|
r.config.IdleTimeout = "120s"
|
|
|
|
// Read config
|
|
if len(config) > 0 {
|
|
err := json.Unmarshal(config, &r.config)
|
|
if err != nil {
|
|
cclog.ComponentError(r.name, "Error reading config:", err.Error())
|
|
return err
|
|
}
|
|
}
|
|
if len(r.config.Port) == 0 {
|
|
return errors.New("not all configuration variables set required by HttpReceiver")
|
|
}
|
|
|
|
// Check idle timeout config
|
|
if len(r.config.IdleTimeout) > 0 {
|
|
t, err := time.ParseDuration(r.config.IdleTimeout)
|
|
if err == nil {
|
|
cclog.ComponentDebug(r.name, "idleTimeout", t)
|
|
r.config.idleTimeout = t
|
|
}
|
|
}
|
|
|
|
// Check basic authentication config
|
|
if len(r.config.Username) > 0 || len(r.config.Password) > 0 {
|
|
r.config.useBasicAuth = true
|
|
}
|
|
if r.config.useBasicAuth && len(r.config.Username) == 0 {
|
|
return errors.New("basic authentication requires username")
|
|
}
|
|
if r.config.useBasicAuth && len(r.config.Password) == 0 {
|
|
return errors.New("basic authentication requires password")
|
|
}
|
|
msgp, err := mp.NewMessageProcessor()
|
|
if err != nil {
|
|
return fmt.Errorf("initialization of message processor failed: %v", err.Error())
|
|
}
|
|
r.mp = msgp
|
|
if len(r.config.MessageProcessor) > 0 {
|
|
err = r.mp.FromConfigJSON(r.config.MessageProcessor)
|
|
if err != nil {
|
|
return fmt.Errorf("failed parsing JSON for message processor: %v", err.Error())
|
|
}
|
|
}
|
|
r.mp.AddAddMetaByCondition("true", "source", r.name)
|
|
|
|
//r.meta = map[string]string{"source": r.name}
|
|
p := r.config.Path
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
addr := fmt.Sprintf("%s:%s", r.config.Addr, r.config.Port)
|
|
uri := addr + p
|
|
cclog.ComponentDebug(r.name, "INIT", "listen on:", uri)
|
|
|
|
// Register handler function r.ServerHttp for path p in the DefaultServeMux
|
|
http.HandleFunc(p, r.ServerHttp)
|
|
|
|
// Create http server
|
|
r.server = &http.Server{
|
|
Addr: addr,
|
|
Handler: nil, // handler to invoke, http.DefaultServeMux if nil
|
|
IdleTimeout: r.config.idleTimeout,
|
|
}
|
|
r.server.SetKeepAlivesEnabled(r.config.KeepAlivesEnabled)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *HttpReceiver) Start() {
|
|
cclog.ComponentDebug(r.name, "START")
|
|
r.wg.Add(1)
|
|
go func() {
|
|
err := r.server.ListenAndServe()
|
|
if err != nil && err.Error() != "http: Server closed" {
|
|
cclog.ComponentError(r.name, err.Error())
|
|
}
|
|
r.wg.Done()
|
|
}()
|
|
}
|
|
|
|
func (r *HttpReceiver) ServerHttp(w http.ResponseWriter, req *http.Request) {
|
|
|
|
// Check request method, only post method is handled
|
|
if req.Method != http.MethodPost {
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
// Check basic authentication
|
|
if r.config.useBasicAuth {
|
|
username, password, ok := req.BasicAuth()
|
|
if !ok || username != r.config.Username || password != r.config.Password {
|
|
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
}
|
|
if r.sink != nil {
|
|
d := influx.NewDecoder(req.Body)
|
|
for d.Next() {
|
|
|
|
// Decode measurement name
|
|
measurement, err := d.Measurement()
|
|
if err != nil {
|
|
msg := "ServerHttp: Failed to decode measurement: " + err.Error()
|
|
cclog.ComponentError(r.name, msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Decode tags
|
|
tags := make(map[string]string)
|
|
for {
|
|
key, value, err := d.NextTag()
|
|
if err != nil {
|
|
msg := "ServerHttp: Failed to decode tag: " + err.Error()
|
|
cclog.ComponentError(r.name, msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if key == nil {
|
|
break
|
|
}
|
|
tags[string(key)] = string(value)
|
|
}
|
|
|
|
// Decode fields
|
|
fields := make(map[string]interface{})
|
|
for {
|
|
key, value, err := d.NextField()
|
|
if err != nil {
|
|
msg := "ServerHttp: Failed to decode field: " + err.Error()
|
|
cclog.ComponentError(r.name, msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if key == nil {
|
|
break
|
|
}
|
|
fields[string(key)] = value.Interface()
|
|
}
|
|
|
|
// Decode time stamp
|
|
t, err := d.Time(influx.Nanosecond, time.Time{})
|
|
if err != nil {
|
|
msg := "ServerHttp: Failed to decode time stamp: " + err.Error()
|
|
cclog.ComponentError(r.name, msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
y, _ := lp.NewMessage(
|
|
string(measurement),
|
|
tags,
|
|
nil,
|
|
fields,
|
|
t,
|
|
)
|
|
|
|
m, err := r.mp.ProcessMessage(y)
|
|
if err == nil && m != nil {
|
|
r.sink <- m
|
|
}
|
|
|
|
}
|
|
// Check for IO errors
|
|
err := d.Err()
|
|
if err != nil {
|
|
msg := "ServerHttp: Failed to decode: " + err.Error()
|
|
cclog.ComponentError(r.name, msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
func (r *HttpReceiver) Close() {
|
|
r.server.Shutdown(context.Background())
|
|
}
|
|
|
|
func NewHttpReceiver(name string, config json.RawMessage) (Receiver, error) {
|
|
r := new(HttpReceiver)
|
|
err := r.Init(name, config)
|
|
return r, err
|
|
}
|