Skip to content

Commit

Permalink
Fix dynamic exec command
Browse files Browse the repository at this point in the history
  • Loading branch information
xincunli-sonic committed Oct 29, 2024
1 parent 4efda84 commit 0c63a18
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ type ContainerHealthInfo struct {
// GetHealthInfo gathers health information for the gNMI container
func GetHealthInfo() ([]ContainerHealthInfo, error) {
// Here we interact with Docker to get container stats
cmd := "docker stats --no-stream --format \"{{.Container}},{{.CPUPerc}},{{.MemPerc}},{{.Name}}\" | grep gnmi"
args := strings.Fields(cmd)
output, err := exec.Command(args[0], args[1:]...).Output()
output, err := exec.Command("docker", "stats", "--no-stream", "--format", "\"{{.Container}},{{.CPUPerc}},{{.MemPerc}},{{.Name}}\"", "| grep gnmi").Output()
if err != nil {
return nil, fmt.Errorf("failed to retrieve container stats: %v", err)
}
Expand Down Expand Up @@ -60,9 +58,7 @@ func GetHealthInfo() ([]ContainerHealthInfo, error) {
// getDiskOccupation retrieves the disk usage for the container
func getDiskOccupation(containerID string) float64 {
// Run the command to get disk usage inside the container
cmd := fmt.Sprintf("docker exec %s df / | tail -1 | awk '{print $5}'", containerID)
args := strings.Fields(cmd)
output, err := exec.Command(args[0], args[1:]...).Output()
output, err := exec.Command("docker", "exec", containerID, "df", "/").Output()
if err != nil {
fmt.Printf("failed to retrieve disk occupation for container %s: %v\n", containerID, err)
return 0.0
Expand All @@ -73,9 +69,7 @@ func getDiskOccupation(containerID string) float64 {
// getCertExpiration retrieves the certificate expiration for the container
func getCertExpiration(containerID string) int64 {
// Run the command to get the certificate from the container
cmd := fmt.Sprintf("docker exec %s cat /path/to/cert.pem", containerID)
args := strings.Fields(cmd)
output, err := exec.Command(args[0], args[1:]...).Output()
output, err := fmt.Sprintf("docker", "exec", containerID, "cat", "/path/to/cert.pem")
if err != nil {
fmt.Printf("failed to retrieve certificate for container %s: %v\n", containerID, err)
return 0
Expand Down

0 comments on commit 0c63a18

Please sign in to comment.