From e7b4f266f5b0ecfc9de22698ecebc5b2c8fe8b9d Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Sat, 28 Dec 2019 15:45:58 -0800 Subject: [PATCH 1/5] Initial commit --- integrations/mysqlunifi/LICENSE | 21 +++++++++++++++++++++ integrations/mysqlunifi/README.md | 2 ++ 2 files changed, 23 insertions(+) create mode 100644 integrations/mysqlunifi/LICENSE create mode 100644 integrations/mysqlunifi/README.md diff --git a/integrations/mysqlunifi/LICENSE b/integrations/mysqlunifi/LICENSE new file mode 100644 index 00000000..3bed6705 --- /dev/null +++ b/integrations/mysqlunifi/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 unifi-poller + +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/integrations/mysqlunifi/README.md b/integrations/mysqlunifi/README.md new file mode 100644 index 00000000..fcd4ef41 --- /dev/null +++ b/integrations/mysqlunifi/README.md @@ -0,0 +1,2 @@ +# mysqlunifi +UniFi Poller Example MySQL Output Plugin From 8f1781aa911726db78bc1b386cf42bce2c065533 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 30 Dec 2019 14:50:09 -0800 Subject: [PATCH 2/5] start something --- integrations/mysqlunifi/.gitignore | 1 + integrations/mysqlunifi/.travis.yml | 11 +++ integrations/mysqlunifi/LICENSE | 36 ++++----- integrations/mysqlunifi/README.md | 27 ++++++- integrations/mysqlunifi/collector.go | 6 ++ integrations/mysqlunifi/go.mod | 8 ++ integrations/mysqlunifi/go.sum | 76 +++++++++++++++++++ integrations/mysqlunifi/main.go | 71 +++++++++++++++++ integrations/mysqlunifi/up.conf.example.mysql | 34 +++++++++ 9 files changed, 250 insertions(+), 20 deletions(-) create mode 100644 integrations/mysqlunifi/.gitignore create mode 100644 integrations/mysqlunifi/.travis.yml create mode 100644 integrations/mysqlunifi/collector.go create mode 100644 integrations/mysqlunifi/go.mod create mode 100644 integrations/mysqlunifi/go.sum create mode 100644 integrations/mysqlunifi/main.go create mode 100644 integrations/mysqlunifi/up.conf.example.mysql diff --git a/integrations/mysqlunifi/.gitignore b/integrations/mysqlunifi/.gitignore new file mode 100644 index 00000000..c4755b77 --- /dev/null +++ b/integrations/mysqlunifi/.gitignore @@ -0,0 +1 @@ +/mysqlunifi diff --git a/integrations/mysqlunifi/.travis.yml b/integrations/mysqlunifi/.travis.yml new file mode 100644 index 00000000..56f7aaaa --- /dev/null +++ b/integrations/mysqlunifi/.travis.yml @@ -0,0 +1,11 @@ +language: go +go: +- 1.13.x +before_install: + # download super-linter: golangci-lint +- curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest +install: +- go mod download +script: +- golangci-lint run --enable-all -D gochecknoinits +- go test ./... diff --git a/integrations/mysqlunifi/LICENSE b/integrations/mysqlunifi/LICENSE index 3bed6705..6d5fa682 100644 --- a/integrations/mysqlunifi/LICENSE +++ b/integrations/mysqlunifi/LICENSE @@ -1,21 +1,21 @@ -MIT License +MIT LICENSE. +Copyright (c) 2018-2020 David Newhall II -Copyright (c) 2019 unifi-poller +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: -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 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. +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/integrations/mysqlunifi/README.md b/integrations/mysqlunifi/README.md index fcd4ef41..d5613624 100644 --- a/integrations/mysqlunifi/README.md +++ b/integrations/mysqlunifi/README.md @@ -1,2 +1,25 @@ -# mysqlunifi -UniFi Poller Example MySQL Output Plugin +# MYSQL Output Plugin Example + +The code here, and the dynamic plugin provided shows an example of how you can +write your own output for unifi-poller. This plugin records some very basic +data about clients on a unifi network into a mysql database. + +You could write outputs that do... anything. An example: They could compare current +connected clients to a previous list (in a db, or stored in memory), and send a +notification if it changes. The possibilities are endless. + +You must compile your plugin using the unifi-poller source for the version you're +using. In other words, to build a plugin for version 2.0.1, do this: +``` +mkdir -p $GOPATH/src/github.com/unifi-poller +cd $GOPATH/src/github.com/unifi-poller + +git clone git@github.com:unifi-poller/unifi-poller.git +cd unifi-poller + +git checkout v2.0.1 + +cp -r plugins/ +GOOS=linux make plugins +``` +The plugin you copy in *must* have a `main.go` file for `make plugins` to build it. diff --git a/integrations/mysqlunifi/collector.go b/integrations/mysqlunifi/collector.go new file mode 100644 index 00000000..f17113d1 --- /dev/null +++ b/integrations/mysqlunifi/collector.go @@ -0,0 +1,6 @@ +package main + +func (p *plugin) runCollector() error { + p.Logf("mysql plugin is not finished") + return nil +} diff --git a/integrations/mysqlunifi/go.mod b/integrations/mysqlunifi/go.mod new file mode 100644 index 00000000..4ff4645a --- /dev/null +++ b/integrations/mysqlunifi/go.mod @@ -0,0 +1,8 @@ +module github.com/unifi-poller/mysqlunifi + +go 1.13 + +require ( + github.com/unifi-poller/poller v0.0.2 + golift.io/cnfg v0.0.5 +) diff --git a/integrations/mysqlunifi/go.sum b/integrations/mysqlunifi/go.sum new file mode 100644 index 00000000..b78374c0 --- /dev/null +++ b/integrations/mysqlunifi/go.sum @@ -0,0 +1,76 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY= +github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +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.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/unifi-poller/poller v0.0.2 h1:qUDSon8UIqRYOXd+V+3jThf2CSE/glQK8MSDmp20STU= +github.com/unifi-poller/poller v0.0.2/go.mod h1:H1YErnDRLaUQBH09kzd7kNTL3bMNCPuYbg94aJBun7Y= +github.com/unifi-poller/unifi v0.0.1 h1:8LTf0RyxMdDLmzm59mk7bz5/qfC1hHmC21u/uqx2vSQ= +github.com/unifi-poller/unifi v0.0.1/go.mod h1:DagVD/I+VMnVUHmTT4Fi76lPI+DHbuMwwtMIzanwMxM= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golift.io/cnfg v0.0.5 h1:HnMU8Z9C/igKvir1dqaHx5BPuNGZrp99FCtdJyP2Z4I= +golift.io/cnfg v0.0.5/go.mod h1:ScFDIJg/rJGHbRaed/i7g1lBhywEjB0JiP2uZr3xC3A= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/integrations/mysqlunifi/main.go b/integrations/mysqlunifi/main.go new file mode 100644 index 00000000..110abdab --- /dev/null +++ b/integrations/mysqlunifi/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "fmt" + + "github.com/unifi-poller/poller" + "golift.io/cnfg" +) + +// Config represents the data that is unmarshalled from the up.conf config file for this plugins. +// See up.conf.example.mysql for sample input data. +type Config struct { + Disable bool `json:"disable" toml:"disable" xml:"disable" yaml:"disable"` + Interval cnfg.Duration `json:"interval" toml:"interval" xml:"interval" yaml:"interval"` + Host string `json:"host" toml:"host" xml:"host" yaml:"host"` + User string `json:"user" toml:"user" xml:"user" yaml:"user"` + Pass string `json:"pass" toml:"pass" xml:"pass" yaml:"pass"` + DB string `json:"db" toml:"db" xml:"db" yaml:"db"` + Devices []Device `json:"devices" toml:"devices" xml:"device" yaml:"devices"` + Clients *Clients `json:"clients" toml:"clients" xml:"clients" yaml:"clients"` +} + +// Device represents the configuration to save a devices' data. +// Type is one of uap, usw, ugw, udm. +// Table represents the mysql table name we save these fields to. +// Fields is a map of api response data key -> mysql column. +type Device struct { + Type string `json:"type" toml:"type" xml:"type" yaml:"type"` + Table string `json:"table" toml:"table" xml:"table" yaml:"table"` + Fields map[string]string `json:"fields" toml:"fields" xml:"field" yaml:"fields"` +} + +// Clients represents the configuration to save clients' data. +// Table represents the mysql table name we save these fields to. +// Fields is a map of api response data key -> mysql column. +type Clients struct { + Table string `json:"table" toml:"table" xml:"table" yaml:"table"` + Fields map[string]string `json:"fields" toml:"fields" xml:"field" yaml:"fields"` +} + +// Pointers are ignored during ENV variable unmarshal, avoid pointers to your config. +// Only capital (exported) members are unmarshaled when passed into poller.NewOutput(). +type plugin struct { + *Config `json:"mysql" toml:"mysql" xml:"mysql" yaml:"mysql"` + poller.Collect +} + +func init() { + u := &plugin{Config: &Config{}} + + poller.NewOutput(&poller.Output{ + Name: "mysql", + Config: u, // pass in the struct *above* your config (so it can see the struct tags). + Method: u.Run, + }) +} + +// main() is required, but it shouldn't do much as it's not used in plugin mode. +func main() { + fmt.Println("this is a unifi-poller plugin; not an application") +} + +// Run gets called by poller core code. Return when the plugin stops working or has an error. +// In other words, don't run your code in a go routine, it already is. +func (p *plugin) Run(c poller.Collect) error { + if p.Collect = c; c == nil || p.Config == nil || p.Disable { + return nil // no config or disabled, bail out. + } + + return p.runCollector() +} diff --git a/integrations/mysqlunifi/up.conf.example.mysql b/integrations/mysqlunifi/up.conf.example.mysql new file mode 100644 index 00000000..4908aa53 --- /dev/null +++ b/integrations/mysqlunifi/up.conf.example.mysql @@ -0,0 +1,34 @@ +[poller] + debug = true + plugins = ["/path/to/mysql.so"] + +[mysql] + interval = "30s" + host = "127.0.0.1:3306" + user = "unifipoller" + pass = "unifipoller" + db = "unifipoller" + +[mysql.clients] + table = "client_records" + [mysql.clients.fields] + tx_bytes = "tx-bytes" + rx_bytes = "rx-bytes" + +[[mysql.devices]] + type = "uap" + table = "uap_records" + [mysql.devices.fields] + tx_bytes = "tx-bytes" + rx_bytes = "rx-bytes" + +[[mysql.devices]] + type = "ugw" + table = "usg_records" + [mysql.devices.fields] + wan_bytes = "wan_bytes" + +[unifi.defaults] + url = "https://127.0.0.1:8443" + user = "unifipoller" + pass = "4BB9345C-2341-48D7-99F5-E01B583FF77F" From b41ca82462db4bee1bfddd0f40985c9bb8017fcc Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Mon, 30 Dec 2019 15:06:52 -0800 Subject: [PATCH 3/5] minor changes --- integrations/mysqlunifi/collector.go | 3 ++ integrations/mysqlunifi/main.go | 50 +++++++++++++++++++++------- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/integrations/mysqlunifi/collector.go b/integrations/mysqlunifi/collector.go index f17113d1..03647796 100644 --- a/integrations/mysqlunifi/collector.go +++ b/integrations/mysqlunifi/collector.go @@ -1,5 +1,8 @@ package main +/* Everything in this file runs after the config is unmarshalled and we've + verified the configuration for the poller. */ + func (p *plugin) runCollector() error { p.Logf("mysql plugin is not finished") return nil diff --git a/integrations/mysqlunifi/main.go b/integrations/mysqlunifi/main.go index 110abdab..975c4269 100644 --- a/integrations/mysqlunifi/main.go +++ b/integrations/mysqlunifi/main.go @@ -7,6 +7,12 @@ import ( "golift.io/cnfg" ) +// Only capital (exported) members are unmarshaled when passed into poller.NewOutput(). +type plugin struct { + *Config `json:"mysql" toml:"mysql" xml:"mysql" yaml:"mysql"` + poller.Collect +} + // Config represents the data that is unmarshalled from the up.conf config file for this plugins. // See up.conf.example.mysql for sample input data. type Config struct { @@ -38,13 +44,6 @@ type Clients struct { Fields map[string]string `json:"fields" toml:"fields" xml:"field" yaml:"fields"` } -// Pointers are ignored during ENV variable unmarshal, avoid pointers to your config. -// Only capital (exported) members are unmarshaled when passed into poller.NewOutput(). -type plugin struct { - *Config `json:"mysql" toml:"mysql" xml:"mysql" yaml:"mysql"` - poller.Collect -} - func init() { u := &plugin{Config: &Config{}} @@ -55,11 +54,6 @@ func init() { }) } -// main() is required, but it shouldn't do much as it's not used in plugin mode. -func main() { - fmt.Println("this is a unifi-poller plugin; not an application") -} - // Run gets called by poller core code. Return when the plugin stops working or has an error. // In other words, don't run your code in a go routine, it already is. func (p *plugin) Run(c poller.Collect) error { @@ -67,5 +61,37 @@ func (p *plugin) Run(c poller.Collect) error { return nil // no config or disabled, bail out. } + if err := p.validateConfig(); err != nil { + return err + } + return p.runCollector() } + +// validateConfig checks input sanity. +func (p *plugin) validateConfig() error { + if p.Interval.Duration == 0 { + return fmt.Errorf("must provide a polling interval") + } + + if p.Clients == nil && len(p.Devices) == 0 { + return fmt.Errorf("must configure client or device collection; both empty") + } + + for _, d := range p.Devices { + if len(d.Fields) == 0 { + return fmt.Errorf("no fields defined for device type %s, table %s", d.Type, d.Table) + } + } + + if p.Clients != nil && p.Clients.Fields == nil { + return fmt.Errorf("no fields defined for clients; if you don't want to store client data, remove it from the config") + } + + return nil +} + +// main() is required, but it shouldn't do much as it's not used in plugin mode. +func main() { + fmt.Println("this is a unifi-poller plugin; not an application") +} From 3b02e8e39c8a02bf6a8cd64b36c1901624c33b81 Mon Sep 17 00:00:00 2001 From: davidnewhall2 Date: Tue, 4 Feb 2020 20:03:25 -0800 Subject: [PATCH 4/5] update readme --- integrations/mysqlunifi/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/integrations/mysqlunifi/README.md b/integrations/mysqlunifi/README.md index d5613624..c25e6d9f 100644 --- a/integrations/mysqlunifi/README.md +++ b/integrations/mysqlunifi/README.md @@ -1,5 +1,8 @@ # MYSQL Output Plugin Example +This plugin is not finished and did not get finished for the release of poller v2. +Sorry about that. I'll try to get it working soon! 2/4/20 + The code here, and the dynamic plugin provided shows an example of how you can write your own output for unifi-poller. This plugin records some very basic data about clients on a unifi network into a mysql database. From fb8e9893ea40e845a9da3c87c826b1922f382bed Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Mon, 15 Mar 2021 01:19:13 -0700 Subject: [PATCH 5/5] ignore lint --- integrations/mysqlunifi/.gitignore | 2 + integrations/mysqlunifi/.travis.yml | 6 +-- integrations/mysqlunifi/go.mod | 6 +-- integrations/mysqlunifi/go.sum | 76 ----------------------------- 4 files changed, 7 insertions(+), 83 deletions(-) delete mode 100644 integrations/mysqlunifi/go.sum diff --git a/integrations/mysqlunifi/.gitignore b/integrations/mysqlunifi/.gitignore index c4755b77..1de288a9 100644 --- a/integrations/mysqlunifi/.gitignore +++ b/integrations/mysqlunifi/.gitignore @@ -1 +1,3 @@ /mysqlunifi +/*.so +/go.sum diff --git a/integrations/mysqlunifi/.travis.yml b/integrations/mysqlunifi/.travis.yml index 56f7aaaa..73d7bbbb 100644 --- a/integrations/mysqlunifi/.travis.yml +++ b/integrations/mysqlunifi/.travis.yml @@ -1,11 +1,9 @@ language: go go: -- 1.13.x +- 1.15.x before_install: # download super-linter: golangci-lint - curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(go env GOPATH)/bin latest -install: -- go mod download script: -- golangci-lint run --enable-all -D gochecknoinits +- golangci-lint run --enable-all -D gochecknoinits,exhaustivestruct,nlreturn,forbidigo,goerr113 - go test ./... diff --git a/integrations/mysqlunifi/go.mod b/integrations/mysqlunifi/go.mod index 4ff4645a..41fd845b 100644 --- a/integrations/mysqlunifi/go.mod +++ b/integrations/mysqlunifi/go.mod @@ -1,8 +1,8 @@ module github.com/unifi-poller/mysqlunifi -go 1.13 +go 1.15 require ( - github.com/unifi-poller/poller v0.0.2 - golift.io/cnfg v0.0.5 + github.com/unifi-poller/poller v0.0.0-20210315075554-47d92433b172 + golift.io/cnfg v0.0.7 ) diff --git a/integrations/mysqlunifi/go.sum b/integrations/mysqlunifi/go.sum deleted file mode 100644 index b78374c0..00000000 --- a/integrations/mysqlunifi/go.sum +++ /dev/null @@ -1,76 +0,0 @@ -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= -github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= -github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= -github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= -github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0= -github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= -github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= -github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= -github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= -github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= -github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= -github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= -github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= -github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= -github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= -github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v1.0.0 h1:vrDKnkGzuGvhNAL56c7DBz29ZL+KxnoR0x7enabFceM= -github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= -github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE= -github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= -github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY= -github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= -github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs= -github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= -github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= -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.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= -github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/unifi-poller/poller v0.0.2 h1:qUDSon8UIqRYOXd+V+3jThf2CSE/glQK8MSDmp20STU= -github.com/unifi-poller/poller v0.0.2/go.mod h1:H1YErnDRLaUQBH09kzd7kNTL3bMNCPuYbg94aJBun7Y= -github.com/unifi-poller/unifi v0.0.1 h1:8LTf0RyxMdDLmzm59mk7bz5/qfC1hHmC21u/uqx2vSQ= -github.com/unifi-poller/unifi v0.0.1/go.mod h1:DagVD/I+VMnVUHmTT4Fi76lPI+DHbuMwwtMIzanwMxM= -golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golift.io/cnfg v0.0.5 h1:HnMU8Z9C/igKvir1dqaHx5BPuNGZrp99FCtdJyP2Z4I= -golift.io/cnfg v0.0.5/go.mod h1:ScFDIJg/rJGHbRaed/i7g1lBhywEjB0JiP2uZr3xC3A= -gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=