// Prepare sets up API and registers the host data to the Mackerel server. // Use returned values to call Run(). func Prepare(conf *config.Config) (*Context, error) { api, err := mackerel.NewAPI(conf.Apibase, conf.Apikey, conf.Verbose) if err != nil { return nil, fmt.Errorf("Failed to prepare an api: %s", err.Error()) } host, err := prepareHost(conf.Root, api, conf.Roles, conf.CheckNames(), conf.DisplayName, conf.HostStatus.OnStart) if err != nil { return nil, fmt.Errorf("Failed to prepare host: %s", err.Error()) } return &Context{ ag: NewAgent(conf), conf: conf, host: host, api: api, }, nil }
func runOncePayload(conf *config.Config) ([]mackerel.CreateGraphDefsPayload, *mackerel.HostSpec, *agent.MetricsResult, error) { hostname, meta, interfaces, err := collectHostSpecs() if err != nil { logger.Errorf("While collecting host specs: %s", err) return nil, nil, nil, err } origInterval := metricsInterval metricsInterval = 1 * time.Second defer func() { metricsInterval = origInterval }() ag := NewAgent(conf) graphdefs := ag.CollectGraphDefsOfPlugins() metrics := ag.CollectMetrics(time.Now()) return graphdefs, &mackerel.HostSpec{ Name: hostname, Meta: meta, Interfaces: interfaces, RoleFullnames: conf.Roles, Checks: conf.CheckNames(), DisplayName: conf.DisplayName, }, metrics, nil }