proc_net_wireless.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. #include <stdbool.h>
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_NETWIRELESS_NAME "/proc/net/wireless"
  4. #define CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_NETWIRELESS_NAME
  5. static struct netwireless {
  6. char *name;
  7. uint32_t hash;
  8. //flags
  9. bool configured;
  10. struct timeval updated;
  11. int do_status;
  12. int do_quality;
  13. int do_discarded_packets;
  14. int do_missed_beacon;
  15. // Data collected
  16. // status
  17. kernel_uint_t status;
  18. // Quality
  19. calculated_number link;
  20. calculated_number level;
  21. calculated_number noise;
  22. // Discarded packets
  23. kernel_uint_t nwid;
  24. kernel_uint_t crypt;
  25. kernel_uint_t frag;
  26. kernel_uint_t retry;
  27. kernel_uint_t misc;
  28. // missed beacon
  29. kernel_uint_t missed_beacon;
  30. const char *chart_id_net_status;
  31. const char *chart_id_net_link;
  32. const char *chart_id_net_level;
  33. const char *chart_id_net_noise;
  34. const char *chart_id_net_discarded_packets;
  35. const char *chart_id_net_missed_beacon;
  36. const char *chart_family;
  37. // charts
  38. // status
  39. RRDSET *st_status;
  40. // Quality
  41. RRDSET *st_link;
  42. RRDSET *st_level;
  43. RRDSET *st_noise;
  44. // Discarded Packets
  45. RRDSET *st_discarded_packets;
  46. // Missed beacon
  47. RRDSET *st_missed_beacon;
  48. // Dimensions
  49. // status
  50. RRDDIM *rd_status;
  51. // Quality
  52. RRDDIM *rd_link;
  53. RRDDIM *rd_level;
  54. RRDDIM *rd_noise;
  55. // Discarded packets
  56. RRDDIM *rd_nwid;
  57. RRDDIM *rd_crypt;
  58. RRDDIM *rd_frag;
  59. RRDDIM *rd_retry;
  60. RRDDIM *rd_misc;
  61. // missed beacon
  62. RRDDIM *rd_missed_beacon;
  63. struct netwireless *next;
  64. } *netwireless_root = NULL;
  65. static void netwireless_free_st(struct netwireless *wireless_dev)
  66. {
  67. if (wireless_dev->st_status) rrdset_is_obsolete(wireless_dev->st_status);
  68. if (wireless_dev->st_link) rrdset_is_obsolete(wireless_dev->st_link);
  69. if (wireless_dev->st_level) rrdset_is_obsolete(wireless_dev->st_level);
  70. if (wireless_dev->st_noise) rrdset_is_obsolete(wireless_dev->st_noise);
  71. if (wireless_dev->st_discarded_packets) rrdset_is_obsolete(wireless_dev->st_discarded_packets);
  72. if (wireless_dev->st_missed_beacon) rrdset_is_obsolete(wireless_dev->st_missed_beacon);
  73. wireless_dev->st_status = NULL;
  74. wireless_dev->st_link = NULL;
  75. wireless_dev->st_level = NULL;
  76. wireless_dev->st_noise = NULL;
  77. wireless_dev->st_discarded_packets = NULL;
  78. wireless_dev->st_missed_beacon = NULL;
  79. }
  80. static void netwireless_free(struct netwireless *wireless_dev)
  81. {
  82. wireless_dev->next = NULL;
  83. freez((void *)wireless_dev->name);
  84. netwireless_free_st(wireless_dev);
  85. freez((void *)wireless_dev->chart_id_net_status);
  86. freez((void *)wireless_dev->chart_id_net_link);
  87. freez((void *)wireless_dev->chart_id_net_level);
  88. freez((void *)wireless_dev->chart_id_net_noise);
  89. freez((void *)wireless_dev->chart_id_net_discarded_packets);
  90. freez((void *)wireless_dev->chart_id_net_missed_beacon);
  91. freez((void *)wireless_dev);
  92. }
  93. static void netwireless_cleanup(struct timeval *timestamp)
  94. {
  95. struct netwireless *previous = NULL;
  96. struct netwireless *current;
  97. // search it, from beginning to the end
  98. for (current = netwireless_root; current;) {
  99. if (timercmp(&current->updated, timestamp, <)) {
  100. struct netwireless *to_free = current;
  101. current = current->next;
  102. netwireless_free(to_free);
  103. if (previous) {
  104. previous->next = current;
  105. } else {
  106. netwireless_root = current;
  107. }
  108. } else {
  109. previous = current;
  110. current = current->next;
  111. }
  112. }
  113. }
  114. // finds an existing interface or creates a new entry
  115. static struct netwireless *find_or_create_wireless(const char *name)
  116. {
  117. struct netwireless *wireless;
  118. uint32_t hash = simple_hash(name);
  119. // search it, from beginning to the end
  120. for (wireless = netwireless_root ; wireless ; wireless = wireless->next) {
  121. if (unlikely(hash == wireless->hash && !strcmp(name, wireless->name))) {
  122. return wireless;
  123. }
  124. }
  125. // create a new one
  126. wireless = callocz(1, sizeof(struct netwireless));
  127. wireless->name = strdupz(name);
  128. wireless->hash = hash;
  129. // link it to the end
  130. if (netwireless_root) {
  131. struct netwireless *last_node;
  132. for (last_node = netwireless_root; last_node->next ; last_node = last_node->next);
  133. last_node->next = wireless;
  134. } else
  135. netwireless_root = wireless;
  136. return wireless;
  137. }
  138. static void configure_device(int do_status, int do_quality, int do_discarded_packets, int do_missed,
  139. struct netwireless *wireless_dev) {
  140. wireless_dev->do_status = do_status;
  141. wireless_dev->do_quality = do_quality;
  142. wireless_dev->do_discarded_packets = do_discarded_packets;
  143. wireless_dev->do_missed_beacon = do_missed;
  144. wireless_dev->configured = true;
  145. char buffer[RRD_ID_LENGTH_MAX + 1];
  146. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_status", wireless_dev->name);
  147. wireless_dev->chart_id_net_status = strdupz(buffer);
  148. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_link_quality", wireless_dev->name);
  149. wireless_dev->chart_id_net_link = strdupz(buffer);
  150. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_signal_level", wireless_dev->name);
  151. wireless_dev->chart_id_net_level = strdupz(buffer);
  152. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_noise_level", wireless_dev->name);
  153. wireless_dev->chart_id_net_noise = strdupz(buffer);
  154. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_discarded_packets", wireless_dev->name);
  155. wireless_dev->chart_id_net_discarded_packets = strdupz(buffer);
  156. snprintfz(buffer, RRD_ID_LENGTH_MAX, "%s_missed_beacon", wireless_dev->name);
  157. wireless_dev->chart_id_net_missed_beacon = strdupz(buffer);
  158. }
  159. int do_proc_net_wireless(int update_every, usec_t dt)
  160. {
  161. UNUSED(dt);
  162. static procfile *ff = NULL;
  163. static int do_status, do_quality = -1, do_discarded_packets, do_beacon;
  164. static char *proc_net_wireless_filename = NULL;
  165. if (unlikely(do_quality == -1)) {
  166. char filename[FILENAME_MAX + 1];
  167. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/wireless");
  168. proc_net_wireless_filename = config_get(CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS,"filename to monitor",
  169. filename);
  170. do_status = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS,
  171. "status for all interfaces", CONFIG_BOOLEAN_AUTO);
  172. do_quality = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS,
  173. "quality for all interfaces", CONFIG_BOOLEAN_AUTO);
  174. do_discarded_packets = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS,
  175. "discarded packets for all interfaces",
  176. CONFIG_BOOLEAN_AUTO);
  177. do_beacon = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_NETWIRELESS,
  178. "missed beacon for all interface", CONFIG_BOOLEAN_AUTO);
  179. }
  180. if (unlikely(!ff)) {
  181. ff = procfile_open(proc_net_wireless_filename, " \t,|", PROCFILE_FLAG_DEFAULT);
  182. if (unlikely(!ff)) return 1;
  183. }
  184. ff = procfile_readall(ff);
  185. if (unlikely(!ff)) return 1;
  186. size_t lines = procfile_lines(ff);
  187. struct timeval timestamp;
  188. size_t l;
  189. gettimeofday(&timestamp, NULL);
  190. for (l = 2; l < lines; l++) {
  191. if (unlikely(procfile_linewords(ff, l) < 11)) continue;
  192. char *name = procfile_lineword(ff, l, 0);
  193. size_t len = strlen(name);
  194. if (name[len - 1] == ':') name[len - 1] = '\0';
  195. struct netwireless *wireless_dev = find_or_create_wireless(name);
  196. if (unlikely(!wireless_dev->configured)) {
  197. configure_device(do_status, do_quality, do_discarded_packets, do_beacon, wireless_dev);
  198. }
  199. if (likely(do_status != CONFIG_BOOLEAN_NO)) {
  200. wireless_dev->status = str2kernel_uint_t(procfile_lineword(ff, l, 1));
  201. if (unlikely(!wireless_dev->st_status)) {
  202. wireless_dev->st_status = rrdset_create_localhost("wireless",
  203. wireless_dev->chart_id_net_status,
  204. NULL,
  205. wireless_dev->name,
  206. "wireless.status",
  207. "Internal status reported by interface.",
  208. "status",
  209. PLUGIN_PROC_NAME,
  210. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  211. NETDATA_CHART_PRIO_WIRELESS_IFACE,
  212. update_every,
  213. RRDSET_TYPE_LINE);
  214. rrdset_flag_set(wireless_dev->st_status, RRDSET_FLAG_DETAIL);
  215. wireless_dev->rd_status = rrddim_add(wireless_dev->st_status, "status", NULL, 1,
  216. 1, RRD_ALGORITHM_ABSOLUTE);
  217. } else {
  218. rrdset_next(wireless_dev->st_status);
  219. }
  220. rrddim_set_by_pointer(wireless_dev->st_status, wireless_dev->rd_status,
  221. (collected_number)wireless_dev->status);
  222. rrdset_done(wireless_dev->st_status);
  223. }
  224. if (likely(do_quality != CONFIG_BOOLEAN_NO)) {
  225. wireless_dev->link = str2ld(procfile_lineword(ff, l, 2), NULL);
  226. wireless_dev->level = str2ld(procfile_lineword(ff, l, 3), NULL);
  227. wireless_dev->noise = str2ld(procfile_lineword(ff, l, 4), NULL);
  228. if (unlikely(!wireless_dev->st_link)) {
  229. wireless_dev->st_link = rrdset_create_localhost("wireless",
  230. wireless_dev->chart_id_net_link,
  231. NULL,
  232. wireless_dev->name,
  233. "wireless.link_quality",
  234. "Overall quality of the link. This is an aggregate value, and depends on the driver and hardware.",
  235. "value",
  236. PLUGIN_PROC_NAME,
  237. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  238. NETDATA_CHART_PRIO_WIRELESS_IFACE + 1,
  239. update_every,
  240. RRDSET_TYPE_LINE);
  241. rrdset_flag_set(wireless_dev->st_link, RRDSET_FLAG_DETAIL);
  242. wireless_dev->rd_link = rrddim_add(wireless_dev->st_link, "link_quality", NULL, 1, 1,
  243. RRD_ALGORITHM_ABSOLUTE);
  244. } else {
  245. rrdset_next(wireless_dev->st_link);
  246. }
  247. if (unlikely(!wireless_dev->st_level)) {
  248. wireless_dev->st_level = rrdset_create_localhost("wireless",
  249. wireless_dev->chart_id_net_level,
  250. NULL,
  251. wireless_dev->name,
  252. "wireless.signal_level",
  253. "The signal level is the wireless signal power level received by the wireless client. The closer the value is to 0, the stronger the signal.",
  254. "dBm",
  255. PLUGIN_PROC_NAME,
  256. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  257. NETDATA_CHART_PRIO_WIRELESS_IFACE + 2,
  258. update_every,
  259. RRDSET_TYPE_LINE);
  260. rrdset_flag_set(wireless_dev->st_level, RRDSET_FLAG_DETAIL);
  261. wireless_dev->rd_level = rrddim_add(wireless_dev->st_level, "signal_level", NULL, 1, 1,
  262. RRD_ALGORITHM_ABSOLUTE);
  263. } else {
  264. rrdset_next(wireless_dev->st_level);
  265. }
  266. if (unlikely(!wireless_dev->st_noise)) {
  267. wireless_dev->st_noise = rrdset_create_localhost("wireless",
  268. wireless_dev->chart_id_net_noise,
  269. NULL,
  270. wireless_dev->name,
  271. "wireless.noise_level",
  272. "The noise level indicates the amount of background noise in your environment. The closer the value to 0, the greater the noise level.",
  273. "dBm",
  274. PLUGIN_PROC_NAME,
  275. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  276. NETDATA_CHART_PRIO_WIRELESS_IFACE + 3,
  277. update_every,
  278. RRDSET_TYPE_LINE);
  279. rrdset_flag_set(wireless_dev->st_noise, RRDSET_FLAG_DETAIL);
  280. wireless_dev->rd_noise = rrddim_add(wireless_dev->st_noise, "noise_level", NULL, 1, 1,
  281. RRD_ALGORITHM_ABSOLUTE);
  282. } else {
  283. rrdset_next(wireless_dev->st_noise);
  284. }
  285. rrddim_set_by_pointer(wireless_dev->st_link, wireless_dev->rd_link,
  286. (collected_number)wireless_dev->link);
  287. rrdset_done(wireless_dev->st_link);
  288. rrddim_set_by_pointer(wireless_dev->st_level, wireless_dev->rd_level,
  289. (collected_number)wireless_dev->level);
  290. rrdset_done(wireless_dev->st_level);
  291. rrddim_set_by_pointer(wireless_dev->st_noise, wireless_dev->rd_noise,
  292. (collected_number)wireless_dev->noise);
  293. rrdset_done(wireless_dev->st_noise);
  294. }
  295. if (likely(do_discarded_packets)) {
  296. wireless_dev->nwid = str2kernel_uint_t(procfile_lineword(ff, l, 5));
  297. wireless_dev->crypt = str2kernel_uint_t(procfile_lineword(ff, l, 6));
  298. wireless_dev->frag = str2kernel_uint_t(procfile_lineword(ff, l, 7));
  299. wireless_dev->retry = str2kernel_uint_t(procfile_lineword(ff, l, 8));
  300. wireless_dev->misc = str2kernel_uint_t(procfile_lineword(ff, l, 9));
  301. if (unlikely(!wireless_dev->st_discarded_packets)) {
  302. wireless_dev->st_discarded_packets = rrdset_create_localhost("wireless",
  303. wireless_dev->chart_id_net_discarded_packets,
  304. NULL,
  305. wireless_dev->name,
  306. "wireless.discarded_packets",
  307. "Packet discarded in the wireless adapter due to \"wireless\" specific problems.",
  308. "packets/s",
  309. PLUGIN_PROC_NAME,
  310. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  311. NETDATA_CHART_PRIO_WIRELESS_IFACE + 4,
  312. update_every,
  313. RRDSET_TYPE_LINE);
  314. rrdset_flag_set(wireless_dev->st_discarded_packets, RRDSET_FLAG_DETAIL);
  315. wireless_dev->rd_nwid = rrddim_add(wireless_dev->st_discarded_packets, "nwid", NULL, 1,
  316. 1, RRD_ALGORITHM_INCREMENTAL);
  317. wireless_dev->rd_crypt = rrddim_add(wireless_dev->st_discarded_packets, "crypt", NULL, 1,
  318. 1, RRD_ALGORITHM_INCREMENTAL);
  319. wireless_dev->rd_frag = rrddim_add(wireless_dev->st_discarded_packets, "frag", NULL, 1,
  320. 1, RRD_ALGORITHM_INCREMENTAL);
  321. wireless_dev->rd_retry = rrddim_add(wireless_dev->st_discarded_packets, "retry", NULL, 1,
  322. 1, RRD_ALGORITHM_INCREMENTAL);
  323. wireless_dev->rd_misc = rrddim_add(wireless_dev->st_discarded_packets, "misc", NULL, 1,
  324. 1, RRD_ALGORITHM_INCREMENTAL);
  325. } else {
  326. rrdset_next(wireless_dev->st_discarded_packets);
  327. }
  328. rrddim_set_by_pointer(wireless_dev->st_discarded_packets, wireless_dev->rd_nwid,
  329. (collected_number)wireless_dev->nwid);
  330. rrddim_set_by_pointer(wireless_dev->st_discarded_packets, wireless_dev->rd_crypt,
  331. (collected_number)wireless_dev->crypt);
  332. rrddim_set_by_pointer(wireless_dev->st_discarded_packets, wireless_dev->rd_frag,
  333. (collected_number)wireless_dev->frag);
  334. rrddim_set_by_pointer(wireless_dev->st_discarded_packets, wireless_dev->rd_retry,
  335. (collected_number)wireless_dev->retry);
  336. rrddim_set_by_pointer(wireless_dev->st_discarded_packets, wireless_dev->rd_misc,
  337. (collected_number)wireless_dev->misc);
  338. rrdset_done(wireless_dev->st_discarded_packets);
  339. }
  340. if (likely(do_beacon)) {
  341. wireless_dev->missed_beacon = str2kernel_uint_t(procfile_lineword(ff, l, 10));
  342. if (unlikely(!wireless_dev->st_missed_beacon)) {
  343. wireless_dev->st_missed_beacon = rrdset_create_localhost("wireless",
  344. wireless_dev->chart_id_net_missed_beacon,
  345. NULL,
  346. wireless_dev->name,
  347. "wireless.missed_beacons",
  348. "Number of missed beacons.",
  349. "frames/s",
  350. PLUGIN_PROC_NAME,
  351. PLUGIN_PROC_MODULE_NETWIRELESS_NAME,
  352. NETDATA_CHART_PRIO_WIRELESS_IFACE + 5,
  353. update_every,
  354. RRDSET_TYPE_LINE);
  355. rrdset_flag_set(wireless_dev->st_missed_beacon, RRDSET_FLAG_DETAIL);
  356. wireless_dev->rd_missed_beacon = rrddim_add(wireless_dev->st_missed_beacon, "missed_beacons",
  357. NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  358. } else {
  359. rrdset_next(wireless_dev->st_missed_beacon);
  360. }
  361. rrddim_set_by_pointer(wireless_dev->st_missed_beacon, wireless_dev->rd_missed_beacon,
  362. (collected_number)wireless_dev->missed_beacon);
  363. rrdset_done(wireless_dev->st_missed_beacon);
  364. }
  365. wireless_dev->updated = timestamp;
  366. }
  367. netwireless_cleanup(&timestamp);
  368. return 0;
  369. }