Fix log endpoint

This commit is contained in:
2026-02-16 09:10:08 +01:00
parent d2ff4a2e02
commit 25ff094bdf
2 changed files with 10 additions and 11 deletions

View File

@@ -12,12 +12,12 @@ import (
"os/exec" "os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"github.com/ClusterCockpit/cc-backend/internal/config" "github.com/ClusterCockpit/cc-backend/internal/config"
"github.com/ClusterCockpit/cc-backend/internal/repository" "github.com/ClusterCockpit/cc-backend/internal/repository"
cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger" cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
"github.com/ClusterCockpit/cc-lib/v2/schema" "github.com/ClusterCockpit/cc-lib/v2/schema"
"github.com/gorilla/mux"
) )
type LogEntry struct { type LogEntry struct {
@@ -60,15 +60,15 @@ func (api *RestAPI) getJournalLog(rw http.ResponseWriter, r *http.Request) {
unit := config.Keys.SystemdUnit unit := config.Keys.SystemdUnit
if unit == "" { if unit == "" {
unit = "clustercockpit" unit = "clustercockpit.service"
} }
args := []string{ args := []string{
"--output=json", "--output=json",
"--no-pager", "--no-pager",
fmt.Sprintf("-n %d", lines), "-n", fmt.Sprintf("%d", lines),
fmt.Sprintf("--since=%s", since), "--since", since,
fmt.Sprintf("-u %s", unit), "-u", unit,
} }
if level := r.URL.Query().Get("level"); level != "" { if level := r.URL.Query().Get("level"); level != "" {
@@ -77,7 +77,7 @@ func (api *RestAPI) getJournalLog(rw http.ResponseWriter, r *http.Request) {
handleError(fmt.Errorf("invalid 'level' parameter (must be 0-7)"), http.StatusBadRequest, rw) handleError(fmt.Errorf("invalid 'level' parameter (must be 0-7)"), http.StatusBadRequest, rw)
return return
} }
args = append(args, fmt.Sprintf("--priority=%d", n)) args = append(args, "--priority", fmt.Sprintf("%d", n))
} }
if search := r.URL.Query().Get("search"); search != "" { if search := r.URL.Query().Get("search"); search != "" {
@@ -85,9 +85,10 @@ func (api *RestAPI) getJournalLog(rw http.ResponseWriter, r *http.Request) {
handleError(fmt.Errorf("invalid 'search' parameter"), http.StatusBadRequest, rw) handleError(fmt.Errorf("invalid 'search' parameter"), http.StatusBadRequest, rw)
return return
} }
args = append(args, fmt.Sprintf("--grep=%s", search)) args = append(args, "--grep", search)
} }
cclog.Debugf("calling journalctl with %s", strings.Join(args, " "))
cmd := exec.CommandContext(r.Context(), "journalctl", args...) cmd := exec.CommandContext(r.Context(), "journalctl", args...)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@@ -105,6 +106,7 @@ func (api *RestAPI) getJournalLog(rw http.ResponseWriter, r *http.Request) {
for scanner.Scan() { for scanner.Scan() {
var raw map[string]any var raw map[string]any
if err := json.Unmarshal(scanner.Bytes(), &raw); err != nil { if err := json.Unmarshal(scanner.Bytes(), &raw); err != nil {
cclog.Debugf("error unmarshal log output: %v", err)
continue continue
} }
@@ -161,7 +163,3 @@ func (api *RestAPI) getJournalLog(rw http.ResponseWriter, r *http.Request) {
cclog.Errorf("Failed to encode log entries: %v", err) cclog.Errorf("Failed to encode log entries: %v", err)
} }
} }
func (api *RestAPI) MountLogAPIRoutes(r *mux.Router) {
r.HandleFunc("/logs/", api.getJournalLog).Methods(http.MethodGet)
}

View File

@@ -158,6 +158,7 @@ func (api *RestAPI) MountConfigAPIRoutes(r chi.Router) {
// MountFrontendAPIRoutes registers frontend-specific API endpoints. // MountFrontendAPIRoutes registers frontend-specific API endpoints.
// These routes support JWT generation and user configuration updates with session authentication. // These routes support JWT generation and user configuration updates with session authentication.
func (api *RestAPI) MountFrontendAPIRoutes(r chi.Router) { func (api *RestAPI) MountFrontendAPIRoutes(r chi.Router) {
r.Get("/logs/", api.getJournalLog)
// Settings Frontend Uses SessionAuth // Settings Frontend Uses SessionAuth
if api.Authentication != nil { if api.Authentication != nil {
r.Get("/jwt/", api.getJWT) r.Get("/jwt/", api.getJWT)