mirror of
https://github.com/ClusterCockpit/cc-metric-collector.git
synced 2024-11-10 04:27:25 +01:00
3f76947f54
* Update configuration.md Add an additional receiver to have better alignment of components * Change default GpfsCollector command to `mmpmon` (#53) * Set default cmd to 'mmpmon' * Reuse looked up path * Cast const to string * Just download LIKWID to get the headers (#54) * Just download LIKWID to get the headers * Remove perl-Data-Dumper from BuildRequires, only required by LIKWID build * Add HttpReceiver as counterpart to the HttpSink (#49) * Use GBytes as unit for large memory numbers * Make maxForward configurable, save old name in meta in rename metrics and make the hostname tag key configurable * Single release action (#55) Building all RPMs and releasing in a single workflow * Makefile target to build binary-only Debian packages (#61) * Add 'install' and 'DEB' make targets to build binary-only Debian packages * Add control file for DEB builds * Use a single line for bash loop in make clean * Add config options for retry intervals of InfluxDB clients (#59) * Refactoring of LikwidCollector and metric units (#62) * Reduce complexity of LikwidCollector and allow metric units * Add unit to LikwidCollector docu and fix some typos * Make library path configurable * Use old metric name in Ganglia if rename has happened in the router (#60) * Use old metric name if rename has happened in the router * Also check for Ganglia renames for the oldname * Derived metrics (#57) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Simplified iota usage * Move unit tag to meta data tags * Derived metrics (#65) * Add time-based derivatived (e.g. bandwidth) to some collectors * Add documentation * Add comments * Fix: Only compute rates with a valid previous state * Only compute rates with a valid previous state * Define const values for net/dev fields * Set default config values * Add comments * Refactor: Consolidate data structures * Refactor: Consolidate data structures * Refactor: Avoid struct deep copy * Refactor: Avoid redundant tag maps * Refactor: Use int64 type for absolut values * Update LustreCollector Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com> * Meta to tags list and map for sinks (#63) * Change ccMetric->Influx functions * Use a meta_as_tags string list in config but create a lookup map afterwards * Add meta as tag logic to sampleSink * Fix staticcheck warnings (#66) Co-authored-by: Holger Obermaier <40787752+ho-ob@users.noreply.github.com>
119 lines
2.7 KiB
Go
119 lines
2.7 KiB
Go
package receivers
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
|
|
cclog "github.com/ClusterCockpit/cc-metric-collector/internal/ccLogger"
|
|
lp "github.com/ClusterCockpit/cc-metric-collector/internal/ccMetric"
|
|
"github.com/gorilla/mux"
|
|
influx "github.com/influxdata/line-protocol"
|
|
)
|
|
|
|
const HTTP_RECEIVER_PORT = "8080"
|
|
|
|
type HttpReceiverConfig struct {
|
|
Type string `json:"type"`
|
|
Addr string `json:"address"`
|
|
Port string `json:"port"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
type HttpReceiver struct {
|
|
receiver
|
|
handler *influx.MetricHandler
|
|
parser *influx.Parser
|
|
meta map[string]string
|
|
config HttpReceiverConfig
|
|
router *mux.Router
|
|
server *http.Server
|
|
wg sync.WaitGroup
|
|
}
|
|
|
|
func (r *HttpReceiver) Init(name string, config json.RawMessage) error {
|
|
r.name = fmt.Sprintf("HttpReceiver(%s)", name)
|
|
r.config.Port = HTTP_RECEIVER_PORT
|
|
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")
|
|
}
|
|
r.meta = map[string]string{"source": r.name}
|
|
p := r.config.Path
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
uri := fmt.Sprintf("%s:%s%s", r.config.Addr, r.config.Port, p)
|
|
cclog.ComponentDebug(r.name, "INIT", uri)
|
|
r.handler = influx.NewMetricHandler()
|
|
r.parser = influx.NewParser(r.handler)
|
|
r.parser.SetTimeFunc(DefaultTime)
|
|
|
|
r.router = mux.NewRouter()
|
|
r.router.Path(p).HandlerFunc(r.ServerHttp)
|
|
r.server = &http.Server{Addr: uri, Handler: r.router}
|
|
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) {
|
|
if req.Method != http.MethodPost {
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(req.Body)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
metrics, err := r.parser.Parse(body)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
}
|
|
|
|
for _, m := range metrics {
|
|
y := lp.FromInfluxMetric(m)
|
|
for k, v := range r.meta {
|
|
y.AddMeta(k, v)
|
|
}
|
|
if r.sink != nil {
|
|
r.sink <- y
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|