From 37465d6b096fb76c16599b25ad735f4de142836a Mon Sep 17 00:00:00 2001 From: Ronald Date: Sun, 1 Sep 2024 21:37:49 +0100 Subject: [PATCH] Fix for current version of API Added usage, CPU information is now written back to LSM every second. --- libs/sysinfo | 2 +- client.odin => main.odin | 109 ++++++++++++++++++++------------------- types.odin | 43 ++++++++------- 3 files changed, 80 insertions(+), 74 deletions(-) rename client.odin => main.odin (73%) diff --git a/libs/sysinfo b/libs/sysinfo index 1e9fd20..f0a16b1 160000 --- a/libs/sysinfo +++ b/libs/sysinfo @@ -1 +1 @@ -Subproject commit 1e9fd20f5d7b43fad6fedc025b0971e6c1f37507 +Subproject commit f0a16b1cbce55a550685d6300f9ca8f0bee6980c diff --git a/client.odin b/main.odin similarity index 73% rename from client.odin rename to main.odin index 46e53c5..2d0a24d 100644 --- a/client.odin +++ b/main.odin @@ -8,6 +8,7 @@ import "core:mem" import "core:os" import "core:strconv" import "core:strings" +import "core:time" import "libs/http" import "libs/http/client" @@ -17,21 +18,6 @@ CONFIG_FILE_PATH :: "./config.ini" PC: Program_Config = {} -// get_config :: proc(config_file_path: string) -> Maybe(ini.INI) { - // bytes, ok := os.read_entire_file_from_filename(config_file_path) - // if !ok { - // return nil - // } - // defer delete(bytes) - - // config, res := ini.parse(bytes) - // if res.err == ini.ParseErr.EOF { - // return config - // } - - // return nil -// } - api_request_get :: proc(endpoint: string) -> (body: client.Body_Type, allocation: bool, ok: bool) { request: client.Request client.request_init(&request, .Get) @@ -123,18 +109,19 @@ destroy_cpus :: proc(cpus: [dynamic]CPU) { delete(cpus) } -add_cpu_to_lsm :: proc(cpu_name: string, socket: int, host_id: int) -> bool { +add_cpu_to_lsm :: proc(cpu_name: string, socket, cores: int, usage: f64, host_id: int) -> bool { request: client.Request client.request_init(&request, .Post) defer client.request_destroy(&request) - post_body := Base_CPU{cpu_name, socket, host_id} + post_body := Base_CPU{cpu_name, socket, cores, usage, host_id} + log.info("TEST TEST TEST", post_body) if err := client.with_json(&request, post_body); err != nil { return false } - response, err := client.request(&request, fmt.tprintf("%s/add_cpu/", PC.api_address)) - if err != nil do log.fatal("Failed to add host to API with the error: %s", err) + response, err := client.request(&request, fmt.tprintf("%s/add_cpu_to_host/", PC.api_address)) + if err != nil do log.fatal("failed to add host to api with the error: %s", err) defer client.response_destroy(&response) if response.status != .Created { @@ -147,7 +134,7 @@ _main :: proc() { context.logger = log.create_console_logger(.Info) defer log.destroy_console_logger(context.logger) - data, ok := os.read_entire_file_from_filename("config.ini") + data, ok := os.read_entire_file_from_filename(CONFIG_FILE_PATH) if !ok { fmt.eprintln("Failed to open config file") return @@ -157,21 +144,19 @@ _main :: proc() { ini_config, err := ini.load_map_from_string(data_str, allocator=context.allocator) if err != nil { - log.fatal("Failed to read config file") + log.fatal("failed to read config file") return } defer ini.delete_map(ini_config) - fmt.println("ini_config[\"api\"][\"address\"]:", ini_config["api"]["address"]) - if _, ok := ini_config["log"]; ok { if level, ok := ini_config["log"]["level"]; ok && level in Log_Level_Map { log_level := strings.to_upper(level, context.temp_allocator) log_level_ok: bool PC.log_level, log_level_ok = Log_Level_Map[log_level] } else { - log.info("Log level has either been not specified in configuration file or an invalid level has been specified") - log.info("Defaulting to Info") + log.info("log level has either been not specified in configuration file or an invalid level has been specified") + log.info("defaulting to info") PC.log_level = log.Level.Info } } @@ -182,24 +167,24 @@ _main :: proc() { if api_address, ok := ini_config["api"]["address"]; ok { PC.api_address = api_address } else { - log.fatal("API address not specified in config file") + log.fatal("api address not specified in config file") return } } - log.debug("Program config:", PC) + log.debug("program config:", PC) - PC.hostname, ok = sysinfo.get_hostname().? + PC.hostname, ok = sysinfo.get_hostname() if !ok { - log.fatal("Failed to get hostname") + log.fatal("failed to get hostname") return } defer delete(PC.hostname) - log.info("Successfully got hostname") + log.info("successfully got hostname") PC.host_id, ok = get_host_id(PC.hostname) if !ok { - log.fatal("Failed to contact API and determine if host already exists in LSM") + log.fatal("failed to contact the lsm api and determine if host already exists in lsm") return } @@ -218,36 +203,52 @@ _main :: proc() { } response, err := client.request(&request, fmt.tprintf("%s/add_host/", PC.api_address)) - if err != nil do log.fatal("Failed to add host to API with the error: %s", err) + if err != nil do log.fatal("failed to add host to api with the error: %s", err) defer client.response_destroy(&response) if response.status == .Created { - log.info("Successfully added host to LSM") + log.info("successfully added host to lsm") PC.host_id, ok = get_host_id(PC.hostname) - if !ok do log.fatal("Failed to get host ID, exiting") + if !ok do log.fatal("failed to get host id, exiting") } } else { - log.info("Host found in LSM") + log.info("host found in lsm") + } + log.debug("host id", PC.host_id) + + cpu_name, socket, cpu_name_and_socket_ok := sysinfo.get_cpu_name_and_socket_number() + if !cpu_name_and_socket_ok { + log.fatal("failed to get cpu name and socket") + return + } + log.debug("got cpu name and socket") + + cores, cores_ok := sysinfo.get_numb_cpu_cores() + if !cores_ok { + log.fatal("failed to get number of cpu cores") + return + } + log.debug("got number of cpu cores") + + log.debug("starting main loop") + for { + usage, usage_ok := sysinfo.get_cpu_usage_perc() + log.info("USAGE:", usage) + if !usage_ok { + log.fatal("failed to get cpu usage") + return + } else { + log.debug("successfully got cpu usage") + } + + if ok := add_cpu_to_lsm(cpu_name, socket, cores, usage, PC.host_id); !ok { + log.error("failed to add cpu") + } else { + log.info("successfully added cpu to lsm") + } + + time.sleep(1 * time.Second) } - log.debug("Host ID", PC.host_id) - - // cpus, cpu_ok := get_cpus_for_host_from_id(PC.host_id) - // if len(cpus) == 0 { - // delete(cpus) - // log.info("Need to add CPUs to LSM") - - // cpu_name, socket, ok := sysinfo.get_cpu_name_and_socket_number() - // log.info("Attempting to add CPU", cpu_name, "to LSM") - // if ok := add_cpu_to_lsm(cpu_name, socket, PC.host_id); !ok { - // log.fatal("Failed to add CPU, exiting") - // os.exit(1) - // } - // log.info("Successfully added CPU to LSM") - // cpus, cpu_ok := get_cpus_for_host_from_id(PC.host_id) - // } - // defer delete(cpus) - - // log.debug("cpus for host: ", cpus) } main :: proc() { diff --git a/types.odin b/types.odin index 05b9582..5910288 100644 --- a/types.odin +++ b/types.odin @@ -3,42 +3,45 @@ package main import "core:log" Log_Level_Map := map[string]log.Level { - "DEBUG" = log.Level.Debug, - "INFO" = log.Level.Info, - "WARNING" = log.Level.Warning, - "ERROR" = log.Level.Error, - "FATAL" = log.Level.Fatal, + "debug" = log.Level.Debug, + "info" = log.Level.Info, + "warning" = log.Level.Warning, + "error" = log.Level.Error, + "fatal" = log.Level.Fatal, } Program_Config :: struct { - log_level: log.Level, + log_level: log.Level, api_address: string, - host_id: int, - hostname: string, + host_id: int, + hostname: string, } CPU :: struct { - id: int, - name: string, - socket: int, - usage: f16, + // Add time? + id: int, + name: string, + socket: int, + cores: int, + usage: f16, + host_id: int, } Memory :: struct { - type: string, // physical/virtual + type: string, // physical/virtual total: int, - used: int, + used: int, usage: f16, } Host :: struct { hostname: string, - host_id: int, + host_id: int, - cpus: [dynamic]CPU, - memory: [dynamic]Memory, + cpus: [dynamic]CPU, + memory: [dynamic]Memory, } Base_Host :: struct { @@ -46,7 +49,9 @@ Base_Host :: struct { } Base_CPU :: struct { - name: string, - socket: int, + name: string, + socket: int, + cores: int, + usage: f64, host_id: int }