func healthHandler()

in otelcollector/main/main.go [266:379]


func healthHandler(w http.ResponseWriter, r *http.Request) {
	osType := os.Getenv("OS_TYPE")
	status := http.StatusOK
	message := "prometheuscollector is running."
	processToCheck := ""

	tokenConfigFileLocation := "/etc/mdsd.d/config-cache/metricsextension/TokenConfig.json"
	if osType == "windows" {
		tokenConfigFileLocation = "C:\\opt\\genevamonitoringagent\\datadirectory\\mcs\\metricsextension\\TokenConfig.json"
	}

	// Checking if TokenConfig file exists
	if _, err := os.Stat(tokenConfigFileLocation); os.IsNotExist(err) {
		fmt.Println("TokenConfig.json does not exist")
		if _, err := os.Stat("/opt/microsoft/liveness/azmon-container-start-time"); err == nil {
			fmt.Println("azmon-container-start-time file exists, reading start time")
			azmonContainerStartTimeStr, err := os.ReadFile("/opt/microsoft/liveness/azmon-container-start-time")
			if err != nil {
				status = http.StatusServiceUnavailable
				message = "Error reading azmon-container-start-time: " + err.Error()
				fmt.Println(message)
				goto response
			}

			azmonContainerStartTime, err := strconv.Atoi(strings.TrimSpace(string(azmonContainerStartTimeStr)))
			if err != nil {
				status = http.StatusServiceUnavailable
				message = "Error converting azmon-container-start-time to integer: " + err.Error()
				fmt.Println(message)
				goto response
			}

			epochTimeNow := int(time.Now().Unix())
			duration := epochTimeNow - azmonContainerStartTime
			durationInMinutes := duration / 60
			fmt.Printf("Container has been running for %d minutes\n", durationInMinutes)

			if durationInMinutes%5 == 0 {
				fmt.Printf("%s No configuration present for the AKS resource\n", time.Now().Format("2006-01-02T15:04:05"))
			}

			if durationInMinutes > 15 {
				status = http.StatusServiceUnavailable
				message = "No configuration present for the AKS resource"
				fmt.Println(message)
				goto response
			}
		} else {
			fmt.Println("azmon-container-start-time file does not exist")
		}
	} else {
		processToCheck = "/usr/sbin/MetricsExtension"
		if osType == "windows" {
			processToCheck = "MetricsExtension.Native.exe"
		}
		if !shared.IsProcessRunning(processToCheck) {
			status = http.StatusServiceUnavailable
			message = "Metrics Extension is not running (configuration exists)"
			fmt.Println(message)
			goto response
		}

		processToCheck = "/usr/sbin/mdsd"
		if osType == "windows" {
			processToCheck = "MonAgentLauncher.exe"
		}
		if !shared.IsProcessRunning(processToCheck) {
			status = http.StatusServiceUnavailable
			message = "mdsd not running (configuration exists)"
			fmt.Println(message)
			goto response
		}
	}
	if osType == "linux" {
		if shared.HasConfigChanged("/opt/inotifyoutput-mdsd-config.txt") {
			status = http.StatusServiceUnavailable
			message = "inotifyoutput-mdsd-config.txt has been updated - mdsd config changed"
			fmt.Println(message)
			goto response
		}
		if shared.HasConfigChanged("/opt/inotifyoutput.txt") {
			status = http.StatusServiceUnavailable
			message = "inotifyoutput.txt has been updated - config changed"
			fmt.Println(message)
			goto response
		}
	} else {
		if shared.HasConfigChanged("C:\\opt\\microsoft\\scripts\\filesystemwatcher.txt") {
			status = http.StatusServiceUnavailable
			message = "Config Map Updated or DCR/DCE updated since agent started"
			fmt.Println(message)
			goto response
		}
	}

	processToCheck = "/opt/microsoft/otelcollector/otelcollector"
	if osType == "windows" {
		processToCheck = "otelcollector.exe"
	}
	if !shared.IsProcessRunning(processToCheck) {
		status = http.StatusServiceUnavailable
		message = "OpenTelemetryCollector is not running."
		fmt.Println(message)
		goto response
	}

response:
	w.WriteHeader(status)
	fmt.Fprintln(w, message)
	if status != http.StatusOK {
		fmt.Printf("Health check failed: %d, Message: %s\n", status, message)
		shared.WriteTerminationLog(message)
	}
}