commit 417fc25db53f4c88a43b864abb95ab368308ed19 Author: lif Date: Mon Jan 20 12:38:50 2025 +0000 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..c8f9d61 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +## Testing +https://github.com/nunojusto/log-spammer \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5bc8ca5 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.lif.sh/lif/docker-logger + +go 1.23.4 diff --git a/main.go b/main.go new file mode 100644 index 0000000..9085775 --- /dev/null +++ b/main.go @@ -0,0 +1,84 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "net" + "net/http" + "time" +) + +type Container struct { + ID string `json:"Id"` +} + +func main() { + // Create a custom transport to use the Unix socket + transport := &http.Transport{ + Dial: func(network, addr string) (net.Conn, error) { + return net.Dial("unix", "/var/run/docker.sock") + }, + } + + client := &http.Client{ + Transport: transport, + Timeout: 10 * time.Second, + } + + // Create a ticker that ticks every 10 seconds + ticker := time.NewTicker(1 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + fetchLogs(client) + } + } +} + +func fetchLogs(client *http.Client) { + // Get the list of container IDs + resp, err := client.Get("http://localhost/containers/json") + if err != nil { + fmt.Println("Error fetching containers:", err) + return + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + fmt.Println("Error reading response body:", err) + return + } + + var containers []Container + if err := json.Unmarshal(body, &containers); err != nil { + fmt.Println("Error unmarshalling JSON:", err) + return + } + + // Loop through each container ID and fetch logs + for _, container := range containers { + fmt.Printf("Fetching logs for container ID: %s\n", container.ID) + logURL := fmt.Sprintf("http://localhost/containers/%s/logs?stdout=true", container.ID) + + // Fetch logs + logResp, err := client.Get(logURL) + if err != nil { + fmt.Println("Error fetching logs:", err) + continue + } + defer logResp.Body.Close() + + logBody, err := ioutil.ReadAll(logResp.Body) + if err != nil { + fmt.Println("Error reading log response body:", err) + continue + } + + // Output the logs + fmt.Println(string(logBody)) + } +}