cos_node_profiler: adding main.go file to run app
main.go imports the cloudlogger and profiler pkgs which log and retrieve
GKE cluster information respectively.
Change-Id: Id1a3b11c4eba7193b6ede2e7be1c8ed0b3798979
Reviewed-on: https://cos-review.googlesource.com/c/cos/tools/+/18770
Cloud-Build: GCB Service account <228075978874@cloudbuild.gserviceaccount.com>
Reviewed-by: Dexter Rivera <riverade@google.com>
Tested-by: Dexter Rivera <riverade@google.com>
diff --git a/src/cmd/nodeprofiler/Makefile b/src/cmd/nodeprofiler/Makefile
new file mode 100644
index 0000000..21f3520
--- /dev/null
+++ b/src/cmd/nodeprofiler/Makefile
@@ -0,0 +1,21 @@
+#
+# Makefile to run the COS_Node_Profiler Application that utilizes the profiler
+# package to fetch performance data from a GKE Cluster, the cloudlogger package
+# to export that data to Google Cloud Logging and the utils package that defines
+# the interface between the first two packages.
+#
+
+# Building the binary file
+build:
+ go build
+# Running the profiler application
+run:
+ go build && ./nodeprofiler --project cos-interns-playground
+# Cleaning used executable file
+clean:
+ go clean
+# Running the profiler application and cleaning binaries
+.PHONY: all
+all:
+ go build && ./nodeprofiler --project cos-interns-playground && go clean
+
diff --git a/src/cmd/nodeprofiler/README.md b/src/cmd/nodeprofiler/README.md
new file mode 100644
index 0000000..8792f3c
--- /dev/null
+++ b/src/cmd/nodeprofiler/README.md
@@ -0,0 +1,21 @@
+# COS Node Profiler
+
+cos\_node\_profiler\_agent is a docker container that can streamline the process of
+collecting debugging information for COS Nodes on GKE. It will collect important
+information on the system that can be used to help the oncall diagnose
+performance issues. The agent will provide functionality to upload all collected
+information to Google Cloud Logging to make it easier for the oncall to
+access and examine debugging information.
+
+## Usage
+
+cos\_node\_profiler\_agent is deployed as a docker container. To run the
+program, open `cos_node_profiler` directory, then run `make`. This will
+produce an executable. The executable takes in flags as follows:
+```
+ ./<executable name> -project <name of project to write logs to>
+```
+or
+```
+ ./<executable name> --project <name of project to write logs to>
+```
diff --git a/src/cmd/nodeprofiler/main.go b/src/cmd/nodeprofiler/main.go
new file mode 100644
index 0000000..9a91bab
--- /dev/null
+++ b/src/cmd/nodeprofiler/main.go
@@ -0,0 +1,79 @@
+// Package main is the entry point for the cos_node_profiler application that
+// imports the cloudlogger, profiler, and utils packages that respectively
+// write logs to Google Cloud Logging backend, fetch debugging information from
+// a given Linux system and provide the interface between cloudlogger and
+// profiler packages.
+package main
+
+import (
+ "context"
+ "flag"
+ "fmt"
+ "log"
+ "os"
+ "time"
+
+ "cloud.google.com/go/logging"
+ "cos.googlesource.com/cos/tools.git/src/pkg/nodeprofiler/cloudlogger"
+ "cos.googlesource.com/cos/tools.git/src/pkg/nodeprofiler/utils"
+)
+
+const cloudLoggerName = "cos_node_profiler"
+
+func main() {
+ projID := flag.String("project", "cos-interns-playground", "Specifies the GCP project where logs will be added.")
+ flag.Parse()
+ // [START client setup]
+ ctx := context.Background()
+ client, err := logging.NewClient(ctx, *projID)
+ if err != nil {
+ log.Fatalf("Failed to create logging client: %v", err)
+ }
+ defer client.Close()
+ client.OnError = func(err error) {
+ // Print an error to the local log.
+ // For example, if Flush() failed.
+ log.Printf("client.OnError: %v", err)
+ }
+ // [END client setup]
+
+ // [BEGIN write entry]
+ log.Print("Writing some log entries.")
+ logger := client.Logger(cloudLoggerName)
+ // Ensure the entry is written
+ defer logger.Flush()
+ cloudlogger.LogUSEReport(logger, GenerateUSEReport())
+ // [END write entry]
+}
+
+// func returns mocked out useReport data
+func GenerateUSEReport() *utils.USEReport {
+ // [Begin Making of mocked out instances]
+ useMetrics := []utils.USEMetrics{
+
+ {
+ Timestamp: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
+ Interval: time.Duration(100) * time.Millisecond,
+ Utilization: 4,
+ Saturation: false,
+ Errors: int64(0),
+ },
+ {
+ Timestamp: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
+ Interval: time.Duration(100) * time.Millisecond,
+ Utilization: 7,
+ Saturation: false,
+ Errors: int64(0),
+ },
+ }
+ analysis := "No action required"
+ useReport := &utils.USEReport{Components: useMetrics, Analysis: analysis}
+ // [End Making of mocked out instances]
+ return useReport
+
+}
+
+func usage(msg string) {
+ fmt.Fprintln(os.Stderr, msg)
+ os.Exit(2)
+}