github.com/mackerelio/mackerel-agent-plugins@v0.89.3/mackerel-plugin-td-table-count/lib/mackerel-plugin-td-table-count.go (about)

     1  package mptdtablecount
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strings"
     7  
     8  	mp "github.com/mackerelio/go-mackerel-plugin-helper"
     9  	td "github.com/mattn/go-treasuredata"
    10  )
    11  
    12  // TDTablePlugin mackerel plugin for td
    13  type TDTablePlugin struct {
    14  	APIKey           string
    15  	Database         string
    16  	IgnoreTableNames []string
    17  	Tempfile         string
    18  }
    19  
    20  func getTables(m TDTablePlugin) ([]td.Table, error) {
    21  	cli := td.NewClient(m.APIKey)
    22  
    23  	tables, err := cli.TableList(m.Database)
    24  	if err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	filteredTables := []td.Table{}
    29  
    30  	for _, table := range tables {
    31  		ignore := false
    32  		for _, ignoreTableName := range m.IgnoreTableNames {
    33  			if table.Name == ignoreTableName {
    34  				ignore = true
    35  			}
    36  		}
    37  
    38  		if !ignore {
    39  			filteredTables = append(filteredTables, table)
    40  		}
    41  	}
    42  
    43  	return filteredTables, nil
    44  }
    45  
    46  // FetchMetrics interface for mackerelplugin
    47  func (m TDTablePlugin) FetchMetrics() (map[string]interface{}, error) {
    48  	stat := make(map[string]interface{})
    49  
    50  	tables, _ := getTables(m)
    51  	for _, table := range tables {
    52  		stat[table.Name] = float64(table.Count)
    53  	}
    54  
    55  	return stat, nil
    56  }
    57  
    58  // GraphDefinition interface for mackerelplugin
    59  func (m TDTablePlugin) GraphDefinition() map[string]mp.Graphs {
    60  	tables, _ := getTables(m)
    61  
    62  	var metrics []mp.Metrics
    63  	for _, table := range tables {
    64  		metrics = append(metrics, mp.Metrics{
    65  			Name:    table.Name,
    66  			Label:   table.Name,
    67  			Diff:    false,
    68  			Stacked: true,
    69  		})
    70  	}
    71  
    72  	graph := mp.Graphs{
    73  		Label:   fmt.Sprintf("TD %s Database Number of rows", m.Database),
    74  		Unit:    "integer",
    75  		Metrics: metrics,
    76  	}
    77  
    78  	graphdef := map[string]mp.Graphs{
    79  		fmt.Sprintf("td-table.%s", m.Database): graph,
    80  	}
    81  
    82  	return graphdef
    83  }
    84  
    85  // Do the plugin
    86  func Do() {
    87  	optAPIKey := flag.String("api-key", "", "API Key")
    88  	optDatabase := flag.String("database", "", "Database name")
    89  	optIgnoreTableNames := flag.String("ignore-table", "", "Ignore Table name (Can be Comma-Separated)")
    90  	optTempfile := flag.String("tempfile", "", "Temp file name")
    91  	flag.Parse()
    92  
    93  	var plugin TDTablePlugin
    94  	plugin.APIKey = *optAPIKey
    95  	plugin.Database = *optDatabase
    96  
    97  	ignoreTableNames := []string{}
    98  	if *optIgnoreTableNames != "" {
    99  		ignoreTableNames = strings.Split(*optIgnoreTableNames, ",")
   100  	}
   101  	plugin.IgnoreTableNames = ignoreTableNames
   102  
   103  	helper := mp.NewMackerelPlugin(plugin)
   104  	helper.Tempfile = *optTempfile
   105  
   106  	helper.Run()
   107  }