### StreamLogs Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/streamer.md Demonstrates how to initialize and start the StreamLogs process. ```go streamController := &streamer.StreamController{ DaemonService: daemonService, } go streamController.StreamLogs(context.Background()) ``` -------------------------------- ### GetUserSettings Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of retrieving user-specific settings. ```Go settings := userdb.GetUserSettings("alice") if theme, ok := settings["theme"]; ok { fmt.Println("Theme:", theme) } ``` -------------------------------- ### EnsureStream Method Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/daemon.md Example demonstrating how to initialize a DaemonService and start log streaming for a specific container. ```go daemon := &daemon.DaemonService{DockerClient: dockerService} ctx := context.Background() daemon.EnsureStream(ctx, "nginx") ``` -------------------------------- ### GetHost Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the GetHost function. ```go hostname := util.GetHost() // Returns "docker-host-1" ``` -------------------------------- ### CreateUser Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of creating a new user account. ```Go err := userdb.CreateUser("alice", "password123") if err != nil { log.Println("User already exists:", err) } ``` -------------------------------- ### GetUsers Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of listing all users with edit permissions. ```Go users := userdb.GetUsers() for _, user := range users { fmt.Printf("%s (editable: %v)\n", user["username"], user["editable"]) } ``` -------------------------------- ### GetDirSize Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the GetDirSize function to get the size of a container's logs directory. ```go size := util.GetDirSize("host1", "nginx") // Returns 123.45 (MiB) ``` -------------------------------- ### CreateOnLogsToken Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/db.md Example usage of the CreateOnLogsToken function. ```go token := db.CreateOnLogsToken() // Returns something like: "aBcD1234eFgH5678iJkL9012_" ``` -------------------------------- ### RunStatisticForContainerWithContext Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/statistics.md Starts a statistics worker that can be cancelled via context. This example demonstrates starting the worker and how to later cancel it. ```Go ctx, cancel := context.WithCancel(context.Background()) defer cancel() go statistics.RunStatisticForContainerWithContext(ctx, "host1", "nginx") // Later, stop the worker cancel() ``` -------------------------------- ### Database Initialization Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example showing database variables initialized at package load time. ```go var ( FavsDB, FavsDBErr = leveldb.OpenFile("leveldb/favourites", nil) // ... other databases ) ``` -------------------------------- ### Get Log With Previous Response Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Example JSON response for the Get Log With Previous API. ```json { "logs": [ ["2024-01-01T12:00:00.000000000Z", "log message including startWith"], ["2024-01-01T11:59:59.000000000Z", "previous message"] ], "last_processed_key": "2024-01-01T11:59:59.000000000Z", "is_end": false } ``` -------------------------------- ### CreateJWT Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the CreateJWT function. ```go token := util.CreateJWT("alice") // Set as cookie value ``` -------------------------------- ### MAX_LOGS_SIZE Examples Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Examples of setting the MAX_LOGS_SIZE environment variable to control log storage. ```bash export MAX_LOGS_SIZE=10GB # 10 gigabytes export MAX_LOGS_SIZE=1024MB # 1 gigabyte export MAX_LOGS_SIZE=100GB # 100 gigabytes export MAX_LOGS_SIZE=5.5TB # 5.5 terabytes ``` -------------------------------- ### CheckUserPassword Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of verifying user credentials. ```Go if userdb.CheckUserPassword("alice", "password123") { fmt.Println("Credentials valid") } else { fmt.Println("Invalid credentials") } ``` -------------------------------- ### Log Entry Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Example of a log entry. ```go []string{ "2024-01-01T12:00:00.000000000Z", "GET / HTTP/1.1 200 OK", } ``` -------------------------------- ### RunStatisticForContainer Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/statistics.md Starts a statistics worker for a container (background goroutine). This example shows how to run it for a specific container on a host. ```Go go statistics.RunStatisticForContainer("host1", "nginx") ``` -------------------------------- ### UpdateUserSettings Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example demonstrating how to use the UpdateUserSettings function to save user settings. ```Go settings := map[string]interface{}{ "theme": "dark", "itemsPerPage": 50, "timezone": "UTC", } userdb.UpdateUserSettings("alice", settings) ``` -------------------------------- ### Get Hosts Response Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Example JSON response for the Get Hosts API. ```json [ { "host": "hostname", "services": [ { "serviceName": "container-name", "isDisabled": false, "isFavorite": true } ] } ] ``` -------------------------------- ### Hosts Response Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Example structure for host and services response. ```go []struct { Host string `json:"host"` Services []map[string]interface{} `json:"services"` }{ { Host: "host1", Services: []map[string]interface{}{ { "serviceName": "nginx", "isDisabled": false, "isFavorite": true, }, }, }, } ``` -------------------------------- ### GetStatisticsByService Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/statistics.md Retrieves log counts for a container over a time period. Examples show how to get stats for the last 30 minutes and the last day. ```Go // Get stats for last 30 minutes stats := statistics.GetStatisticsByService("host1", "nginx", 1) fmt.Printf("Errors: %d, Warnings: %d\n", stats["error"], stats["warn"]) // Get stats for last 1 day stats := statistics.GetStatisticsByService("host1", "nginx", 48) ``` -------------------------------- ### Get Logs Response Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Example JSON response for the Get Logs API. ```json { "logs": [ ["2024-01-01T12:00:00.000000000Z", "log message"], ["2024-01-01T12:00:01.000000000Z", "another message"] ], "last_processed_key": "2024-01-01T12:00:01.000000000Z", "is_end": false } ``` -------------------------------- ### IsUserExists Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of checking if a user account exists. ```Go if userdb.IsUserExists("alice") { fmt.Println("User alice exists") } ``` -------------------------------- ### JSON Example for UserData Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example JSON payload for UserData. ```json { "login": "alice", "password": "password123" } ``` -------------------------------- ### DeleteUser Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of removing a user account. ```Go err := userdb.DeleteUser("alice", "") if err != nil { log.Println("User not found:", err) } ``` -------------------------------- ### EditUser Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/userdb.md Example of updating a user's password. ```Go userdb.EditUser("alice", "newpassword456") ``` -------------------------------- ### Get Previous Logs Response Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Example JSON response for the Get Previous Logs API. ```json { "logs": [ ["2024-01-01T11:59:59.000000000Z", "log message"], ["2024-01-01T11:59:58.000000000Z", "another message"] ], "last_processed_key": "2024-01-01T11:59:58.000000000Z", "is_end": false } ``` -------------------------------- ### Access Pattern for Databases Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example of how to check for errors and use a database. ```go // Check for errors if vars.TokensDBErr != nil { log.Fatal("Cannot open tokens database:", vars.TokensDBErr) } // Use database vars.TokensDB.Put([]byte("key"), []byte("value"), nil) ``` -------------------------------- ### Slice Types Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Examples of common slice types used. ```go []string - Container names, hosts, services []ContainerNamesResult - Container listing [][]string - Log entries (timestamp + message) ``` -------------------------------- ### Access Pattern for Database Instances Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example of how to access a database instance, fetching it if it doesn't exist. ```go db := vars.ActiveDBs[containerName] if db == nil { db = util.GetDB(host, container, "logs") } ``` -------------------------------- ### GetChartData Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/statistics.md Retrieves historical statistics for visualization. Examples show how to get hourly and daily data, and how to iterate through the results. ```Go // Get hourly data for last 24 hours data := statistics.GetChartData("host1", "nginx", "hour", 24) // Get daily data for last 30 days data := statistics.GetChartData("host1", "nginx", "day", 30) // Build chart from data for timestamp, counts := range data { fmt.Printf("%s: %d errors\n", timestamp, counts["error"]) } ``` -------------------------------- ### GenerateJWTSecret Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the GenerateJWTSecret function. ```go token := util.GenerateJWTSecret() // Returns something like: "aBcD1234eFgH5678iJkL9012_" ``` -------------------------------- ### Set up Agent Instance Source: https://github.com/devforth/onlogs/blob/main/_autodocs/README.md Docker run command to set up and start an onlogs agent instance, configuring essential environment variables and mounting the Docker socket. ```bash docker run -e AGENT=true \ -e HOST=https://onlogs.example.com \ -e ONLOGS_TOKEN= \ -v /var/run/docker.sock:/var/run/docker.sock \ devforth/onlogs ``` -------------------------------- ### GetDB Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the GetDB function to retrieve different types of databases. ```go logsDB := util.GetDB("host1", "nginx", "logs") statsDB := util.GetDB("host1", "nginx", "statistics") ``` -------------------------------- ### String-indexed Maps Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Examples of map[string]T usage for keyed access. ```go ActiveDBs map[string]*leveldb.DB // Key: container name Stat_Containers_DBs map[string]*leveldb.DB // Key: host/container Container_Stat_Counter map[string]map[string]uint64 // Key: host/container Connections map[string][]*websocket.Conn // Key: container name ``` -------------------------------- ### GetLogs Examples Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/containerdb.md Examples of retrieving logs with different filtering and pagination options. ```go // Get first 50 logs forward result := containerdb.GetLogs(false, false, "host1", "nginx", "", 50, "", false, nil) logs := result["logs"].([][]string) fmt.Printf("Got %d logs\n", len(logs)) // Get next batch using pagination lastKey := result["last_processed_key"].(string) result2 := containerdb.GetLogs(false, false, "host1", "nginx", "", 50, lastKey, false, nil) // Search with status filter status := "error" errors := containerdb.GetLogs(false, false, "host1", "nginx", "timeout", 100, "", false, &status) ``` -------------------------------- ### Example Usage of DBMutex (RWLock) Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Demonstrates read and write locking using DBMutex. ```go // Many readers can read simultaneously vars.DBMutex.RLock() db := vars.ActiveDBs[container] vars.DBMutex.RUnlock() // Single writer vars.DBMutex.Lock() defer vars.DBMutex.Unlock() vars.ActiveDBs[container] = newDB ``` -------------------------------- ### Error Handling in Initialization Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example of checking database errors during initialization. ```go if vars.FavsDBErr != nil || vars.StateDBErr != nil || vars.UsersDBErr != nil { return } ``` -------------------------------- ### Usage of WebSocket Connections Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example of adding a new WebSocket connection and broadcasting a message. ```go // Add new connection vars.Connections[container] = append(vars.Connections[container], ws) // Broadcast log to all subscribers for _, conn := range vars.Connections[container] { conn.WriteMessage(websocket.TextMessage, logData) } ``` -------------------------------- ### SendInitRequest Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of how to call SendInitRequest. ```go agent.SendInitRequest([]string{"nginx", "redis"}) // Sends to central server, registers this agent ``` -------------------------------- ### Example Usage of Mutex Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Demonstrates locking and unlocking the Mutex for read/write operations. ```go vars.Mutex.Lock() defer vars.Mutex.Unlock() vars.Container_Stat_Counter[location]["error"]++ ``` -------------------------------- ### Memory Safety Best Practices Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Examples demonstrating correct and incorrect concurrent access patterns. ```go // DON'T - Race condition vars.Container_Stat_Counter[location]["error"]++ // DO - Proper locking vars.Mutex.Lock() defer vars.Mutex.Unlock() vars.Container_Stat_Counter[location]["error"]++ // DON'T - Race condition db := vars.ActiveDBs[container] // DO - Proper locking (using GetDB) db := util.GetDB(host, container, "logs") ``` -------------------------------- ### Get Statistics for Last 24 Hours Source: https://github.com/devforth/onlogs/blob/main/_autodocs/README.md Example cURL command to fetch chart data (statistics) for a service over the last 24 hours. ```bash curl -X POST "http://localhost:2874/api/v1/getChartData" \ -H "Content-Type: application/json" \ -d '{ "host": "host1", "service": "nginx", "unit": "hour", "unitsAmount": 24 }' ``` -------------------------------- ### Docker Compose Configuration Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Example Docker Compose configuration for running OnLogs. ```yaml services: onlogs: image: devforth/onlogs:latest restart: always ports: - "2874:2874" environment: - ADMIN_USERNAME=admin - ADMIN_PASSWORD=MySecurePassword123 - PORT=2874 - DOCKER_HOST=unix:///var/run/docker.sock - MAX_LOGS_SIZE=50GB # - ONLOGS_PATH_PREFIX=/onlogs # If using with path prefix volumes: - /var/run/docker.sock:/var/run/docker.sock:ro - /var/lib/docker/containers:/var/lib/docker/containers:ro - /etc/hostname:/etc/hostname:ro - onlogs-data:/leveldb networks: - default volumes: onlogs-data: ``` -------------------------------- ### SendLogMessage Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of how to call SendLogMessage and handle the success/failure. ```go success := agent.SendLogMessage(token, "nginx", []string{"2024-01-01T12:00:00.000000000Z", "GET / 200"}) if !success { // Message stored locally and will be retried } ``` -------------------------------- ### GetContainersList Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/daemon.md Example usage of the GetContainersList function. ```go containers := daemon.GetContainersList(ctx) for _, name := range containers { fmt.Println("Container:", name) } ``` -------------------------------- ### IsTokenExists Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/db.md Example usage of the IsTokenExists function for token validation. ```go if db.IsTokenExists(tokenFromRequest) { // Token is valid, process the request } else { // Invalid or expired token http.Error(w, "Unauthorized", http.StatusUnauthorized) } ``` -------------------------------- ### PutLogMessage Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/containerdb.md Example of storing a log message using PutLogMessage. ```go db := util.GetDB("host1", "nginx", "logs") err := containerdb.PutLogMessage(db, "host1", "nginx", []string{"2024-01-01T12:00:00.000000000Z", "GET / 200 OK"}) if err != nil { log.Println(err) } ``` -------------------------------- ### StopStream Method Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/daemon.md Example showing how to stop log streaming for a container. ```go daemon.StopStream("nginx") ``` -------------------------------- ### AskForDelete Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of calling AskForDelete to retrieve and process pending deletions. ```go agent.AskForDelete() // Retrieves and processes pending deletions ``` -------------------------------- ### Local Docker Testing Setup Source: https://github.com/devforth/onlogs/blob/main/README.md Commands to set up and run OnLogs with a socket proxy and logprinter using Docker Compose for local testing. ```sh cd application docker compose -f compose-local-test.yml up --build ``` -------------------------------- ### DeleteUnusedTokens Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/db.md Example of how to call DeleteUnusedTokens as a background goroutine. ```go // In main.go go db.DeleteUnusedTokens() ``` -------------------------------- ### SendUpdate Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of calling SendUpdate with the current list of containers. ```go currentContainers := []string{"nginx", "redis", "postgres"} agent.SendUpdate(currentContainers) ``` -------------------------------- ### Docker Run example with traefik Source: https://github.com/devforth/onlogs/blob/main/README.md This snippet demonstrates how to run OnLogs using a single Docker command, also configured to work with Traefik for routing. ```sh docker run --restart always -e ADMIN_USERNAME=admin -e PASSWORD= -e PORT=8798 \ -v /var/run/docker.sock:/var/run/docker.sock:ro \ -v /var/lib/docker/containers:/var/lib/docker/containers \ -v /etc/hostname:/etc/hostname \ -v onlogs-volume:/leveldb \ --label traefik.enable=true \ --label traefik.http.routers.onlogs.rule=Host\(\`\`\) \ --label traefik.http.services.onlogs.loadbalancer.server.port=8798 devforth/onlogs ``` -------------------------------- ### GetLogStatusKey Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/containerdb.md Example of classifying a log message status using GetLogStatusKey. ```go status := containerdb.GetLogStatusKey("ERROR: Connection timeout") // Returns "error" ``` -------------------------------- ### AskForDelete Request Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example JSON payload for AskForDelete request. ```json { "Hostname": "remote-host-1", "Token": "token" } ``` -------------------------------- ### AskForDelete Response Expected Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of the expected JSON response from AskForDelete. ```json { "Services": ["container1", "container2"] } ``` -------------------------------- ### ENV_NAME for Development Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Example of setting ENV_NAME to 'local' to enable development features like CORS. ```bash export ENV_NAME=local ``` -------------------------------- ### TryResend Example Usage Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example of using TryResend in a goroutine for periodic retries. ```go go func() { for range time.Tick(time.Minute) { agent.TryResend() // Retry unsent logs } }() ``` -------------------------------- ### GetUserFromJWT Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Example usage of the GetUserFromJWT function to retrieve and validate a user from a JWT. ```go user, err := util.GetUserFromJWT(*req) if err != nil { http.Error(w, err.Error(), http.StatusUnauthorized) return } fmt.Printf("Logged in as: %s\n", user) ``` -------------------------------- ### Docker Compose example with traefik Source: https://github.com/devforth/onlogs/blob/main/README.md This snippet shows how to configure OnLogs using Docker Compose, integrating with Traefik for routing and exposing necessary volumes and environment variables. ```sh onlogs: image: devforth/onlogs restart: always environment: - ADMIN_USERNAME=admin - ADMIN_PASSWORD= - PORT=8798 # - ONLOGS_PATH_PREFIX=/onlogs if want to use with path prefix labels: - "traefik.enable=true" - "traefik.http.routers.onlogs.rule=Host(\`\`)" # if using on subdomain, e.g. https://onlogs.yourdomain.com # - traefik.http.routers.onlogs.rule=PathPrefix(`/onlogs`) # if want to use with a path prefix, e.g. https://yourdomain.com/onlogs - "traefik.http.services.onlogs.loadbalancer.server.port=8798" volumes: - /var/run/docker.sock:/var/run/docker.sock - /var/lib/docker/containers:/var/lib/docker/containers # if you want to delete duplicating logs from docker - /etc/hostname:/etc/hostname - onlogs-volume:/leveldb volumes: onlogs-volume: ``` -------------------------------- ### Value Structure for Statistics Counters Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Example structure of the value for statistics counters. ```go map[string]uint64{ "error": 100, "warn": 50, "info": 200, "debug": 25, "meta": 10, "other": 15, } ``` -------------------------------- ### Retrieve Logs for a Container Source: https://github.com/devforth/onlogs/blob/main/_autodocs/README.md Example cURL command to retrieve the latest logs for a specific container on a given host. ```bash curl -X GET "http://localhost:2874/api/v1/getLogs?host=host1&id=nginx&limit=50" ``` -------------------------------- ### Persistent Databases Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Main application databases opened at startup. ```go var ( FavsDB, FavsDBErr = leveldb.OpenFile("leveldb/favourites", nil) StateDB, StateDBErr = leveldb.OpenFile("leveldb/state", nil) UsersDB, UsersDBErr = leveldb.OpenFile("leveldb/users", nil) TokensDB, TokensDBErr = leveldb.OpenFile("leveldb/tokens", nil) SettingsDB, SettingsDBErr = leveldb.OpenFile("leveldb/usersSettings", nil) ) ``` -------------------------------- ### Get Hosts Success Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for listing all hosts and their containers. ```json [ { "host": "host1", "services": [ { "serviceName": "nginx", "isDisabled": false, "isFavorite": true }, { "serviceName": "redis", "isDisabled": true, "isFavorite": false } ] } ] ``` -------------------------------- ### Docker Compose Agent Configuration Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Example Docker Compose configuration for an OnLogs agent sending logs to a central server. ```yaml services: onlogs-agent: image: devforth/onlogs:latest restart: always environment: - AGENT=true - HOST=https://onlogs.central-server.com - ONLOGS_TOKEN=your_generated_token_here - DOCKER_HOST=unix:///var/run/docker.sock volumes: - /var/run/docker.sock:/var/run/docker.sock:ro - /etc/hostname:/etc/hostname:ro - onlogs-agent-data:/leveldb networks: - default volumes: onlogs-agent-data: ``` -------------------------------- ### ensureStreams Method Signature Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/streamer.md Starts streams and statistics workers for a given list of containers. ```go func (ctrl *StreamController) ensureStreams(ctx context.Context, containers []string) ``` -------------------------------- ### Shared JWT_SECRET for Load Balancing Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Example of setting a shared JWT_SECRET for multiple OnLogs instances behind a load balancer. ```bash export JWT_SECRET=shared_secret_for_all_instances ``` -------------------------------- ### Generate Agent Token Source: https://github.com/devforth/onlogs/blob/main/_autodocs/README.md Example cURL command to generate a secret token for an agent, requiring an existing JWT token for authentication. ```bash curl -X GET "http://localhost:2874/api/v1/getSecret" \ -H "Cookie: onlogs-cookie=" ``` -------------------------------- ### Configuration Variables Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Constants and variables related to application configuration. ```go const ( StatisticsSaveInterval = 1 * time.Minute ) var ( Year = strconv.Itoa(time.Now().UTC().Year()) ) ``` -------------------------------- ### SendLogMessage Request Sent Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example JSON payload for SendLogMessage. ```json { "Host": "remote-host-1", "Token": "token", "Container": "nginx", "LogLine": ["2024-01-01T12:00:00.000000000Z", "log message"] } ``` -------------------------------- ### HOST Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md URL of the central OnLogs server (for agents). ```bash export HOST=https://onlogs.example.com export HOST=http://onlogs-internal:2874 ``` -------------------------------- ### SendInitRequest Request Sent Example Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/agent.md Example JSON payload for SendInitRequest. ```json { "Hostname": "remote-host-1", "Token": "token_from_env", "Services": ["nginx", "redis", "postgres"] } ``` -------------------------------- ### AGENT Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Enables agent mode. ```bash export AGENT=true export HOST=https://onlogs.example.com export ONLOGS_TOKEN=generated_token_from_main_server ``` -------------------------------- ### Database Instances Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Cached LevelDB instances for different purposes. ```go var ( ActiveDBs = map[string]*leveldb.DB{} Stat_Containers_DBs = map[string]*leveldb.DB{} Stat_Hosts_DBs = map[string]*leveldb.DB{} Statuses_DBs = map[string]*leveldb.DB{} BrokenLogs_DBs = map[string]*leveldb.DB{} ContainersMeta_DBs = map[string]*leveldb.DB{} StreamState_DBs = map[string]*leveldb.DB{} ) ``` -------------------------------- ### Get Logs Success Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md The success response for the GET /api/v1/getLogs endpoint. ```json { "logs": [ ["2024-01-01T12:00:00.000000000Z", "log message 1"], ["2024-01-01T12:00:01.000000000Z", "log message 2"] ], "last_processed_key": "2024-01-01T12:00:01.000000000Z", "is_end": false } ``` -------------------------------- ### Get Users Success Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md The success response for the GET /api/v1/getUsers endpoint. ```json { "users": [ { "username": "admin", "editable": false }, { "username": "alice", "editable": true } ], "error": null } ``` -------------------------------- ### ReplacePrefixVariableForFrontend Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Replaces path prefix placeholder in static files. ```Go func ReplacePrefixVariableForFrontend() ``` -------------------------------- ### GetHost Function Signature Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Returns the current system hostname. ```go func GetHost() string ``` -------------------------------- ### Get User Settings Success Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md The success response for the GET /api/v1/getUserSettings endpoint. ```json { "customSetting1": "value1", "customSetting2": "value2" } ``` -------------------------------- ### CreateInitUser Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Creates the initial admin user if it doesn't exist. ```Go func CreateInitUser() ``` -------------------------------- ### ONLOGS_PATH_PREFIX Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Base path prefix for the application (when not on subdomain). ```bash export ONLOGS_PATH_PREFIX=/onlogs # Frontend routes become: /onlogs/api/v1/getHosts, etc. ``` -------------------------------- ### Get Stats Request Body Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON request body for getting log statistics for a container. ```json { "host": "string", "service": "string", "period": 1 } ``` -------------------------------- ### Get Size By Service Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md API endpoint to get the storage size for a specific container. ```HTTP GET /api/v1/getSizeByService?host=&service= ``` ```JSON { "sizeMiB": "123.4" } ``` -------------------------------- ### PORT Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Port number for the HTTP server. ```bash export PORT=8080 ``` -------------------------------- ### ADMIN_USERNAME Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Username for the admin user. ```bash export ADMIN_USERNAME=administrator ``` -------------------------------- ### Get Chart Data Request Body Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON request body for getting historical statistics for charting. ```json { "host": "string", "service": "string", "unit": "hour", "unitsAmount": 24 } ``` -------------------------------- ### DockerService Type Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/docker.md Wraps the Docker client and provides simplified access to Docker operations. Initialized in `main.go` with Docker SDK's `client.Client`. ```go type DockerService struct { Client *client.Client } ``` -------------------------------- ### Get Size By All Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md API endpoint to get the total logs storage size across all containers. ```HTTP GET /api/v1/getSizeByAll ``` ```JSON { "sizeMiB": "1234.5" } ``` -------------------------------- ### Get Docker Size Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md API endpoint to get the Docker daemon's JSON log file size. ```HTTP GET /api/v1/getDockerSize?host=&service= ``` ```JSON { "sizeMiB": "567.8" } ``` -------------------------------- ### UserData Type Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Standard request format for login/user creation endpoints. ```go type UserData struct { Login string `json:"login"` Password string `json:"password"` } ``` -------------------------------- ### Get Stats Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for log statistics of a container. ```json { "error": 100, "warn": 50, "info": 200, "debug": 25, "meta": 10, "other": 15 } ``` -------------------------------- ### startEventsLoop Method Signature Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/streamer.md Opens a Docker event stream and forwards events, handling reconnections. ```go func (ctrl *StreamController) startEventsLoop(ctx context.Context) ``` -------------------------------- ### Get Hosts API Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Lists all hosts and their containers with status. ```http GET /api/v1/getHosts ``` -------------------------------- ### Get Storage Data Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for filesystem storage statistics. ```json { "total_space_GB": 1000.0, "free_space_GB": 500.0, "free_space_percent": 50.0 } ``` -------------------------------- ### Get Size By All Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for total logs storage size. ```json { "sizeMiB": "1234.5" } ``` -------------------------------- ### GetDB Function Signature Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/util.md Retrieves or opens a LevelDB database for a container. ```go func GetDB(host string, container string, dbType string) *leveldb.DB ``` -------------------------------- ### Container Event Actions Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Constants for Docker container event actions. ```go const ( ActionStart = "start" ActionStop = "stop" ActionDie = "die" ActionRestart = "restart" ActionPause = "pause" ActionUnpause = "unpause" ActionDestroy = "destroy" ) ``` -------------------------------- ### Get Secret Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response containing a generated agent authentication token. ```json { "token": "abc123def456ghi789jkl012mno3456" } ``` -------------------------------- ### Get Size By Service Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for the storage size of a specific container. ```json { "sizeMiB": "123.4" } ``` -------------------------------- ### Login Request Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Login request structure. ```go struct { Login string `json:"login"` Password string `json:"password"` } ``` -------------------------------- ### Get Chart Data Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for historical statistics used in charting. ```json { "2024-01-01T00:00Z": { "error": 100, "warn": 50, "info": 200, "debug": 25, "meta": 10, "other": 15 }, "now": { "error": 5, "warn": 2, "info": 10, "debug": 1, "meta": 0, "other": 2 } } ``` -------------------------------- ### ADMIN_PASSWORD Source: https://github.com/devforth/onlogs/blob/main/_autodocs/configuration.md Password for the admin user. ```bash export ADMIN_PASSWORD=securePassword123 ``` -------------------------------- ### Get Secret Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Generates a new agent token for remote log forwarding. ```HTTP GET /api/v1/getSecret ``` ```JSON { "token": "abcd1234efgh5678ijkl9012mnop3456" } ``` -------------------------------- ### buildLogKey Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/containerdb.md Generates a unique log entry key by combining timestamp, nanosecond, and a counter. ```go func buildLogKey(timestamp string) string ``` -------------------------------- ### Get Storage Data Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Returns filesystem storage statistics for the current host. ```HTTP POST /api/v1/getStorageData ``` ```JSON { "host": "string" } ``` ```JSON { "total_space_GB": 1000.0, "free_space_GB": 500.0, "free_space_percent": 50.0 } ``` -------------------------------- ### Get Logs API Endpoint Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/routes.md Retrieves logs for a container with optional filtering and search. ```http GET /api/v1/getLogs?host=&id=&limit=&search=&startWith=&caseSens=&status= ``` -------------------------------- ### Ask For Delete Request Body Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON request body for an agent to get pending deletions. ```json { "hostname": "string", "token": "string" } ``` -------------------------------- ### WebSocket Connections Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/vars.md Map storing active WebSocket connections for real-time log streaming. ```go var Connections = map[string][]*websocket.Conn{} ``` -------------------------------- ### Get Storage Data Request Body Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON request body for filesystem storage statistics. ```json { "host": "string" } ``` -------------------------------- ### Get Docker Size Response Source: https://github.com/devforth/onlogs/blob/main/_autodocs/endpoints.md JSON response for the Docker daemon log file size. ```json { "sizeMiB": "567.8" } ``` -------------------------------- ### Database Types Source: https://github.com/devforth/onlogs/blob/main/_autodocs/types.md Constants for database types used with util.GetDB(). ```go const ( TypeLogs = "logs" TypeStatistics = "statistics" TypeStatuses = "statuses" TypeBrokenLogs = "brokenlogs" TypeStreamState = "streamstate" ) ``` -------------------------------- ### ensureStatisticsWorker Method Signature Source: https://github.com/devforth/onlogs/blob/main/_autodocs/api-reference/streamer.md Starts a statistics collector for a specific container, preventing duplicates. ```go func (ctrl *StreamController) ensureStatisticsWorker(ctx context.Context, host, container string) ```