github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-conntrack/lib/cli.go (about) 1 package mpconntrack 2 3 import ( 4 "flag" 5 "fmt" 6 "io" 7 8 mp "github.com/mackerelio/go-mackerel-plugin-helper" 9 ) 10 11 // Exit codes are int values that represent an exit code for a particular error. 12 const ( 13 ExitCodeOK int = 0 14 ExitCodeParseFlagError int = 1 + iota 15 ) 16 17 // ConntrackPlugin mackerel plugin for *_conntrack. 18 type ConntrackPlugin struct{} 19 20 // GraphDefinition interface for mackerelplugin. 21 func (c ConntrackPlugin) GraphDefinition() map[string]mp.Graphs { 22 // graphdef is Graph definition for mackerelplugin. 23 var graphdef = map[string]mp.Graphs{ 24 "conntrack.count": { 25 Label: "Conntrack Count", 26 Unit: "integer", 27 Metrics: []mp.Metrics{ 28 {Name: "*", Label: "%1", Diff: false, Stacked: true, Type: "uint64"}, 29 }, 30 }, 31 } 32 33 return graphdef 34 } 35 36 // FetchMetrics interface for mackerelplugin. 37 func (c ConntrackPlugin) FetchMetrics() (map[string]interface{}, error) { 38 conntrackCount, err := CurrentValue(ConntrackCountPaths) 39 if err != nil { 40 return nil, err 41 } 42 43 conntrackMax, err := CurrentValue(ConntrackMaxPaths) 44 if err != nil { 45 return nil, err 46 } 47 48 stat := make(map[string]interface{}) 49 stat["conntrack.count.used"] = conntrackCount 50 stat["conntrack.count.free"] = (conntrackMax - conntrackCount) 51 52 return stat, nil 53 } 54 55 // CLI is the object for command line interface. 56 type CLI struct { 57 outStream, errStream io.Writer 58 } 59 60 // Run is to parse flags and Run helper (MackerelPlugin) with the given arguments. 61 func (c *CLI) Run(args []string) int { 62 // Flags 63 var ( 64 tempfile string 65 version bool 66 ) 67 68 // Define option flag parse 69 flags := flag.NewFlagSet(Name, flag.ContinueOnError) 70 flags.BoolVar(&version, "version", false, "Print version information and quit.") 71 flags.StringVar(&tempfile, "tempfile", "", "Temp file name") 72 73 // Parse commandline flag 74 if err := flags.Parse(args[1:]); err != nil { 75 return ExitCodeParseFlagError 76 } 77 78 // Show version 79 if version { 80 fmt.Fprintf(c.errStream, "%s version %s\n", Name, Version) 81 return ExitCodeOK 82 } 83 84 // Create MackerelPlugin for Conntrack 85 var cp ConntrackPlugin 86 helper := mp.NewMackerelPlugin(cp) 87 helper.Tempfile = tempfile 88 89 helper.Run() 90 91 return ExitCodeOK 92 }