Created
February 8, 2018 12:05
-
-
Save subuk/da7fa586463ababec9f54d4f85891b4d to your computer and use it in GitHub Desktop.
go simple mon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bufio" | |
| "io" | |
| "os" | |
| "os/exec" | |
| "time" | |
| ) | |
| func worker(host string, output chan string) { | |
| cmd := exec.Command("ssh", host) | |
| stdin, err := cmd.StdinPipe() | |
| if err != nil { | |
| panic(err) | |
| } | |
| stdout, err := cmd.StdoutPipe() | |
| if err != nil { | |
| panic(err) | |
| } | |
| if err := cmd.Start(); err != nil { | |
| panic(err) | |
| } | |
| go func() { | |
| for { | |
| if _, err := io.WriteString(stdin, "ps aux\n"); err != nil { | |
| panic(err) | |
| } | |
| time.Sleep(time.Second * 1) | |
| } | |
| }() | |
| go func() { | |
| out := bufio.NewScanner(stdout) | |
| for out.Scan() { | |
| output <- out.Text() + "\n" | |
| } | |
| }() | |
| if err := cmd.Wait(); err != nil { | |
| panic(err) | |
| } | |
| } | |
| func main() { | |
| hosts := []string{"[email protected]"} | |
| if err := os.MkdirAll("/tmp/gomon", 0755); err != nil { | |
| panic(err) | |
| } | |
| for _, host := range hosts { | |
| output := make(chan string, 10) | |
| file, err := os.Create("/tmp/gomon/" + host + ".txt") | |
| if err != nil { | |
| panic(err) | |
| } | |
| go worker(host, output) | |
| for line := range output { | |
| if _, err := io.WriteString(file, line); err != nil { | |
| panic(err) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment