Fix for current version of API

Added usage, CPU information is now written back to LSM every second.
master
Ronald 1 year ago
parent aadf045f41
commit 37465d6b09

@ -1 +1 @@
Subproject commit 1e9fd20f5d7b43fad6fedc025b0971e6c1f37507 Subproject commit f0a16b1cbce55a550685d6300f9ca8f0bee6980c

@ -8,6 +8,7 @@ import "core:mem"
import "core:os" import "core:os"
import "core:strconv" import "core:strconv"
import "core:strings" import "core:strings"
import "core:time"
import "libs/http" import "libs/http"
import "libs/http/client" import "libs/http/client"
@ -17,21 +18,6 @@ CONFIG_FILE_PATH :: "./config.ini"
PC: Program_Config = {} 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) { api_request_get :: proc(endpoint: string) -> (body: client.Body_Type, allocation: bool, ok: bool) {
request: client.Request request: client.Request
client.request_init(&request, .Get) client.request_init(&request, .Get)
@ -123,18 +109,19 @@ destroy_cpus :: proc(cpus: [dynamic]CPU) {
delete(cpus) 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 request: client.Request
client.request_init(&request, .Post) client.request_init(&request, .Post)
defer client.request_destroy(&request) 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 { if err := client.with_json(&request, post_body); err != nil {
return false return false
} }
response, err := client.request(&request, fmt.tprintf("%s/add_cpu/", PC.api_address)) 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) if err != nil do log.fatal("failed to add host to api with the error: %s", err)
defer client.response_destroy(&response) defer client.response_destroy(&response)
if response.status != .Created { if response.status != .Created {
@ -147,7 +134,7 @@ _main :: proc() {
context.logger = log.create_console_logger(.Info) context.logger = log.create_console_logger(.Info)
defer log.destroy_console_logger(context.logger) 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 { if !ok {
fmt.eprintln("Failed to open config file") fmt.eprintln("Failed to open config file")
return return
@ -157,21 +144,19 @@ _main :: proc() {
ini_config, err := ini.load_map_from_string(data_str, allocator=context.allocator) ini_config, err := ini.load_map_from_string(data_str, allocator=context.allocator)
if err != nil { if err != nil {
log.fatal("Failed to read config file") log.fatal("failed to read config file")
return return
} }
defer ini.delete_map(ini_config) defer ini.delete_map(ini_config)
fmt.println("ini_config[\"api\"][\"address\"]:", ini_config["api"]["address"])
if _, ok := ini_config["log"]; ok { if _, ok := ini_config["log"]; ok {
if level, ok := ini_config["log"]["level"]; ok && level in Log_Level_Map { if level, ok := ini_config["log"]["level"]; ok && level in Log_Level_Map {
log_level := strings.to_upper(level, context.temp_allocator) log_level := strings.to_upper(level, context.temp_allocator)
log_level_ok: bool log_level_ok: bool
PC.log_level, log_level_ok = Log_Level_Map[log_level] PC.log_level, log_level_ok = Log_Level_Map[log_level]
} else { } else {
log.info("Log level has either been not specified in configuration file or an invalid level has been specified") 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("defaulting to info")
PC.log_level = log.Level.Info PC.log_level = log.Level.Info
} }
} }
@ -182,24 +167,24 @@ _main :: proc() {
if api_address, ok := ini_config["api"]["address"]; ok { if api_address, ok := ini_config["api"]["address"]; ok {
PC.api_address = api_address PC.api_address = api_address
} else { } else {
log.fatal("API address not specified in config file") log.fatal("api address not specified in config file")
return 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 { if !ok {
log.fatal("Failed to get hostname") log.fatal("failed to get hostname")
return return
} }
defer delete(PC.hostname) defer delete(PC.hostname)
log.info("Successfully got hostname") log.info("successfully got hostname")
PC.host_id, ok = get_host_id(PC.hostname) PC.host_id, ok = get_host_id(PC.hostname)
if !ok { 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 return
} }
@ -218,36 +203,52 @@ _main :: proc() {
} }
response, err := client.request(&request, fmt.tprintf("%s/add_host/", PC.api_address)) 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) defer client.response_destroy(&response)
if response.status == .Created { 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) 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 { } else {
log.info("Host found in LSM") log.info("host found in lsm")
} }
log.debug("Host ID", PC.host_id) log.debug("host id", PC.host_id)
// cpus, cpu_ok := get_cpus_for_host_from_id(PC.host_id) cpu_name, socket, cpu_name_and_socket_ok := sysinfo.get_cpu_name_and_socket_number()
// if len(cpus) == 0 { if !cpu_name_and_socket_ok {
// delete(cpus) log.fatal("failed to get cpu name and socket")
// log.info("Need to add CPUs to LSM") return
}
log.debug("got cpu name and socket")
// cpu_name, socket, ok := sysinfo.get_cpu_name_and_socket_number() cores, cores_ok := sysinfo.get_numb_cpu_cores()
// log.info("Attempting to add CPU", cpu_name, "to LSM") if !cores_ok {
// if ok := add_cpu_to_lsm(cpu_name, socket, PC.host_id); !ok { log.fatal("failed to get number of cpu cores")
// log.fatal("Failed to add CPU, exiting") return
// os.exit(1) }
// } log.debug("got number of cpu cores")
// log.info("Successfully added CPU to LSM")
// cpus, cpu_ok := get_cpus_for_host_from_id(PC.host_id) log.debug("starting main loop")
// } for {
// defer delete(cpus) 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")
}
// log.debug("cpus for host: ", cpus) 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)
}
} }
main :: proc() { main :: proc() {

@ -3,11 +3,11 @@ package main
import "core:log" import "core:log"
Log_Level_Map := map[string]log.Level { Log_Level_Map := map[string]log.Level {
"DEBUG" = log.Level.Debug, "debug" = log.Level.Debug,
"INFO" = log.Level.Info, "info" = log.Level.Info,
"WARNING" = log.Level.Warning, "warning" = log.Level.Warning,
"ERROR" = log.Level.Error, "error" = log.Level.Error,
"FATAL" = log.Level.Fatal, "fatal" = log.Level.Fatal,
} }
Program_Config :: struct { Program_Config :: struct {
@ -20,10 +20,13 @@ Program_Config :: struct {
} }
CPU :: struct { CPU :: struct {
// Add time?
id: int, id: int,
name: string, name: string,
socket: int, socket: int,
cores: int,
usage: f16, usage: f16,
host_id: int,
} }
Memory :: struct { Memory :: struct {
@ -48,5 +51,7 @@ Base_Host :: struct {
Base_CPU :: struct { Base_CPU :: struct {
name: string, name: string,
socket: int, socket: int,
cores: int,
usage: f64,
host_id: int host_id: int
} }

Loading…
Cancel
Save