Ejemplo n.º 1
0
func main() {
	// First log our starting config, and then set up.
	flag.Set("logtostderr", "true") // This spoofs glog into teeing logs to stderr.
	defer log.Flush()
	pflag.Parse()
	log.Infof("Invoked by %v", os.Args)

	resolution := time.Second * time.Duration(*res)

	// Initialize the configuration.
	kubeletCfg, ctrlCfg, err := config.NewConfigs(*zone, *project, *cluster, *kubeletHost, *kubeletPort, *ctrlPort, resolution)
	if err != nil {
		log.Fatalf("Failed to initialize configuration: %v", err)
	}

	// Create objects for kubelet monitoring.
	kubeletSrc, err := kubelet.NewSource(kubeletCfg)
	if err != nil {
		log.Fatalf("Failed to create a kubelet source with config %v: %v", kubeletCfg, err)
	}
	log.Infof("The kubelet source is initialized with config %v.", kubeletCfg)

	// Create objects for kube-controller monitoring.
	ctrlSrc, err := controller.NewSource(ctrlCfg)
	if err != nil {
		log.Fatalf("Failed to create a kube-controller source with config %v: %v", ctrlCfg, err)
	}
	log.Infof("The kube-controller source is initialized with config %v.", ctrlCfg)

	// Create a GCM client.
	client, err := google.DefaultClient(context.Background(), scope)
	if err != nil {
		log.Fatalf("Failed to create a client with default context and scope %s, err: %v", scope, err)
	}
	service, err := v3.New(client)
	if err != nil {
		log.Fatalf("Failed to create a GCM v3 API service object: %v", err)
	}
	// Determine the GCE endpoint.
	if *gcmEndpoint != "" {
		service.BasePath = *gcmEndpoint
	}
	log.Infof("Using GCM endpoint %q", service.BasePath)

	for {
		go monitor.Once(kubeletSrc, service)
		go monitor.Once(ctrlSrc, service)
		time.Sleep(resolution)
	}
}
Ejemplo n.º 2
0
func main() {
	// First log our starting config, and then set up.
	flag.Set("logtostderr", "true") // This spoofs glog into teeing logs to stderr.
	defer log.Flush()
	log.Infof("Invoked by %v", os.Args)
	pflag.Parse()

	// Determine what zone and project we're monitoring.
	zone, err := getZone(*zoneArg)
	if err != nil {
		log.Fatalf("Failed to get zone: %v", err)
	}
	project, err := getProjectID(*projectArg)
	if err != nil {
		log.Fatalf("Failed to get project: %v", err)
	}
	cluster, err := getCluster(*clusterArg)
	if err != nil {
		log.Fatalf("Failed to get cluster: %v", err)
	}
	kubeletHost, err := getKubeletHost(*kubeletHostArg)
	if err != nil {
		log.Fatalf("Failed to get kubelet host: %v", err)
	}
	log.Infof("Monitoring kubelet %s in cluster {%s, %s, %s}", kubeletHost, zone, project, cluster)
	resolution := time.Second * time.Duration(*resolutionArg)

	translator := monitor.NewTranslator(zone, project, cluster, kubeletHost, resolution)
	log.Info("New Translator successfully created.")

	// NewKubeletClient validates its own inputs.
	kubelet, err := monitor.NewKubeletClient(kubeletHost, *port, nil)
	if err != nil {
		log.Fatalf("Failed to create a Kubelet client with host %s and port %d: %v", kubeletHost, *port, err)
	}
	log.Info("Successfully created kubelet client.")

	// Create a GCM client.
	name := fmt.Sprintf("projects/%s", project)
	client, err := google.DefaultClient(context.Background(), scope)
	if err != nil {
		log.Fatalf("Failed to create a client with default context and scope %s, err: %v", scope, err)
	}
	service, err := v3.New(client)
	if err != nil {
		log.Fatalf("Failed to create a GCM v3 API service object: %v", err)
	}
	// Determine the GCE endpoint.
	if *gcmEndpoint != "" {
		service.BasePath = *gcmEndpoint
	}
	log.Infof("Using GCM endpoint %q", service.BasePath)

	for {
		go func() {
			// Get the latest summary.
			summary, err := kubelet.GetSummary()
			if err != nil {
				log.Errorf("Failed to get summary from kubelet: %v", err)
				return
			}

			// Translate kubelet's data to GCM v3's format.
			tsReq, err := translator.Translate(summary)
			if err != nil {
				log.Errorf("Failed to translate data from summary %v: %v", summary, err)
				return
			}

			// Push that data to GCM's v3 API.
			createCall := service.Projects.TimeSeries.Create(name, tsReq)
			if empty, err := createCall.Do(); err != nil {
				log.Errorf("Failed to write time series data, empty: %v, err: %v", empty, err)

				jsonReq, err := tsReq.MarshalJSON()
				if err != nil {
					log.Errorf("Failed to marshal time series as JSON")
					return
				}
				log.Errorf("JSON GCM: %s", string(jsonReq[:]))
				return
			}
		}()
		time.Sleep(resolution)
	}
}