2020-06-14 14:26:08 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-02 18:10:16 +01:00
|
|
|
"encoding/json"
|
2020-06-14 14:26:08 +02:00
|
|
|
"fmt"
|
2021-01-02 18:10:16 +01:00
|
|
|
"log"
|
2020-06-14 14:26:08 +02:00
|
|
|
|
|
|
|
"github.com/hamburghammer/gstat/args"
|
|
|
|
"github.com/hamburghammer/gstat/commands"
|
2021-01-02 18:10:16 +01:00
|
|
|
"github.com/hamburghammer/gstat/output"
|
2020-06-14 14:26:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
args := args.Parse()
|
|
|
|
|
2021-01-02 18:10:16 +01:00
|
|
|
output, err := newOutput(args)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
func oldOutput(args args.Arguments) string {
|
2020-06-14 14:26:08 +02:00
|
|
|
result := commands.NewResult(args)
|
2020-08-09 17:52:20 +02:00
|
|
|
executs := []commands.Executor{
|
2020-08-10 20:00:23 +02:00
|
|
|
commands.NewDate(),
|
2020-08-09 17:52:20 +02:00
|
|
|
commands.NewCPU(),
|
|
|
|
commands.NewMem(),
|
|
|
|
commands.NewDisk(),
|
|
|
|
commands.NewProcesses(),
|
|
|
|
}
|
2020-06-14 14:26:08 +02:00
|
|
|
output := result.ExecCommands(executs)
|
|
|
|
|
2021-01-02 18:10:16 +01:00
|
|
|
return formatToJSON(output.Collection.Results)
|
2020-06-14 14:26:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func formatToJSON(strings []string) string {
|
|
|
|
stringBuilder := "{"
|
|
|
|
elements := len(strings)
|
|
|
|
|
|
|
|
for i, s := range strings {
|
|
|
|
stringBuilder = stringBuilder + s
|
|
|
|
if i != (elements - 1) {
|
|
|
|
stringBuilder = stringBuilder + ","
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stringBuilder = stringBuilder + "}"
|
|
|
|
|
|
|
|
return stringBuilder
|
|
|
|
}
|
2021-01-02 18:10:16 +01:00
|
|
|
|
|
|
|
func newOutput(args args.Arguments) ([]byte, error) {
|
|
|
|
result := output.NewResult(args)
|
|
|
|
col, errs := result.GatherResults()
|
|
|
|
if len(errs) != 0 {
|
|
|
|
for _, err := range errs {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return []byte{}, fmt.Errorf("Collection the stats produced an error")
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(col)
|
|
|
|
}
|