├── .github ├── dependabot.yml └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── _example └── memcached.go ├── go.mod ├── go.sum ├── mackerel-plugin.go └── mackerel-plugin_test.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: "gomod" 4 | directory: "/" 5 | schedule: 6 | interval: "weekly" 7 | - package-ecosystem: "github-actions" 8 | directory: "/" 9 | schedule: 10 | interval: "weekly" 11 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | pull_request: 8 | jobs: 9 | lint: 10 | uses: mackerelio/workflows/.github/workflows/go-lint.yml@v1.4.0 11 | test: 12 | uses: mackerelio/workflows/.github/workflows/go-test.yml@v1.4.0 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: test 2 | test: 3 | go test -v 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | go-mackerel-plugin [![GoDev](https://pkg.go.dev/badge/github.com/mackerelio/go-mackerel-plugin)](https://pkg.go.dev/github.com/mackerelio/go-mackerel-plugin) [![Build Status](https://github.com/mackerelio/go-mackerel-plugin/workflows/Build/badge.svg)](https://github.com/mackerelio/go-mackerel-plugin/actions?workflow=Build) 2 | ================== 3 | 4 | This package provides helper methods to create mackerel agent plugin easily. 5 | 6 | 7 | How to use 8 | ========== 9 | 10 | ## Graph Definition 11 | A plugin can specify `Graphs` and `Metrics`. 12 | `Graphs` represents one graph and includes some `Metrics`s which represent each line. 13 | 14 | `Graphs` includes followings: 15 | 16 | - `Label`: Label for the graph 17 | - `Unit`: Unit for lines, `float`, `integer`, `percentage`, `seconds`, `milliseconds`, `bytes`, `bytes/sec`, `bits/sec`, `iops` can be specified. 18 | - `Metrics`: Array of `Metrics` which represents each line. 19 | 20 | `Metrics` includes followings: 21 | 22 | - `Name`: Key of the line 23 | - `Label`: Label of the line 24 | - `Diff`: If `Diff` is true, differential is used as value. 25 | - `Stacked`: If `Stacked` is true, the line is stacked. 26 | - `Scale`: Each value is multiplied by `Scale`. 27 | 28 | Example of graph definition. 29 | ```golang 30 | var graphdef = map[string]mackerelplugin.Graphs{ 31 | "memcached.connections": { 32 | Label: "Memcached Connections", 33 | Unit: "integer", 34 | Metrics: []mackerelplugin.Metrics{ 35 | {Name: "curr_connections", Label: "Connections", Diff: false}, 36 | }, 37 | }, 38 | } 39 | ``` 40 | 41 | ## Method 42 | 43 | A plugin must implement this interface and the `main` method. 44 | 45 | ```go 46 | type PluginWithPrefix interface { 47 | FetchMetrics() (map[string]float64, error) 48 | GraphDefinition() map[string]Graphs 49 | MetricKeyPrefix() string 50 | } 51 | ``` 52 | 53 | ```go 54 | func main() { 55 | optHost := flag.String("host", "localhost", "Hostname") 56 | optPort := flag.String("port", "11211", "Port") 57 | optTempfile := flag.String("tempfile", "", "Temp file name") 58 | optMetricKeyPrefix := flag.String("metric-key-prefix", "memcached", "Metric Key Prefix") 59 | flag.Parse() 60 | 61 | var memcached MemcachedPlugin 62 | 63 | memcached.Target = fmt.Sprintf("%s:%s", *optHost, *optPort) 64 | memcached.prefix = *optMetricKeyPrefix 65 | helper := mackerelplugin.NewMackerelPlugin(memcached) 66 | helper.Tempfile = *optTempfile 67 | 68 | helper.Run() 69 | } 70 | ``` 71 | 72 | You can find an example implementation in _example/ directory. 73 | 74 | ## Calculate Differential of Counter 75 | 76 | Many status values of popular middle-wares are provided as counter. 77 | But current Mackerel API can accept only absolute values, so differential values must be calculated beside agent plugins. 78 | 79 | `Diff` of `Metrics` is a flag whether values must be treated as counter or not. 80 | If this flag is set, this package calculate differential values automatically with current values and previous values, which are saved to a temporally file. 81 | 82 | ## Adjust Scale Value 83 | 84 | Some status values such as `jstat` memory usage are provided as scaled values. 85 | For example, `OGC` value are provided KB scale. 86 | 87 | `Scale` of `Metrics` is a multiplier for adjustment of the scale values. 88 | 89 | ```golang 90 | var graphdef = map[string]mackerelplugin.Graphs{ 91 | "jvm.old_space": { 92 | Label: "JVM Old Space memory", 93 | Unit: "float", 94 | Metrics: []mackerelplugin.Metrics{ 95 | {Name: "OGCMX", Label: "Old max", Diff: false, Scale: 1024}, 96 | {Name: "OGC", Label: "Old current", Diff: false, Scale: 1024}, 97 | {Name: "OU", Label: "Old used", Diff: false, Scale: 1024}, 98 | }, 99 | }, 100 | } 101 | ``` 102 | 103 | ## Tempfile 104 | 105 | `MackerelPlugin` interface has `Tempfile` field. The Tempfile is used to calculate differences in metrics with `Diff: true`. 106 | If this field is omitted, the filename of the temporaty file is automatically generated from plugin filename. 107 | 108 | ### Default value of Tempfile 109 | 110 | mackerel-agent's plugins should place its Tempfile under `os.Getenv("MACKEREL_PLUGIN_WORKDIR")` unless specified explicitly. 111 | Since this helper handles the environmental value, it's recommended not to set default Tempfile path. 112 | But if a plugin wants to set default Tempfile filename by itself, use `MackerelPlugin.SetTempfileByBasename()`, which sets Tempfile path considering the environmental value. 113 | 114 | ```go 115 | helper.Tempfile = *optTempfile 116 | if optTempfile == nil { 117 | helper.SetTempfileByBasename("YOUR_DEFAULT_FILENAME") 118 | } 119 | ``` 120 | -------------------------------------------------------------------------------- /_example/memcached.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "flag" 6 | "fmt" 7 | "log" 8 | "net" 9 | "strconv" 10 | "strings" 11 | 12 | "github.com/mackerelio/go-mackerel-plugin" 13 | ) 14 | 15 | var graphdef map[string]mackerelplugin.Graphs = map[string]mackerelplugin.Graphs{ 16 | "memcached.connections": { 17 | Label: "Memcached Connections", 18 | Unit: "integer", 19 | Metrics: []mackerelplugin.Metrics{ 20 | {Name: "curr_connections", Label: "Connections", Diff: false}, 21 | }, 22 | }, 23 | "memcached.cmd": { 24 | Label: "Memcached Command", 25 | Unit: "integer", 26 | Metrics: []mackerelplugin.Metrics{ 27 | {Name: "cmd_get", Label: "Get", Diff: true}, 28 | {Name: "cmd_set", Label: "Set", Diff: true}, 29 | {Name: "cmd_flush", Label: "Flush", Diff: true}, 30 | {Name: "cmd_touch", Label: "Touch", Diff: true}, 31 | }, 32 | }, 33 | "memcached.hitmiss": { 34 | Label: "Memcached Hits/Misses", 35 | Unit: "integer", 36 | Metrics: []mackerelplugin.Metrics{ 37 | {Name: "get_hits", Label: "Get Hits", Diff: true}, 38 | {Name: "get_misses", Label: "Get Misses", Diff: true}, 39 | {Name: "delete_hits", Label: "Delete Hits", Diff: true}, 40 | {Name: "delete_misses", Label: "Delete Misses", Diff: true}, 41 | {Name: "incr_hits", Label: "Incr Hits", Diff: true}, 42 | {Name: "incr_misses", Label: "Incr Misses", Diff: true}, 43 | {Name: "cas_hits", Label: "Cas Hits", Diff: true}, 44 | {Name: "cas_misses", Label: "Cas Misses", Diff: true}, 45 | {Name: "touch_hits", Label: "Touch Hits", Diff: true}, 46 | {Name: "touch_misses", Label: "Touch Misses", Diff: true}, 47 | }, 48 | }, 49 | "memcached.evictions": { 50 | Label: "Memcached Evictions", 51 | Unit: "integer", 52 | Metrics: []mackerelplugin.Metrics{ 53 | {Name: "evictions", Label: "Evictions", Diff: true}, 54 | }, 55 | }, 56 | "memcached.unfetched": { 57 | Label: "Memcached Unfetched", 58 | Unit: "integer", 59 | Metrics: []mackerelplugin.Metrics{ 60 | {Name: "expired_unfetched", Label: "Expired unfetched", Diff: true}, 61 | {Name: "evicted_unfetched", Label: "Evicted unfetched", Diff: true}, 62 | }, 63 | }, 64 | "memcached.rusage": { 65 | Label: "Memcached Resouce Usage", 66 | Unit: "float", 67 | Metrics: []mackerelplugin.Metrics{ 68 | {Name: "rusage_user", Label: "User", Diff: true}, 69 | {Name: "rusage_system", Label: "System", Diff: true}, 70 | }, 71 | }, 72 | "memcached.bytes": { 73 | Label: "Memcached Traffics", 74 | Unit: "bytes", 75 | Metrics: []mackerelplugin.Metrics{ 76 | {Name: "bytes_read", Label: "Read", Diff: true}, 77 | {Name: "bytes_written", Label: "Write", Diff: true}, 78 | }, 79 | }, 80 | } 81 | 82 | type MemcachedPlugin struct { 83 | Target string 84 | Tempfile string 85 | } 86 | 87 | func (m MemcachedPlugin) FetchMetrics() (map[string]float64, error) { 88 | conn, err := net.Dial("tcp", m.Target) 89 | if err != nil { 90 | return nil, err 91 | } 92 | fmt.Fprintln(conn, "stats") 93 | scanner := bufio.NewScanner(conn) 94 | stat := make(map[string]float64) 95 | 96 | for scanner.Scan() { 97 | line := scanner.Text() 98 | s := string(line) 99 | if s == "END" { 100 | return stat, nil 101 | } 102 | 103 | res := strings.Split(s, " ") 104 | if res[0] == "STAT" { 105 | stat[res[1]], err = strconv.ParseFloat(res[2], 64) 106 | if err != nil { 107 | log.Println("FetchMetrics:", err) 108 | } 109 | } 110 | } 111 | if err := scanner.Err(); err != nil { 112 | return stat, err 113 | } 114 | return nil, nil 115 | } 116 | 117 | func (m MemcachedPlugin) GraphDefinition() map[string]mackerelplugin.Graphs { 118 | return graphdef 119 | } 120 | 121 | func main() { 122 | optHost := flag.String("host", "localhost", "Hostname") 123 | optPort := flag.String("port", "11211", "Port") 124 | optTempfile := flag.String("tempfile", "", "Temp file name") 125 | flag.Parse() 126 | 127 | var memcached MemcachedPlugin 128 | 129 | memcached.Target = fmt.Sprintf("%s:%s", *optHost, *optPort) 130 | helper := mackerelplugin.NewMackerelPlugin(memcached) 131 | helper.Tempfile = *optTempfile 132 | helper.Run() 133 | } 134 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/mackerelio/go-mackerel-plugin 2 | 3 | go 1.18 4 | 5 | require ( 6 | github.com/mackerelio/golib v1.2.1 7 | golang.org/x/text v0.17.0 8 | ) 9 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/mackerelio/golib v1.2.1 h1:SDcDn6Jw3p9bi1N0bg1Z/ilG5qcBB23qL8xNwrU0gg4= 2 | github.com/mackerelio/golib v1.2.1/go.mod h1:b8ZaapsHGH1FlEJlCqfD98CqafLeyMevyATDlID2BsM= 3 | golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= 4 | golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= 5 | -------------------------------------------------------------------------------- /mackerel-plugin.go: -------------------------------------------------------------------------------- 1 | package mackerelplugin 2 | 3 | import ( 4 | "crypto/sha1" 5 | "encoding/json" 6 | "errors" 7 | "fmt" 8 | "io" 9 | "log" 10 | "math" 11 | "os" 12 | "path/filepath" 13 | "regexp" 14 | "strings" 15 | "time" 16 | 17 | "golang.org/x/text/cases" 18 | "golang.org/x/text/language" 19 | 20 | "github.com/mackerelio/golib/pluginutil" 21 | ) 22 | 23 | // Metric units 24 | const ( 25 | UnitFloat = "float" 26 | UnitInteger = "integer" 27 | UnitPercentage = "percentage" 28 | UnitSeconds = "seconds" 29 | UnitMilliseconds = "milliseconds" 30 | UnitBytes = "bytes" 31 | UnitBytesPerSecond = "bytes/sec" 32 | UnitBitsPerSecond = "bits/sec" 33 | UnitIOPS = "iops" 34 | ) 35 | 36 | // Metrics represents definition of a metric 37 | type Metrics struct { 38 | Name string `json:"name"` 39 | Label string `json:"label"` 40 | Diff bool `json:"-"` 41 | Stacked bool `json:"stacked"` 42 | Scale float64 `json:"-"` 43 | } 44 | 45 | // Graphs represents definition of a graph 46 | type Graphs struct { 47 | Label string `json:"label"` 48 | Unit string `json:"unit"` 49 | Metrics []Metrics `json:"metrics"` 50 | } 51 | 52 | // Plugin is old interface of mackerel-plugin 53 | type Plugin interface { 54 | FetchMetrics() (map[string]float64, error) 55 | GraphDefinition() map[string]Graphs 56 | } 57 | 58 | // PluginWithPrefix is recommended interface 59 | type PluginWithPrefix interface { 60 | Plugin 61 | MetricKeyPrefix() string 62 | } 63 | 64 | // MackerelPlugin is for mackerel-agent-plugins 65 | type MackerelPlugin struct { 66 | Plugin 67 | Tempfile string 68 | diff *bool 69 | writer io.Writer 70 | } 71 | 72 | // NewMackerelPlugin returns new MackrelPlugin 73 | func NewMackerelPlugin(plugin Plugin) *MackerelPlugin { 74 | return &MackerelPlugin{Plugin: plugin} 75 | } 76 | 77 | func (mp *MackerelPlugin) getWriter() io.Writer { 78 | if mp.writer == nil { 79 | mp.writer = os.Stdout 80 | } 81 | return mp.writer 82 | } 83 | 84 | func (mp *MackerelPlugin) hasDiff() bool { 85 | if mp.diff == nil { 86 | diff := false 87 | mp.diff = &diff 88 | DiffCheck: 89 | for _, graph := range mp.GraphDefinition() { 90 | for _, metric := range graph.Metrics { 91 | if metric.Diff { 92 | *mp.diff = true 93 | break DiffCheck 94 | } 95 | } 96 | } 97 | } 98 | return *mp.diff 99 | } 100 | 101 | func (mp *MackerelPlugin) printValue(w io.Writer, key string, value float64, now time.Time) { 102 | if math.IsNaN(value) || math.IsInf(value, 0) { 103 | log.Printf("Invalid value: key = %s, value = %f\n", key, value) 104 | return 105 | } 106 | 107 | if value == float64(int(value)) { 108 | fmt.Fprintf(w, "%s\t%d\t%d\n", key, int(value), now.Unix()) 109 | } else { 110 | fmt.Fprintf(w, "%s\t%f\t%d\n", key, value, now.Unix()) 111 | } 112 | } 113 | 114 | var errStateRecentlyUpdated = errors.New("state was recently updated") 115 | 116 | const oldEnoughDuration = time.Second 117 | 118 | func (mp *MackerelPlugin) fetchLastValues(now time.Time) (map[string]float64, time.Time, error) { 119 | if !mp.hasDiff() { 120 | return nil, time.Time{}, nil 121 | } 122 | 123 | f, err := os.Open(mp.tempfilename()) 124 | if err != nil { 125 | if os.IsNotExist(err) { 126 | return nil, time.Time{}, nil 127 | } 128 | return nil, time.Time{}, err 129 | } 130 | defer f.Close() 131 | 132 | stat := make(map[string]float64) 133 | decoder := json.NewDecoder(f) 134 | err = decoder.Decode(&stat) 135 | if err != nil { 136 | return stat, time.Time{}, err 137 | } 138 | lastTime := time.Unix(int64(stat["_lastTime"]), 0) 139 | if now.Sub(lastTime) < oldEnoughDuration { 140 | return stat, time.Time{}, errStateRecentlyUpdated 141 | } 142 | return stat, lastTime, nil 143 | } 144 | 145 | func (mp *MackerelPlugin) saveValues(values map[string]float64, now time.Time) error { 146 | if !mp.hasDiff() { 147 | return nil 148 | } 149 | f, err := os.Create(mp.tempfilename()) 150 | if err != nil { 151 | return err 152 | } 153 | defer f.Close() 154 | 155 | // Since Go 1.15 strconv.ParseFloat returns +Inf if it couldn't parse a string. 156 | // But JSON does not accept invalid numbers, such as +Inf, -Inf or NaN. 157 | // We perhaps have some plugins that is affected above change, 158 | // so saveState should clear invalid numbers in the values before saving it. 159 | for k, v := range values { 160 | if math.IsInf(v, 0) || math.IsNaN(v) { 161 | delete(values, k) 162 | } 163 | } 164 | 165 | values["_lastTime"] = float64(now.Unix()) 166 | encoder := json.NewEncoder(f) 167 | err = encoder.Encode(values) 168 | if err != nil { 169 | return err 170 | } 171 | 172 | return nil 173 | } 174 | 175 | func (mp *MackerelPlugin) calcDiff(value float64, now time.Time, lastValue float64, lastTime time.Time) (float64, error) { 176 | diffTime := now.Unix() - lastTime.Unix() 177 | if diffTime > 600 { 178 | return 0, errors.New("too long duration") 179 | } 180 | 181 | diff := (value - lastValue) * 60 / float64(diffTime) 182 | if diff < 0 { 183 | return 0, errors.New("counter seems to be reset") 184 | } 185 | return diff, nil 186 | } 187 | 188 | func (mp *MackerelPlugin) tempfilename() string { 189 | if mp.Tempfile == "" { 190 | mp.Tempfile = mp.generateTempfilePath(os.Args) 191 | } 192 | return mp.Tempfile 193 | } 194 | 195 | var tempfileSanitizeReg = regexp.MustCompile(`[^-_.A-Za-z0-9]`) 196 | 197 | // SetTempfileByBasename sets Tempfile under proper directory with specified basename. 198 | func (mp *MackerelPlugin) SetTempfileByBasename(base string) { 199 | mp.Tempfile = filepath.Join(pluginutil.PluginWorkDir(), base) 200 | } 201 | 202 | func (mp *MackerelPlugin) generateTempfilePath(args []string) string { 203 | commandPath := args[0] 204 | var prefix string 205 | if p, ok := mp.Plugin.(PluginWithPrefix); ok { 206 | prefix = p.MetricKeyPrefix() 207 | } else { 208 | name := filepath.Base(commandPath) 209 | prefix = strings.TrimPrefix(tempfileSanitizeReg.ReplaceAllString(name, "_"), "mackerel-plugin-") 210 | } 211 | filename := fmt.Sprintf( 212 | "mackerel-plugin-%s-%x", 213 | prefix, 214 | // When command-line options are different, mostly different metrics. 215 | // e.g. `-host` and `-port` options for mackerel-plugin-mysql 216 | sha1.Sum([]byte(strings.Join(args[1:], " "))), 217 | ) 218 | return filepath.Join(pluginutil.PluginWorkDir(), filename) 219 | } 220 | 221 | // OutputValues output the metrics 222 | func (mp *MackerelPlugin) OutputValues() { 223 | now := time.Now() 224 | stat, err := mp.FetchMetrics() 225 | if err != nil { 226 | log.Fatalln("OutputValues: ", err) 227 | } 228 | 229 | lastStat, lastTime, err := mp.fetchLastValues(now) 230 | if err != nil { 231 | if err == errStateRecentlyUpdated { 232 | log.Println("OutputValues:", err) 233 | return 234 | } 235 | log.Println("fetchLastValues (ignore):", err) 236 | } 237 | 238 | for key, graph := range mp.GraphDefinition() { 239 | for _, metric := range graph.Metrics { 240 | if strings.ContainsAny(key+metric.Name, "*#") { 241 | mp.formatValuesWithWildcard(key, metric, stat, lastStat, now, lastTime) 242 | } else { 243 | mp.formatValues(key, metric, stat, lastStat, now, lastTime) 244 | } 245 | } 246 | } 247 | 248 | err = mp.saveValues(stat, now) 249 | if err != nil { 250 | log.Fatalf("saveValues: %s", err) 251 | } 252 | } 253 | 254 | func (mp *MackerelPlugin) formatValuesWithWildcard(prefix string, metric Metrics, stat map[string]float64, lastStat map[string]float64, now time.Time, lastTime time.Time) { 255 | regexpStr := `\A` + prefix + "." + metric.Name 256 | regexpStr = strings.Replace(regexpStr, ".", `\.`, -1) 257 | regexpStr = strings.Replace(regexpStr, "*", `[-a-zA-Z0-9_]+`, -1) 258 | regexpStr = strings.Replace(regexpStr, "#", `[-a-zA-Z0-9_]+`, -1) 259 | re, err := regexp.Compile(regexpStr) 260 | if err != nil { 261 | log.Fatalln("Failed to compile regexp: ", err) 262 | } 263 | for k := range stat { 264 | if re.MatchString(k) { 265 | metricEach := metric 266 | metricEach.Name = k 267 | mp.formatValues("", metricEach, stat, lastStat, now, lastTime) 268 | } 269 | } 270 | } 271 | 272 | func (mp *MackerelPlugin) formatValues(prefix string, metric Metrics, stat map[string]float64, lastStat map[string]float64, now time.Time, lastTime time.Time) { 273 | name := metric.Name 274 | value, ok := stat[name] 275 | if !ok { 276 | return 277 | } 278 | if metric.Diff { 279 | lastValue, ok := lastStat[name] 280 | if ok { 281 | var err error 282 | value, err = mp.calcDiff(value, now, lastValue, lastTime) 283 | if err != nil { 284 | log.Println("OutputValues: ", err) 285 | } 286 | } else { 287 | log.Printf("%s does not exist at last fetch\n", metric.Name) 288 | return 289 | } 290 | } 291 | 292 | if metric.Scale != 0 { 293 | value *= metric.Scale 294 | } 295 | 296 | metricNames := []string{} 297 | if p, ok := mp.Plugin.(PluginWithPrefix); ok { 298 | metricNames = append(metricNames, p.MetricKeyPrefix()) 299 | } 300 | if prefix != "" { 301 | metricNames = append(metricNames, prefix) 302 | } 303 | metricNames = append(metricNames, metric.Name) 304 | mp.printValue(mp.getWriter(), strings.Join(metricNames, "."), value, now) 305 | } 306 | 307 | // GraphDef is graph definitions 308 | type GraphDef struct { 309 | Graphs map[string]Graphs `json:"graphs"` 310 | } 311 | 312 | func title(s string) string { 313 | r := strings.NewReplacer(".", " ", "_", " ", "*", "", "#", "") 314 | return strings.TrimSpace(cases.Title(language.Und, cases.NoLower).String(r.Replace(s))) 315 | } 316 | 317 | // OutputDefinitions outputs graph definitions 318 | func (mp *MackerelPlugin) OutputDefinitions() { 319 | fmt.Fprintln(mp.getWriter(), "# mackerel-agent-plugin") 320 | graphs := make(map[string]Graphs) 321 | for key, graph := range mp.GraphDefinition() { 322 | g := graph 323 | k := key 324 | if p, ok := mp.Plugin.(PluginWithPrefix); ok { 325 | prefix := p.MetricKeyPrefix() 326 | if k == "" { 327 | k = prefix 328 | } else { 329 | k = prefix + "." + k 330 | } 331 | } 332 | if g.Label == "" { 333 | g.Label = title(k) 334 | } 335 | metrics := []Metrics{} 336 | for _, v := range g.Metrics { 337 | if v.Label == "" { 338 | v.Label = title(v.Name) 339 | } 340 | metrics = append(metrics, v) 341 | } 342 | g.Metrics = metrics 343 | graphs[k] = g 344 | } 345 | var graphdef GraphDef 346 | graphdef.Graphs = graphs 347 | b, err := json.Marshal(graphdef) 348 | if err != nil { 349 | log.Fatalln("OutputDefinitions: ", err) 350 | } 351 | fmt.Fprintln(mp.getWriter(), string(b)) 352 | } 353 | 354 | // Run the plugin 355 | func (mp *MackerelPlugin) Run() { 356 | if os.Getenv("MACKEREL_AGENT_PLUGIN_META") != "" { 357 | mp.OutputDefinitions() 358 | } else { 359 | mp.OutputValues() 360 | } 361 | } 362 | -------------------------------------------------------------------------------- /mackerel-plugin_test.go: -------------------------------------------------------------------------------- 1 | package mackerelplugin 2 | 3 | import ( 4 | "bytes" 5 | "crypto/sha1" 6 | "fmt" 7 | "io/ioutil" 8 | "math" 9 | "os" 10 | "path/filepath" 11 | "reflect" 12 | "sort" 13 | "strings" 14 | "testing" 15 | "time" 16 | ) 17 | 18 | func TestCalcDiff(t *testing.T) { 19 | var mp *MackerelPlugin 20 | 21 | val1 := 10.0 22 | val2 := 0.0 23 | now := time.Now() 24 | last := time.Unix(now.Unix()-10, 0) 25 | 26 | diff, err := mp.calcDiff(val1, now, val2, last) 27 | if diff != 60.0 { 28 | t.Errorf("calcDiff: %f should be %f", diff, 60.0) 29 | } 30 | if err != nil { 31 | t.Error("calcDiff causes an error") 32 | } 33 | } 34 | 35 | func TestCalcDiffWithReset(t *testing.T) { 36 | var mp *MackerelPlugin 37 | 38 | val := 10.0 39 | lastval := 12345.0 40 | now := time.Now() 41 | last := time.Unix(now.Unix()-60, 0) 42 | 43 | diff, err := mp.calcDiff(val, now, lastval, last) 44 | if err == nil { 45 | t.Errorf("calcDiff with counter reset should cause an error: %f", diff) 46 | } 47 | } 48 | 49 | func TestFormatValues(t *testing.T) { 50 | wtr := &bytes.Buffer{} 51 | mp := &MackerelPlugin{writer: wtr} 52 | 53 | prefix := "foo" 54 | metric := Metrics{Name: "cmd_get", Label: "Get", Diff: true} 55 | stat := map[string]float64{"cmd_get": 1000.0} 56 | lastStat := map[string]float64{"cmd_get": 500.0, ".last_diff.cmd_get": 300.0} 57 | now := time.Unix(1437227240, 0) 58 | lastTime := now.Add(time.Second * (-60)) 59 | mp.formatValues(prefix, metric, stat, lastStat, now, lastTime) 60 | 61 | got := wtr.String() 62 | expect := "foo.cmd_get 500 1437227240\n" 63 | if got != expect { 64 | t.Errorf("result of formatValues is not expected one: %s", got) 65 | } 66 | } 67 | 68 | // an example implementation 69 | type testMemcachedPlugin struct { 70 | } 71 | 72 | func (m testMemcachedPlugin) GraphDefinition() map[string]Graphs { 73 | return map[string]Graphs{ 74 | "memcached.cmd": { 75 | Label: "Memcached Command", 76 | Unit: "integer", 77 | Metrics: []Metrics{ 78 | {Name: "cmd_get", Label: "Get"}, 79 | }, 80 | }, 81 | } 82 | } 83 | 84 | func (m testMemcachedPlugin) FetchMetrics() (map[string]float64, error) { 85 | return map[string]float64{ 86 | "cmd_get": 11.0, 87 | "cmd_set": 8.0, 88 | }, nil 89 | } 90 | 91 | func TestOutputDefinitions(t *testing.T) { 92 | var m testMemcachedPlugin 93 | mp := NewMackerelPlugin(m) 94 | wtr := &bytes.Buffer{} 95 | mp.writer = wtr 96 | mp.OutputDefinitions() 97 | 98 | expect := `# mackerel-agent-plugin 99 | {"graphs":{"memcached.cmd":{"label":"Memcached Command","unit":"integer","metrics":[{"name":"cmd_get","label":"Get","stacked":false}]}}} 100 | ` 101 | got := wtr.String() 102 | if got != expect { 103 | t.Errorf("result of OutputDefinitions is invalid :%s", got) 104 | } 105 | } 106 | 107 | func TestOutputValues(t *testing.T) { 108 | var m testMemcachedPlugin 109 | mp := NewMackerelPlugin(m) 110 | wtr := &bytes.Buffer{} 111 | mp.writer = wtr 112 | mp.OutputValues() 113 | epoch := time.Now().Unix() 114 | expect := fmt.Sprintf("memcached.cmd.cmd_get\t%d\t%d\n", 11, epoch) 115 | got := wtr.String() 116 | if got != expect { 117 | t.Errorf("result of OutputValues is invalid :%s", got) 118 | } 119 | } 120 | 121 | type testP struct{} 122 | 123 | func (t testP) FetchMetrics() (map[string]float64, error) { 124 | return map[string]float64{ 125 | "bar": 15.0, 126 | "baz": 18.0, 127 | }, nil 128 | } 129 | 130 | func (t testP) GraphDefinition() map[string]Graphs { 131 | return map[string]Graphs{ 132 | "": { 133 | Unit: "integer", 134 | Metrics: []Metrics{ 135 | {Name: "bar"}, 136 | }, 137 | }, 138 | "fuga": { 139 | Unit: "float", 140 | Metrics: []Metrics{ 141 | {Name: "baz"}, 142 | }, 143 | }, 144 | } 145 | } 146 | 147 | func (t testP) MetricKeyPrefix() string { 148 | return "testP" 149 | } 150 | 151 | func TestDefaultTempfile(t *testing.T) { 152 | mp := &MackerelPlugin{} 153 | filename := filepath.Base(os.Args[0]) 154 | expect := filepath.Join(os.TempDir(), fmt.Sprintf( 155 | "mackerel-plugin-%s-%x", 156 | filename, 157 | sha1.Sum([]byte(strings.Join(os.Args[1:], " "))), 158 | )) 159 | if mp.tempfilename() != expect { 160 | t.Errorf("mp.tempfilename() should be %s, but: %s", expect, mp.tempfilename()) 161 | } 162 | 163 | pPrefix := NewMackerelPlugin(testP{}) 164 | expectForPrefix := filepath.Join(os.TempDir(), fmt.Sprintf( 165 | "mackerel-plugin-testP-%x", 166 | sha1.Sum([]byte(strings.Join(os.Args[1:], " "))), 167 | )) 168 | if pPrefix.tempfilename() != expectForPrefix { 169 | t.Errorf("pPrefix.tempfilename() should be %s, but: %s", expectForPrefix, pPrefix.tempfilename()) 170 | } 171 | } 172 | 173 | func TestTempfilenameFromExecutableFilePath(t *testing.T) { 174 | mp := &MackerelPlugin{} 175 | 176 | wd, _ := os.Getwd() 177 | // not PluginWithPrefix, regular filename 178 | expect1 := filepath.Join(os.TempDir(), "mackerel-plugin-foobar-da39a3ee5e6b4b0d3255bfef95601890afd80709") 179 | filename1 := mp.generateTempfilePath([]string{filepath.Join(wd, "foobar")}) 180 | if filename1 != expect1 { 181 | t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect1, filename1) 182 | } 183 | 184 | // not PluginWithPrefix, contains some characters to be sanitized 185 | expect2 := filepath.Join(os.TempDir(), "mackerel-plugin-some_sanitized_name_1.2-da39a3ee5e6b4b0d3255bfef95601890afd80709") 186 | filename2 := mp.generateTempfilePath([]string{filepath.Join(wd, "some sanitized:name+1.2")}) 187 | if filename2 != expect2 { 188 | t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect2, filename2) 189 | } 190 | 191 | // not PluginWithPrefix, begins with "mackerel-plugin-" 192 | expect3 := filepath.Join(os.TempDir(), "mackerel-plugin-trimmed-da39a3ee5e6b4b0d3255bfef95601890afd80709") 193 | filename3 := mp.generateTempfilePath([]string{filepath.Join(wd, "mackerel-plugin-trimmed")}) 194 | if filename3 != expect3 { 195 | t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect3, filename3) 196 | } 197 | 198 | // PluginWithPrefix ignores current filename 199 | pPrefix := NewMackerelPlugin(testP{}) 200 | expectForPrefix := filepath.Join(os.TempDir(), "mackerel-plugin-testP-da39a3ee5e6b4b0d3255bfef95601890afd80709") 201 | filenameForPrefix := pPrefix.generateTempfilePath([]string{filepath.Join(wd, "foo")}) 202 | if filenameForPrefix != expectForPrefix { 203 | t.Errorf("pPrefix.generateTempfilePath() should be %s, but: %s", expectForPrefix, filenameForPrefix) 204 | } 205 | 206 | // Generate sha1 using command-line options, and use it for filename 207 | expect5 := filepath.Join(os.TempDir(), "mackerel-plugin-mysql-9045504f8fadd7ddcc8962ec1d9fc70e3f7ba627") 208 | filename5 := mp.generateTempfilePath([]string{filepath.Join(wd, "mackerel-plugin-mysql"), "-host", "hostname1", "-port", "3306"}) 209 | if filename5 != expect5 { 210 | t.Errorf("p.generateTempfilePath() should be %s, but: %s", expect5, filename5) 211 | } 212 | } 213 | 214 | func TestPluginOutputDefinitionsWithPrefix(t *testing.T) { 215 | mp := NewMackerelPlugin(testP{}) 216 | wtr := &bytes.Buffer{} 217 | mp.writer = wtr 218 | mp.OutputDefinitions() 219 | expect := `# mackerel-agent-plugin 220 | {"graphs":{"testP":{"label":"TestP","unit":"integer","metrics":[{"name":"bar","label":"Bar","stacked":false}]},"testP.fuga":{"label":"TestP Fuga","unit":"float","metrics":[{"name":"baz","label":"Baz","stacked":false}]}}} 221 | ` 222 | got := wtr.String() 223 | if got != expect { 224 | t.Errorf("result of OutputDefinitions is invalid: %s", got) 225 | } 226 | } 227 | 228 | func TestOutputValuesWithPrefix(t *testing.T) { 229 | mp := NewMackerelPlugin(testP{}) 230 | wtr := &bytes.Buffer{} 231 | mp.writer = wtr 232 | mp.OutputValues() 233 | epoch := time.Now().Unix() 234 | expect := fmt.Sprintf("testP.bar\t15\t%[1]d\ntestP.fuga.baz\t18\t%[1]d\n", epoch) 235 | got := wtr.String() 236 | if sortLines(got) != sortLines(expect) { 237 | t.Errorf("result of OutputValues is invalid :%s", got) 238 | } 239 | } 240 | 241 | type testPHasDiff struct{} 242 | 243 | func (t testPHasDiff) FetchMetrics() (map[string]float64, error) { 244 | return nil, nil 245 | } 246 | 247 | func (t testPHasDiff) GraphDefinition() map[string]Graphs { 248 | return map[string]Graphs{ 249 | "hoge": { 250 | Metrics: []Metrics{ 251 | {Name: "hoge1", Label: "hoge1", Diff: true}, 252 | }, 253 | }, 254 | } 255 | } 256 | 257 | type testPHasntDiff struct{} 258 | 259 | func (t testPHasntDiff) FetchMetrics() (map[string]float64, error) { 260 | return nil, nil 261 | } 262 | 263 | func (t testPHasntDiff) GraphDefinition() map[string]Graphs { 264 | return map[string]Graphs{ 265 | "hoge": { 266 | Metrics: []Metrics{ 267 | {Name: "hoge1", Label: "hoge1"}, 268 | }, 269 | }, 270 | } 271 | } 272 | 273 | func TestPluginHasDiff(t *testing.T) { 274 | pHasDiff := NewMackerelPlugin(testPHasDiff{}) 275 | if !pHasDiff.hasDiff() { 276 | t.Errorf("something went wrong") 277 | } 278 | 279 | pHasntDiff := NewMackerelPlugin(testPHasntDiff{}) 280 | if pHasntDiff.hasDiff() { 281 | t.Errorf("something went wrong") 282 | } 283 | } 284 | 285 | func TestFormatValuesWithWildcard(t *testing.T) { 286 | wtr := &bytes.Buffer{} 287 | mp := &MackerelPlugin{writer: wtr} 288 | prefix := "foo.#" 289 | metric := Metrics{Name: "bar", Label: "Get", Diff: true} 290 | stat := map[string]float64{"foo.1.bar": 1000.0, "foo.2.bar": 2000.0} 291 | lastStat := map[string]float64{"foo.1.bar": 500.0, ".last_diff.foo.1.bar": 2.0} 292 | now := time.Unix(1437227240, 0) 293 | lastTime := now.Add(time.Second * (-60)) 294 | mp.formatValuesWithWildcard(prefix, metric, stat, lastStat, now, lastTime) 295 | 296 | expect := "foo.1.bar 500 1437227240\n" 297 | got := wtr.String() 298 | if got != expect { 299 | t.Errorf("something went wrong: %s", got) 300 | } 301 | } 302 | 303 | func TestFormatValuesWithWildcardAndNoDiff(t *testing.T) { 304 | wtr := &bytes.Buffer{} 305 | mp := &MackerelPlugin{writer: wtr} 306 | prefix := "foo.#" 307 | metric := Metrics{Name: "bar", Label: "Get", Diff: false} 308 | stat := map[string]float64{"foo.1.bar": 1000.0} 309 | lastStat := map[string]float64{"foo.1.bar": 500.0, ".last_diff.foo.1.bar": 2.0} 310 | now := time.Unix(1437227240, 0) 311 | lastTime := now.Add(time.Second * (-60)) 312 | mp.formatValuesWithWildcard(prefix, metric, stat, lastStat, now, lastTime) 313 | 314 | expect := "foo.1.bar 1000 1437227240\n" 315 | got := wtr.String() 316 | if got != expect { 317 | t.Errorf("something went wrong: %s", got) 318 | } 319 | } 320 | 321 | func TestFormatValuesWithWildcardAstarisk(t *testing.T) { 322 | wtr := &bytes.Buffer{} 323 | mp := &MackerelPlugin{writer: wtr} 324 | prefix := "foo" 325 | metric := Metrics{Name: "*", Label: "Get", Diff: true} 326 | stat := map[string]float64{"foo.1": 1000.0, "foo.2": 2000.0} 327 | lastStat := map[string]float64{"foo.1": 500.0, ".last_diff.foo.1": 2.0} 328 | now := time.Unix(1437227240, 0) 329 | lastTime := now.Add(time.Second * (-60)) 330 | mp.formatValuesWithWildcard(prefix, metric, stat, lastStat, now, lastTime) 331 | 332 | expect := "foo.1 500 1437227240\n" 333 | got := wtr.String() 334 | if got != expect { 335 | t.Errorf("something went wrong: %s", got) 336 | } 337 | } 338 | 339 | type testPWithWildcard struct{} 340 | 341 | func (t testPWithWildcard) FetchMetrics() (map[string]float64, error) { 342 | return map[string]float64{ 343 | "piyo.1.bar": 11, 344 | "piyo.2.bar": 12, 345 | "piyo.3.bar": 13, 346 | "baz": 18.0, 347 | }, nil 348 | } 349 | 350 | func (t testPWithWildcard) GraphDefinition() map[string]Graphs { 351 | return map[string]Graphs{ 352 | "piyo.#": { 353 | Metrics: []Metrics{ 354 | {Name: "bar"}, 355 | }, 356 | }, 357 | "fuga": { 358 | Metrics: []Metrics{ 359 | {Name: "baz"}, 360 | }, 361 | }, 362 | } 363 | } 364 | 365 | func (t testPWithWildcard) MetricKeyPrefix() string { 366 | return "testPWithWildcard" 367 | } 368 | 369 | func TestPluginOutputDefinitionsWithPrefixAndWildcard(t *testing.T) { 370 | mp := NewMackerelPlugin(testPWithWildcard{}) 371 | wtr := &bytes.Buffer{} 372 | mp.writer = wtr 373 | os.Setenv("MACKEREL_AGENT_PLUGIN_META", "1") 374 | defer os.Setenv("MACKEREL_AGENT_PLUGIN_META", "") 375 | mp.Run() 376 | expect := `# mackerel-agent-plugin 377 | {"graphs":{"testPWithWildcard.fuga":{"label":"TestPWithWildcard Fuga","unit":"","metrics":[{"name":"baz","label":"Baz","stacked":false}]},"testPWithWildcard.piyo.#":{"label":"TestPWithWildcard Piyo","unit":"","metrics":[{"name":"bar","label":"Bar","stacked":false}]}}} 378 | ` 379 | got := wtr.String() 380 | if got != expect { 381 | t.Errorf("result of OutputDefinitions is invalid: %s", got) 382 | } 383 | } 384 | 385 | func TestOutputValuesWithPrefixAndWildcard(t *testing.T) { 386 | mp := NewMackerelPlugin(testPWithWildcard{}) 387 | wtr := &bytes.Buffer{} 388 | mp.writer = wtr 389 | mp.Run() 390 | epoch := time.Now().Unix() 391 | expect := fmt.Sprintf("testPWithWildcard.piyo.1.bar\t11\t%[1]d\n"+ 392 | "testPWithWildcard.piyo.2.bar\t12\t%[1]d\n"+ 393 | "testPWithWildcard.piyo.3.bar\t13\t%[1]d\n"+ 394 | "testPWithWildcard.fuga.baz\t18\t%[1]d\n", epoch) 395 | got := wtr.String() 396 | if sortLines(got) != sortLines(expect) { 397 | t.Errorf("result of OutputValues is invalid :%s", got) 398 | } 399 | } 400 | 401 | func TestSetTempfileWithBasename(t *testing.T) { 402 | var p MackerelPlugin 403 | 404 | expect1 := filepath.Join(os.TempDir(), "my-super-tempfile") 405 | p.SetTempfileByBasename("my-super-tempfile") 406 | if p.Tempfile != expect1 { 407 | t.Errorf("p.SetTempfileByBasename() should set %s, but: %s", expect1, p.Tempfile) 408 | } 409 | 410 | origDir := os.Getenv("MACKEREL_PLUGIN_WORKDIR") 411 | os.Setenv("MACKEREL_PLUGIN_WORKDIR", "/tmp/somewhere") 412 | defer os.Setenv("MACKEREL_PLUGIN_WORKDIR", origDir) 413 | 414 | expect2 := filepath.FromSlash("/tmp/somewhere/my-great-tempfile") 415 | p.SetTempfileByBasename("my-great-tempfile") 416 | if p.Tempfile != expect2 { 417 | t.Errorf("p.SetTempfileByBasename() should set %s, but: %s", expect2, p.Tempfile) 418 | } 419 | } 420 | 421 | func sortLines(s string) string { 422 | xs := strings.Split(s, "\n") 423 | sort.Strings(xs) 424 | return strings.Join(xs, "\n") 425 | } 426 | 427 | func TestFetchLastValuesIfNotExist(t *testing.T) { 428 | p := NewMackerelPlugin(testPHasDiff{}) 429 | p.Tempfile = "state_file_should_not_exist.json" 430 | _, last, err := p.fetchLastValues(time.Now()) 431 | if err != nil { 432 | t.Fatal(err) 433 | } 434 | if !last.IsZero() { 435 | t.Errorf("Timestamp = %v; want 0001-01-01", last) 436 | } 437 | } 438 | 439 | func TestFetchLastValuesIfFileIsBroken(t *testing.T) { 440 | p := NewMackerelPlugin(testPHasDiff{}) 441 | f := createTempState(t) 442 | defer f.Close() 443 | p.Tempfile = f.Name() 444 | 445 | if _, err := f.Write([]byte("{{{-0")); err != nil { 446 | t.Fatal(err) 447 | } 448 | if _, err := f.Seek(0, 0); err != nil { 449 | t.Fatal(err) 450 | } 451 | _, last, err := p.fetchLastValues(time.Now()) 452 | if err == nil { 453 | t.Errorf("fetchLastValues should return an error; state is broken") 454 | } 455 | if !last.IsZero() { 456 | t.Errorf("Timestamp = %v; want 0001-01-01", last) 457 | } 458 | } 459 | 460 | func TestFetchLastValuesReadStateSameTime(t *testing.T) { 461 | p := NewMackerelPlugin(testPHasDiff{}) 462 | f := createTempState(t) 463 | defer f.Close() 464 | p.Tempfile = f.Name() 465 | now := time.Now() 466 | stats := make(map[string]float64) 467 | if err := p.saveValues(stats, now); err != nil { 468 | t.Fatal(err) 469 | } 470 | 471 | _, last, err := p.fetchLastValues(now) 472 | if err != errStateRecentlyUpdated { 473 | t.Errorf("fetchLastValues: %v; want %v", err, errStateRecentlyUpdated) 474 | } 475 | if !last.IsZero() { 476 | t.Errorf("fetchLastValues: last should be zero; but %v", last) 477 | } 478 | } 479 | 480 | func TestSaveStateIfContainsInvalidNumbers(t *testing.T) { 481 | p := NewMackerelPlugin(testPHasDiff{}) 482 | f := createTempState(t) 483 | defer f.Close() 484 | p.Tempfile = f.Name() 485 | 486 | stats := map[string]float64{ 487 | "key1": 3.0, 488 | "key2": math.Inf(1), 489 | "key3": math.Inf(-1), 490 | "key4": math.NaN(), 491 | } 492 | const lastTime = 1624848982 493 | 494 | now := time.Unix(lastTime, 0) 495 | if err := p.saveValues(stats, now); err != nil { 496 | t.Errorf("saveValues: %v", err) 497 | } 498 | stats, _, err := p.fetchLastValues(now.Add(time.Second)) 499 | if err != nil { 500 | t.Fatal("fetchLastValues:", err) 501 | } 502 | want := map[string]float64{ 503 | "_lastTime": lastTime, 504 | "key1": 3.0, 505 | } 506 | if !reflect.DeepEqual(stats, want) { 507 | t.Errorf("saveValues stores only valid numbers: got %v; want %v", stats, want) 508 | } 509 | } 510 | 511 | func createTempState(t testing.TB) *os.File { 512 | t.Helper() 513 | f, err := ioutil.TempFile("", "mackerel-plugin.") 514 | if err != nil { 515 | t.Fatal(err) 516 | } 517 | t.Cleanup(func() { 518 | if err := os.Remove(f.Name()); err != nil { 519 | t.Fatal(err) 520 | } 521 | }) 522 | return f 523 | } 524 | --------------------------------------------------------------------------------