]> git.ipfire.org Git - pakfire.git/commitdiff
daemon: Build out submitting stats
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Sep 2024 04:17:18 +0000 (04:17 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 15 Sep 2024 04:17:18 +0000 (04:17 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/buildservice.c
src/libpakfire/daemon.c
src/libpakfire/include/pakfire/buildservice.h
src/libpakfire/libpakfire.sym

index 314d428d6e9825fd1b15e1fa394c458736da1541..64dfc07b1271cbf70bdd08be0d5fff6dbf119172 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <json.h>
 
-#include <pakfire/arch.h>
 #include <pakfire/buildservice.h>
 #include <pakfire/config.h>
 #include <pakfire/ctx.h>
@@ -35,7 +34,6 @@
 #include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/string.h>
-#include <pakfire/util.h>
 #include <pakfire/xfer.h>
 
 #include <krb5/krb5.h>
@@ -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;
index 0a51f9619d56c78511562fd3f98996a67423358d..0233a4827bca7f7d877eaad2181578d155f7ac5e 100644 (file)
@@ -24,6 +24,7 @@
 #include <systemd/sd-daemon.h>
 #include <systemd/sd-event.h>
 
+#include <pakfire/arch.h>
 #include <pakfire/ctx.h>
 #include <pakfire/daemon.h>
 #include <pakfire/httpclient.h>
@@ -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) {
index 1f1ea40aa4af01448cbde8e46a0ea0d788b95116..3508458a1ccd69fc59b4b6e1502a3f0dc1a08669 100644 (file)
@@ -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,
index 425f20f56747ddabd421bd328ec75b24f0869a67..328e5be47d0635ab6a73f2c6a419dbf1e1aaef8e 100644 (file)
@@ -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