fixed small prob with memory stuff being wrong
[monky] / src / linux.c
index 3adcf76..fb57fe7 100644 (file)
@@ -29,8 +29,6 @@
 #include <net/if.h>
 #include <math.h>
 
-#include <linux/major.h>
-
 static struct sysinfo s_info;
 
 static int show_nice_processes;
@@ -111,25 +109,29 @@ void update_meminfo()
                        break;
 
                if (strncmp(buf, "MemTotal:", 9) == 0) {
-                       sscanf(buf, "%*s %u", &info.memmax);
+                       sscanf(buf, "%*s %lu", &info.memmax);
                } else if (strncmp(buf, "MemFree:", 8) == 0) {
-                       sscanf(buf, "%*s %u", &info.mem);
+                       sscanf(buf, "%*s %lu", &info.mem);
                } else if (strncmp(buf, "SwapTotal:", 10) == 0) {
-                       sscanf(buf, "%*s %u", &info.swapmax);
+                       sscanf(buf, "%*s %lu", &info.swapmax);
                } else if (strncmp(buf, "SwapFree:", 9) == 0) {
-                       sscanf(buf, "%*s %u", &info.swap);
+                       sscanf(buf, "%*s %lu", &info.swap);
                } else if (strncmp(buf, "Buffers:", 8) == 0) {
-                       sscanf(buf, "%*s %u", &info.buffers);
+                       sscanf(buf, "%*s %lu", &info.buffers);
                } else if (strncmp(buf, "Cached:", 7) == 0) {
-                       sscanf(buf, "%*s %u", &info.cached);
+                       sscanf(buf, "%*s %lu", &info.cached);
                }
        }
-
+       
        info.mem = info.memmax - info.mem;
        info.swap = info.swapmax - info.swap;
 
        info.bufmem = info.cached + info.buffers;
 
+       /*if (no_buffers) {
+               info.mem -= info.bufmem;
+       }*/
+
        info.mask |= (1 << INFO_MEM) | (1 << INFO_BUFFERS);
 }
 
@@ -309,7 +311,8 @@ inline void update_wifi_stats()
 
                sscanf(p, "%*d   %d.  %d.  %d", &l, &m, &n);
 
-               ns->linkstatus = (int) (log(l) / log(92) * 100);
+               ns->linkstatus = (int) (log(MIN(MAX(l,1),92)) / log(92) * 100);
+
        }
 
        /*** end wireless patch ***/
@@ -322,21 +325,24 @@ void update_total_processes()
        update_sysinfo();
 }
 
-static unsigned int cpu_user, cpu_system, cpu_nice;
-static double last_cpu_sum;
-static int clock_ticks;
+#define CPU_SAMPLE_COUNT 15
+struct cpu_info {
+       unsigned long cpu_user;
+       unsigned long cpu_system;
+       unsigned long cpu_nice;
+       double last_cpu_sum;
+       unsigned long clock_ticks;
+       double cpu_val[CPU_SAMPLE_COUNT];
+};
+static short cpu_setup = 0;
+static int rep;
+
 
 static FILE *stat_fp;
 
-inline static void update_stat()
+void get_cpu_count()
 {
-       // FIXME: arbitrary size?
-       static double cpu_val[15];
-       static int rep;
        char buf[256];
-       unsigned int i;
-       double curtmp;
-
        if (stat_fp == NULL)
                stat_fp = open_file("/proc/stat", &rep);
        else
@@ -350,37 +356,86 @@ inline static void update_stat()
                if (fgets(buf, 255, stat_fp) == NULL)
                        break;
 
+               if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
+                       info.cpu_count++;
+               }
+       }
+       info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
+}
+
+
+inline static void update_stat()
+{
+       static struct cpu_info *cpu = NULL;
+       char buf[256];
+       unsigned int i;
+       unsigned int index;
+       double curtmp;
+       if (!cpu_setup) {
+               get_cpu_count();
+               cpu_setup = 1;
+       }
+       if (cpu == NULL) {
+               cpu = malloc((info.cpu_count + 1) * sizeof(struct cpu_info));
+               for (index = 0; index < info.cpu_count + 1; ++index) {
+                       cpu[index].clock_ticks = 0;
+                       cpu[index].last_cpu_sum = 0;
+                       for (i = 0; i < CPU_SAMPLE_COUNT; ++i) {
+                               cpu[index].cpu_val[i] = 0;
+                       }
+               }
+       }
+       if (stat_fp == NULL) {
+               stat_fp = open_file("/proc/stat", &rep);
+       } else {
+               fseek(stat_fp, 0, SEEK_SET);
+       }
+       if (stat_fp == NULL) {
+               return;
+       }
+       index = 0;
+       while (!feof(stat_fp)) {
+               if (fgets(buf, 255, stat_fp) == NULL)
+                       break;
+
                if (strncmp(buf, "procs_running ", 14) == 0) {
                        sscanf(buf, "%*s %d", &info.run_procs);
                        info.mask |= (1 << INFO_RUN_PROCS);
                } else if (strncmp(buf, "cpu ", 4) == 0) {
-                       sscanf(buf, "%*s %u %u %u", &cpu_user, &cpu_nice,
-                              &cpu_system);
+                       sscanf(buf, "%*s %lu %lu %lu", &(cpu[index].cpu_user), &(cpu[index].cpu_nice), &(cpu[index].cpu_system));
+                       index++;
+                       info.mask |= (1 << INFO_CPU);
+               } else if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3]) && index <= info.cpu_count) {
+                       sscanf(buf, "%*s %lu %lu %lu", &(cpu[index].cpu_user), &(cpu[index].cpu_nice), &(cpu[index].cpu_system));
+                       index++;
                        info.mask |= (1 << INFO_CPU);
-               } else if (strncmp(buf, "cpu", 3) == 0 && isdigit(buf[3])) {
-                       info.cpu_count++;
                }
        }
-
-       {
+       for (index = 0; index < info.cpu_count + 1; index++) {
                double delta;
                delta = current_update_time - last_update_time;
-               if (delta <= 0.001)
+               if (delta <= 0.001) {
                        return;
+               }
 
-               if (clock_ticks == 0)
-                       clock_ticks = sysconf(_SC_CLK_TCK);
+               if (cpu[index].clock_ticks == 0) {
+                       cpu[index].clock_ticks = sysconf(_SC_CLK_TCK);
+               }
                curtmp = 0;
-               cpu_val[0] =
-                   (cpu_user + cpu_nice + cpu_system -
-                    last_cpu_sum) / delta / (double) clock_ticks /
-                   info.cpu_count;
-               for (i = 0; i < info.cpu_avg_samples; i++)
-                       curtmp += cpu_val[i];
-               info.cpu_usage = curtmp / info.cpu_avg_samples;
-               last_cpu_sum = cpu_user + cpu_nice + cpu_system;
+               cpu[index].cpu_val[0] =
+                               (cpu[index].cpu_user + cpu[index].cpu_nice + cpu[index].cpu_system -
+                               cpu[index].last_cpu_sum) / delta / (double) cpu[index].clock_ticks;
+               for (i = 0; i < info.cpu_avg_samples; i++) {
+                       curtmp += cpu[index].cpu_val[i];
+               }
+               if (index == 0) {
+                       info.cpu_usage[index] = curtmp / info.cpu_avg_samples / info.cpu_count;
+               } else {
+                       info.cpu_usage[index] = curtmp / info.cpu_avg_samples;
+               }
+               cpu[index].last_cpu_sum = cpu[index].cpu_user + cpu[index].cpu_nice + cpu[index].cpu_system;
                for (i = info.cpu_avg_samples; i > 1; i--)
-                       cpu_val[i - 1] = cpu_val[i - 2];
+                       cpu[index].cpu_val[i - 1] = cpu[index].cpu_val[i - 2];
 
        }
 
@@ -432,6 +487,42 @@ void update_load_average()
 #endif
 }
 
+#define PROC_I8K "/proc/i8k"
+#define I8K_DELIM " "
+static char *i8k_procbuf = NULL;
+void update_i8k()
+{
+       FILE *fp;
+       if (!i8k_procbuf) {
+               i8k_procbuf = (char*)malloc(128*sizeof(char));
+       }
+       if ((fp = fopen(PROC_I8K,"r")) == NULL) {
+               CRIT_ERR("/proc/i8k doesn't exist! use insmod to make sure the kernel driver is loaded...");
+       }
+
+       memset(&i8k_procbuf[0],0,128);
+       if (fread(&i8k_procbuf[0],sizeof(char),128,fp) == 0) {
+               ERR("something wrong with /proc/i8k...");
+       }
+
+       fclose(fp);
+
+  i8k.version = strtok(&i8k_procbuf[0],I8K_DELIM);
+       i8k.bios = strtok(NULL,I8K_DELIM);
+       i8k.serial = strtok(NULL,I8K_DELIM);
+       i8k.cpu_temp = strtok(NULL,I8K_DELIM);
+       i8k.left_fan_status = strtok(NULL,I8K_DELIM);   
+       i8k.right_fan_status = strtok(NULL,I8K_DELIM);  
+       i8k.left_fan_rpm = strtok(NULL,I8K_DELIM);
+       i8k.right_fan_rpm = strtok(NULL,I8K_DELIM);
+       i8k.ac_status = strtok(NULL,I8K_DELIM);
+       i8k.buttons_status = strtok(NULL,I8K_DELIM);
+}
+
+
+/***********************************************************/
+/***********************************************************/
+/***********************************************************/
 
 static int no_dots(const struct dirent *d)
 {
@@ -476,7 +567,7 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
                char *devtype)
 {
        char path[256];
-       char buf[64];
+       char buf[256];
        int fd;
        int divfd;
 
@@ -493,12 +584,11 @@ open_i2c_sensor(const char *dev, const char *type, int n, int *div,
                type = "in";
 
        if (strcmp(type, "tempf") == 0) {
-               snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp",
-                        n);
+               snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, "temp", n);
        } else {
                snprintf(path, 255, I2C_DIR "%s/%s%d_input", dev, type, n);
        }
-       strcpy(devtype, path);
+       strncpy(devtype, path, 255);
 
        /* open file */
        fd = open(path, O_RDONLY);
@@ -824,7 +914,7 @@ passive:                 73 C: tc1=4 tc2=3 tsp=40 devices=0xcdf6e6c0
 int open_acpi_temperature(const char *name)
 {
        char path[256];
-       char buf[64];
+       char buf[256];
        int fd;
 
        if (name == NULL || strcmp(name, "*") == 0) {
@@ -1086,8 +1176,27 @@ void update_top()
 {
        show_nice_processes = 1;
        process_find_top(info.cpu, info.memu);
+       info.first_process = get_first_process();
 }
 
+
+/*
+ *  The following ifdefs were adapted from gkrellm
+ */
+#include <linux/major.h>
+
+#if ! defined (MD_MAJOR)
+#define MD_MAJOR 9
+#endif
+
+#if !defined(LVM_BLK_MAJOR)
+#define LVM_BLK_MAJOR 58
+#endif
+
+#if !defined(NBD_MAJOR)
+#define NBD_MAJOR 43
+#endif
+
 void update_diskio()
 {
        static unsigned int last = UINT_MAX;
@@ -1113,12 +1222,12 @@ void update_diskio()
                fgets(buf, 512, fp);
                col_count = sscanf(buf, "%u %u %*s %*u %*u %u %*u %*u %*u %u",
                                   &major, &minor, &reads, &writes);
-               /* ignore subdevices (they have only 7 entries in their line)
+               /* ignore subdevices (they have only 3 matching entries in their line)
                 * and virtual devices (LVM, network block devices, RAM disks, Loopback)
                 *
                 * XXX ignore devices which are part of a SW RAID (MD_MAJOR)
                 */
-               if (col_count > 7 &&
+               if (col_count > 3 &&
                    major != LVM_BLK_MAJOR && major != NBD_MAJOR &&
                    major != RAMDISK_MAJOR && major != LOOP_MAJOR) {
                        current += reads + writes;
@@ -1140,3 +1249,4 @@ void update_diskio()
 
        diskio_value = tot;
 }
+