zabbix_helper_mac80211.c 1.8 KB

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