Exemple #1
0
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
}
func (s *AWSSession) updateAWSElementList(elbs []*AWSElement, client *mkr.Client) {
	for _, elb := range elbs {
		hosts, err := client.FindHosts(&mkr.FindHostsParam{Name: elb.DNSName})
		if err != nil {
			logger.Log("error", fmt.Sprintf("Mackerel FindHosts: %s", err.Error()))
			continue
		}

		if len(hosts) == 1 {
			elb.HostID = hosts[0].ID
			logger.Log("info", fmt.Sprintf("Host Found: %s -> %s", hosts[0].ID, hosts[0].Name))
		}
		if len(hosts) == 0 {
			elb.HostID, err = client.CreateHost(&mkr.CreateHostParam{
				Name: elb.DNSName,
			})
			if err != nil {
				logger.Log("error", fmt.Sprintf("Mackerel CreateHost: %s", err.Error()))
			}
		}
	}
	return
}
func (s *AWSSession) crawlRDSMetrics(client *mkr.Client, rdss []*AWSElement) {
	for _, rds := range rdss {
		dimensions := []*cloudwatch.Dimension{
			{
				Name:  aws.String("DBInstanceIdentifier"),
				Value: aws.String(rds.Name),
			}}
		metricValues := s.getMetricStatistics(rds, rdsGraphdefs, "AWS/RDS", dimensions)
		logger.Log("info", fmt.Sprintf("%s", metricValues))
		err := client.PostHostMetricValuesByHostID(rds.HostID, metricValues)
		//logger.DieIf(err)
		if err != nil {
			logger.Log("error", err.Error())
		}

		for _, metric := range metricValues {
			logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", rds.HostID, metric.Name, metric.Value, metric.Time))
		}
	}
}
func (s *AWSSession) crawlELBMetrics(client *mkr.Client, elbs []*AWSElement) {
	for _, elb := range elbs {
		dimensions := []*cloudwatch.Dimension{
			{
				Name:  aws.String("LoadBalancerName"),
				Value: aws.String(elb.Name),
			}}
		metricValues := s.getMetricStatistics(elb, elbGraphdefs, "AWS/ELB", dimensions)
		logger.Log("info", fmt.Sprintf("%s", metricValues))
		err := client.PostHostMetricValuesByHostID(elb.HostID, metricValues)
		//logger.DieIf(err)
		if err != nil {
			logger.Log("error", err.Error())
		}

		for _, metric := range metricValues {
			logger.Log("thrown", fmt.Sprintf("%s '%s\t%f\t%d'", elb.HostID, metric.Name, metric.Value, metric.Time))
		}
	}
}