X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=pkg%2Fxapp%2Frestapi.go;h=54e18ea6d00d17400e31ecf67a83db86ae2433ba;hb=refs%2Fchanges%2F12%2F8912%2F1;hp=40ad8d24d1f098b58fffa98f6c654cfa70063d4a;hpb=0ce03386e6dbdb82b88406b22bb91c8cbe18e349;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 40ad8d2..54e18ea 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -20,15 +20,21 @@ package xapp import ( + "bytes" "encoding/json" "fmt" - "github.com/gorilla/mux" - "github.com/spf13/viper" "io/ioutil" "net/http" "os" + "path" + "strings" + "time" "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" + "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/expfmt" + "github.com/spf13/viper" ) const ( @@ -66,7 +72,7 @@ func NewRouter() *Router { func (r *Router) serviceChecker(inner http.HandlerFunc) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - Logger.Info("restapi: method=%s url=%s", req.Method, req.URL.RequestURI()) + Logger.Debug("restapi: method=%s url=%s", req.Method, req.URL.RequestURI()) if req.URL.RequestURI() == AliveURL || r.CheckStatus() { inner.ServeHTTP(w, req) } else { @@ -106,6 +112,8 @@ func (r *Router) GetSymptomDataParams(w http.ResponseWriter, req *http.Request) params := SymptomDataParams{} queryParams := req.URL.Query() + Logger.Info("GetSymptomDataParams: %+v", queryParams) + for p := range queryParams { if p == "timeout" { fmt.Sscanf(p, "%d", ¶ms.Timeout) @@ -120,35 +128,106 @@ func (r *Router) GetSymptomDataParams(w http.ResponseWriter, req *http.Request) return params } +func (r *Router) CollectDefaultSymptomData(fileName string, data interface{}) string { + baseDir := Config.GetString("controls.symptomdata.baseDir") + if baseDir == "" { + baseDir = "/tmp/xapp/" + } + + if err := Util.CreateDir(baseDir); err != nil { + Logger.Error("CreateDir failed: %v", err) + return "" + } + + // + // Collect some general information into one file + // + var lines []string + + // uptime + d := XappUpTime() + h := d / time.Hour + d -= h * time.Hour + m := d / time.Minute + d -= m * time.Minute + s := d / time.Second + lines = append(lines, fmt.Sprintf("uptime: %02d:%02d:%02d", h, m, s)) + + Util.WriteToFile(baseDir+"information.txt", strings.Join(lines, "\n")) + + // + // Collect metrics + // + if metrics, err := r.GetLocalMetrics(); err == nil { + if err := Util.WriteToFile(baseDir+"metrics.json", metrics); err != nil { + Logger.Error("writeToFile failed for metrics.json: %v", err) + } + } + + // + // Collect currently used config file + // + cfile := viper.ConfigFileUsed() + input, err := ioutil.ReadFile(cfile) + if err == nil { + Util.WriteToFile(baseDir+path.Base(cfile), string(input)) + } else { + Logger.Error("ioutil.ReadFile failed: %v", err) + } + + // + // Collect environment + // + Util.WriteToFile(baseDir+"environment.txt", strings.Join(os.Environ(), "\n")) + + // + // Collect RMR rt table + // + rtPath := os.Getenv("RMR_STASH_RT") + if rtPath != "" { + input, err = ioutil.ReadFile(rtPath) + if err == nil { + Util.WriteToFile(baseDir+"rttable.txt", string(input)) + } else { + Logger.Error("ioutil.ReadFile failed: %v", err) + } + } + + // + // Put data that was provided as argument + // + if data != nil { + if b, err := json.MarshalIndent(data, "", " "); err == nil { + Util.WriteToFile(baseDir+fileName, string(b)) + } + } + + return baseDir +} + func (r *Router) SendSymptomDataJson(w http.ResponseWriter, req *http.Request, data interface{}, n string) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Disposition", "attachment; filename="+n) w.WriteHeader(http.StatusOK) if data != nil { - response, _ := json.Marshal(data) + response, _ := json.MarshalIndent(data, "", " ") w.Write(response) } } func (r *Router) SendSymptomDataFile(w http.ResponseWriter, req *http.Request, baseDir, zipFile string) { - // Compress and reply with attachment - tmpFile, err := ioutil.TempFile("", "symptom") - if err != nil { - r.SendSymptomDataError(w, req, "Failed to create a tmp file: "+err.Error()) - return - } - defer os.Remove(tmpFile.Name()) var fileList []string fileList = Util.FetchFiles(baseDir, fileList) - err = Util.ZipFiles(tmpFile, baseDir, fileList) + tmpFileName, err := Util.ZipFilesToTmpFile(baseDir, "symptom", fileList) if err != nil { - r.SendSymptomDataError(w, req, "Failed to zip the files: "+err.Error()) + r.SendSymptomDataError(w, req, err.Error()) return } + defer os.Remove(tmpFileName) w.Header().Set("Content-Disposition", "attachment; filename="+zipFile) - http.ServeFile(w, req, tmpFile.Name()) + http.ServeFile(w, req, tmpFileName) } func (r *Router) SendSymptomDataError(w http.ResponseWriter, req *http.Request, message string) { @@ -156,6 +235,30 @@ func (r *Router) SendSymptomDataError(w http.ResponseWriter, req *http.Request, http.Error(w, message, http.StatusInternalServerError) } +func (r *Router) GetLocalMetrics() (string, error) { + buf := &bytes.Buffer{} + enc := expfmt.NewEncoder(buf, expfmt.FmtText) + vals, err := prometheus.DefaultGatherer.Gather() + if err != nil { + return fmt.Sprintf("#metrics get error: %s\n", err.Error()), fmt.Errorf("Could get local metrics %w", err) + } + for _, val := range vals { + err = enc.Encode(val) + if err != nil { + buf.WriteString(fmt.Sprintf("#metrics enc err:%s\n", err.Error())) + } + } + return string(buf.Bytes()), nil +} + +//Resource.InjectRoute(url, metricsHandler, "GET") +//func metricsHandler(w http.ResponseWriter, r *http.Request) { +// w.Header().Set("Content-Type", "text/plain") +// w.WriteHeader(http.StatusOK) +// metrics, _ := Resource.GetLocalMetrics() +// w.Write([]byte(metrics)) +//} + func IsHealthProbeReady() bool { return healthReady }