commit 7037a051abc2fc8638ae69dd02d01ea72c5ab5da Author: Zoe <62722391+juls0730@users.noreply.github.com> Date: Thu Sep 25 23:43:12 2025 -0500 initial commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5bf2f83 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +IndentWidth: 4 \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6061543 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +ulandscape-sysinfo \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0e259d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,121 @@ +Creative Commons Legal Code + +CC0 1.0 Universal + + CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE + LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN + ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS + INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES + REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS + PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM + THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED + HEREUNDER. + +Statement of Purpose + +The laws of most jurisdictions throughout the world automatically confer +exclusive Copyright and Related Rights (defined below) upon the creator +and subsequent owner(s) (each and all, an "owner") of an original work of +authorship and/or a database (each, a "Work"). + +Certain owners wish to permanently relinquish those rights to a Work for +the purpose of contributing to a commons of creative, cultural and +scientific works ("Commons") that the public can reliably and without fear +of later claims of infringement build upon, modify, incorporate in other +works, reuse and redistribute as freely as possible in any form whatsoever +and for any purposes, including without limitation commercial purposes. +These owners may contribute to the Commons to promote the ideal of a free +culture and the further production of creative, cultural and scientific +works, or to gain reputation or greater distribution for their Work in +part through the use and efforts of others. + +For these and/or other purposes and motivations, and without any +expectation of additional consideration or compensation, the person +associating CC0 with a Work (the "Affirmer"), to the extent that he or she +is an owner of Copyright and Related Rights in the Work, voluntarily +elects to apply CC0 to the Work and publicly distribute the Work under its +terms, with knowledge of his or her Copyright and Related Rights in the +Work and the meaning and intended legal effect of CC0 on those rights. + +1. Copyright and Related Rights. A Work made available under CC0 may be +protected by copyright and related or neighboring rights ("Copyright and +Related Rights"). Copyright and Related Rights include, but are not +limited to, the following: + + i. the right to reproduce, adapt, distribute, perform, display, + communicate, and translate a Work; + ii. moral rights retained by the original author(s) and/or performer(s); +iii. publicity and privacy rights pertaining to a person's image or + likeness depicted in a Work; + iv. rights protecting against unfair competition in regards to a Work, + subject to the limitations in paragraph 4(a), below; + v. rights protecting the extraction, dissemination, use and reuse of data + in a Work; + vi. database rights (such as those arising under Directive 96/9/EC of the + European Parliament and of the Council of 11 March 1996 on the legal + protection of databases, and under any national implementation + thereof, including any amended or successor version of such + directive); and +vii. other similar, equivalent or corresponding rights throughout the + world based on applicable law or treaty, and any national + implementations thereof. + +2. Waiver. To the greatest extent permitted by, but not in contravention +of, applicable law, Affirmer hereby overtly, fully, permanently, +irrevocably and unconditionally waives, abandons, and surrenders all of +Affirmer's Copyright and Related Rights and associated claims and causes +of action, whether now known or unknown (including existing as well as +future claims and causes of action), in the Work (i) in all territories +worldwide, (ii) for the maximum duration provided by applicable law or +treaty (including future time extensions), (iii) in any current or future +medium and for any number of copies, and (iv) for any purpose whatsoever, +including without limitation commercial, advertising or promotional +purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each +member of the public at large and to the detriment of Affirmer's heirs and +successors, fully intending that such Waiver shall not be subject to +revocation, rescission, cancellation, termination, or any other legal or +equitable action to disrupt the quiet enjoyment of the Work by the public +as contemplated by Affirmer's express Statement of Purpose. + +3. Public License Fallback. Should any part of the Waiver for any reason +be judged legally invalid or ineffective under applicable law, then the +Waiver shall be preserved to the maximum extent permitted taking into +account Affirmer's express Statement of Purpose. In addition, to the +extent the Waiver is so judged Affirmer hereby grants to each affected +person a royalty-free, non transferable, non sublicensable, non exclusive, +irrevocable and unconditional license to exercise Affirmer's Copyright and +Related Rights in the Work (i) in all territories worldwide, (ii) for the +maximum duration provided by applicable law or treaty (including future +time extensions), (iii) in any current or future medium and for any number +of copies, and (iv) for any purpose whatsoever, including without +limitation commercial, advertising or promotional purposes (the +"License"). The License shall be deemed effective as of the date CC0 was +applied by Affirmer to the Work. Should any part of the License for any +reason be judged legally invalid or ineffective under applicable law, such +partial invalidity or ineffectiveness shall not invalidate the remainder +of the License, and in such case Affirmer hereby affirms that he or she +will not (i) exercise any of his or her remaining Copyright and Related +Rights in the Work or (ii) assert any associated claims and causes of +action with respect to the Work, in either case contrary to Affirmer's +express Statement of Purpose. + +4. Limitations and Disclaimers. + + a. No trademark or patent rights held by Affirmer are waived, abandoned, + surrendered, licensed or otherwise affected by this document. + b. Affirmer offers the Work as-is and makes no representations or + warranties of any kind concerning the Work, express, implied, + statutory or otherwise, including without limitation warranties of + title, merchantability, fitness for a particular purpose, non + infringement, or the absence of latent or other defects, accuracy, or + the present or absence of errors, whether or not discoverable, all to + the greatest extent permissible under applicable law. + c. Affirmer disclaims responsibility for clearing rights of other persons + that may apply to the Work or any use thereof, including without + limitation any person's Copyright and Related Rights in the Work. + Further, Affirmer disclaims responsibility for obtaining any necessary + consents, permissions or other rights required for any use of the + Work. + d. Affirmer understands and acknowledges that Creative Commons is not a + party to this document and has no duty or obligation with respect to + this CC0 or use of the Work. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7e4813e --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +all: ulandscape-sysinfo + +build: main.c + gcc -o ulandscape-sysinfo -Wall -Wpedantic ulandscape-sysinfo.c + +clean: + rm ulandscape-sysinfo \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ef41624 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ulandscape-sysinfo + +A lightweight /etc/update-motd.d/50-landscape-sysinfo replacement for Linux written in C. diff --git a/ulandscape-sysinfo.c b/ulandscape-sysinfo.c new file mode 100644 index 0000000..f1d4374 --- /dev/null +++ b/ulandscape-sysinfo.c @@ -0,0 +1,310 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +typedef struct { + char *label; + char *value; +} OutputEntry; + +OutputEntry *output_entries = NULL; +int output_count = 0; +int max_label_length = 0; + +void add_output_entry(char *label, char *value) { + OutputEntry *entry = malloc(sizeof(OutputEntry)); + if (entry == NULL) { + perror("Error allocating memory for output entry"); + return; + } + + entry->label = malloc(strlen(label) + 1); + if (entry->label == NULL) { + perror("Error allocating memory for output entry label"); + return; + } + strcpy(entry->label, label); + + entry->value = malloc(strlen(value) + 1); + if (entry->value == NULL) { + perror("Error allocating memory for output entry value"); + return; + } + strcpy(entry->value, value); + + output_entries = + realloc(output_entries, sizeof(OutputEntry) * output_count); + if (output_entries == NULL) { + perror("Error allocating memory for output entries"); + return; + } + + output_entries = + realloc(output_entries, sizeof(OutputEntry) * (output_count + 1)); + if (output_entries == NULL) { + perror("Error allocating memory for output entries"); + return; + } + + output_entries[output_count] = *entry; + output_count++; + if (strlen(label) > max_label_length) { + max_label_length = strlen(label); + } +} + +typedef struct { + long long mem_total; + long long mem_available; + long long swap_total; + long long swap_free; +} BasicMemInfo; + +// returns NULL on error +int get_basic_memory_info(BasicMemInfo *meminfo) { + FILE *fp; + char *line = NULL; + size_t len = 0; + ssize_t read; + char key[64]; + uint64_t value; + char unit[16]; + + // get available ram and swap info via /proc/meminfo + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + perror("Error opening /proc/meminfo"); + return 1; + } + + uint8_t count = 0; + while ((read = getline(&line, &len, fp)) != -1) { + if (sscanf(line, "%63[^:]: %ld %15s", key, &value, unit) == 3) { + char *p = key + strlen(key) - 1; + while (p >= key && *p == ' ') { + *p = '\0'; + p--; + } + + // convert all kilobytes to bytes + if (strcmp(key, "MemAvailable") == 0) { + meminfo->mem_available = value * 1024; + count++; + } else if (strcmp(key, "MemTotal") == 0) { + meminfo->mem_total = value * 1024; + count++; + } else if (strcmp(key, "SwapTotal") == 0) { + meminfo->swap_total = value * 1024; + count++; + } else if (strcmp(key, "SwapFree") == 0) { + meminfo->swap_free = value * 1024; + count++; + } + + // Check if we found all values from /proc/meminfo + if (count == 4) { + break; + } + } + } + + if (line) + free(line); + fclose(fp); + + if (count != 4) { + printf("Error parsing /proc/meminfo\n"); + return 1; + } + + return 0; +} + +int count_processes(void) { + int count = 0; + + DIR *dir = opendir("/proc"); + if (dir == NULL) { + perror("Error opening /proc"); + return -1; + } + + struct dirent *ent; + while ((ent = readdir(dir)) != NULL) { + if (ent->d_type == DT_DIR) { + bool is_pid = true; + for (int i = 0; ent->d_name[i] != '\0'; i++) { + // check if the name is a number (PID) + if (!isdigit(ent->d_name[i])) { + is_pid = false; + break; + } + } + + count++; + } + } + closedir(dir); + + return count; +} + +int main(void) { + // TODO: should we cache this like the shell script does? + + double loadavg[1] = {0}; + getloadavg(loadavg, 1); + + time_t curtime; + time(&curtime); + + struct statfs sf; + if (statfs("/", &sf) != 0) { + perror("Error calling statfs"); + return 1; + } + + uint64_t total_bytes = (uint64_t)sf.f_blocks * sf.f_frsize; + uint64_t free_bytes = (uint64_t)sf.f_bfree * sf.f_frsize; + uint64_t available_bytes = + (uint64_t)sf.f_bavail * sf.f_frsize; // For non-root users + uint64_t used_bytes = total_bytes - free_bytes; + + float formatted_total_space = total_bytes; + char *unit = "B"; + + if (formatted_total_space > (uint64_t)1 << 40) { + formatted_total_space /= (uint64_t)1 << 40; + unit = "TiB"; + } else if (formatted_total_space > (uint64_t)1 << 30) { + formatted_total_space /= (uint64_t)1 << 30; + unit = "GiB"; + } else if (formatted_total_space > (uint64_t)1 << 20) { + formatted_total_space /= (uint64_t)1 << 20; + unit = "MiB"; + } else if (formatted_total_space > (uint64_t)1 << 10) { + formatted_total_space /= (uint64_t)1 << 10; + unit = "KiB"; + } + + BasicMemInfo meminfo = {0}; + + if (get_basic_memory_info(&meminfo) != 0) { + return 1; + } + + int processes = count_processes(); + + // count logged in users + int users = 0; + struct utmp *ut; + setutent(); + while ((ut = getutent()) != NULL) { + if (ut->ut_type == USER_PROCESS) { + users++; + } + } + endutent(); + + printf("\n System information as of %s\n", ctime(&curtime)); + + char value_buffer[128]; + char label_buffer[128]; + + snprintf(value_buffer, 128, "%.2f", loadavg[0]); + add_output_entry("System load", value_buffer); + + snprintf(value_buffer, 128, "%.2f%% of %.2f%s", + (float)((float)used_bytes * 100.0 / (float)total_bytes), + formatted_total_space, unit); + add_output_entry("Usage of /", value_buffer); + + snprintf(value_buffer, 128, "%d%%", + (uint8_t)(((float)(meminfo.mem_total - meminfo.mem_available) / + (float)meminfo.mem_total) * + 100.0)); + add_output_entry("Memory usage", value_buffer); + + snprintf(value_buffer, 128, "%d%%", + (uint8_t)(((float)(meminfo.swap_total - meminfo.swap_free) / + (float)meminfo.swap_total) * + 100.0)); + add_output_entry("Swap usage", value_buffer); + + snprintf(value_buffer, 128, "%d", processes); + add_output_entry("Processes", value_buffer); + + snprintf(value_buffer, 128, "%d", users); + add_output_entry("Users", value_buffer); + + struct ifaddrs *ifaddr, *ifa; + int family, s; + char host[128]; + getifaddrs(&ifaddr); + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) { + continue; + } + + if (ifa->ifa_addr->sa_family == AF_INET || + ifa->ifa_addr->sa_family == AF_INET6) { + + family = ifa->ifa_addr->sa_family; + + if (family == AF_INET || family == AF_INET6) { + s = getnameinfo(ifa->ifa_addr, + (family == AF_INET) + ? sizeof(struct sockaddr_in) + : sizeof(struct sockaddr_in6), + host, 128, NULL, 0, NI_NUMERICHOST); + + if (s != 0) { + printf("Error calling getnameinfo: %s\n", gai_strerror(s)); + continue; + } + + if (strcmp(ifa->ifa_name, "lo") == 0) { + continue; + } + if (strncmp(ifa->ifa_name, "docker", 6) == 0 || + strncmp(ifa->ifa_name, "br-", 3) == + 0 || // Docker bridge interfaces + strncmp(ifa->ifa_name, "veth", 4) == + 0 || // Docker/VirtualBox interfaces + strncmp(ifa->ifa_name, "virbr", 5) == 0 || + strcmp(ifa->ifa_name, "lo") == + 0) { // libvirt bridge interfaces + continue; // Skip container interfaces + } + + snprintf(label_buffer, 128, "%s address for %s", + (family == AF_INET) ? "IPv4" : "IPv6", ifa->ifa_name); + snprintf(value_buffer, 128, "%s", host); + add_output_entry(label_buffer, value_buffer); + } + } + } + + for (int i = 0; i < output_count; i++) { + printf(" %s:%*s%s\n", output_entries[i].label, + (int)((max_label_length + 1) - strlen(output_entries[i].label)), + "", output_entries[i].value); + } + + printf("\n"); + + return 0; +}