mirror of
https://github.com/hamburghammer/gstat.git
synced 2024-05-17 04:34:37 +02:00
Compare commits
17 commits
Author | SHA1 | Date | |
---|---|---|---|
Augusto Dwenger J. | 381dd4819d | ||
1c9b053ec0 | |||
874960b897 | |||
7698718458 | |||
e6ab66ad2e | |||
Augusto Dwenger J. | 676c0ae573 | ||
Augusto Dwenger J. | ecd40f940d | ||
fefa87ad52 | |||
966735a184 | |||
7b1a54d697 | |||
Augusto Dwenger J. | d27183d362 | ||
Augusto Dwenger J. | 8e32c292a5 | ||
66abdebf3b | |||
e62c4853c9 | |||
0b81a55351 | |||
68eaf1d195 | |||
421faacb21 |
19
.drone.yml
19
.drone.yml
|
@ -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: {}
|
2
LICENSE
2
LICENSE
|
@ -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
|
||||
|
|
83
README.md
83
README.md
|
@ -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}}
|
||||
```
|
||||
|
|
19
args/args.go
19
args/args.go
|
@ -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
31
commands/date.go
Normal 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
22
commands/date_test.go
Normal 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))
|
||||
})
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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}}
|
||||
|
|
|
@ -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
56
docs/competition.md
Normal 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
|
||||
|
8
main.go
8
main.go
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue