Skip to content

Commit

Permalink
Merge pull request #15 from inexio/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TheFireMike authored Mar 2, 2021
2 parents 0d0378d + 70e7a83 commit 8fa0505
Show file tree
Hide file tree
Showing 91 changed files with 482 additions and 374 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
go-version: ^1.16

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand All @@ -25,9 +25,6 @@ jobs:
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Generate configs
run: go generate
- name: Build main
run: go build -v .
Expand Down
13 changes: 0 additions & 13 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,4 @@ jobs:
uses: grandcolline/golang-github-actions@v1.1.0
with:
run: fmt
token: ${{ secrets.GITHUB_TOKEN }}

vet:
name: vet
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Generate configs
run: go generate
- name: check
uses: grandcolline/golang-github-actions@v1.1.0
with:
run: vet
token: ${{ secrets.GITHUB_TOKEN }}
8 changes: 1 addition & 7 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.14

- name: Generate configs
run: go generate

- name: Tidy go mod
run: go mod tidy
go-version: ^1.16

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
go-version: ^1.16

- name: Check out code into the Go module directory
uses: actions/checkout@v2
Expand All @@ -25,9 +25,6 @@ jobs:
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
- name: Generate configs
run: go generate
- name: Run tests
run: cd test && go test
Expand Down
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
FROM golang:latest AS build
WORKDIR /go/src/thola
COPY . .
RUN go generate
RUN CGO_ENABLED=0 go build -v -o thola .

FROM alpine:latest
Expand Down
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ You can download the latest compiled version for your platform under the "Releas

git clone https://github.com/inexio/thola.git
cd thola
go generate
go build

If you also want to build the client binary, which can be used for sending requests to a running Thola API, use the following build command:
Expand Down Expand Up @@ -71,7 +70,7 @@ Use the `identify` mode to automatically discover some properties of a network d
Specify the address of the network device using the `--ip` flag.
The `--format` flag modifies the format of the output. `--format pretty` is set by default and is useful when reading the output manually. Other options are `json` and `xml`.

$ thola identify --ip 10.204.2.90
$ thola identify 10.204.2.90

Device:
Class: ceraos/ip10
Expand All @@ -82,7 +81,7 @@ The `--format` flag modifies the format of the output. `--format pretty` is set
OSVersion: 2.9.25-1
Next we want to print the interfaces of the network device and their relevant data. We use the `read interfaces` command for this.

$ thola read interfaces --ip 10.204.2.90
$ thola read interfaces 10.204.2.90

Interfaces: [8]
IfIndex: 1
Expand Down Expand Up @@ -117,7 +116,7 @@ Thola can be executed as a REST API. You can start the API using the `api` comma

For sending requests to the Thola API you can use the Thola client. When executing the Thola client you can specify the address of the API with the `--target-api` flag.

$ thola-client identify --ip 10.204.2.90 --target-api http://192.168.10.20:8237
$ thola-client identify 10.204.2.90 --target-api http://192.168.10.20:8237

Device:
Class: ceraos/ip10
Expand Down
2 changes: 1 addition & 1 deletion api/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"strconv"
"time"
Expand Down
2 changes: 1 addition & 1 deletion api/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"github.com/inexio/thola/core/tholaerr"
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/ulule/limiter/v3"
Expand Down
35 changes: 17 additions & 18 deletions api/request_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/inexio/thola/core/database"
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/tholaerr"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"net/http"
Expand All @@ -27,9 +27,10 @@ var deviceLocks struct {

// StartAPI starts the API.
func StartAPI() {
log.Trace().Msg("starting the server")
ctx := log.Logger.WithContext(context.Background())

log.Ctx(ctx).Trace().Msg("starting the server")

ctx := context.Background()
db, err := database.GetDB(ctx)
if err != nil {
log.Fatal().Err(err).Msg("starting the server failed")
Expand All @@ -46,7 +47,7 @@ func StartAPI() {
" \\/_/ \\/_/\\/_/ \\/_____/ \\/_____/ \\/_/\\/_/\n\n")

if (viper.GetString("api.username") != "") && (viper.GetString("api.password") != "") {
log.Trace().Msg("set authorization for api")
log.Ctx(ctx).Trace().Msg("set authorization for api")
e.Use(middleware.BasicAuth(func(username, password string, c echo.Context) (bool, error) {
// Be careful to use constant time comparison to prevent timing attacks
if subtle.ConstantTimeCompare([]byte(username), []byte(viper.GetString("restapi.username"))) == 1 &&
Expand All @@ -58,7 +59,7 @@ func StartAPI() {
}

if viper.GetString("api.ratelimit") != "" {
log.Trace().Msg("set ratelimit for api")
log.Ctx(ctx).Trace().Msg("set ratelimit for api")
e.Use(ipRateLimit())
}

Expand Down Expand Up @@ -563,16 +564,14 @@ func StartAPI() {
err = e.Start(":" + viper.GetString("api.port"))
}

log.Trace().Msg("closing connection to the database")

if dbErr := db.CloseConnection(ctx); dbErr != nil {
log.Err(dbErr).Msg("failed to close connection to the db")
log.Ctx(ctx).Err(dbErr).Msg("failed to close connection to the db")
}

if err != nil && err == http.ErrServerClosed {
log.Info().Msg("shutting down the server")
log.Ctx(ctx).Info().Msg("shutting down the server")
} else {
log.Fatal().Err(err).Msg("unexpected server error")
log.Ctx(ctx).Fatal().Err(err).Msg("unexpected server error")
}
}()

Expand All @@ -582,13 +581,13 @@ func StartAPI() {
signal.Notify(quit, os.Interrupt)
<-quit

log.Trace().Msg("received shutdown signal")
log.Ctx(ctx).Trace().Msg("received shutdown signal")

ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()

if err = e.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("shutting down the server failed")
log.Ctx(ctx).Fatal().Err(err).Msg("shutting down the server failed")
}
}

Expand Down Expand Up @@ -849,19 +848,19 @@ func getDeviceLock(ip string) *sync.Mutex {
}

func handleAPIRequest(echoCTX echo.Context, r request.Request, ip *string) (request.Response, error) {
logger := log.With().Str("request_id", echoCTX.Request().Header.Get(echo.HeaderXRequestID)).Logger()
ctx := logger.WithContext(context.Background())

if ip != nil && !viper.GetBool("request.no-ip-lock") {
lock := getDeviceLock(*ip)
lock.Lock()
defer func() {
lock.Unlock()
log.Trace().Msg("unlocked IP " + *ip)
log.Ctx(ctx).Trace().Msg("unlocked IP " + *ip)
}()

log.Trace().Msg("locked IP " + *ip)
log.Ctx(ctx).Trace().Msg("locked IP " + *ip)
}

logger := log.With().Str("request_id", echoCTX.Request().Header.Get(echo.HeaderXRequestID)).Logger()
ctx := logger.WithContext(context.Background())

return request.ProcessRequest(ctx, r)
}
2 changes: 1 addition & 1 deletion api/request_id.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"github.com/rs/xid"
)

Expand Down
2 changes: 1 addition & 1 deletion api/statistics/statistics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package statistics

import (
"github.com/labstack/echo"
"github.com/labstack/echo/v4"
"math"
"net/http"
"sync"
Expand Down
37 changes: 35 additions & 2 deletions cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ var checkCMD = &cobra.Command{
},
}

func getCheckDeviceRequest() request.CheckDeviceRequest {
func getCheckDeviceRequest(host string) request.CheckDeviceRequest {
return request.CheckDeviceRequest{
BaseRequest: getBaseRequest(),
BaseRequest: getBaseRequest(host),
CheckRequest: getCheckRequest(),
}
}
Expand All @@ -63,3 +63,36 @@ func getCheckRequest() request.CheckRequest {
PrintPerformanceData: true,
}
}

func generateCheckThresholds(cmd *cobra.Command, warningMin, warningMax, criticalMin, criticalMax string) request.CheckThresholds {
var thresholds request.CheckThresholds
if flagName := warningMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMin = &v
}
if flagName := warningMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.WarningMax = &v
}
if flagName := criticalMin; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMin = &v
}
if flagName := criticalMax; flagName != "" && cmd.Flags().Changed(flagName) {
v, err := cmd.Flags().GetFloat64(flagName)
if err != nil {
log.Fatal().Err(err).Msgf("flag '%s' is not a float64", flagName)
}
thresholds.CriticalMax = &v
}
return thresholds
}
12 changes: 4 additions & 8 deletions cmd/check_cpu_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkCpuLoad)
checkCMD.AddCommand(checkCpuLoad)

checkCpuLoad.Flags().String("warning", "", "warning threshold for cpu load")
checkCpuLoad.Flags().String("critical", "", "critical threshold for cpu load")
checkCpuLoad.Flags().Float64("warning", 0, "warning threshold for cpu load")
checkCpuLoad.Flags().Float64("critical", 0, "critical threshold for cpu load")
}

var checkCpuLoad = &cobra.Command{
Expand All @@ -21,11 +20,8 @@ var checkCpuLoad = &cobra.Command{
"The usage will be printed as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckCPULoadRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
CPULoadThresholds: request.CheckThresholds{
WarningMax: value.New(cmd.Flags().Lookup("warning").Value),
CriticalMax: value.New(cmd.Flags().Lookup("critical").Value),
},
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
CPULoadThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
}
handleRequest(&r)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_hardware_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var checkHardwareHealthCMD = &cobra.Command{
Long: "Check hardware-health of a device and return various performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckHardwareHealthRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
}
handleRequest(&r)
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_identify.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var checkIdentifyCMD = &cobra.Command{
osVersion := cmd.Flags().Lookup("os-version").Value.String()

r := request.CheckIdentifyRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
Expectations: device.Device{
Class: cmd.Flags().Lookup("os").Value.String(),
Properties: device.Properties{
Expand Down
2 changes: 1 addition & 1 deletion cmd/check_interface_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var checkInterfaceMetricsCMD = &cobra.Command{
Long: "Reads all interface metrics and prints them as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckInterfaceMetricsRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
PrintInterfaces: viper.GetBool("checkInterfaceMetrics.print-interfaces"),
Filter: viper.GetStringSlice("checkInterfaceMetrics.filter"),
}
Expand Down
12 changes: 4 additions & 8 deletions cmd/check_memory_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package cmd

import (
"github.com/inexio/thola/core/request"
"github.com/inexio/thola/core/value"
"github.com/spf13/cobra"
)

func init() {
addDeviceFlags(checkMemoryUsage)
checkCMD.AddCommand(checkMemoryUsage)

checkMemoryUsage.Flags().String("warning", "", "warning threshold for memory usage")
checkMemoryUsage.Flags().String("critical", "", "critical threshold for system voltage")
checkMemoryUsage.Flags().Float64("warning", 0, "warning threshold for memory usage")
checkMemoryUsage.Flags().Float64("critical", 0, "critical threshold for system voltage")
}

var checkMemoryUsage = &cobra.Command{
Expand All @@ -21,11 +20,8 @@ var checkMemoryUsage = &cobra.Command{
"The usage will be printed as performance data.",
Run: func(cmd *cobra.Command, args []string) {
r := request.CheckMemoryUsageRequest{
CheckDeviceRequest: getCheckDeviceRequest(),
MemoryUsageThresholds: request.CheckThresholds{
WarningMax: value.New(cmd.Flags().Lookup("warning").Value),
CriticalMax: value.New(cmd.Flags().Lookup("critical").Value),
},
CheckDeviceRequest: getCheckDeviceRequest(args[0]),
MemoryUsageThresholds: generateCheckThresholds(cmd, "", "warning", "", "critical"),
}
handleRequest(&r)
},
Expand Down
Loading

0 comments on commit 8fa0505

Please sign in to comment.