diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml new file mode 100644 index 0000000..8be0695 --- /dev/null +++ b/.github/workflows/codeql.yml @@ -0,0 +1,70 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ master ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ master ] + schedule: + - cron: '39 13 * * 5' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://git.io/codeql-language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v1 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + # queries: ./path/to/local/query, your-org/your-repo/queries@main + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v1 + + # âšī¸ Command-line programs to run using the OS shell. + # đ https://git.io/JvXDl + + # âī¸ If the Autobuild fails above, remove it and uncomment the following three lines + # and modify them (or add more) to build your code if your project + # uses a compiled language + + #- run: | + # make bootstrap + # make release + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v1 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..5304066 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,30 @@ +name: goreleaser + +on: + push: + tags: + - '*' + branches: + - master + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Set up Go + uses: WillAbides/setup-go-faster@v1.7.0 + with: + go-version: 1.17 + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v2 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml new file mode 100644 index 0000000..b198097 --- /dev/null +++ b/.github/workflows/security.yml @@ -0,0 +1,27 @@ +name: "Security Scan" + +# Run workflow each time code is pushed to your repository and on a schedule. +# The scheduled workflow runs every at 00:00 on Sunday UTC time. +on: + push: + schedule: + - cron: '3 4 * * 0' + +jobs: + tests: + runs-on: ubuntu-latest + env: + GO111MODULE: on + steps: + - name: Checkout Source + uses: actions/checkout@v2 + - name: Run Gosec Security Scanner + uses: securego/gosec@master + with: + # we let the report trigger content trigger a failure using the GitHub Security features. + args: '-no-fail -fmt sarif -out results.sarif ./...' + - name: Upload SARIF file + uses: github/codeql-action/upload-sarif@v1 + with: + # Path to SARIF file relative to the root of the repository + sarif_file: results.sarif diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..3b9a2a7 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,56 @@ +--- +name: gotest +on: push + +jobs: + gotest: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up Go + uses: WillAbides/setup-go-faster@v1.7.0 + with: + go-version: 1.17 + + - name: Test Go Code + run: "go test ./..." + + - name: Run Go Vet + run: "go vet ./..." + + - name: Run StaticCheck + uses: dominikh/staticcheck-action@v1.1.0 + with: + version: "2021.1.1" + install-go: false + + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: latest + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the action will use pre-installed Go. + # skip-go-installation: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..ea9c2b5 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,31 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +--- +ci: + autofix_prs: true + skip: [] + submodules: false +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.2.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v1.2.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] + - repo: https://github.com/golangci/golangci-lint + rev: v1.44.0 # Keep this in-sync with `.tool-versions` + hooks: + - id: golangci-lint + - repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-beta.5 + hooks: + - id: go-fmt + - id: go-fmt-repo + - id: go-sec-mod + - id: go-build-mod + - id: go-mod-tidy diff --git a/CHANGELOG.md b/CHANGELOG.md index c11217a..bb162d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,19 +9,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 added: unit tests around the whole project changed: `collectors` package now use the API client changed: `online` package now provide an API client -changed: replaced `used` by `usage` in dedibackup usage metrics -changed: removed labels `id` and `sender` on dedibox_pending_abuse metrics -changed: removed label `id` on dedibox_ddos metric -changed: dedibox_ddos_count_total is now named dedibox_ddos_count -changed: dedibox_abuse_count_total is now named dedibox_pending_abuse_count -changed: dedibox_abuse is now named dedibox_pending_abuse -fixed: don't dynamically guess the abuse status as we're only retrieving pending abuses now -added: create a CHANGELOG file -fixed: metrics description consistency through the collectors and the README -added: command line flag to manage log level -changed: logging is now done through `sirupsen/logrus` -added: command line flag to select collectors to enable -added: metrics on ddos attacks -added: total abuse count metric -added: parse URL before querying it in the `online` module -changed: only fetch unresolved abuses +changed: replaced `used` by `usage` in dedibackup usage metrics +changed: removed labels `id` and `sender` on dedibox_pending_abuse metrics +changed: removed label `id` on dedibox_ddos metric +changed: dedibox_ddos_count_total is now named dedibox_ddos_count +changed: dedibox_abuse_count_total is now named dedibox_pending_abuse_count +changed: dedibox_abuse is now named dedibox_pending_abuse +fixed: don't dynamically guess the abuse status as we're only retrieving pending abuses now +added: create a CHANGELOG file +fixed: metrics description consistency through the collectors and the README +added: command line flag to manage log level +changed: logging is now done through `sirupsen/logrus` +added: command line flag to select collectors to enable +added: metrics on ddos attacks +added: total abuse count metric +added: parse URL before querying it in the `online` module +changed: only fetch unresolved abuses diff --git a/README.md b/README.md index 1015125..b2c1720 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A Prometheus Exporter for the [Dedibox API](https://console.online.net/fr/api/) ## Metrics -Note: for metrics like `abuse` or `ddos`, only the first page (latest infos) is fetched from the API to avoid overloading it. +Note: for metrics like `abuse` or `ddos`, only the first page (latest infos) is fetched from the API to avoid overloading it. The goal of this exporter is to create alerts on new DDoS attacks or unresolved abuses, not provide advanced statistic on your account. | Name | Description | Labels | @@ -31,5 +31,5 @@ Require `ONLINE_API_TOKEN` to be set with your Online.net's API token. | metric-path | /metrics | Path under which to expose metrics | ## Issues & Contribution -All bug report, packaging requests, features requests or PR are accepted. +All bug report, packaging requests, features requests or PR are accepted. I mainly created this exporter for my personal usage but I'll be happy to hear about your needs. diff --git a/collectors.go b/collectors.go index a8e2b87..7996b53 100644 --- a/collectors.go +++ b/collectors.go @@ -3,6 +3,8 @@ package main import ( "fmt" "strings" + + log "github.com/sirupsen/logrus" ) var validCollectors = []string{"abuse", "ddos", "plan", "dedibackup"} @@ -44,5 +46,9 @@ func (cs *collectorSlice) Set(cltr string) error { } func (cs *collectorSlice) SetDefaultCollector() { - cs.Set(validCollectors[0]) + err := cs.Set(validCollectors[0]) + if err != nil { + log.Debug(err) + log.WithField("collectors", validCollectors[0]).Fatal("unable to set default collector") + } } diff --git a/dedibox-exporter.go b/dedibox-exporter.go index 86a435a..6544f7d 100644 --- a/dedibox-exporter.go +++ b/dedibox-exporter.go @@ -69,13 +69,20 @@ func main() { }).Info("Starting Dedibox Exporter") http.Handle(metricsPath, promhttp.Handler()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - w.Write([]byte(` -