static int parse_imon_status(const char *channel) { char buf[256], status[25]; char *s; int age; qprintf(buf, sizeof(buf), "status %s", channel); /* reread every half sec only */ age = hash_age(&IMON, buf); if (age > 0 && age <= 500) return 0; init(); /* establish connection */ if (err) return -1; qprintf(buf, sizeof(buf), "status %s", channel); s = get_value(buf); if (sscanf(s, "%s", status) != 1) return -1; qprintf(buf, sizeof(buf), "status %s", channel); if (strcasecmp(status, "Online") == 0) hash_put(&IMON, buf, "1"); else hash_put(&IMON, buf, "0"); return 0; }
static void my_imon_version(RESULT * result) { char *val; int age; if (configure_imon() < 0) { SetResult(&result, R_STRING, ""); return; } /* read only once */ age = hash_age(&IMON, "version"); if (age < 0) { char *s; init(); if (err) { SetResult(&result, R_STRING, ""); return; } s = get_value("version"); for (;;) { /* interne Versionsnummer killen */ if (s[0] == ' ') { s = s + 1; break; } s = s + 1; } hash_put(&IMON, "version", s); } val = hash_get(&IMON, "version", NULL); if (val == NULL) val = ""; SetResult(&result, R_STRING, val); }
static int parse_imon_quantity(const char *channel) { char buf[256], fill1[25], in[25], fill2[25], out[25]; char *s; int age; qprintf(buf, sizeof(buf), "quantity %s in", channel); /* reread every half sec only */ age = hash_age(&IMON, buf); if (age > 0 && age <= 500) return 0; init(); /* establish connection */ if (err) return -1; qprintf(buf, sizeof(buf), "quantity %s", channel); s = get_value(buf); if (sscanf(s, "%s %s %s %s", fill1, in, fill2, out) != 4) return -1; qprintf(buf, sizeof(buf), "quantity %s in", channel); hash_put(&IMON, buf, in); qprintf(buf, sizeof(buf), "quantity %s out", channel); hash_put(&IMON, buf, out); return 0; }
static int get_essid(const char *dev, const char *key) { /* Get ESSID */ struct iwreq req; char key_buffer[32]; char essid_buffer[IW_ESSID_MAX_SIZE + 1]; int age; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } memset(essid_buffer, 0, sizeof(essid_buffer)); req.u.essid.pointer = (caddr_t) essid_buffer; req.u.essid.length = IW_ESSID_MAX_SIZE + 1; req.u.essid.flags = 0; if (do_ioctl(sock, dev, SIOCGIWESSID, &req) < 0) { ioctl_error(__LINE__); return -1; } hash_put(&wireless, key_buffer, essid_buffer); return (0); }
static int get_op_mode(const char *dev, const char *key) { /* Get operation mode */ struct iwreq req; char key_buffer[32]; int age; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } if (do_ioctl(sock, dev, SIOCGIWMODE, &req) < 0) { ioctl_error(__LINE__); return -1; } /* Fixme: req.u.mode is unsigned and therefore never < 0 */ /* if((req.u.mode > 6) || (req.u.mode < 0)) { */ if (req.u.mode > 6) { req.u.mode = 7; /* mode not available */ } hash_put(&wireless, key_buffer, operation_mode[req.u.mode]); return (0); }
static int parse_proc_apm(void) { char *key[] = { "driver_version", "bios_version", "bios_flags", "line_status", "battery_status", "battery_flag", "battery_percent", "battery_remaining", "time_units" }; char buffer[128], *beg, *end; int age, i; /* reread every 10 msec only */ age = hash_age(&APM, NULL); if (age > 0 && age <= 10) return 0; if (fd == -2) { fd = open("/proc/apm", O_RDONLY | O_NDELAY); if (fd == -1) { error("open(/proc/apm) failed: %s", strerror(errno)); return -1; } } if (lseek(fd, 0L, SEEK_SET) != 0) { error("lseek(/proc/apm) failed: %s", strerror(errno)); fd = -1; return -1; } if (read(fd, &buffer, sizeof(buffer) - 1) == -1) { error("read(/proc/apm) failed: %s", strerror(errno)); fd = -1; return -1; } beg = buffer; for (i = 0; i < 9 && beg != NULL; i++) { while (*beg == ' ') beg++; if ((end = strpbrk(beg, " \n"))) *end = '\0'; hash_put(&APM, key[i], beg); beg = end ? end + 1 : NULL; } return 0; }
static int parse_meminfo(void) { int age; /* reread every 10 msec only */ age = hash_age(&MemInfo, NULL); if (age > 0 && age <= 10) return 0; if (stream == NULL) stream = fopen("/proc/meminfo", "r"); if (stream == NULL) { error("fopen(/proc/meminfo) failed: %s", strerror(errno)); return -1; } rewind(stream); while (!feof(stream)) { char buffer[256]; char *c, *key, *val; fgets(buffer, sizeof(buffer), stream); c = strchr(buffer, ':'); if (c == NULL) continue; key = buffer; val = c + 1; /* strip leading blanks from key */ while (isspace(*key)) *key++ = '\0'; /* strip trailing blanks from key */ do *c = '\0'; while (isspace(*--c)); /* strip leading blanks from value */ while (isspace(*val)) *val++ = '\0'; /* strip trailing blanks from value */ for (c = val; *c != '\0'; c++); while (isspace(*--c)) *c = '\0'; /* skip lines that do not end with " kB" */ if (*c == 'B' && *(c - 1) == 'k' && *(c - 2) == ' ') { /* strip trailing " kB" from value */ *(c - 2) = '\0'; /* add entry to hash table */ hash_put(&MemInfo, key, val); } } return 0; }
static int parse_xmms_info(void) { int age; FILE *xmms_stream; char zeile[200]; /* reread every 100msec only */ age = hash_age(&xmms, NULL); if (age >= 0 && age <= 200) return 0; /* Open Filestream for '/tmp/xmms-info' */ xmms_stream = fopen("/tmp/xmms-info", "r"); /* Check for File */ if (!xmms_stream) { error("Error: Cannot open XMMS-Info Stream! Is XMMS started?"); return -1; } /* Read Lines from the Stream */ while (fgets(zeile, sizeof(zeile), xmms_stream)) { char *c, *key, *val; c = strchr(zeile, ':'); if (c == NULL) continue; key = zeile; val = c + 1; /* strip leading blanks from key */ while (isspace(*key)) *key++ = '\0'; /* strip trailing blanks from key */ do *c = '\0'; while (isspace(*--c)); /* strip leading blanks from value */ while (isspace(*val)) *val++ = '\0'; /* strip trailing blanks from value */ for (c = val; *c != '\0'; c++); while (isspace(*--c)) *c = '\0'; hash_put(&xmms, key, val); } fclose(xmms_stream); return 0; }
static int parse_imon(const char *cmd) { /* reread every half sec only */ int age = hash_age(&IMON, cmd); if (age > 0 && age <= 500) return 0; init(); /* establish connection */ if (err) return -1; hash_put(&IMON, cmd, get_value(cmd)); return 0; }
static int get_sec_mode(const char *dev, const char *key) { /* Get encryption information */ struct iwreq req; char key_buffer[32]; char encrypt_key[IW_ENCODING_TOKEN_MAX + 1]; int age; int has_key = 0; int key_flags = 0; int key_size = 0; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } req.u.data.pointer = (caddr_t) & encrypt_key; req.u.data.length = IW_ENCODING_TOKEN_MAX; req.u.data.flags = 0; if (do_ioctl(sock, dev, SIOCGIWENCODE, &req) >= 0) { has_key = 1; key_flags = req.u.data.flags; key_size = req.u.data.length; } else { return (-1); } /* Display encryption information */ /*if(has_key && (key_flags & IW_ENCODE_INDEX) > 1) */ /* printf(" [%d]", info->key_flags & IW_ENCODE_INDEX); */ if (has_key && (key_flags & IW_ENCODE_RESTRICTED)) hash_put(&wireless, key_buffer, "restricted"); else if (has_key && (key_flags & IW_ENCODE_OPEN)) hash_put(&wireless, key_buffer, "open"); return (0); }
static int get_sens(const char *dev, const char *key) { /* Get sensitivity */ struct iwreq req; struct iw_range range; char key_buffer[32]; char buffer[64]; int age; int has_sens = 0; int has_range = 0; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } if (do_ioctl(sock, dev, SIOCGIWSENS, &req) >= 0) { has_sens = 1; } if (get_range_info(sock, dev, &range) < 0) { memset(&range, 0, sizeof(range)); } else { has_range = 1; } if (has_range) { if (req.u.sens.value < 0) { qprintf(buffer, sizeof(buffer), "%d dBm", req.u.sens.value); } else { qprintf(buffer, sizeof(buffer), "%d/%d", req.u.sens.value, range.sensitivity); } } else { qprintf(buffer, sizeof(buffer), "%d", req.u.sens.value); } hash_put(&wireless, key_buffer, buffer); return (0); }
static int get_frequency(const char *dev, const char *key) { /* Get frequency / channel */ struct iwreq req; char qprintf_buffer[1024]; char key_buffer[32]; double freq; int age; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } req.u.data.length = 0; req.u.data.flags = 1; /* Clear updated flag */ if (do_ioctl(sock, dev, SIOCGIWFREQ, &req) < 0) { ioctl_error(__LINE__); return -1; } freq = FREQ2FLOAT(req.u.freq.m, req.u.freq.e); /*if( freq>= GIGA) snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g GHz", freq / GIGA); else if(freq>= MEGA) snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g MHz", freq / MEGA); else snprintf(qprintf_buffer,sizeof(qprintf_buffer), "%g kHz", freq / KILO); */ snprintf(qprintf_buffer, sizeof(qprintf_buffer), "%g", freq); hash_put(&wireless, key_buffer, qprintf_buffer); return (0); }
static int get_bitrate(const char *dev, const char *key) { /* Get bit rate */ struct iwreq req; char key_buffer[32]; char bitrate_buffer[64]; int age; double bitrate = 0; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } if (do_ioctl(sock, dev, SIOCGIWRATE, &req) < 0) { ioctl_error(__LINE__); return -1; } bitrate_buffer[0] = '\0'; bitrate = (double) (req.u.bitrate.value); /* if( bitrate>= GIGA) snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gGb/s", bitrate / GIGA); else if(bitrate >= MEGA) snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gMb/s", bitrate / MEGA); else snprintf(bitrate_buffer,sizeof(bitrate_buffer), "%gkb/s", bitrate / KILO); */ snprintf(bitrate_buffer, sizeof(bitrate_buffer), "%g", bitrate); hash_put(&wireless, key_buffer, bitrate_buffer); return (0); }
static int parse_telmon() { static int telmond_fd = -2; static char oldanswer[128]; int age; /* reread every 1 sec only */ age = hash_age(&TELMON, NULL); if (age > 0 && age <= 1000) return 0; if (telmond_fd != -1) { char telbuf[128]; telmond_fd = service_connect(thost, tport); if (telmond_fd >= 0) { int l = read(telmond_fd, telbuf, 127); if ((l > 0) && (strcmp(telbuf, oldanswer))) { char date[11]; char time[11]; char number[256]; char msn[256]; sscanf(telbuf, "%s %s %s %s", date, time, number, msn); hash_put(&TELMON, "time", time); date[4] = '\0'; date[7] = '\0'; qprintf(time, sizeof(time), "%s.%s.%s", date + 8, date + 5, date); hash_put(&TELMON, "number", number); hash_put(&TELMON, "msn", msn); hash_put(&TELMON, "date", time); phonebook(number); phonebook(msn); hash_put(&TELMON, "name", number); hash_put(&TELMON, "msnname", msn); } close(telmond_fd); strcpy(oldanswer, telbuf); } } return 0; }
static int get_stats(const char *dev, const char *key) { struct iw_statistics stats; struct iwreq req; char qprintf_buffer[1024]; char key_buffer[32]; int age; struct iw_range range; qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, key); age = hash_age(&wireless, key); /* reread every HASH_TTL msec only */ if (age > 0 && age <= HASH_TTL) { return (0); } if (get_ifname(&req, dev) != 0) { return (-1); } req.u.data.pointer = (caddr_t) & stats; req.u.data.length = sizeof(stats); req.u.data.flags = 1; /* Clear updated flag */ if (do_ioctl(sock, dev, SIOCGIWSTATS, &req) < 0) { ioctl_error(__LINE__); return -1; } if (get_range_info(sock, dev, &range) < 0) memset(&range, 0, sizeof(range)); if (stats.qual.level > range.max_qual.level) { qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.level - 0x100); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL); hash_put(&wireless, key_buffer, qprintf_buffer); qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d", stats.qual.noise - 0x100); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); hash_put(&wireless, key_buffer, qprintf_buffer); qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); hash_put(&wireless, key_buffer, qprintf_buffer); } else { qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.level, range.max_qual.level); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_LEVEL); hash_put(&wireless, key_buffer, qprintf_buffer); qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.noise, range.max_qual.noise); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_NOISE); hash_put(&wireless, key_buffer, qprintf_buffer); qprintf(qprintf_buffer, sizeof(qprintf_buffer), "%d/%d", stats.qual.qual, range.max_qual.qual); qprintf(key_buffer, sizeof(key_buffer), "%s.%s", dev, KEY_QUALITY); hash_put(&wireless, key_buffer, qprintf_buffer); } return 0; }
static int parse_proc_stat(void) { int age; /* reread every 10 msec only */ age = hash_age(&Stat, NULL); if (age > 0 && age <= 10) return 0; #ifndef __MAC_OS_X_VERSION_10_3 /* Linux Kernel, /proc-filesystem */ if (stream == NULL) stream = fopen("/proc/stat", "r"); if (stream == NULL) { error("fopen(/proc/stat) failed: %s", strerror(errno)); return -1; } rewind(stream); while (!feof(stream)) { char buffer[1024]; if (fgets(buffer, sizeof(buffer), stream) == NULL) break; if (strncmp(buffer, "cpu", 3) == 0) { char *key[] = { "user", "nice", "system", "idle", "iow", "irq", "sirq" }; char delim[] = " \t\n"; char *cpu, *beg, *end; int i; cpu = buffer; /* skip "cpu" or "cpu0" block */ if ((end = strpbrk(buffer, delim)) != NULL) *end = '\0'; beg = end ? end + 1 : NULL; for (i = 0; i < 7 && beg != NULL; i++) { while (strchr(delim, *beg)) beg++; if ((end = strpbrk(beg, delim))) *end = '\0'; hash_put2(cpu, key[i], beg); beg = end ? end + 1 : NULL; } } else if (strncmp(buffer, "page ", 5) == 0) { char *key[] = { "in", "out" }; char delim[] = " \t\n"; char *beg, *end; int i; for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) { while (strchr(delim, *beg)) beg++; if ((end = strpbrk(beg, delim))) *end = '\0'; hash_put2("page", key[i], beg); beg = end ? end + 1 : NULL; } } else if (strncmp(buffer, "swap ", 5) == 0) { char *key[] = { "in", "out" }; char delim[] = " \t\n"; char *beg, *end; int i; for (i = 0, beg = buffer + 5; i < 2 && beg != NULL; i++) { while (strchr(delim, *beg)) beg++; if ((end = strpbrk(beg, delim))) *end = '\0'; hash_put2("swap", key[i], beg); beg = end ? end + 1 : NULL; } } else if (strncmp(buffer, "intr ", 5) == 0) { char delim[] = " \t\n"; char *beg, *end, num[4]; int i; for (i = 0, beg = buffer + 5; i < 17 && beg != NULL; i++) { while (strchr(delim, *beg)) beg++; if ((end = strpbrk(beg, delim))) *end = '\0'; if (i == 0) strcpy(num, "sum"); else qprintf(num, sizeof(num), "%d", i - 1); hash_put2("intr", num, beg); beg = end ? end + 1 : NULL; } } else if (strncmp(buffer, "disk_io:", 8) == 0) { char *key[] = { "io", "rio", "rblk", "wio", "wblk" }; char delim[] = " ():,\t\n"; char *dev, *beg, *end, *p; int i; dev = buffer + 8; while (dev != NULL) { while (strchr(delim, *dev)) dev++; if ((end = strchr(dev, ')'))) *end = '\0'; while ((p = strchr(dev, ',')) != NULL) *p = ':'; beg = end ? end + 1 : NULL; for (i = 0; i < 5 && beg != NULL; i++) { while (strchr(delim, *beg)) beg++; if ((end = strpbrk(beg, delim))) *end = '\0'; hash_put3("disk_io", dev, key[i], beg); beg = end ? end + 1 : NULL; } dev = beg; } } else { char delim[] = " \t\n"; char *beg, *end; beg = buffer; if ((end = strpbrk(beg, delim))) *end = '\0'; beg = end ? end + 1 : NULL; if ((end = strpbrk(beg, delim))) *end = '\0'; while (strchr(delim, *beg)) beg++; 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; }