本文整理汇总了Golang中github.com/mackerelio/mkr/logger.DieIf函数的典型用法代码示例。如果您正苦于以下问题:Golang DieIf函数的具体用法?Golang DieIf怎么用?Golang DieIf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了DieIf函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: joinMonitorsAndHosts
func joinMonitorsAndHosts(client *mkr.Client, alerts []*mkr.Alert) []*alertSet {
hostsJSON, err := client.FindHosts(&mkr.FindHostsParam{
Statuses: []string{"working", "standby", "poweroff", "maintenance"},
})
logger.DieIf(err)
hosts := map[string]*mkr.Host{}
for _, host := range hostsJSON {
hosts[host.ID] = host
}
monitorsJSON, err := client.FindMonitors()
logger.DieIf(err)
monitors := map[string]*mkr.Monitor{}
for _, monitor := range monitorsJSON {
monitors[monitor.ID] = monitor
}
alertSets := []*alertSet{}
for _, alert := range alerts {
alertSets = append(
alertSets,
&alertSet{Alert: alert, Host: hosts[alert.HostID], Monitor: monitors[alert.MonitorID]},
)
}
return alertSets
}
示例2: doCreate
func doCreate(c *cli.Context) {
conffile := c.GlobalString("conf")
argHostName := c.Args().Get(0)
optRoleFullnames := c.StringSlice("roleFullname")
optStatus := c.String("status")
if argHostName == "" {
cli.ShowCommandHelp(c, "create")
os.Exit(1)
}
client := newMackerel(conffile)
hostID, err := client.CreateHost(&mkr.CreateHostParam{
Name: argHostName,
RoleFullnames: optRoleFullnames,
})
logger.DieIf(err)
logger.Log("created", hostID)
if optStatus != "" {
err := client.UpdateHostStatus(hostID, optStatus)
logger.DieIf(err)
logger.Log("updated", fmt.Sprintf("%s %s", hostID, optStatus))
}
}
示例3: doThrow
func doThrow(c *cli.Context) error {
conffile := c.GlobalString("conf")
optHostID := c.String("host")
optService := c.String("service")
var metricValues []*(mkr.MetricValue)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
// name, value, timestamp
// ex.) tcp.CLOSING 0 1397031808
items := strings.Fields(line)
if len(items) != 3 {
continue
}
value, err := strconv.ParseFloat(items[1], 64)
if err != nil {
logger.Log("warning", fmt.Sprintf("Failed to parse values: %s", err))
continue
}
time, err := strconv.ParseInt(items[2], 10, 64)
if err != nil {
logger.Log("warning", fmt.Sprintf("Failed to parse values: %s", err))
continue
}
metricValue := &mkr.MetricValue{
Name: items[0],
Value: value,
Time: time,
}
metricValues = append(metricValues, metricValue)
}
logger.ErrorIf(scanner.Err())
client := newMackerel(conffile)
if optHostID != "" {
err := client.PostHostMetricValuesByHostID(optHostID, metricValues)
logger.DieIf(err)
for _, metric := range metricValues {
logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", optHostID, metric.Name, metric.Value, metric.Time))
}
} else if optService != "" {
err := client.PostServiceMetricValues(optService, metricValues)
logger.DieIf(err)
for _, metric := range metricValues {
logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", optService, metric.Name, metric.Value, metric.Time))
}
} else {
cli.ShowCommandHelp(c, "throw")
os.Exit(1)
}
return nil
}
示例4: doUpdate
func doUpdate(c *cli.Context) {
conffile := c.GlobalString("conf")
argHostIDs := c.Args()
optName := c.String("name")
optStatus := c.String("status")
optRoleFullnames := c.StringSlice("roleFullname")
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = LoadHostIDFromConfig(conffile); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
}
needUpdateHostStatus := optStatus != ""
needUpdateHost := (optName != "" || len(optRoleFullnames) > 0)
if !needUpdateHostStatus && !needUpdateHost {
cli.ShowCommandHelp(c, "update")
os.Exit(1)
}
client := newMackerel(conffile)
var wg sync.WaitGroup
for _, hostID := range argHostIDs {
wg.Add(1)
go func(hostID string) {
defer wg.Done()
if needUpdateHostStatus {
err := client.UpdateHostStatus(hostID, optStatus)
logger.DieIf(err)
}
if needUpdateHost {
_, err := client.UpdateHost(hostID, &mkr.UpdateHostParam{
Name: optName,
RoleFullnames: optRoleFullnames,
})
logger.DieIf(err)
}
logger.Log("updated", hostID)
}(hostID)
}
wg.Wait()
}
示例5: doStatus
func doStatus(c *cli.Context) {
conffile := c.GlobalString("conf")
argHostID := c.Args().Get(0)
isVerbose := c.Bool("verbose")
if argHostID == "" {
if argHostID = LoadHostIDFromConfig(conffile); argHostID == "" {
cli.ShowCommandHelp(c, "status")
os.Exit(1)
}
}
host, err := newMackerel(conffile).FindHost(argHostID)
logger.DieIf(err)
if isVerbose {
PrettyPrintJSON(host)
} else {
format := &HostFormat{
ID: host.ID,
Name: host.Name,
Status: host.Status,
RoleFullnames: host.GetRoleFullnames(),
IsRetired: host.IsRetired,
CreatedAt: host.DateStringFromCreatedAt(),
IPAddresses: host.IPAddresses(),
}
PrettyPrintJSON(format)
}
}
示例6: doRetire
func doRetire(c *cli.Context) {
conffile := c.GlobalString("conf")
argHostIDs := c.Args()
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = LoadHostIDFromConfig(conffile); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "retire")
os.Exit(1)
}
}
client := newMackerel(conffile)
var wg sync.WaitGroup
for _, hostID := range argHostIDs {
wg.Add(1)
go func(hostID string) {
defer wg.Done()
err := client.RetireHost(hostID)
logger.DieIf(err)
logger.Log("retired", hostID)
}(hostID)
}
wg.Wait()
}
示例7: doRetire
func doRetire(c *cli.Context) error {
conffile := c.GlobalString("conf")
force := c.Bool("force")
argHostIDs := c.Args()
if len(argHostIDs) < 1 {
argHostIDs = make([]string, 1)
if argHostIDs[0] = LoadHostIDFromConfig(conffile); argHostIDs[0] == "" {
cli.ShowCommandHelp(c, "retire")
os.Exit(1)
}
}
if !force && !prompter.YN("Retire following hosts.\n "+strings.Join(argHostIDs, "\n ")+"\nAre you sure?", true) {
logger.Log("", "retirement is canceled.")
return nil
}
client := newMackerel(conffile)
for _, hostID := range argHostIDs {
err := client.RetireHost(hostID)
logger.DieIf(err)
logger.Log("retired", hostID)
}
return nil
}
示例8: doAlertsRetrieve
func doAlertsRetrieve(c *cli.Context) error {
conffile := c.GlobalString("conf")
client := newMackerel(conffile)
alerts, err := client.FindAlerts()
logger.DieIf(err)
PrettyPrintJSON(alerts)
return nil
}
示例9: doMonitorsList
func doMonitorsList(c *cli.Context) error {
conffile := c.GlobalString("conf")
monitors, err := newMackerel(conffile).FindMonitors()
logger.DieIf(err)
PrettyPrintJSON(monitors)
return nil
}
示例10: checkMonitorsDiff
func checkMonitorsDiff(c *cli.Context) monitorDiff {
conffile := c.GlobalString("conf")
filePath := c.String("file-path")
var monitorDiff monitorDiff
monitorsRemote, err := newMackerel(conffile).FindMonitors()
logger.DieIf(err)
flagNameUniquenessRemote, err := validateRules(monitorsRemote, "remote rules")
logger.DieIf(err)
monitorsLocal, err := monitorLoadRules(filePath)
logger.DieIf(err)
flagNameUniquenessLocal, err := validateRules(monitorsLocal, "local rules")
logger.DieIf(err)
flagNameUniqueness := flagNameUniquenessLocal && flagNameUniquenessRemote
for _, remote := range monitorsRemote {
found := false
for i, local := range monitorsLocal {
diff, isSame := isSameMonitor(remote, local, flagNameUniqueness)
if isSame || diff != "" {
monitorsLocal[i] = nil
found = true
if diff != "" {
monitorDiff.diff = append(monitorDiff.diff, &monitorDiffPair{remote, local})
}
break
}
}
if found == false {
monitorDiff.onlyRemote = append(monitorDiff.onlyRemote, remote)
}
}
for _, local := range monitorsLocal {
if local != nil {
monitorDiff.onlyLocal = append(monitorDiff.onlyLocal, local)
}
}
return monitorDiff
}
示例11: doHosts
func doHosts(c *cli.Context) error {
conffile := c.GlobalString("conf")
isVerbose := c.Bool("verbose")
hosts, err := newMackerel(conffile).FindHosts(&mkr.FindHostsParam{
Name: c.String("name"),
Service: c.String("service"),
Roles: c.StringSlice("role"),
Statuses: c.StringSlice("status"),
})
logger.DieIf(err)
format := c.String("format")
if format != "" {
t := template.Must(template.New("format").Parse(format))
err := t.Execute(os.Stdout, hosts)
logger.DieIf(err)
} else if isVerbose {
PrettyPrintJSON(hosts)
} else {
var hostsFormat []*HostFormat
for _, host := range hosts {
format := &HostFormat{
ID: host.ID,
Name: host.Name,
DisplayName: host.DisplayName,
Status: host.Status,
RoleFullnames: host.GetRoleFullnames(),
IsRetired: host.IsRetired,
CreatedAt: host.DateStringFromCreatedAt(),
IPAddresses: host.IPAddresses(),
}
hostsFormat = append(hostsFormat, format)
}
PrettyPrintJSON(hostsFormat)
}
return nil
}
示例12: doMonitorsPush
func doMonitorsPush(c *cli.Context) error {
monitorDiff := checkMonitorsDiff(c)
isDryRun := c.Bool("dry-run")
isVerbose := c.Bool("verbose")
conffile := c.GlobalString("conf")
client := newMackerel(conffile)
if isVerbose {
client.Verbose = true
}
for _, m := range monitorDiff.onlyLocal {
logger.Log("info", "Create a new rule.")
fmt.Println(stringifyMonitor(m, ""))
if !isDryRun {
_, err := client.CreateMonitor(m)
logger.DieIf(err)
}
}
for _, m := range monitorDiff.onlyRemote {
logger.Log("info", "Delete a rule.")
fmt.Println(stringifyMonitor(m, ""))
if !isDryRun {
_, err := client.DeleteMonitor(m.ID)
logger.DieIf(err)
}
}
for _, d := range monitorDiff.diff {
logger.Log("info", "Update a rule.")
fmt.Println(stringifyMonitor(d.local, ""))
if !isDryRun {
_, err := client.UpdateMonitor(d.remote.ID, d.local)
logger.DieIf(err)
}
}
return nil
}
示例13: doFetch
func doFetch(c *cli.Context) {
conffile := c.GlobalString("conf")
argHostIDs := c.Args()
optMetricNames := c.StringSlice("name")
if len(argHostIDs) < 1 || len(optMetricNames) < 1 {
cli.ShowCommandHelp(c, "fetch")
os.Exit(1)
}
metricValues, err := newMackerel(conffile).FetchLatestMetricValues(argHostIDs, optMetricNames)
logger.DieIf(err)
PrettyPrintJSON(metricValues)
}
示例14: newMackerel
func newMackerel(conffile string) *mkr.Client {
apiKey := LoadApikeyFromEnvOrConfig(conffile)
if apiKey == "" {
logger.Log("error", `
Not set MACKEREL_APIKEY environment variable. (Try "export MACKEREL_APIKEY='<Your apikey>'")
`)
os.Exit(1)
}
if os.Getenv("DEBUG") != "" {
mackerel, err := mkr.NewClientWithOptions(apiKey, "https://mackerel.io/api/v0", true)
logger.DieIf(err)
return mackerel
}
return mkr.NewClient(apiKey)
}
示例15: doMonitorsPull
func doMonitorsPull(c *cli.Context) error {
conffile := c.GlobalString("conf")
isVerbose := c.Bool("verbose")
filePath := c.String("file-path")
monitors, err := newMackerel(conffile).FindMonitors()
logger.DieIf(err)
monitorSaveRules(monitors, filePath)
if isVerbose {
PrettyPrintJSON(monitors)
}
if filePath == "" {
filePath = "monitors.json"
}
logger.Log("info", fmt.Sprintf("Monitor rules are saved to '%s' (%d rules).", filePath, len(monitors)))
return nil
}