From 4474f2911fe3e03c0aef708afdc2e7639674ed00 Mon Sep 17 00:00:00 2001 From: hauke Date: Wed, 5 Feb 2014 21:27:47 +0000 Subject: [PATCH] zabbix-extra-mac80211: fix mac80211.phydiscovery /sys/kernel/debug/ is now only accessible to root, use zabbix_helper_mac80211 (suid helper) to compute mac80211.phydiscovery (the discovery rule) Signed-off-by: Etienne CHAMPETIER git-svn-id: svn://svn.openwrt.org/openwrt/packages@39490 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- admin/zabbix/files/mac80211 | 2 +- admin/zabbix/files/zabbix_helper_mac80211.c | 89 ++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 23 deletions(-) diff --git a/admin/zabbix/files/mac80211 b/admin/zabbix/files/mac80211 index 1e5c08641..93d835189 100644 --- a/admin/zabbix/files/mac80211 +++ b/admin/zabbix/files/mac80211 @@ -7,7 +7,7 @@ # mac80211 phy discovery (like 'phy0') # exemple: {"data":[{"{#PHY}":"phy0"}]} # -UserParameter=mac80211.phydiscovery,for phy in $(ls /sys/kernel/debug/ieee80211/); do list="$list,"'{"{#PHY}":"'$phy'"}'; done; echo '{"data":['${list#,}']}' +UserParameter=mac80211.phydiscovery,zabbix_helper_mac80211 discovery #phy statistics (you need {#PHY} as parameter) # diff --git a/admin/zabbix/files/zabbix_helper_mac80211.c b/admin/zabbix/files/zabbix_helper_mac80211.c index cb6d622cc..1442d2743 100644 --- a/admin/zabbix/files/zabbix_helper_mac80211.c +++ b/admin/zabbix/files/zabbix_helper_mac80211.c @@ -2,31 +2,76 @@ #include #include #include +#include +#include +#include -int main(int argc, char *argv[]) { +int discovery() +{ + DIR *dir; + struct dirent *ent; + bool comma = false; + if ((dir = opendir ("/sys/kernel/debug/ieee80211/")) != NULL) { + printf("{\"data\":["); + while ((ent = readdir (dir)) != NULL) { + if (strcmp(".", ent->d_name) && strcmp("..", ent->d_name)) { + if (comma) + printf(","); + printf("{\"{#PHY}\":\"%s\"}", ent->d_name); + comma = true; + } + } + printf("]}\n"); + closedir(dir); + } else { + perror(""); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} - if(argc == 3) { - char *phy = NULL; - char *stat = NULL; - char *filename = NULL; - FILE *f = NULL; - phy = basename(argv[1]); - stat = basename(argv[2]); - if(asprintf(&filename, "/sys/kernel/debug/ieee80211/%s/statistics/%s", phy, stat) > 0) - f = fopen(filename, "r"); +int get_param(char *phy, char *stat) +{ + char *filename = NULL; + FILE *f = NULL; + phy = basename(phy); + stat = basename(stat); + if (asprintf(&filename, "/sys/kernel/debug/ieee80211/%s/statistics/%s", phy, stat) > 0) + f = fopen(filename, "r"); - if(f != NULL) { - char temp[256]; - while (fgets(temp, 256, f) != NULL) - printf("%s",temp); + if (f != NULL) { + char temp[256]; + while (fgets(temp, 256, f) != NULL) + printf("%s",temp); - fclose(f); - } - free(filename); + fclose(f); } else { - fprintf(stderr, "Usage: %s PHY STAT\n",argv[0]); - fprintf(stderr, " cat /sys/kernel/debug/ieee80211/PHY/statistics/STAT as root\n"); - return 1; - } - return 0; + perror(""); + return EXIT_FAILURE; + } + free(filename); + return EXIT_SUCCESS; +} + +int usage(char *name) +{ + fprintf(stderr, "Usage:\n"); + fprintf(stderr, " %s discovery\n", name); + fprintf(stderr, " => print mac80211.phydiscovery discovery rule\n"); + fprintf(stderr, " %s PHY STAT\n", name); + fprintf(stderr, " => cat /sys/kernel/debug/ieee80211/PHY/statistics/STAT as root\n"); + return EXIT_FAILURE; +} + +int main(int argc, char *argv[]) +{ + + switch (argc) { + case 2: + return discovery(); + case 3: + return get_param(argv[1], argv[2]); + default: + return usage(argv[0]); + } } -- 2.11.0