+ if (fp == NULL) {
+ warn("Error opening stat file");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
+ fclose(fp);
+
+ delay = (UPDATE_INTERVAL - (UPDATE_INTERVAL - 1));
+ sleep(delay);
+
+ fp = fopen("/proc/stat","r");
+ if (fp == NULL) {
+ warn("Error opening stat file");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
+ fclose(fp);
+ perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
+
+ return smprintf("%d%%", perc);
+}
+
+static char *
+datetime(const char *timeformat)
+{
+ time_t t;
+ char timestr[80];
+
+ t = time(NULL);
+ if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
+ return smprintf(UNKNOWN_STR);
+
+ return smprintf("%s", timestr);
+}
+
+static char *
+disk_free(const char *mountpoint)
+{
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ warn("Could not get filesystem info");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_perc(const char *mountpoint)
+{
+ int perc = 0;
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ warn("Could not get filesystem info");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
+
+ return smprintf("%d%%", perc);
+}
+
+static char *
+disk_total(const char *mountpoint)
+{
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ warn("Could not get filesystem info");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
+}
+
+static char *
+disk_used(const char *mountpoint)
+{
+ struct statvfs fs;
+
+ if (statvfs(mountpoint, &fs) < 0) {
+ warn("Could not get filesystem info");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
+}
+
+static char *
+entropy(void)
+{
+ int entropy = 0;
+ FILE *fp = fopen("/proc/sys/kernel/random/entropy_avail", "r");
+
+ if (fp == NULL) {
+ warn("Could not open entropy file");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fscanf(fp, "%d", &entropy);
+ fclose(fp);
+
+ return smprintf("%d", entropy);
+}
+
+static char *
+gid(void)
+{
+ return smprintf("%d", getgid());
+}
+
+static char *
+hostname(void)
+{
+ char hostname[HOST_NAME_MAX];
+ FILE *fp = fopen("/proc/sys/kernel/hostname", "r");
+
+ if (fp == NULL) {
+ warn("Could not open hostname file");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ fgets(hostname, sizeof(hostname), fp);
+ hostname[strlen(hostname)-1] = '\0';
+ fclose(fp);
+
+ return smprintf("%s", hostname);
+}
+
+static char *
+ip(const char *interface)
+{
+ struct ifaddrs *ifaddr, *ifa;
+ int s;
+ char host[NI_MAXHOST];
+
+ if (getifaddrs(&ifaddr) == -1) {
+ warn("Error getting IP address");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr == NULL)
+ continue;
+
+ s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST,
+ NULL, 0, NI_NUMERICHOST);
+
+ if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
+ if (s != 0) {
+ warnx("Error getting IP address.");
+ return smprintf(UNKNOWN_STR);
+ }
+ return smprintf("%s", host);
+ }
+ }
+
+ freeifaddrs(ifaddr);
+
+ return smprintf(UNKNOWN_STR);
+}
+
+static char *
+load_avg(void)
+{
+ double avgs[3];
+
+ if (getloadavg(avgs, 3) < 0) {
+ warnx("Error getting load avg.");
+ return smprintf(UNKNOWN_STR);
+ }
+
+ return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
+}
+
+static char *
+ram_free(void)