Skip to content

Commit

Permalink
implemented it to the point where it's functional for now.
Browse files Browse the repository at this point in the history
detailed error handling, preparation of thorough test code, and tidying up the formatting will be addressed later.
  • Loading branch information
tukaelu committed Nov 2, 2023
1 parent f2aafab commit 97bb05b
Show file tree
Hide file tree
Showing 21 changed files with 1,123 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
day: "sunday"
time: "09:00"
timezone: "Asia/Tokyo"
reviewers:
- "tukaelu"
open-pull-requests-limit: 5
labels:
- "dependencies"
- package-ecosystem: gomod
directory: "/"
schedule:
interval: "weekly"
day: "sunday"
time: "09:00"
timezone: "Asia/Tokyo"
open-pull-requests-limit: 5
35 changes: 35 additions & 0 deletions .github/workflow/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
name: test
on:
pull_request: {}
push:
branches: [main]
tags:
- "v[0-9]+.[0-9]+.[0-9]+"
env:
DEBIAN_FRONTEND: noninteractive
TZ: 'Asia/Tokyo'
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: golangci/golangci-lint-action@v3
test:
strategy:
matrix:
os: ['ubuntu-latest', 'macOS-latest', 'windows-latest']
go: ["1.21.x", "1.20.x"]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- uses: actions/checkout@v4
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- run: go test -v ./...
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist/
*.yml
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PROG_NAME ?= ikesu
VERSION ?= $(shell git describe --tags --abbrev=0)
REVISION := $(shell git rev-parse --short HEAD)

OUTPUT_DIR ?= dist

OS := $(shell go env GOOS)
ARCH := $(shell go env GOARCH)
BUILD_LDFLAGS := "-X main.version=$(VERSION) -X main.revision=$(REVISION)"

SOURCES = $(shell find . -type f -name '*.go')

.PHONY: test
test:
go clean -testcache
go test -v ./...

lint:
golangci-lint run

build: $(OUTPUT_DIR) $(SOURCES)
go mod tidy
GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build -ldflags=$(BUILD_LDFLAGS) -o $(OUTPUT_DIR)/$(PROG_NAME) ./cmd/$(PROG_NAME)/

clean:
rm -r $(OUTPUT_DIR)/*

$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)
6 changes: 6 additions & 0 deletions cmd/ikesu/loaders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

// Register as loader.
import (
_ "github.com/tukaelu/ikesu/internal/config/loader/file"
)
131 changes: 131 additions & 0 deletions cmd/ikesu/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package main

import (
"context"
"fmt"
"os"
"os/signal"
"strings"
"syscall"

"github.com/aws/aws-lambda-go/lambda"
"github.com/mackerelio/mackerel-client-go"
"github.com/urfave/cli/v2"

"github.com/tukaelu/ikesu/cmd/ikesu/subcommand"
"github.com/tukaelu/ikesu/internal/config"
"github.com/tukaelu/ikesu/internal/constants"
"github.com/tukaelu/ikesu/internal/logger"
)

var version string
var revision string

func main() {
cli := &cli.App{
Name: "ikesu",
Usage: "We monitor the health of the fish in the \"Ikesu\".",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "mackerel-apikey",
DefaultText: "**********",
EnvVars: []string{"MACKEREL_APIKEY", "IKESU_MACKEREL_APIKEY"},
Required: true,
},
},
Commands: []*cli.Command{
{
Name: "checker",
Usage: "Detects disruptions in posted metrics and notifies the host as a CRITICAL alert.",
UsageText: "ikesu checker -config <config file> [-dry-run]",
Action: func(ctx *cli.Context) error {

// Show the provider name and metric name, then terminate.
if ctx.Bool("show-providers") {
showProvidersInspectionMetricMap()
return nil
}

var l *logger.Logger
var err error
if l, err = logger.NewDefaultLogger("info", ctx.Bool("dry-run")); err != nil {
return err
}

config, err := config.NewCheckerConfig(ctx.Context, ctx.String("config"))
if err != nil {
return err
}
if err := config.Validate(); err != nil {
return err
}
checker := &subcommand.Checker{
Config: config,
Client: mackerel.NewClient(ctx.String("mackerel-apikey")),
DryRun: ctx.Bool("dry-run"),
Logger: l,
}

// wrap function
handler := func(ctx context.Context) error {
return checker.Run(ctx)
}
l.Log.Info("Run command", "version", ctx.App.Version)
l.Log.V(1).Info(fmt.Sprintf("config: %+v", config))

if isLambda() {
lambda.StartWithOptions(handler, lambda.WithContext(ctx.Context))
return nil
}
return handler(ctx.Context)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Usage: "Specify the path to the configuration file.",
Aliases: []string{"c"},
EnvVars: []string{"IKESU_CHECKER_CONFIG"},
},
&cli.BoolFlag{
Name: "show-providers",
Usage: "List the inspection metric names corresponding to the provider for each integration.",
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "Only a simplified display of the check results is performed, and no alerts are issued.",
},
},
},
},
}

cli.Version = fmt.Sprintf("%s (rev.%s)", version, revision)

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
defer cancel()

if err := cli.RunContext(ctx, os.Args); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}

func isLambda() bool {
return os.Getenv("AWS_EXECUTION_ENV") != "" || os.Getenv("AWS_LAMBDA_RUNTIME_API") != ""
}

func showProvidersInspectionMetricMap() {
integrations := constants.GetIntegrations()
providers := constants.GetProviders()
immap := constants.GetProvidersInspectionMetricMap()
for i := 0; i < len(integrations); i++ {
fmt.Printf("Integration: %s\n", integrations[i])
fmt.Println(strings.Repeat("-", 30))
for _, key := range providers {
if metric, ok := immap[integrations[i]][key]; ok {
fmt.Printf("provider: %-20s, metric: %s\n", key, metric)
}
}
fmt.Println("")
}
}
Loading

0 comments on commit 97bb05b

Please sign in to comment.