package xapp
import (
+ "bytes"
"encoding/json"
"fmt"
"io/ioutil"
"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"
-
- "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models"
)
const (
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(GetPortData("http").Port); err == nil {
if err := Util.WriteToFile(baseDir+"metrics.json", metrics); err != nil {
}
//
- if data != nil {
- if b, err := json.MarshalIndent(data, "", " "); err == nil {
- Util.WriteToFile(baseDir+fileName, string(b))
- }
- }
-
+ // Collect currently used config file
//
cfile := viper.ConfigFileUsed()
input, err := ioutil.ReadFile(cfile)
}
//
- Util.WriteToFile(baseDir+"environment", strings.Join(os.Environ(), "\n"))
+ // Collect environment
+ //
+ Util.WriteToFile(baseDir+"environment.txt", strings.Join(os.Environ(), "\n"))
+ //
+ // Collect RMR rt table
//
rtPath := os.Getenv("RMR_STASH_RT")
if rtPath != "" {
}
}
+ //
+ // 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) GetLocalMetrics(port int) (string, error) {
- resp, err := http.Get(fmt.Sprintf("http://localhost:%d/ric/v1/metrics", port))
+ buf := &bytes.Buffer{}
+ enc := expfmt.NewEncoder(buf, expfmt.FmtText)
+ vals, err := prometheus.DefaultGatherer.Gather()
if err != nil {
- Logger.Error("GetLocalMetrics: http.Get failed: %v", err)
- return "", err
+ return fmt.Sprintf("#metrics get error: %s\n", err.Error()), fmt.Errorf("Could get local metrics %w", err)
}
- defer resp.Body.Close()
-
- metrics, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- Logger.Error("GetLocalMetrics: ioutil.ReadAll failed: %v", err)
- return "", 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(metrics), nil
+ return string(buf.Bytes()), nil
}
func IsHealthProbeReady() bool {