Initial commit
This commit is contained in:
2
README.md
Normal file
2
README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
## Testing
|
||||
https://github.com/nunojusto/log-spammer
|
||||
84
main.go
Normal file
84
main.go
Normal file
@@ -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))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user