github.com/mackerelio/mackerel-agent-plugins@v0.89.3/tool/starter (about)

     1  #!/usr/bin/env perl
     2  use 5.016;
     3  use warnings;
     4  use autodie;
     5  
     6  use File::Path qw/mkpath/;
     7  
     8  my $plug = shift or die 'plugin name required';
     9  my $pkg_base = lc($plug =~ s/[^a-zA-Z0-9]//gr);
    10  my $struct = ucfirst $pkg_base;
    11  
    12  my $main_go = qq{package main
    13  
    14  import "github.com/mackerelio/mackerel-agent-plugins/mackerel-plugin-$plug/lib"
    15  
    16  func main() {
    17  	mp$pkg_base.Do()
    18  }
    19  };
    20  
    21  my $readme = qq{mackerel-plugin-$plug
    22  =====================
    23  
    24  $plug custom metrics plugin for mackerel.io agent.
    25  
    26  ## Synopsis
    27  
    28  ```shell
    29  mackerel-plugin-$plug
    30  ```
    31  
    32  ## Example of mackerel-agent.conf
    33  
    34  ```
    35  [plugin.metrics.$plug]
    36  command = "/path/to/mackerel-plugin-$plug"
    37  ```
    38  };
    39  
    40  my $src = qq|package mp$pkg_base
    41  
    42  import (
    43  	"flag"
    44  
    45  	mp "github.com/mackerelio/go-mackerel-plugin"
    46  )
    47  
    48  // ${struct}Plugin mackerel plugin
    49  type ${struct}Plugin struct {
    50  	prefix string
    51  }
    52  
    53  // MetricKeyPrefix interface for PluginWithPrefix
    54  func (p ${struct}Plugin) MetricKeyPrefix() string {
    55  	if p.prefix == "" {
    56  		p.prefix = "$pkg_base"
    57  	}
    58  	return p.prefix
    59  }
    60  
    61  // GraphDefinition interface for mackerelplugin
    62  func (p ${struct}Plugin) GraphDefinition() map[string]mp.Graphs {
    63      return nil
    64  	// labelPrefix := strings.Title(p.prefix)
    65  	// return map[string]mp.Graphs{
    66  	// 	"sample": {
    67  	// 		Label: labelPrefix,
    68  	// 		Unit:  "float",
    69  	// 		Metrics: []mp.Metrics{
    70  	// 			{Name: "seconds", Label: "Seconds"},
    71  	// 		},
    72  	// 	},
    73  	// }
    74  }
    75  
    76  // FetchMetrics interface for mackerelplugin
    77  func (p ${struct}Plugin) FetchMetrics() (map[string]float64, error) {
    78      return nil, nil
    79  	// return map[string]float64{"sample.seconds": 3}, nil
    80  }
    81  
    82  // Do the plugin
    83  func Do() {
    84  	optPrefix := flag.String("metric-key-prefix", "", "Metric key prefix")
    85  	optTempfile := flag.String("tempfile", "", "Temp file name")
    86  	flag.Parse()
    87  
    88  	p := ${struct}Plugin{
    89          prefix: *optPrefix,
    90  	}
    91  	helper := mp.NewMackerelPlugin(p)
    92  	helper.Tempfile = *optTempfile
    93  	helper.Run()
    94  }
    95  |;
    96  
    97  mkpath("mackerel-plugin-$plug/lib");
    98  
    99  sub spew {
   100      my ($file, $content) = @_;
   101      open my $fh, '>', $file;
   102      print $fh $content;
   103  }
   104  
   105  spew("mackerel-plugin-$plug/main.go", $main_go);
   106  spew("mackerel-plugin-$plug/README.md", $readme);
   107  spew("mackerel-plugin-$plug/lib/$plug.go", $src);