zabbix-extra-mac80211: fix mac80211.phydiscovery
[packages.git] / admin / zabbix / files / zabbix_helper_mac80211.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <dirent.h>
7 #include <stdbool.h>
8
9 int discovery()
10 {
11     DIR *dir;
12     struct dirent *ent;
13     bool comma = false;
14     if ((dir = opendir ("/sys/kernel/debug/ieee80211/")) != NULL) {
15         printf("{\"data\":[");
16         while ((ent = readdir (dir)) != NULL) {
17             if (strcmp(".", ent->d_name) && strcmp("..", ent->d_name)) {
18                 if (comma)
19                     printf(",");
20                 printf("{\"{#PHY}\":\"%s\"}", ent->d_name);
21                 comma = true;
22             }
23         }
24         printf("]}\n");
25         closedir(dir);
26     } else {
27         perror("");
28         return EXIT_FAILURE;
29     }
30     return EXIT_SUCCESS;
31 }
32
33 int get_param(char *phy, char *stat)
34 {
35     char *filename = NULL;
36     FILE *f = NULL;
37     phy = basename(phy);
38     stat = basename(stat);
39     if (asprintf(&filename, "/sys/kernel/debug/ieee80211/%s/statistics/%s", phy, stat) > 0)
40         f = fopen(filename, "r");
41
42     if (f != NULL) {
43         char temp[256];
44         while (fgets(temp, 256, f) != NULL)
45             printf("%s",temp);
46
47         fclose(f);
48     } else {
49         perror("");
50         return EXIT_FAILURE;
51     }
52     free(filename);
53     return EXIT_SUCCESS;
54 }
55
56 int usage(char *name)
57 {
58     fprintf(stderr, "Usage:\n");
59     fprintf(stderr, " %s discovery\n", name);
60     fprintf(stderr, " => print mac80211.phydiscovery discovery rule\n");
61     fprintf(stderr, " %s PHY STAT\n", name);
62     fprintf(stderr, " => cat /sys/kernel/debug/ieee80211/PHY/statistics/STAT as root\n");
63     return EXIT_FAILURE;
64 }
65
66 int main(int argc, char *argv[])
67 {
68
69     switch (argc) {
70     case 2:
71         return discovery();
72     case 3:
73         return get_param(argv[1], argv[2]);
74     default:
75         return usage(argv[0]);
76     }
77 }