Skip to content

Commit

Permalink
patching pull request rapidloop#20 take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevan committed Jul 18, 2015
1 parent 7f5f019 commit ee81f15
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
14 changes: 14 additions & 0 deletions consolehelper_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !windows

package main

import (
"io"
"os"
)

func clearConsole() {}

func getOutput() io.Writer {
return os.Stdout
}
18 changes: 18 additions & 0 deletions consolehelper_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"github.com/mattn/go-colorable"
"io"
"os"
"os/exec"
)

func clearConsole() {
cmd := exec.Command("cmd", "/c", "cls")
cmd.Stdout = os.Stdout
cmd.Run()
}

func getOutput() io.Writer {
return colorable.NewColorableStdout()
}
21 changes: 12 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ package main
import (
"fmt"
"golang.org/x/crypto/ssh"
"io"
"log"
"os"
"os/signal"
Expand All @@ -48,7 +49,7 @@ const DEFAULT_REFRESH = 5 // default refresh interval in seconds

func usage(code int) {
fmt.Printf(
`rtop %s - (c) 2015 RapidLoop - MIT Licensed - http://rtop-monitor.org
`rtop %s - (c) 2015 RapidLoop - MIT Licensed - http://rtop-monitor.org
rtop monitors server statistics over an ssh connection
Usage: rtop [-i private-key-file] [user@]host[:port] [interval]
Expand Down Expand Up @@ -155,8 +156,9 @@ func main() {

client := sshConnect(username, addr, keyPath)

output := getOutput()
// the loop
showStats(client)
showStats(output, client)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
timer := time.Tick(interval)
Expand All @@ -167,17 +169,18 @@ func main() {
done = true
fmt.Println()
case <-timer:
showStats(client)
showStats(output, client)
}
}
}

func showStats(client *ssh.Client) {
func showStats(output io.Writer, client *ssh.Client) {
stats := Stats{}
getAllStats(client, &stats)
clearConsole()
used := stats.MemTotal - stats.MemFree - stats.MemBuffers - stats.MemCached
fmt.Printf(
`%s%s%s%s up %s%s%s
fmt.Fprintf(output,
`%s%s%s%s up %s%s%s
Load:
%s%s %s %s%s
Expand Down Expand Up @@ -220,7 +223,7 @@ Memory:
if len(stats.FSInfos) > 0 {
fmt.Println("Filesystems:")
for _, fs := range stats.FSInfos {
fmt.Printf(" %s%8s%s: %s%s%s free of %s%s%s\n",
fmt.Fprintf(output, " %s%8s%s: %s%s%s free of %s%s%s\n",
escBrightWhite, fs.MountPoint, escReset,
escBrightWhite, fmtBytes(fs.Free), escReset,
escBrightWhite, fmtBytes(fs.Used+fs.Free), escReset,
Expand All @@ -237,12 +240,12 @@ Memory:
sort.Strings(keys)
for _, intf := range keys {
info := stats.NetIntf[intf]
fmt.Printf(" %s%s%s - %s%s%s, %s%s%s\n",
fmt.Fprintf(output, " %s%s%s - %s%s%s, %s%s%s\n",
escBrightWhite, intf, escReset,
escBrightWhite, info.IPv4, escReset,
escBrightWhite, info.IPv6, escReset,
)
fmt.Printf(" rx = %s%s%s, tx = %s%s%s\n",
fmt.Fprintf(output, " rx = %s%s%s, tx = %s%s%s\n",
escBrightWhite, fmtBytes(info.Rx), escReset,
escBrightWhite, fmtBytes(info.Tx), escReset,
)
Expand Down

0 comments on commit ee81f15

Please sign in to comment.