Browse Source

Added colors

master
Felix 2 years ago
parent
commit
fe3e48a170
  1. 62
      gmon.go

62
gmon.go

@ -14,8 +14,20 @@ import (
"strings"
"strconv"
"encoding/xml"
"golang.org/x/crypto/ssh/terminal"
)
// Terminal color codes
const KNRM = "\x1B[0m"
const KRED = "\x1B[31m"
const KGRN = "\x1B[32m"
const KYEL = "\x1B[33m"
const KBLU = "\x1B[34m"
const KMAG = "\x1B[35m"
const KCYN = "\x1B[36m"
const KWHT = "\x1B[37m"
type GangliaXML struct {
Cluster Cluster `xml:"CLUSTER"`
}
@ -39,14 +51,22 @@ type Metric struct {
Unit string `xml:"UNITS,attr"`
}
func condIf(cond bool, true_s string, false_s string) string {
if(cond) {
return true_s
} else {
return false_s
}
}
func HostRow(host Host) string {
func HostRow(host Host, useColors bool) string {
cpu := -1.0
mem_tot := 0.0
mem_free := 0.0
load1 := -1.0
load5 := -1.0
load15 := -1.0
cpu_num := 1
// Get relevant metrics
for _, metric := range host.Metrics {
@ -69,6 +89,9 @@ func HostRow(host Host) string {
if metric.Name == "load_fifteen" {
load15, _ = strconv.ParseFloat(metric.Value, 32)
}
if metric.Name == "cpu_num" {
cpu_num, _ = strconv.Atoi(metric.Value)
}
}
mem := -1.0
@ -78,10 +101,35 @@ func HostRow(host Host) string {
}
then := time.Unix(host.Time, 0) // In UTC
time := then.Format("2006-01-02-15:04:05")
//return fmt.Sprintf("%25s\t%15s\t%20s%5.0f %%\t%5.1f %%\t%5.1f %5.1f %5.1f", host.Name, host.Ip, time, cpu, mem*100.0, load1, load5, load15)
return fmt.Sprintf("%-23s\t%20s%5.0f%%\t%5.1f%%\t%4.1f %4.1f %4.1f", host.Name, time, cpu, mem*100.0, load1, load5, load15)
stime := then.Format("2006-01-02-15:04:05")
if useColors {
ret := fmt.Sprintf("%-23s\t", host.Name)
ret += fmt.Sprintf("%s", condIf( time.Since(then).Hours()>1,KRED,KGRN))
ret += fmt.Sprintf("%20s", stime)
ret += fmt.Sprintf("%s", KNRM)
ret += fmt.Sprintf("%s", condIf( cpu<0,KRED, condIf(cpu<25, KYEL, KGRN)))
ret += fmt.Sprintf("%5.0f%%\t", cpu)
ret += fmt.Sprintf("%s", KNRM)
ret += fmt.Sprintf("%s", condIf( mem<0,KRED, condIf(mem>0.8, KRED, condIf(mem>0.6, KYEL, KGRN))))
ret += fmt.Sprintf("%5.1f%%\t", mem*100.0)
ret += fmt.Sprintf("%s", KNRM)
fload1 := load1 / float64(cpu_num)
fload5 := load5 / float64(cpu_num)
fload15 := load15 / float64(cpu_num)
ret += fmt.Sprintf("%s", condIf( fload1<0,KRED, condIf(fload1>0.8, KRED, condIf(fload1>0.6, KYEL, KGRN))))
ret += fmt.Sprintf("%4.1f ", load1)
ret += fmt.Sprintf("%s", condIf( fload5<0,KRED, condIf(fload5>0.8, KRED, condIf(fload5>0.6, KYEL, KGRN))))
ret += fmt.Sprintf("%4.1f ", load5)
ret += fmt.Sprintf("%s", condIf( fload15<0,KRED, condIf(fload15>0.8, KRED, condIf(fload15>0.6, KYEL, KGRN))))
ret += fmt.Sprintf("%4.1f", load15)
ret += fmt.Sprintf("%s", KNRM)
return ret
} else {
return fmt.Sprintf("%-23s\t%20s%5.0f%%\t%5.1f%%\t%4.1f %4.1f %4.1f", host.Name, stime, cpu, mem*100.0, load1, load5, load15)
}
}
func readStream(reader io.Reader) ([]byte, error) {
@ -121,7 +169,7 @@ func main() {
return
}
useColors := terminal.IsTerminal(int(os.Stdout.Fd()))
for _, arg := range(args[1:]) {
port := 8649
remote := arg
@ -161,7 +209,7 @@ func main() {
fmt.Printf("%-23s\t%20s%7s\t%7s\t%16s\n", "Host", "Last Update", "CPU", "Memory", "Load (1-5-15)")
fmt.Println("--------------------------------------------------------------------------------")
for _, host := range hosts {
fmt.Printf("%s\n", HostRow(host))
fmt.Printf("%s\n", HostRow(host, useColors))
}
fmt.Println("--------------------------------------------------------------------------------")
}

Loading…
Cancel
Save