From: Michael Tremer Date: Sun, 15 Sep 2024 04:17:18 +0000 (+0000) Subject: daemon: Build out submitting stats X-Git-Tag: 0.9.30~1186 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=56a8fcdeeab8847c05c1183738088132a71e1adc;p=pakfire.git daemon: Build out submitting stats Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/buildservice.c b/src/libpakfire/buildservice.c index 314d428d6..64dfc07b1 100644 --- a/src/libpakfire/buildservice.c +++ b/src/libpakfire/buildservice.c @@ -24,7 +24,6 @@ #include -#include #include #include #include @@ -35,7 +34,6 @@ #include #include #include -#include #include #include @@ -717,227 +715,6 @@ ERROR: return r; } -PAKFIRE_EXPORT int pakfire_buildservice_submit_stats(struct pakfire_buildservice* service) { - struct pakfire_cpuinfo cpuinfo = {}; - struct pakfire_cpustat cpustat = {}; - struct pakfire_loadavg loadavg = {}; - struct pakfire_meminfo meminfo = {}; - const char* arch = NULL; - int r; - - struct pakfire_xfer* xfer = NULL; - - // Fetch the distro - const struct pakfire_distro* distro = pakfire_ctx_get_distro(service->ctx); - - // Fetch CPU info - r = pakfire_cpuinfo(&cpuinfo); - if (r) { - CTX_ERROR(service->ctx, "Failed to fetch CPU info: %s\n", strerror(-r)); - goto ERROR; - } - - // Fetch CPU stats - r = pakfire_cpustat(&cpustat); - if (r) { - CTX_ERROR(service->ctx, "Failed to fetch CPU stats: %s\n", strerror(-r)); - goto ERROR; - } - - // Fetch load average - r = pakfire_loadavg(&loadavg); - if (r) { - CTX_ERROR(service->ctx, "Failed to fetch load average: %s\n", strerror(-r)); - goto ERROR; - } - - // Fetch meminfo - r = pakfire_meminfo(&meminfo); - if (r) { - CTX_ERROR(service->ctx, "Failed to fetch meminfo: %s\n", strerror(-r)); - goto ERROR; - } - - // Fetch native arch - arch = pakfire_arch_native(); - if (!arch) { - CTX_ERROR(service->ctx, "Failed to fetch native arch: %s\n", strerror(errno)); - r = -errno; - goto ERROR; - } - - // Create a new xfer - r = pakfire_httpclient_create_xfer(&xfer, service->httpclient, "/api/v1/builders/stats"); - if (r) - goto ERROR; - - // Enable authentication - r = pakfire_xfer_auth(xfer); - if (r) - goto ERROR; - - // CPU Model - r = pakfire_xfer_add_param(xfer, "cpu_model", "%s", cpuinfo.model); - if (r) - goto ERROR; - - // CPU Count - r = pakfire_xfer_add_param(xfer, "cpu_count", "%u", cpuinfo.count); - if (r) - goto ERROR; - - // CPU Arch - r = pakfire_xfer_add_param(xfer, "cpu_arch", "%s", arch); - if (r) - goto ERROR; - - // Pakfire Version - r = pakfire_xfer_add_param(xfer, "pakfire_version", "%s", PACKAGE_VERSION); - if (r) - goto ERROR; - - // OS - r = pakfire_xfer_add_param(xfer, "os_name", "%s", distro->pretty_name); - if (r) - goto ERROR; - - // CPU Stats: User - r = pakfire_xfer_add_param(xfer, "cpu_user", "%.6f", cpustat.user); - if (r) - goto ERROR; - - // CPU Stats: Nice - r = pakfire_xfer_add_param(xfer, "cpu_nice", "%.6f", cpustat.nice); - if (r) - goto ERROR; - - // CPU Stats: System - r = pakfire_xfer_add_param(xfer, "cpu_system", "%.6f", cpustat.system); - if (r) - goto ERROR; - - // CPU Stats: Idle - r = pakfire_xfer_add_param(xfer, "cpu_idle", "%.6f", cpustat.idle); - if (r) - goto ERROR; - - // CPU Stats: IO Wait - r = pakfire_xfer_add_param(xfer, "cpu_iowait", "%.6f", cpustat.iowait); - if (r) - goto ERROR; - - // CPU Stats: IRQ - r = pakfire_xfer_add_param(xfer, "cpu_irq", "%.6f", cpustat.irq); - if (r) - goto ERROR; - - // CPU Stats: Soft IRQ - r = pakfire_xfer_add_param(xfer, "cpu_softirq", "%.6f", cpustat.softirq); - if (r) - goto ERROR; - - // CPU Stats: Steal - r = pakfire_xfer_add_param(xfer, "cpu_steal", "%.6f", cpustat.steal); - if (r) - goto ERROR; - - // CPU Stats: Guest - r = pakfire_xfer_add_param(xfer, "cpu_guest", "%.6f", cpustat.guest); - if (r) - goto ERROR; - - // CPU Stats: Guest Nice - r = pakfire_xfer_add_param(xfer, "cpu_guest_nice", "%.6f", cpustat.guest_nice); - if (r) - goto ERROR; - - // Load Average: 1 - r = pakfire_xfer_add_param(xfer, "loadavg1", "%.2f", loadavg.load1); - if (r) - goto ERROR; - - // Load Average: 5 - r = pakfire_xfer_add_param(xfer, "loadavg5", "%.2f", loadavg.load5); - if (r) - goto ERROR; - - // Load Average: 15 - r = pakfire_xfer_add_param(xfer, "loadavg15", "%.2f", loadavg.load15); - if (r) - goto ERROR; - - // Memory: Total - r = pakfire_xfer_add_param(xfer, "mem_total", "%lu", meminfo.total); - if (r) - goto ERROR; - - // Memory: Available - r = pakfire_xfer_add_param(xfer, "mem_available", "%lu", meminfo.available); - if (r) - goto ERROR; - - // Memory: Used - r = pakfire_xfer_add_param(xfer, "mem_used", "%lu", meminfo.used); - if (r) - goto ERROR; - - // Memory: Free - r = pakfire_xfer_add_param(xfer, "mem_free", "%lu", meminfo.free); - if (r) - goto ERROR; - - // Memory: Active - r = pakfire_xfer_add_param(xfer, "mem_active", "%lu", meminfo.active); - if (r) - goto ERROR; - - // Memory: Inactive - r = pakfire_xfer_add_param(xfer, "mem_inactive", "%lu", meminfo.inactive); - if (r) - goto ERROR; - - // Memory: Buffers - r = pakfire_xfer_add_param(xfer, "mem_buffers", "%lu", meminfo.buffers); - if (r) - goto ERROR; - - // Memory: Cached - r = pakfire_xfer_add_param(xfer, "mem_cached", "%lu", meminfo.cached); - if (r) - goto ERROR; - - // Memory: Shared - r = pakfire_xfer_add_param(xfer, "mem_shared", "%lu", meminfo.shared); - if (r) - goto ERROR; - - // Swap: Total - r = pakfire_xfer_add_param(xfer, "swap_total", "%lu", meminfo.swap_total); - if (r) - goto ERROR; - - // Swap: Used - r = pakfire_xfer_add_param(xfer, "swap_used", "%lu", meminfo.swap_used); - if (r) - goto ERROR; - - // Swap: Free - r = pakfire_xfer_add_param(xfer, "swap_free", "%lu", meminfo.swap_free); - if (r) - goto ERROR; - - // Send the request - r = pakfire_xfer_run_api_request(xfer, NULL); - if (r) - goto ERROR; - -ERROR: - if (xfer) - pakfire_xfer_unref(xfer); - - return r; -} - PAKFIRE_EXPORT int pakfire_buildservice_job_finished(struct pakfire_buildservice* service, const char* uuid, int success, const char* logfile, const char** packages) { struct pakfire_xfer* xfer = NULL; diff --git a/src/libpakfire/daemon.c b/src/libpakfire/daemon.c index 0a51f9619..0233a4827 100644 --- a/src/libpakfire/daemon.c +++ b/src/libpakfire/daemon.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -234,6 +235,13 @@ ERROR: static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* data) { struct pakfire_daemon* daemon = data; + struct pakfire_cpuinfo cpuinfo = {}; + struct pakfire_cpustat cpustat = {}; + struct pakfire_loadavg loadavg = {}; + struct pakfire_meminfo meminfo = {}; + struct json_object* stats = NULL; + const char* arch = NULL; + size_t length = 0; int r; // If we have any workers running, we will submit our stats @@ -253,14 +261,212 @@ static int pakfire_daemon_submit_stats(sd_event_source* s, uint64_t usec, void* return 0; } - // XXX TODO + // Fetch the distro + const struct pakfire_distro* distro = pakfire_ctx_get_distro(daemon->ctx); - // Send the message - r = pakfire_xfer_send_message(daemon->control, "{}", 2); + // Fetch CPU info + r = pakfire_cpuinfo(&cpuinfo); + if (r) { + CTX_ERROR(daemon->ctx, "Failed to fetch CPU info: %s\n", strerror(-r)); + goto ERROR; + } + + // Fetch CPU stats + r = pakfire_cpustat(&cpustat); + if (r) { + CTX_ERROR(daemon->ctx, "Failed to fetch CPU stats: %s\n", strerror(-r)); + goto ERROR; + } + + // Fetch load average + r = pakfire_loadavg(&loadavg); + if (r) { + CTX_ERROR(daemon->ctx, "Failed to fetch load average: %s\n", strerror(-r)); + goto ERROR; + } + + // Fetch meminfo + r = pakfire_meminfo(&meminfo); + if (r) { + CTX_ERROR(daemon->ctx, "Failed to fetch meminfo: %s\n", strerror(-r)); + goto ERROR; + } + + // Fetch native arch + arch = pakfire_arch_native(); + if (!arch) { + CTX_ERROR(daemon->ctx, "Failed to fetch native arch: %s\n", strerror(errno)); + r = -errno; + goto ERROR; + } + + // Create new stats object + stats = json_object_new_object(); + if (!stats) + goto ERROR; + + // CPU Model + r = pakfire_json_add_string(stats, "cpu_model", cpuinfo.model); if (r) - return r; + goto ERROR; - return 0; + // CPU Count + r = pakfire_json_add_uint64(stats, "cpu_count", cpuinfo.count); + if (r) + goto ERROR; + + // CPU Arch + r = pakfire_json_add_string(stats, "cpu_arch", arch); + if (r) + goto ERROR; + + // Pakfire Version + r = pakfire_json_add_string(stats, "pakfire_version", PACKAGE_VERSION); + if (r) + goto ERROR; + + // OS + r = pakfire_json_add_string(stats, "os_name", distro->pretty_name); + if (r) + goto ERROR; + + // CPU Stats: User + r = pakfire_json_add_double(stats, "cpu_user", cpustat.user); + if (r) + goto ERROR; + + // CPU Stats: Nice + r = pakfire_json_add_double(stats, "cpu_nice", cpustat.nice); + if (r) + goto ERROR; + + // CPU Stats: System + r = pakfire_json_add_double(stats, "cpu_system", cpustat.system); + if (r) + goto ERROR; + + // CPU Stats: Idle + r = pakfire_json_add_double(stats, "cpu_idle", cpustat.idle); + if (r) + goto ERROR; + + // CPU Stats: IO Wait + r = pakfire_json_add_double(stats, "cpu_iowait", cpustat.iowait); + if (r) + goto ERROR; + + // CPU Stats: IRQ + r = pakfire_json_add_double(stats, "cpu_irq", cpustat.irq); + if (r) + goto ERROR; + + // CPU Stats: Soft IRQ + r = pakfire_json_add_double(stats, "cpu_softirq", cpustat.softirq); + if (r) + goto ERROR; + + // CPU Stats: Steal + r = pakfire_json_add_double(stats, "cpu_steal", cpustat.steal); + if (r) + goto ERROR; + + // CPU Stats: Guest + r = pakfire_json_add_double(stats, "cpu_guest", cpustat.guest); + if (r) + goto ERROR; + + // CPU Stats: Guest Nice + r = pakfire_json_add_double(stats, "cpu_guest_nice", cpustat.guest_nice); + if (r) + goto ERROR; + + // Load Average: 1 + r = pakfire_json_add_double(stats, "loadavg1", loadavg.load1); + if (r) + goto ERROR; + + // Load Average: 5 + r = pakfire_json_add_double(stats, "loadavg5", loadavg.load5); + if (r) + goto ERROR; + + // Load Average: 15 + r = pakfire_json_add_double(stats, "loadavg15", loadavg.load15); + if (r) + goto ERROR; + + // Memory: Total + r = pakfire_json_add_uint64(stats, "mem_total", meminfo.total); + if (r) + goto ERROR; + + // Memory: Available + r = pakfire_json_add_uint64(stats, "mem_available", meminfo.available); + if (r) + goto ERROR; + + // Memory: Used + r = pakfire_json_add_int64(stats, "mem_used", meminfo.used); + if (r) + goto ERROR; + + // Memory: Free + r = pakfire_json_add_uint64(stats, "mem_free", meminfo.free); + if (r) + goto ERROR; + + // Memory: Active + r = pakfire_json_add_uint64(stats, "mem_active", meminfo.active); + if (r) + goto ERROR; + + // Memory: Inactive + r = pakfire_json_add_uint64(stats, "mem_inactive", meminfo.inactive); + if (r) + goto ERROR; + + // Memory: Buffers + r = pakfire_json_add_uint64(stats, "mem_buffers", meminfo.buffers); + if (r) + goto ERROR; + + // Memory: Cached + r = pakfire_json_add_uint64(stats, "mem_cached", meminfo.cached); + if (r) + goto ERROR; + + // Memory: Shared + r = pakfire_json_add_uint64(stats, "mem_shared", meminfo.shared); + if (r) + goto ERROR; + + // Swap: Total + r = pakfire_json_add_uint64(stats, "swap_total", meminfo.swap_total); + if (r) + goto ERROR; + + // Swap: Used + r = pakfire_json_add_uint64(stats, "swap_used", meminfo.swap_used); + if (r) + goto ERROR; + + // Swap: Free + r = pakfire_json_add_uint64(stats, "swap_free", meminfo.swap_free); + if (r) + goto ERROR; + + // Serialize to string + const char* buffer = json_object_to_json_string_length(stats, + JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY, &length); + + // Send the message + r = pakfire_xfer_send_message(daemon->control, buffer, length); + +ERROR: + if (stats) + json_object_put(stats); + + return r; } static int pakfire_daemon_setup_loop(struct pakfire_daemon* daemon) { diff --git a/src/libpakfire/include/pakfire/buildservice.h b/src/libpakfire/include/pakfire/buildservice.h index 1f1ea40aa..3508458a1 100644 --- a/src/libpakfire/include/pakfire/buildservice.h +++ b/src/libpakfire/include/pakfire/buildservice.h @@ -64,10 +64,6 @@ int pakfire_buildservice_create_repo(struct pakfire_buildservice* service, int pakfire_buildservice_delete_repo(struct pakfire_buildservice* service, const char* distro, const char* name); -// Stats - -int pakfire_buildservice_submit_stats(struct pakfire_buildservice* service); - // Jobs int pakfire_buildservice_job_finished(struct pakfire_buildservice* service, diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 425f20f56..328e5be47 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -97,7 +97,6 @@ global: pakfire_buildservice_create_repo; pakfire_buildservice_delete_repo; pakfire_buildservice_get_url; - pakfire_buildservice_submit_stats; pakfire_buildservice_job_finished; # dependencies