[systemd-devel] [PATCH 2/2] systemd-analyze: read host and system information from remote

Lennart Poettering lennart at poettering.net
Wed Apr 23 14:08:01 PDT 2014


On Mon, 07.04.14 22:38, Djalal Harouni (tixxdz at opendz.org) wrote:

> This makes "systemd-analyze plot" read host information from remote.
> 
> While we are it show if this is a virtualized system.
> 
> https://bugs.freedesktop.org/show_bug.cgi?id=76498

Looks great! Can you rebase this after renaming the props? will merge then!

> 
> Reported-by: Zach <zachcook1991 at gmail.com>
> ---
>  src/analyze/analyze.c | 105 ++++++++++++++++++++++++++++++++++++++------------
>  1 file changed, 81 insertions(+), 24 deletions(-)
> 
> diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
> index 3d2d08f..49b28d4 100644
> --- a/src/analyze/analyze.c
> +++ b/src/analyze/analyze.c
> @@ -100,6 +100,15 @@ struct unit_times {
>          usec_t time;
>  };
>  
> +struct host_info {
> +        char *hostname;
> +        char *os_release;
> +        char *os_version;
> +        char *os_pretty_name;
> +        char *virtualization;
> +        char *architecture;
> +};
> +
>  static void pager_open_if_enabled(void) {
>  
>          if (arg_no_pager)
> @@ -170,21 +179,6 @@ static int compare_unit_start(const void *a, const void *b) {
>                         ((struct unit_times *)b)->activating);
>  }
>  
> -static int get_os_name(char **_n) {
> -        char *n = NULL;
> -        int r;
> -
> -        r = parse_env_file("/etc/os-release", NEWLINE, "PRETTY_NAME", &n, NULL);
> -        if (r < 0)
> -                return r;
> -
> -        if (!n)
> -                return -ENOENT;
> -
> -        *_n = n;
> -        return 0;
> -}
> -
>  static void free_unit_times(struct unit_times *t, unsigned n) {
>          struct unit_times *p;
>  
> @@ -372,6 +366,61 @@ finish:
>          return 0;
>  }
>  
> +static void free_host_info(struct host_info *hi) {
> +        free(hi->hostname);
> +        free(hi->os_release);
> +        free(hi->os_version);
> +        free(hi->os_pretty_name);
> +        free(hi->virtualization);
> +        free(hi->architecture);
> +        free(hi);
> +}
> +
> +static int acquire_host_info(sd_bus *bus, struct host_info **hi) {
> +        int r;
> +        struct host_info *host;
> +
> +        static const struct bus_properties_map hostname_map[] = {
> +                { "Hostname", "s", NULL, offsetof(struct host_info, hostname) },
> +                { "OperatingSystemRelease", "s", NULL, offsetof(struct host_info, os_release) },
> +                { "OperatingSystemVersion", "s", NULL, offsetof(struct host_info, os_version) },
> +                { "OperatingSystemPrettyName", "s", NULL, offsetof(struct host_info, os_pretty_name) },
> +                {}
> +        };
> +
> +        static const struct bus_properties_map manager_map[] = {
> +                { "Virtualization", "s", NULL, offsetof(struct host_info, virtualization) },
> +                { "Architecture",   "s", NULL, offsetof(struct host_info, architecture) },
> +                {}
> +        };
> +
> +        host = new0(struct host_info, 1);
> +        if (!host)
> +                return log_oom();
> +
> +        r = bus_map_all_properties(bus,
> +                                   "org.freedesktop.hostname1",
> +                                   "/org/freedesktop/hostname1",
> +                                   hostname_map,
> +                                   host);
> +        if (r < 0)
> +                goto fail;
> +
> +        r = bus_map_all_properties(bus,
> +                                   "org.freedesktop.systemd1",
> +                                   "/org/freedesktop/systemd1",
> +                                   manager_map,
> +                                   host);
> +        if (r < 0)
> +                goto fail;
> +
> +        *hi = host;
> +        return 0;
> +fail:
> +        free_host_info(host);
> +        return r;
> +}
> +
>  static int pretty_boot_time(sd_bus *bus, char **_buf) {
>          char ts[FORMAT_TIMESPAN_MAX];
>          struct boot_times *t;
> @@ -437,10 +486,10 @@ static void svg_graph_box(double height, double begin, double end) {
>  static int analyze_plot(sd_bus *bus) {
>          struct unit_times *times;
>          struct boot_times *boot;
> -        struct utsname name;
> +        struct host_info *host = NULL;
>          int n, m = 1, y=0;
>          double width;
> -        _cleanup_free_ char *pretty_times = NULL, *osname = NULL;
> +        _cleanup_free_ char *pretty_times = NULL;
>          struct unit_times *u;
>  
>          n = acquire_boot_times(bus, &boot);
> @@ -451,12 +500,13 @@ static int analyze_plot(sd_bus *bus) {
>          if (n < 0)
>                  return n;
>  
> -        get_os_name(&osname);
> -        assert_se(uname(&name) >= 0);
> +        n = acquire_host_info(bus, &host);
> +        if (n < 0)
> +                return n;
>  
>          n = acquire_time_data(bus, &times);
>          if (n <= 0)
> -                return n;
> +                goto out;
>  
>          qsort(times, n, sizeof(struct unit_times), compare_unit_start);
>  
> @@ -551,9 +601,13 @@ static int analyze_plot(sd_bus *bus) {
>  
>          svg("<rect class=\"background\" width=\"100%%\" height=\"100%%\" />\n");
>          svg("<text x=\"20\" y=\"50\">%s</text>", pretty_times);
> -        svg("<text x=\"20\" y=\"30\">%s %s (%s %s) %s</text>",
> -            isempty(osname) ? "Linux" : osname,
> -            name.nodename, name.release, name.version, name.machine);
> +        svg("<text x=\"20\" y=\"30\">%s %s (%s %s) %s %s</text>",
> +            isempty(host->os_pretty_name) ? "Linux" : host->os_pretty_name,
> +            isempty(host->hostname) ? "" : host->hostname,
> +            isempty(host->os_release) ? "" : host->os_release,
> +            isempty(host->os_version) ? "" : host->os_version,
> +            isempty(host->architecture) ? "" : host->architecture,
> +            isempty(host->virtualization) ? "" : host->virtualization);
>  
>          svg("<g transform=\"translate(%.3f,100)\">\n", 20.0 + (SCALE_X * boot->firmware_time));
>          svg_graph_box(m, -(double) boot->firmware_time, boot->finish_time);
> @@ -636,7 +690,10 @@ static int analyze_plot(sd_bus *bus) {
>  
>          free_unit_times(times, (unsigned) n);
>  
> -        return 0;
> +        n = 0;
> +out:
> +        free_host_info(host);
> +        return n;
>  }
>  
>  static int list_dependencies_print(const char *name, unsigned int level, unsigned int branches,


Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list