Skip to content

Commit

Permalink
feat: remove requirement of binding at /etc/hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt committed Mar 19, 2024
1 parent c0ea45e commit 38db66a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 31 deletions.
4 changes: 2 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ It will collect system stats and usage stats of all running swarm services and w
- SUBMISSION_ENDPOINT: The endpoint to submit the stats to
- AUTHORIZATION_HEADER_VAL (optional): The value of the authorization header
- DOCKER_HOST: unix or tcp socket to connect to
- HOSTNAME: The hostname of the server (not the container)

**Volume Bind**
- bind **<host docker socket path>** to -> **/var/run/docker.sock**
- bind /etc/hostname to /app/etc/hostname (read only)
- bind **<host docker socket path>** to -> **provided path in env DOCKER_HOST**
### License
Apache License 2.0
38 changes: 9 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,48 @@ package main

import (
"bytes"
"github.com/docker/docker/client"
"github.com/swiftwave-org/stats_ninja/host"
"io"
"log"
"net/http"
"os"
"strings"
"time"

"github.com/docker/docker/client"
"github.com/swiftwave-org/stats_ninja/host"
)

func main() {
/* Configure the environment variables
* SUBMISSION_ENDPOINT: The endpoint to submit the stats to
* AUTHORIZATION_HEADER_VAL: The value of the authorization header
* DOCKER_HOST: unix or tcp socket to connect to
* HOSTNAME: The hostname of the host
* This will send the stats to the endpoint using the authorization header
*
* Configure Volume Mounts
* <docker socket of host>:/var/run/docker.sock
* /etc/hostname:/app/etc/hostname:ro
*/
submissionEndpoint := os.Getenv("SUBMISSION_ENDPOINT")
authorizationHeaderVal := os.Getenv("AUTHORIZATION_HEADER_VAL")
hostname := os.Getenv("HOSTNAME")
// reject if the submission endpoint is not set
if submissionEndpoint == "" {
panic("SUBMISSION_ENDPOINT is not set")
}
if os.Getenv("DOCKER_HOST") == "" {
panic("DOCKER_HOST is not set")
}
// if hostname is not set, fetch it from the system
if hostname == "" {
panic("HOSTNAME is not set")
}
_, _ = host.Stats() // intentionally called. just to initialize current network stats
// create a new docker client
dockerClient, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
log.Println("Error creating docker client:")
panic(err)
}
// fetch hostname
hostname, err := getHostName()
if err != nil {
log.Println("Error fetching hostname: ")
panic(err)
}
for {
<-time.After(1 * time.Minute)
// fetch stats
Expand Down Expand Up @@ -94,22 +93,3 @@ func sendStats(submissionEndpoint string, authorizationHeaderVal string, jsonDat
}(resp.Body)
return nil
}

func getHostName() (string, error) {
fileName := "/app/etc/hostname"
file, err := os.Open(fileName)
if err != nil {
return "", err
}
defer func(file *os.File) {
_ = file.Close()
}(file)
buf := make([]byte, 1000)
n, err := file.Read(buf)
if err != nil {
return "", err
}
h := string(buf[:n])
h = strings.TrimSpace(h)
return h, nil
}

0 comments on commit 38db66a

Please sign in to comment.