]> git.webhop.me Git - lcd4linux.git/commitdiff
plugins work on MacOS X: proc_stat, cpuinfo
authorvolker <volker@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Tue, 5 Jan 2010 22:29:16 +0000 (22:29 +0000)
committervolker <volker@3ae390bd-cb1e-0410-b409-cd5a39f66f1f>
Tue, 5 Jan 2010 22:29:16 +0000 (22:29 +0000)
git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@1077 3ae390bd-cb1e-0410-b409-cd5a39f66f1f

event.h
plugin_cpuinfo.c
plugin_netinfo.c
plugin_proc_stat.c

diff --git a/event.h b/event.h
index b6808d207ab2f4cef08e3c1691d0f4992b32b109..6486a54ca82563969bf28b91a523671aaf76be2f 100644 (file)
--- a/event.h
+++ b/event.h
@@ -28,6 +28,8 @@
 #ifndef _EVENT_H_
 #define _EVENT_H_
 
+#include <time.h>
+
 //events are identified by their file descriptor only
 
 /*
index 5975af7ce8ce2a7ad496e5091cc09870b1adbc86..8c87573bf1af77980b0f213591766858e545a013 100644 (file)
 #include "plugin.h"
 #include "hash.h"
 
+#ifdef __MAC_OS_X_VERSION_10_3
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
 
 static HASH CPUinfo;
 static FILE *stream = NULL;
 
-static int parse_cpuinfo(void)
+static int parse_cpuinfo(char *oid)
 {
     int age;
 
@@ -58,6 +62,10 @@ static int parse_cpuinfo(void)
     if (age > 0 && age <= 1000)
        return 0;
 
+#ifndef __MAC_OS_X_VERSION_10_3
+
+    /* Linux Kernel, /proc-filesystem */
+
     if (stream == NULL)
        stream = fopen("/proc/cpuinfo", "r");
     if (stream == NULL) {
@@ -93,6 +101,34 @@ static int parse_cpuinfo(void)
        hash_put(&CPUinfo, key, val);
 
     }
+    /* to avoid compiler unused warning */
+    oid = 0;
+
+#else
+
+    /* MACH Kernel, MacOS X */
+
+    char val_ret[256];
+    int *val;
+    size_t val_len;
+
+    if (sysctlbyname(oid, NULL, &val_len, NULL, 0) != 0) {
+       error("Error %d by sysctl(%s): %s", errno, oid, strerror(errno));
+       return -1;
+    }
+    if (val_len > sizeof(val_ret)) {
+       error("Error: Result of sysctl(%s) too big (%zd > %zd)!", oid, val_len, sizeof(val_ret));
+       return -1;
+    }
+    sysctlbyname(oid, &val_ret, &val_len, NULL, 0);
+    if (val_len == sizeof(int)) {
+       /* we got an integer instead of a string */
+       val = (int*)val_ret;
+       snprintf(val_ret, sizeof(val_ret), "%d", *val);
+    }
+    hash_put(&CPUinfo, oid, val_ret);
+#endif
+
     return 0;
 }
 
@@ -101,12 +137,12 @@ static void my_cpuinfo(RESULT * result, RESULT * arg1)
 {
     char *key, *val;
 
-    if (parse_cpuinfo() < 0) {
+    key = R2S(arg1);
+    if (parse_cpuinfo(key) < 0) {
        SetResult(&result, R_STRING, "");
        return;
     }
 
-    key = R2S(arg1);
     val = hash_get(&CPUinfo, key, NULL);
     if (val == NULL)
        val = "";
index c2af6fd26b0c270aaaadd380e92cf8ecc603c63b..57c79b267014d71ddd9bfe4a44ab73312d7eb0ba 100644 (file)
@@ -143,8 +143,10 @@ static void my_hwaddr(RESULT * result, RESULT * arg1)
     }
 
     strncpy(ifreq.ifr_name, R2S(arg1), sizeof(ifreq.ifr_name));
-    if (ioctl(socknr, SIOCGIFHWADDR, &ifreq) < 0) {
-       errcount++;
+    // if (ioctl(socknr, SIOCGIFHWADDR, &ifreq) < 0) {
+    // if (ioctl(socknr, SIOCGIFMAC, &ifreq) < 0) {
+    if (ioctl(socknr, SIOCGLIFPHYADDR, &ifreq) < 0) {
+               errcount++;
        if (1 == errcount % 1000) {
            error("%s: ioctl(IFHWADDR %s) failed: %s", "plugin_netinfo", ifreq.ifr_name, strerror(errno));
            error("  (skip next 1000 errors)");
@@ -152,7 +154,8 @@ static void my_hwaddr(RESULT * result, RESULT * arg1)
        SetResult(&result, R_STRING, "");
        return;
     }
-    hw = (unsigned char *) ifreq.ifr_hwaddr.sa_data;
+    // hw = (unsigned char *) ifreq.ifr_hwaddr.sa_data;
+    hw = (unsigned char *) ifreq.ifr_data;
     qprintf(value, sizeof(value), "%02x:%02x:%02x:%02x:%02x:%02x",
            *hw, *(hw + 1), *(hw + 2), *(hw + 3), *(hw + 4), *(hw + 5));
 
@@ -215,7 +218,8 @@ static void my_netmask(RESULT * result, RESULT * arg1)
        SetResult(&result, R_STRING, "");
        return;
     }
-    sin = (struct sockaddr_in *) &ifreq.ifr_netmask;
+    // sin = (struct sockaddr_in *) &ifreq.ifr_netmask;
+    sin = (struct sockaddr_in *) &ifreq.ifr_data;
     qprintf(value, sizeof(value), "%s", inet_ntoa(sin->sin_addr));
 
     SetResult(&result, R_STRING, value);
index d6a926fd5aca965553734642eaf948b70d7ea239..a8c91cebd76959392fc638f34726e085bf58a554 100644 (file)
 #include <ctype.h>
 #include <errno.h>
 
+#ifdef __MAC_OS_X_VERSION_10_3
+#include <mach/mach_host.h>
+#include <mach/host_info.h>
+#endif
+
 #include "debug.h"
 #include "plugin.h"
 #include "qprintf.h"
@@ -84,6 +89,10 @@ static int parse_proc_stat(void)
     if (age > 0 && age <= 10)
        return 0;
 
+#if 0
+
+    /* Linux Kernel, /proc-filesystem */
+
     if (stream == NULL)
        stream = fopen("/proc/stat", "r");
     if (stream == NULL) {
@@ -214,6 +223,35 @@ static int parse_proc_stat(void)
            hash_put1(buffer, beg);
        }
     }
+
+#else
+
+    /* MACH Kernel, MacOS X */
+
+    kern_return_t  err;
+    mach_msg_type_number_t  count;
+    host_info_t  r_load;
+    host_cpu_load_info_data_t  cpu_load;
+    char  s_val[8];
+
+    r_load = &cpu_load;
+    count = HOST_CPU_LOAD_INFO_COUNT;
+    err = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, r_load, &count);
+    if (KERN_SUCCESS != err) {
+        error("Error getting cpu load");
+        return -1;
+    }
+    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_USER]);
+    hash_put2("cpu", "user", s_val);
+    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_NICE]);
+    hash_put2("cpu", "nice", s_val);
+    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_SYSTEM]);
+    hash_put2("cpu", "system", s_val);
+    snprintf(s_val, sizeof(s_val), "%d", cpu_load.cpu_ticks[CPU_STATE_IDLE]);
+    hash_put2("cpu", "idle", s_val);
+
+#endif
+
     return 0;
 }