Compare commits

...

17 commits

Author SHA1 Message Date
Augusto Dwenger J. 381dd4819d
Merge pull request #7 from hamburghammer/docs
Add Docs
2020-11-25 14:06:49 +01:00
Augusto Dwenger 1c9b053ec0 Fix link to the competition details 2020-11-21 16:58:05 +01:00
Augusto Dwenger 874960b897 Add note to the usage section regarding the flags 2020-11-21 16:58:05 +01:00
Augusto Dwenger 7698718458 Replace competition terms with installation, usage etc documentation 2020-11-21 16:58:05 +01:00
Augusto Dwenger e6ab66ad2e Add competition terms in side an own file 2020-11-21 16:57:56 +01:00
Augusto Dwenger J. 676c0ae573
Fix wrong argument descriptions 2020-11-15 20:25:31 +01:00
Augusto Dwenger J. ecd40f940d
Merge pull request #6 from hamburghammer/develop
Change memory representation
2020-11-15 20:08:18 +01:00
Augusto Dwenger fefa87ad52 Fix printing the help message twice 2020-11-10 14:50:31 +01:00
Augusto Dwenger 966735a184 Update CI scripts
Add test coverage output.
Add race detector step.
Disable cgo for the build
2020-11-09 21:36:01 +01:00
Augusto Dwenger 7b1a54d697 Change usage representation of memory field
Changes apply to Disk and Mem. The show the state as an object.
This should improve the readablility.
2020-11-09 21:27:18 +01:00
Augusto Dwenger J. d27183d362 Add go report card badge 2020-10-20 16:47:04 +02:00
Augusto Dwenger J. 8e32c292a5 Fix misspells 2020-10-20 16:44:59 +02:00
Augusto Dwenger 66abdebf3b Fix Copyright section 2020-08-10 20:23:36 +02:00
Augusto Dwenger e62c4853c9 Merge branch 'feature/date' into develop 2020-08-10 20:04:14 +02:00
Augusto Dwenger 0b81a55351 Move Date to the beginning of the JSON 2020-08-10 20:02:24 +02:00
Augusto Dwenger 68eaf1d195 Implement date as command for the json output 2020-08-09 17:52:20 +02:00
Augusto Dwenger 421faacb21 Add name and year to the license 2020-08-07 12:22:13 +02:00
14 changed files with 195 additions and 71 deletions

View file

@ -2,13 +2,22 @@ kind: pipeline
name: default
steps:
- name: test
- name: unit-test
image: golang
volumes:
- name: deps
- name: cache
path: /go
commands:
- go test -cover ./...
- go test -coverprofile=coverage.out -covermode=count ./...
- go tool cover -func=coverage.out | grep total
- name: race-test
image: golang
volumes:
- name: cache
path: /go
commands:
- go test -race ./...
- name: build
image: golang
@ -16,8 +25,8 @@ steps:
- name: deps
path: /go
commands:
- go build
- CGO_ENABLED=0 go build
volumes:
- name: deps
- name: cache
temp: {}

View file

@ -1,4 +1,4 @@
MIT License Copyright (c) <year> <copyright holders>
MIT License Copyright (c) 2020 Augusto Dwenger J.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -1,66 +1,51 @@
# gstat
[![Build Status](https://cloud.drone.io/api/badges/hamburghammer/gstat/status.svg?ref=refs/heads/master)](https://cloud.drone.io/hamburghammer/gstat)
[![Go Report Card](https://goreportcard.com/badge/github.com/hamburghammer/gstat)](https://goreportcard.com/report/github.com/hamburghammer/gstat)
Is a tool to get the system stats in a parsable format.
The tool is part of the competition with [Niklas](https://github.com/nhh).
Is a cli tool to get some system stats in a machine parsable format (JSON). It supports only Linux but might also run on some UNIX based operation systems.
Checkout his solution: [cstat](https://github.com/nhh/cstat)
## Terms of the competition
This tool is part of a competition with [nhh](https://github.com/nhh) -> [Details](docs/competition.md)
### Allgemein:
**WIP: expect some changes for the stats gathering**
Name des Programms:
- cstat
- gstat
## Features
- Easy to use
- Single executable
- Runs on Linux
Das Tool kann mehrere Metriken auf einmal zurückgeben.
## Installation
For the time there are no binaries provided this means you need to install it through go.
### Anforderung:
Requirements:
- Go is installed.
- You have the `$GOPATH` defined.
- The `$GOPATH/bin` directory is in your `$PATH`.
- Aktuelle CPU Auslastung
- Gesamtverbrauch aller CPU Kerne in %
- Die nach cpu sortierten Prozesse als Liste
- Aktueller Speicherplatzverbrauch
-used / free in megabyte
- Aktueller RAM Verbrauch
- used / free in megabyte
- Healthchecks mit Latency
- http / https / *ICMP*
- GET /
- *Aktueller Network IO (optional)*
- *Disk IO (optional)*
- JSON Output
- Datum und Uhrzeit im ISO Format
Install and update it with `go get -u github.com/hamburghammer/gstat`.
### Kriterien:
## Usage
```
Usage:
gstat [OPTIONS]
- Single Executable
- Linux
Application Options:
-c, --cpu Include the total CPU consumption.
-m, --mem Include the RAM usage.
-d, --disk Include the Disk usage.
-p, --proc Include the top 10 running processes with the highest CPU consumption.
--health= Make a healthcheck call against the URI.
### Bewertungs:
Help Options:
-h, --help Show this help message
```
*not all flags a jet supported or fully implemented!
1. Wie groß ist das Binary
2. Performance von Befehlen
3. Cpu Auslastung
4. Ram Auslastung
example output:
### Kommandozeilenaufrufe:
Alphabetische Reihenfolge
-c -h -d // --healtheck=http://example.com --disk --format=json
[https://de.wikipedia.org/wiki/Uniform_Resource_Identifier](https://de.wikipedia.org/wiki/Uniform_Resource_Identifier)
`$ cstat/gstat --cpu --format=json`
`$ cstat/gstat --metric=disk`
`$ cstat/gstat --check`
#### Goals:
cstat/gstat -c -d -i -h https://my-server.com > log.json
curl -X POST -d $(cstat/gstat -c -d -i -h https://my-server.com) https://my-logging.com/logs
`gstat -cmd`
```json
{"Date":"2020-11-21T16:32:18+01:00","CPU":3.49999999997029,"mem":{"used":5777,"total":16022},"disk":{"used":90319,"total":224323}}
```

View file

@ -2,6 +2,8 @@ package args
import (
"errors"
"fmt"
"os"
"strings"
"github.com/jessevdk/go-flags"
@ -9,11 +11,11 @@ import (
// Arguments represent the flags given at program start.
type Arguments struct {
CPU bool `short:"c" long:"cpu" description:"Include the total CPU consumption"`
Mem bool `short:"m" long:"mem" description:"Include the total RAM consumption"`
Disk bool `short:"d" long:"disk" description:"Include the total CPU consumption"`
Processes bool `short:"p" long:"proc" description:"Include the top 10 processes"`
Health string `long:"health" description:"Make a healthcheck call against the URI"`
CPU bool `short:"c" long:"cpu" description:"Include the total CPU consumption."`
Mem bool `short:"m" long:"mem" description:"Include the RAM usage."`
Disk bool `short:"d" long:"disk" description:"Include the Disk usage."`
Processes bool `short:"p" long:"proc" description:"Include the top 10 running processes with the highest CPU consumption."`
Health string `long:"health" description:"Make a healthcheck call against the URI."`
rest []string
}
@ -61,7 +63,12 @@ func Parse() Arguments {
_, err := flags.Parse(&args)
if err != nil {
panic(err)
if _, ok := err.(*flags.Error); ok {
os.Exit(1)
} else {
fmt.Println(err)
os.Exit(1)
}
}
return args

31
commands/date.go Normal file
View file

@ -0,0 +1,31 @@
package commands
import (
"encoding/json"
"time"
"github.com/hamburghammer/gstat/args"
)
// Date is a the configuration struct to execute the date command
type Date struct {
// GetTime is the function to get the actual Time in string format.
// It should be use to replace/cusomise the time output.
GetTime func() string
}
// NewDate is a convinice constructor for the Date struct.
// It sets the GetTime function to standard formatting.
func NewDate() Date {
return Date{GetTime: getFormattedTime}
}
// Exec is the implementation of the execution interface for the Date struct.
func (d Date) Exec(args args.Arguments) ([]byte, error) {
data := struct{ Date string }{Date: d.GetTime()}
return json.Marshal(data)
}
func getFormattedTime() string {
return time.Now().Format(time.RFC3339)
}

22
commands/date_test.go Normal file
View file

@ -0,0 +1,22 @@
package commands_test
import (
"testing"
"github.com/hamburghammer/gstat/args"
"github.com/hamburghammer/gstat/commands"
"github.com/stretchr/testify/assert"
)
func TestDate(t *testing.T) {
t.Run("", func(t *testing.T) {
customTime := func() string { return "2020-08-09T17:43:31+02:00" }
date := commands.Date{GetTime: customTime}
got, err := date.Exec(args.Arguments{})
want := "{\"Date\":\"2020-08-09T17:43:31+02:00\"}"
assert.Nil(t, err)
assert.Equal(t, want, string(got))
})
}

View file

@ -2,7 +2,6 @@ package commands
import (
"encoding/json"
"fmt"
"github.com/hamburghammer/gstat/args"
goDisk "github.com/shirou/gopsutil/disk"
@ -29,6 +28,8 @@ func (d Disk) Exec(args args.Arguments) ([]byte, error) {
return []byte{}, err
}
data := struct{ Disk string }{Disk: fmt.Sprintf("%d/%d", bytesToMegaByte(usage.Used), bytesToMegaByte(usage.Total))}
data := struct {
Disk Memory `json:"disk"`
}{Disk: Memory{Used: bytesToMegaByte(usage.Used), Total: bytesToMegaByte(usage.Total)}}
return json.Marshal(data)
}

View file

@ -41,7 +41,7 @@ func TestDiskExec(t *testing.T) {
}}
got, err := disk.Exec(args.Arguments{Disk: true})
want := "{\"Disk\":\"47/95\"}"
want := "{\"disk\":{\"used\":47,\"total\":95}}"
assertNoError(err, t)

View file

@ -2,12 +2,17 @@ package commands
import (
"encoding/json"
"fmt"
"github.com/hamburghammer/gstat/args"
"github.com/shirou/gopsutil/mem"
)
// Memory usage representation.
type Memory struct {
Used uint64 `json:"used"`
Total uint64 `json:"total"`
}
// Mem holds the memory usage for the json transformation
type Mem struct {
ReadVirtualMemoryStat func() (*mem.VirtualMemoryStat, error)
@ -29,8 +34,10 @@ func (m Mem) Exec(args args.Arguments) ([]byte, error) {
return []byte{}, err
}
usage := fmt.Sprintf("%d/%d", bytesToMegaByte(mem.Used), bytesToMegaByte(mem.Total))
data := struct{ Mem string }{usage}
usage := Memory{Used: bytesToMegaByte(mem.Used), Total: bytesToMegaByte(mem.Total)}
data := struct {
Mem Memory `json:"mem"`
}{usage}
return json.Marshal(data)
}

View file

@ -41,7 +41,7 @@ func TestMemExec(t *testing.T) {
}}
got, err := disk.Exec(args.Arguments{Mem: true})
want := "{\"Mem\":\"47/95\"}"
want := "{\"mem\":{\"used\":47,\"total\":95}}"
assertNoError(err, t)

View file

@ -123,7 +123,7 @@ func TestGetProcessesCPUInfos(t *testing.T) {
assert.Equal(t, want, got)
})
t.Run("return error directly if one happends", func(t *testing.T) {
t.Run("return error directly if one happens", func(t *testing.T) {
processes := []*Process{{Pid: 2, Name: nameFunc, CPUPercent: cpuProcessFunc}, {Pid: 1, Name: nameErrFunc, CPUPercent: cpuProcessFunc}}
got, gotErr := getProcessesCPUInfos(processes)
want := []cpuProcess{{Name: "foo", CPU: 0, Pid: 2}}

View file

@ -45,7 +45,7 @@ func TestResultEquals(t *testing.T) {
}
func TestExecCommands(t *testing.T) {
t.Run("should return string array without opening and closeing brackets", func(t *testing.T) {
t.Run("should return string array without opening and closing brackets", func(t *testing.T) {
arguments := args.Arguments{}
result := commands.NewResult(arguments)

56
docs/competition.md Normal file
View file

@ -0,0 +1,56 @@
# Competition
The tool is part of the competition with [Niklas](https://github.com/nhh).
Checkout his solution: [cstat](https://github.com/nhh/cstat)
## Terms of the competition
### Allgemein:
Name des Programms:
- cstat
- gstat
Das Tool kann mehrere Metriken auf einmal zurückgeben.
### Anforderung:
- Aktuelle CPU Auslastung
- Gesamtverbrauch aller CPU Kerne in %
- Die nach cpu sortierten Prozesse als Liste
- Aktueller Speicherplatzverbrauch
-used / free in megabyte
- Aktueller RAM Verbrauch
- used / free in megabyte
- Healthchecks mit Latency
- http / https / *ICMP*
- GET /
- *Aktueller Network IO (optional)*
- *Disk IO (optional)*
- JSON Output
- Datum und Uhrzeit im ISO Format
### Kriterien:
- Single Executable
- Linux
### Bewertungs:
1. Wie groß ist das Binary
2. Performance von Befehlen
3. Cpu Auslastung
4. Ram Auslastung
### Kommandozeilenaufrufe:
Alphabetische Reihenfolge
-c -h -d // --healtheck=http://example.com --disk --format=json
[https://de.wikipedia.org/wiki/Uniform_Resource_Identifier](https://de.wikipedia.org/wiki/Uniform_Resource_Identifier)
`$ cstat/gstat --cpu --format=json`
`$ cstat/gstat --metric=disk`
`$ cstat/gstat --check`
#### Goals:
cstat/gstat -c -d -i -h https://my-server.com > log.json
curl -X POST -d $(cstat/gstat -c -d -i -h https://my-server.com) https://my-logging.com/logs

View file

@ -11,7 +11,13 @@ func main() {
args := args.Parse()
result := commands.NewResult(args)
executs := []commands.Executor{commands.NewCPU(), commands.NewMem(), commands.NewDisk(), commands.NewProcesses()}
executs := []commands.Executor{
commands.NewDate(),
commands.NewCPU(),
commands.NewMem(),
commands.NewDisk(),
commands.NewProcesses(),
}
output := result.ExecCommands(executs)
fmt.Println(formatToJSON(output.Collection.Results))