From 8793a15306ce9cf56bc79e084f37a9e38d1a278e Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Wed, 29 Jul 2020 11:43:55 -0700 Subject: [PATCH] hello world Signed-off-by: Sean Porter --- .bonsai.yml | 51 +++++++++++++ .github/workflows/lint.yml | 16 ++++ .github/workflows/release.yml | 26 +++++++ .github/workflows/test.yml | 21 ++++++ .gitignore | 20 +++++ .goreleaser.yml | 44 +++++++++++ CHANGELOG.md | 13 ++++ LICENSE | 7 ++ README.md | 136 ++++++++++++++++++++++++++++++++++ go.mod | 8 ++ main.go | 54 ++++++++++++++ main_test.go | 8 ++ 12 files changed, 404 insertions(+) create mode 100644 .bonsai.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/test.yml create mode 100644 .gitignore create mode 100644 .goreleaser.yml create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 main.go create mode 100644 main_test.go diff --git a/.bonsai.yml b/.bonsai.yml new file mode 100644 index 0000000..17856a2 --- /dev/null +++ b/.bonsai.yml @@ -0,0 +1,51 @@ +--- + description: "#{repo}" + builds: + - platform: "linux" + arch: "amd64" + asset_filename: "#{repo}_#{version}_linux_amd64.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'linux'" + - "entity.system.arch == 'amd64'" + + - platform: "linux" + arch: "386" + asset_filename: "#{repo}_#{version}_linux_386.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'linux'" + - "entity.system.arch == '386'" + + - platform: "linux" + arch: "arm64" + asset_filename: "#{repo}_#{version}_linux_arm64.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'linux'" + - "entity.system.arch == 'arm64'" + + - platform: "linux" + arch: "armv7" + asset_filename: "#{repo}_#{version}_linux_armv7.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'linux'" + - "entity.system.arch == 'armv7'" + + - platform: "OSX" + arch: "amd64" + asset_filename: "#{repo}_#{version}_darwin_amd64.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'darwin'" + - "entity.system.arch == 'amd64'" + + - platform: "Windows" + arch: "amd64" + asset_filename: "#{repo}_#{version}_windows_amd64.tar.gz" + sha_filename: "#{repo}_#{version}_sha512-checksums.txt" + filter: + - "entity.system.os == 'windows'" + - "entity.system.arch == 'amd64'" + \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..2583a9a --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,16 @@ +name: Go Lint + +on: [push] + +jobs: + test: + name: Lint + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ubuntu-latest] + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Run golangci-lint + uses: actions-contrib/golangci-lint@v1 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..f6545bd --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,26 @@ +name: goreleaser + +on: + push: + tags: + - '*' + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Unshallow + run: git fetch --prune --unshallow + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: 1.13.x + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v1 + with: + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..ef978f6 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,21 @@ +name: Go Test + +on: [push] + +jobs: + test: + name: Test + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [macos-latest, windows-latest, ubuntu-latest] + steps: + - name: Checkout code + uses: actions/checkout@v2 + - name: Set up Go 1.13 + uses: actions/setup-go@v1 + with: + go-version: 1.13 + id: go + - name: Test + run: go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2abe684 --- /dev/null +++ b/.gitignore @@ -0,0 +1,20 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +sensu-prometheus-pushgateway-handler + +# Test binary, build with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# dep +vendor/ + +# deploy +bonsai/ +dist/ diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..b508573 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,44 @@ +builds: + # List of builds + - # First Build + env: + - CGO_ENABLED=0 + main: main.go + ldflags: '-s -w -X github.com/sensu-community/sensu-plugin-sdk/version.version={{.Version}} -X github.com/sensu-community/sensu-plugin-sdk/version.commit={{.Commit}} -X github.com/sensu-community/sensu-plugin-sdk/version.date={{.Date}}' + # Set the binary output location to bin/ so archive will comply with Sensu Go Asset structure + binary: bin/{{ .ProjectName }} + goos: + - darwin + - linux + - windows + goarch: + - amd64 + - 386 + - arm + - arm64 + goarm: + - 5 + - 6 + - 7 + targets: + - darwin_amd64 + - linux_386 + - linux_amd64 + - linux_arm_5 + - linux_arm_6 + - linux_arm_7 + - linux_arm64 + - windows_386 + - windows_amd64 + +checksum: + name_template: "{{ .ProjectName }}_{{ .Version }}_sha512-checksums.txt" + algorithm: sha512 + +archives: + - id: tar + format: tar.gz + files: + - LICENSE + - README.md + - CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..103bb40 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,13 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) +and this project adheres to [Semantic +Versioning](http://semver.org/spec/v2.0.0.html). + +## Unreleased + +## [0.0.1] - 2000-01-01 + +### Added +- Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c422f4b --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Sean Porter + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a7999b --- /dev/null +++ b/README.md @@ -0,0 +1,136 @@ +[![Sensu Bonsai Asset](https://img.shields.io/badge/Bonsai-Download%20Me-brightgreen.svg?colorB=89C967&logo=sensu)](https://bonsai.sensu.io/assets/portertech/sensu-prometheus-pushgateway-handler) +![Go Test](https://github.com/portertech/sensu-prometheus-pushgateway-handler/workflows/Go%20Test/badge.svg) +![goreleaser](https://github.com/portertech/sensu-prometheus-pushgateway-handler/workflows/goreleaser/badge.svg) + +# Handler Plugin Template + +## Overview +handler-plugin-template is a template repository which wraps the [Sensu Plugin SDK][2]. +To use this project as a template, click the "Use this template" button from the main project page. +Once the repository is created from this template, you can use the [Sensu Plugin Tool][9] to +populate the templated fields with the proper values. + +## Functionality + +After successfully creating a project from this template, update the `Config` struct with any +configuration options for the plugin, map those values as plugin options in the variable `options`, +and customize the `checkArgs` and `executeHandler` functions in [main.go][7]. + +When writing or updating a plugin's README from this template, review the Sensu Community +[plugin README style guide][3] for content suggestions and guidance. Remove everything +prior to `# sensu-prometheus-pushgateway-handler` from the generated README file, and add additional context about the +plugin per the style guide. + +## Releases with Github Actions + +To release a version of your project, simply tag the target sha with a semver release without a `v` +prefix (ex. `1.0.0`). This will trigger the [GitHub action][5] workflow to [build and release][4] +the plugin with goreleaser. Register the asset with [Bonsai][8] to share it with the community! + +*** + +# sensu-prometheus-pushgateway-handler + +## Table of Contents +- [Overview](#overview) +- [Files](#files) +- [Usage examples](#usage-examples) +- [Configuration](#configuration) + - [Asset registration](#asset-registration) + - [Handler definition](#handler-definition) + - [Annotations](#annotations) +- [Installation from source](#installation-from-source) +- [Additional notes](#additional-notes) +- [Contributing](#contributing) + +## Overview + +The sensu-prometheus-pushgateway-handler is a [Sensu Handler][6] that ... + +## Files + +## Usage examples + +## Configuration + +### Asset registration + +[Sensu Assets][10] are the best way to make use of this plugin. If you're not using an asset, please +consider doing so! If you're using sensuctl 5.13 with Sensu Backend 5.13 or later, you can use the +following command to add the asset: + +``` +sensuctl asset add portertech/sensu-prometheus-pushgateway-handler +``` + +If you're using an earlier version of sensuctl, you can find the asset on the [Bonsai Asset Index][https://bonsai.sensu.io/assets/portertech/sensu-prometheus-pushgateway-handler]. + +### Handler definition + +```yml +--- +type: Handler +api_version: core/v2 +metadata: + name: sensu-prometheus-pushgateway-handler + namespace: default +spec: + command: sensu-prometheus-pushgateway-handler --example example_arg + type: pipe + runtime_assets: + - portertech/sensu-prometheus-pushgateway-handler +``` + +#### Proxy Support + +This handler supports the use of the environment variables HTTP_PROXY, +HTTPS_PROXY, and NO_PROXY (or the lowercase versions thereof). HTTPS_PROXY takes +precedence over HTTP_PROXY for https requests. The environment values may be +either a complete URL or a "host[:port]", in which case the "http" scheme is assumed. + +### Annotations + +All arguments for this handler are tunable on a per entity or check basis based on annotations. The +annotations keyspace for this handler is `sensu.io/plugins/sensu-prometheus-pushgateway-handler/config`. + +#### Examples + +To change the example argument for a particular check, for that checks's metadata add the following: + +```yml +type: CheckConfig +api_version: core/v2 +metadata: + annotations: + sensu.io/plugins/sensu-prometheus-pushgateway-handler/config/example-argument: "Example change" +[...] +``` + +## Installation from source + +The preferred way of installing and deploying this plugin is to use it as an Asset. If you would +like to compile and install the plugin from source or contribute to it, download the latest version +or create an executable script from this source. + +From the local path of the sensu-prometheus-pushgateway-handler repository: + +``` +go build +``` + +## Additional notes + +## Contributing + +For more information about contributing to this plugin, see [Contributing][1]. + +[1]: https://github.com/sensu/sensu-go/blob/master/CONTRIBUTING.md +[2]: https://github.com/sensu-community/sensu-plugin-sdk +[3]: https://github.com/sensu-plugins/community/blob/master/PLUGIN_STYLEGUIDE.md +[4]: https://github.com/sensu-community/handler-plugin-template/blob/master/.github/workflows/release.yml +[5]: https://github.com/sensu-community/handler-plugin-template/actions +[6]: https://docs.sensu.io/sensu-go/latest/reference/handlers/ +[7]: https://github.com/sensu-community/handler-plugin-template/blob/master/main.go +[8]: https://bonsai.sensu.io/ +[9]: https://github.com/sensu-community/sensu-plugin-tool +[10]: https://docs.sensu.io/sensu-go/latest/reference/assets/ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8dca287 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/portertech/sensu-prometheus-pushgateway-handler + +go 1.13 + +require ( + github.com/sensu-community/sensu-plugin-sdk v0.6.0 + github.com/sensu/sensu-go v0.0.0-20200131164840-40b1d5938251 +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..984c40a --- /dev/null +++ b/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "log" + + "github.com/sensu-community/sensu-plugin-sdk/sensu" + "github.com/sensu/sensu-go/types" +) + +// Config represents the handler plugin config. +type Config struct { + sensu.PluginConfig + Example string +} + +var ( + plugin = Config{ + PluginConfig: sensu.PluginConfig{ + Name: "sensu-prometheus-pushgateway-handler", + Short: "Send Sensu Go event metrics to the Prometheus Pushgateway.", + Keyspace: "sensu.io/plugins/sensu-prometheus-pushgateway-handler/config", + }, + } + + options = []*sensu.PluginConfigOption{ + &sensu.PluginConfigOption{ + Path: "example", + Env: "HANDLER_EXAMPLE", + Argument: "example", + Shorthand: "e", + Default: "", + Usage: "An example string configuration option", + Value: &plugin.Example, + }, + } +) + +func main() { + handler := sensu.NewGoHandler(&plugin.PluginConfig, options, checkArgs, executeHandler) + handler.Execute() +} + +func checkArgs(_ *types.Event) error { + if len(plugin.Example) == 0 { + return fmt.Errorf("--example or HANDLER_EXAMPLE environment variable is required") + } + return nil +} + +func executeHandler(event *types.Event) error { + log.Println("executing handler with --example", plugin.Example) + return nil +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..330f64d --- /dev/null +++ b/main_test.go @@ -0,0 +1,8 @@ +package main + +import ( + "testing" +) + +func TestMain(t *testing.T) { +}