Skip to content

Instantly share code, notes, and snippets.

@iankronquist
Created April 14, 2016 02:08
Show Gist options
  • Save iankronquist/07f7ac47f346e545e3276d861d2fd81e to your computer and use it in GitHub Desktop.
Save iankronquist/07f7ac47f346e545e3276d861d2fd81e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <unistd.h>
int main () {
unsigned long long i;
while (1) {
printf("tick\n");
for (i = 1; i != 0; i++){}
}
}
package main
import (
"bufio"
"fmt"
"io"
_ "io/ioutil"
"os"
"os/exec"
)
type ExecMonitor struct {
MonitorName string
ContainerIds []string
}
func main() {
execMessages := make(chan []byte)
execMonitor := ExecMonitor{}
DockerComposeName := "docker-compose" // could be fig
go execMonitor.Start(execMessages, DockerComposeName)
for {
select {
case message := <-execMessages:
fmt.Println("exec: ", string(message))
// Other channels here
}
}
}
// Dummy
func getDockerContainerIds(dockerComposeName string) []string {
_ = dockerComposeName
ids := []string{"00f593c3086f4925b1574764b2e24e0ad62046789676b061ac15233100dc801d", "529f08ac44849b40687fb5696c50354e6da22b3d4c656b018085cd79e3b29b5f"}
return ids
}
func (e ExecMonitor) Start(messages chan<- []byte, dockerComposeName string) {
ids := getDockerContainerIds(dockerComposeName)
// This program writes the names of any programs which are exec'd to stdout
err := runCommandAndChannelOutput("./a.out", ids, messages)
if err != nil {
fmt.Println("Exec monitor failed")
panic(err)
}
}
func runCommandAndChannelOutput(commandname string, args []string, output chan<- []byte) error {
command := exec.Command(commandname, args...)
fmt.Print("running the command: ")
fmt.Println(commandname, args)
stdout, err := command.StdoutPipe()
if err != nil {
return err
}
stderr, err := command.StderrPipe()
if err != nil {
return err
}
go io.Copy(os.Stderr, stderr)
command.Start()
defer command.Wait()
stdoutreader := bufio.NewReader(stdout)
slurp := true
for slurp {
fetch := true
line := []byte{}
for fetch {
fmt.Println("before reading line")
partial_line, f, err := stdoutreader.ReadLine()
fmt.Println("after reading line")
fetch = f
line = append(line, partial_line...)
if err == io.EOF {
slurp = false
break
} else if err != nil {
return err
}
}
if len(line) > 0 {
output <- line
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment