Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjaminBoehm committed Oct 25, 2024
0 parents commit 35f718d
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Main

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
22 changes: 22 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: release

on:
release:
types: [created]

permissions:
contents: write
packages: write

jobs:
release-linux-amd64:
name: release linux/amd64
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: wangyoucao577/go-release-action@v1
with:
github_token: ${{ secrets.ACTIONS_KEY }}
goos: linux
goarch: amd64
compress_assets: FALSE
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode/
10 changes: 10 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: lint vet

build:
go build -o check_ecs_snapshots main.go
lint:
go fmt $(go list ./... | grep -v /vendor/)
vet:
go vet $(go list ./... | grep -v /vendor/)
test:
go test -v -cover ./...
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Description
Check for Elastic Cloud Server (ECS) snapshot age on Open Telekom Cloud (OTC)

# Environment

* Option 1: clouds.yaml
* create config file (~/.config/openstack/clouds.yaml or /etc/openstack/clouds.yaml)
```yaml
---
clouds:
myCloud:
auth:
username: '<USERNAME>'
password: '<PASSWORD>'
project_name: 'eu-de'
user_domain_name: '<DOMAIN_NAME>'
auth_url: 'https://iam.eu-de.otc.t-systems.com/v3'
interface: 'public'
identity_api_version: 3 # !Important
```
* set env which cloud credentials should be used
```bash
export OS_CLOUD=myCloud
```

* Option 2: Environment Variables
```bash
export OS_PROJECT_NANME=eu-de
export OS_AUTH_URL="https://iam.eu-de.otc.t-systems.com:443/v3"
export OS_USER_DOMAIN_NAME="<DOMAIN_NAME>"
export OS_USERNAME="<USERNAME>"
export OS_PASSWORD="<PASSWORD>"
```

# Run
```bash
go run cmd/main.go
```

# Build
```bash
make build
```

# Dependencies

* OTC Cloud SDK: https://github.com/opentelekomcloud/gophertelekomcloud/
* Netways monitoring plugins library: https://github.com/NETWAYS/go-check
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/BenjaminBoehm/check-ecs-snapshots

go 1.23.2

require (
github.com/NETWAYS/go-check v0.6.2
github.com/opentelekomcloud/gophertelekomcloud v0.9.3
)

require (
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
github.com/NETWAYS/go-check v0.6.2 h1:H/9Q/+rVzLbSUV/6QS9eDhyMUml7sf67TF4nSuHBwJo=
github.com/NETWAYS/go-check v0.6.2/go.mod h1:qx8m3s2Xul9wqfUjrCSArEFkOdtwxIyUmBYYrCkqA/8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/opentelekomcloud/gophertelekomcloud v0.9.3 h1:zdttgRAWc4uHgJ3PX5hP8ulhT1VYBh2JeRsItNPp8dg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.3/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
83 changes: 83 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"fmt"
"log"
"time"

check "github.com/NETWAYS/go-check"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cbr/v3/backups"
)

func main() {
config := check.NewConfig()
config.Name = "check-ecs-snapshots"
config.Readme = `Check for Elastic Cloud Server (ECS) snapshot age on Open Telekom Cloud (OTC)`
config.Version = "1.0.0"

critical := config.FlagSet.IntP("critical", "c", 60, "critical threshold for age of snapshot in days")
warning := config.FlagSet.IntP("warning", "w", 30, "warning threshold for age of snapshot in days")

config.ParseArguments()

env := openstack.NewEnv("OS_", true)
cloud, err := env.Cloud()

if err != nil {
panic(err)
}

providerClient, err := openstack.AuthenticatedClientFromCloud(cloud)

if err != nil {
log.Fatalf("Can't authenticate: %s", err)
}

cbr, err := openstack.NewCBRService(providerClient, golangsdk.EndpointOpts{})

if err != nil {
log.Fatalf("Problem with cbr Service: %s", err)
}

snaps, err := backups.List(cbr, backups.ListOpts{})

if err != nil {
log.Fatal(err)
}
currentTime := time.Now()
layout := "2006-01-02T15:04:05.999999"

criticalCount := 0
warningCount := 0
okCount := 0

for _, s := range snaps {
createdAtParsed, err := time.Parse(layout, s.CreatedAt)
if err != nil {
fmt.Println("Could not parse time:", err, createdAtParsed, currentTime)
}

duration := currentTime.Sub(createdAtParsed)
daysDiff := int(duration.Hours() / 24)

if *critical <= daysDiff {
criticalCount += 1

} else if *warning <= daysDiff {
warningCount += 1
} else {
okCount += 1
}
}

if criticalCount > 0 {
check.Exitf(check.Critical, "%d Snapshots older than %d days\n", criticalCount, *critical)
} else if warningCount > 0 {
check.Exitf(check.Warning, "%d Snapshots older than %d days\n", warningCount, *warning)
} else {
check.Exitf(check.OK, "No snapshots (%d) older than %d days\n", okCount, *warning)
}

}

0 comments on commit 35f718d

Please sign in to comment.