From d2e41d6367a6e09a9b2a9559501f31bd3c856820 Mon Sep 17 00:00:00 2001 From: The_Spider Date: Sat, 30 Oct 2021 16:44:05 -0500 Subject: [PATCH] almost finished --- cmd/go-temper/init.go | 83 +++++++++++++++++++++++++++++++++++++++++++ cmd/go-temper/main.go | 30 +++++++++------- 2 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 cmd/go-temper/init.go diff --git a/cmd/go-temper/init.go b/cmd/go-temper/init.go new file mode 100644 index 0000000..a725027 --- /dev/null +++ b/cmd/go-temper/init.go @@ -0,0 +1,83 @@ +package main + +import ( + "flag" + "log" + "os" + "strconv" + "time" + + "github.com/hashicorp/logutils" + "github.com/prometheus/client_golang/prometheus" +) + +func getEnvString(env, def string) (val string) { //nolint:deadcode + val = os.Getenv(env) + + if val == "" { + return def + } + + return +} + +func getEnvInt(env string, def int) (ret int) { + val := os.Getenv(env) + + if val == "" { + return def + } + + ret, err := strconv.Atoi(val) + if err != nil { + log.Fatalf("[ERROR] Environment variable is not numeric: %v\n", env) + } + + return +} + +func initialize() { + config.TimeZone, _ = time.LoadLocation("America/Chicago") + config.TimeZoneUTC, _ = time.LoadLocation("UTC") + + var logLevel int + + flag.IntVar(&logLevel, + "log", + getEnvInt("LOG_LEVEL", 50), + "(LOG_LEVEL)\nlog level") + flag.IntVar(&config.WebSrvPort, + "http-port", + getEnvInt("HTTP_PORT", 8080), + "(HTTP_PORT)\nlisten port for internal webserver") + flag.StringVar(&config.WebSrvIP, + "http-ip", + getEnvString("HTTP_IP", "0.0.0.0"), + "(HTTP_IP)\nlisten ip for internal webserver") + + flag.Parse() + + switch { + case logLevel <= 20: + config.Log.SetMinLevel(logutils.LogLevel("ERROR")) + case logLevel > 20 && logLevel <= 40: + config.Log.SetMinLevel(logutils.LogLevel("WARNING")) + case logLevel > 40 && logLevel <= 60: + config.Log.SetMinLevel(logutils.LogLevel("INFO")) + case logLevel > 60 && logLevel <= 80: + config.Log.SetMinLevel(logutils.LogLevel("DEBUG")) + case logLevel > 80: + config.Log.SetMinLevel(logutils.LogLevel("TRACE")) + } + log.SetOutput(config.Log) + + // print current configuration + log.Printf("[DEBUG] configuration value set: LOG_LEVEL = %v\n", strconv.Itoa(logLevel)) + log.Printf("[DEBUG] configuration value set: HTTP_PORT = %v\n", strconv.Itoa(config.WebSrvPort)) + log.Printf("[DEBUG] configuration value set: HTTP_IP = %v\n", config.WebSrvIP) + + prometheus.MustRegister(config.Prometheus.Temperature) + log.Println("[DEBUG] Prometheus metrics registered") + + log.Println("[INFO] initialization complete") +} diff --git a/cmd/go-temper/main.go b/cmd/go-temper/main.go index 756065c..199d749 100644 --- a/cmd/go-temper/main.go +++ b/cmd/go-temper/main.go @@ -5,7 +5,9 @@ import ( "os" "os/signal" "syscall" + "time" + "gitea.smoothnet.org/nhyatt/go-temper/internal/temper" "github.com/prometheus/client_golang/prometheus" ) @@ -17,28 +19,32 @@ func forever() { log.Println("[INFO] Shutdown signal received...") } -func main() { - prometheus.MustRegister(config.Prometheus.Temperature) - config.Prometheus.Temperature.With(prometheus.Labels{"unit": "celsius"}).Set(100) - config.Prometheus.Temperature.With(prometheus.Labels{"unit": "fahrenheit"}).Set(100) - config.Prometheus.Temperature.With(prometheus.Labels{"unit": "kelvin"}).Set(100) - go httpServer("0.0.0.0", 8080) - forever() - - /*for { +func readTemp() { + for { temp, err := temper.GetTemperature() if err != nil { log.Printf("[WARNING] Unable to get temperature from device: %v\n", err) } else { log.Printf( - "Temperature: %.2f°K %.2f°F %.2f°C\n", + "[DEBUG] Temperature: %.2f°K %.2f°F %.2f°C\n", temp+273.15, 9.0/5.0*temp+32, temp, ) + config.Prometheus.Temperature.With(prometheus.Labels{"unit": "celsius"}).Set(temp) + config.Prometheus.Temperature.With(prometheus.Labels{"unit": "fahrenheit"}).Set(9.0/5.0*temp+32) + config.Prometheus.Temperature.With(prometheus.Labels{"unit": "kelvin"}).Set(temp+273.15) } - time.Sleep(time.Duration(5 * time.Second)) } - */ } + +func main() { + initialize() + + go httpServer(config.WebSrvIP, config.WebSrvPort) + + go readTemp() + + forever() +} \ No newline at end of file