]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
TC plugin now cleanups properly; added TC packets and dropped packets charts
[netdata.git] / src / plugin_tc.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/resource.h>
9
10 #include "avl.h"
11 #include "log.h"
12 #include "common.h"
13 #include "appconfig.h"
14 #include "rrd.h"
15 #include "popen.h"
16 #include "plugin_tc.h"
17 #include "main.h"
18 #include "../config.h"
19
20 #define RRD_TYPE_TC                                     "tc"
21 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
22
23 // ----------------------------------------------------------------------------
24 // /sbin/tc processor
25 // this requires the script plugins.d/tc-qos-helper.sh
26
27 #define TC_LINE_MAX 1024
28
29 struct tc_class {
30         avl avl;
31
32         char *id;
33         uint32_t hash;
34
35         char *name;
36
37         char *leafid;
38         uint32_t leaf_hash;
39
40         char *parentid;
41         uint32_t parent_hash;
42
43         char hasparent;
44         char isleaf;
45         unsigned long long bytes;
46         unsigned long long packets;
47         unsigned long long dropped;
48         unsigned long long overlimits;
49         unsigned long long requeues;
50         unsigned long long lended;
51         unsigned long long borrowed;
52         unsigned long long giants;
53         unsigned long long tokens;
54         unsigned long long ctokens;
55
56         RRDDIM *rd_bytes;
57         RRDDIM *rd_packets;
58         RRDDIM *rd_dropped;
59
60         char name_updated;
61         char updated;   // updated bytes
62         int seen;               // seen in the tc list (even without bytes)
63
64         struct tc_class *next;
65         struct tc_class *prev;
66 };
67
68 struct tc_device {
69         avl avl;
70
71         char *id;
72         uint32_t hash;
73
74         char *name;
75         char *family;
76
77         char name_updated;
78         char family_updated;
79
80         char enabled;
81         RRDSET *st_bytes;
82         RRDSET *st_packets;
83         RRDSET *st_dropped;
84
85         avl_tree classes_index;
86
87         struct tc_class *classes;
88
89         struct tc_device *next;
90         struct tc_device *prev;
91 };
92
93
94 struct tc_device *tc_device_root = NULL;
95
96 // ----------------------------------------------------------------------------
97 // tc_device index
98
99 static int tc_device_compare(void* a, void* b) {
100         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
101         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
102         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
103 }
104
105 avl_tree tc_device_root_index = {
106                 NULL,
107                 tc_device_compare
108 };
109
110 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
111 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
112
113 static inline struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
114         struct tc_device tmp;
115         tmp.id = (char *)id;
116         tmp.hash = (hash)?hash:simple_hash(tmp.id);
117
118         return (struct tc_device *)avl_search(&(tc_device_root_index), (avl *)&tmp);
119 }
120
121
122 // ----------------------------------------------------------------------------
123 // tc_class index
124
125 static int tc_class_compare(void* a, void* b) {
126         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
127         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
128         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
129 }
130
131 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
132 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
133
134 static inline struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
135         struct tc_class tmp;
136         tmp.id = (char *)id;
137         tmp.hash = (hash)?hash:simple_hash(tmp.id);
138
139         return (struct tc_class *)avl_search(&(st->classes_index), (avl *) &tmp);
140 }
141
142 // ----------------------------------------------------------------------------
143
144 static inline void tc_class_free(struct tc_device *n, struct tc_class *c) {
145         debug(D_TC_LOOP, "Removing from device '%s' class '%s', parentid '%s', leafid '%s', seen=%d", n->id, c->id, c->parentid?c->parentid:"", c->leafid?c->leafid:"", c->seen);
146
147         if(c->next) c->next->prev = c->prev;
148         if(c->prev) c->prev->next = c->next;
149         if(n->classes == c) {
150                 if(c->next) n->classes = c->next;
151                 else n->classes = c->prev;
152         }
153
154         tc_class_index_del(n, c);
155
156         if(c->id) free(c->id);
157         if(c->name) free(c->name);
158         if(c->leafid) free(c->leafid);
159         if(c->parentid) free(c->parentid);
160
161         free(c);
162 }
163
164 static inline void tc_device_classes_cleanup(struct tc_device *d) {
165         static int cleanup_every = 999;
166
167         if(unlikely(cleanup_every > 0)) {
168                 cleanup_every = (int) config_get_number("plugin:tc", "cleanup unused classes every", 60);
169                 if(cleanup_every < 0) cleanup_every = -cleanup_every;
170         }
171
172         d->name_updated = 0;
173         d->family_updated = 0;
174
175         struct tc_class *c = d->classes;
176         while(c) {
177                 if(unlikely(cleanup_every > 0 && c->seen >= cleanup_every)) {
178                         struct tc_class *nc = c->next;
179                         tc_class_free(d, c);
180                         c = nc;
181                 }
182                 else {
183                         c->updated = 0;
184                         c->name_updated = 0;
185
186                         c = c->next;
187                 }
188         }
189 }
190
191 static inline void tc_device_commit(struct tc_device *d) {
192         static int enable_new_interfaces = -1;
193
194         if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
195
196         // we only need to add leaf classes
197         struct tc_class *c, *x;
198
199         // set all classes
200         for(c = d->classes ; c ; c = c->next) {
201                 c->isleaf = 1;
202                 c->hasparent = 0;
203         }
204
205         // mark the classes as leafs and parents
206         for(c = d->classes ; c ; c = c->next) {
207                 if(!c->updated) continue;
208
209                 for(x = d->classes ; x ; x = x->next) {
210                         if(!x->updated) continue;
211
212                         if(c == x) continue;
213
214                         if(x->parentid && (
215                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
216                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
217                                 // debug(D_TC_LOOP, "TC: In device '%s', class '%s' (leafid: '%s') has as leaf class '%s' (parentid: '%s').", d->name?d->name:d->id, c->name?c->name:c->id, c->leafid?c->leafid:c->id, x->name?x->name:x->id, x->parentid?x->parentid:x->id);
218                                 c->isleaf = 0;
219                                 x->hasparent = 1;
220                         }
221                 }
222         }
223
224         // debugging:
225         /*
226         for ( c = d->classes ; c ; c = c->next) {
227                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
228                 else debug(D_TC_LOOP, "TC: Device %s, class %s, IGNORE (isleaf: %d, hasparent: %d, parent: %s)", d->name, c->id, c->isleaf, c->hasparent, c->parentid);
229         }
230         */
231
232         // we need at least a class
233         for(c = d->classes ; c ; c = c->next) {
234                 // debug(D_TC_LOOP, "TC: Device '%s', class '%s', isLeaf=%d, HasParent=%d, Seen=%d", d->name?d->name:d->id, c->name?c->name:c->id, c->isleaf, c->hasparent, c->seen);
235                 if(!c->updated) continue;
236                 if(c->isleaf && c->hasparent) break;
237         }
238         if(!c) {
239                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
240                 tc_device_classes_cleanup(d);
241                 return;
242         }
243
244         if(unlikely(d->enabled == -1)) {
245                 char var_name[CONFIG_MAX_NAME + 1];
246                 snprintfz(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
247                 d-> enabled = config_get_boolean("plugin:tc", var_name, enable_new_interfaces);
248         }
249
250         if(likely(d->enabled)) {
251                 // --------------------------------------------------------------------
252                 // bytes
253
254                 if(unlikely(!d->st_bytes)) {
255                         d->st_bytes = rrdset_find_bytype(RRD_TYPE_TC, d->id);
256                         if(unlikely(!d->st_bytes)) {
257                                 debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
258                                 d->st_bytes = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, RRD_TYPE_TC ".qos", "Class Usage", "kilobits/s", 7000, rrd_update_every, RRDSET_TYPE_STACKED);
259                         }
260                 }
261                 else {
262                         debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
263                         rrdset_next_plugins(d->st_bytes);
264
265                         if(unlikely(d->name_updated && d->name && strcmp(d->id, d->name) != 0)) {
266                                 rrdset_set_name(d->st_bytes, d->name);
267                                 d->name_updated = 0;
268                         }
269
270                         // FIXME
271                         // update the family
272                 }
273
274                 for(c = d->classes ; c ; c = c->next) {
275                         if(unlikely(!c->updated)) continue;
276
277                         c->seen++;
278
279                         if(c->isleaf && c->hasparent) {
280                                 if(unlikely(!c->rd_bytes)) {
281                                         c->rd_bytes = rrddim_find(d->st_bytes, c->id);
282                                         if(unlikely(!c->rd_bytes)) {
283                                                 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_bytes->id, c->id, c->name);
284
285                                                 // new class, we have to add it
286                                                 c->rd_bytes = rrddim_add(d->st_bytes, c->id, c->name?c->name:c->id, 8, 1024, RRDDIM_INCREMENTAL);
287                                         }
288                                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_bytes->id, c->id);
289                                 }
290
291                                 rrddim_set_by_pointer(d->st_bytes, c->rd_bytes, c->bytes);
292
293                                 // if it has a name, different to the id
294                                 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
295                                         // update the rrd dimension with the new name
296                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_bytes->id, c->rd_bytes->id, c->name);
297                                         rrddim_set_name(d->st_bytes, c->rd_bytes, c->name);
298                                 }
299                         }
300                 }
301                 rrdset_done(d->st_bytes);
302
303                 // --------------------------------------------------------------------
304                 // packets
305                 
306                 if(unlikely(!d->st_packets)) {
307                         char id[RRD_ID_LENGTH_MAX + 1];
308                         char name[RRD_ID_LENGTH_MAX + 1];
309                         snprintfz(id, RRD_ID_LENGTH_MAX, "%s_packets", d->id);
310                         snprintfz(name, RRD_ID_LENGTH_MAX, "%s_packets", d->name?d->name:d->id);
311
312                         d->st_packets = rrdset_find_bytype(RRD_TYPE_TC, id);
313                         if(unlikely(!d->st_packets)) {
314                                 debug(D_TC_LOOP, "TC: Creating new _packets chart for device '%s'", d->name?d->name:d->id);
315                                 d->st_packets = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_packets", "Class Packets", "packets/s", 7010, rrd_update_every, RRDSET_TYPE_STACKED);
316                         }
317                 }
318                 else {
319                         debug(D_TC_LOOP, "TC: Updating _packets chart for device '%s'", d->name?d->name:d->id);
320                         rrdset_next_plugins(d->st_packets);
321
322                         // FIXME
323                         // update the family
324                 }
325
326                 for(c = d->classes ; c ; c = c->next) {
327                         if(unlikely(!c->updated)) continue;
328
329                         c->seen++;
330
331                         if(c->isleaf && c->hasparent) {
332                                 if(unlikely(!c->rd_packets)) {
333                                         c->rd_packets = rrddim_find(d->st_packets, c->id);
334                                         if(unlikely(!c->rd_packets)) {
335                                                 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_packets->id, c->id, c->name);
336
337                                                 // new class, we have to add it
338                                                 c->rd_packets = rrddim_add(d->st_packets, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
339                                         }
340                                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_packets->id, c->id);
341                                 }
342
343                                 rrddim_set_by_pointer(d->st_packets, c->rd_packets, c->packets);
344
345                                 // if it has a name, different to the id
346                                 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
347                                         // update the rrd dimension with the new name
348                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_packets->id, c->rd_packets->id, c->name);
349                                         rrddim_set_name(d->st_packets, c->rd_packets, c->name);
350                                 }
351                         }
352                 }
353                 rrdset_done(d->st_packets);
354
355                 // --------------------------------------------------------------------
356                 // dropped
357                 
358                 if(unlikely(!d->st_dropped)) {
359                         char id[RRD_ID_LENGTH_MAX + 1];
360                         char name[RRD_ID_LENGTH_MAX + 1];
361                         snprintfz(id, RRD_ID_LENGTH_MAX, "%s_dropped", d->id);
362                         snprintfz(name, RRD_ID_LENGTH_MAX, "%s_dropped", d->name?d->name:d->id);
363
364                         d->st_dropped = rrdset_find_bytype(RRD_TYPE_TC, id);
365                         if(unlikely(!d->st_dropped)) {
366                                 debug(D_TC_LOOP, "TC: Creating new _dropped chart for device '%s'", d->name?d->name:d->id);
367                                 d->st_dropped = rrdset_create(RRD_TYPE_TC, id, name, d->family?d->family:d->id, RRD_TYPE_TC ".qos_dropped", "Class Dropped Packets", "packets/s", 7020, rrd_update_every, RRDSET_TYPE_STACKED);
368                         }
369                 }
370                 else {
371                         debug(D_TC_LOOP, "TC: Updating _dropped chart for device '%s'", d->name?d->name:d->id);
372                         rrdset_next_plugins(d->st_dropped);
373
374                         // FIXME
375                         // update the family
376                 }
377
378                 for(c = d->classes ; c ; c = c->next) {
379                         if(unlikely(!c->updated)) continue;
380
381                         c->seen++;
382
383                         if(c->isleaf && c->hasparent) {
384                                 if(unlikely(!c->rd_dropped)) {
385                                         c->rd_dropped = rrddim_find(d->st_dropped, c->id);
386                                         if(unlikely(!c->rd_dropped)) {
387                                                 debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s' (name: '%s')", d->st_dropped->id, c->id, c->name);
388
389                                                 // new class, we have to add it
390                                                 c->rd_dropped = rrddim_add(d->st_dropped, c->id, c->name?c->name:c->id, 1, 1, RRDDIM_INCREMENTAL);
391                                         }
392                                         else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", d->st_dropped->id, c->id);
393                                 }
394
395                                 rrddim_set_by_pointer(d->st_dropped, c->rd_dropped, c->dropped);
396
397                                 // if it has a name, different to the id
398                                 if(unlikely(c->name_updated && c->name && strcmp(c->id, c->name) != 0)) {
399                                         // update the rrd dimension with the new name
400                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", d->st_dropped->id, c->rd_dropped->id, c->name);
401                                         rrddim_set_name(d->st_dropped, c->rd_dropped, c->name);
402                                 }
403                         }
404                 }
405                 rrdset_done(d->st_dropped);
406         }
407
408         tc_device_classes_cleanup(d);
409 }
410
411 static inline void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
412 {
413         struct tc_class *c = tc_class_index_find(d, id, 0);
414         if(likely(c)) {
415                 if(likely(c->name))
416                         free(c->name);
417
418                 c->name = NULL;
419
420                 if(likely(name && *name && strcmp(c->id, name) != 0)) {
421                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
422                         c->name = strdup(name);
423                         if(likely(c->name))
424                                 c->name_updated = 1;
425                 }
426         }
427 }
428
429 static inline void tc_device_set_device_name(struct tc_device *d, char *name) {
430         if(likely(d->name))
431                 free(d->name);
432
433         d->name = NULL;
434
435         if(likely(name && *name && strcmp(d->id, name) != 0)) {
436                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
437                 d->name = strdup(name);
438                 if(likely(d->name))
439                         d->name_updated = 1;
440         }
441 }
442
443 static inline void tc_device_set_device_family(struct tc_device *d, char *family) {
444         if(likely(d->family))
445                 free(d->family);
446
447         d->family = NULL;
448
449         if(likely(family && *family && strcmp(d->id, family) != 0)) {
450                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
451                 d->family = strdup(family);
452                 if(likely(d->family))
453                         d->family_updated = 1;
454         }
455         // no need for null termination - it is already null
456 }
457
458 static inline struct tc_device *tc_device_create(char *id)
459 {
460         struct tc_device *d = tc_device_index_find(id, 0);
461
462         if(!d) {
463                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
464
465                 d = calloc(1, sizeof(struct tc_device));
466                 if(!d) {
467                         fatal("Cannot allocate memory for tc_device %s", id);
468                         return NULL;
469                 }
470
471                 d->id = strdup(id);
472                 d->hash = simple_hash(d->id);
473                 d->enabled = -1;
474
475                 avl_init(&d->classes_index, tc_class_compare);
476                 tc_device_index_add(d);
477
478                 if(!tc_device_root) {
479                         tc_device_root = d;
480                 }
481                 else {
482                         d->next = tc_device_root;
483                         tc_device_root->prev = d;
484                         tc_device_root = d;
485                 }
486         }
487
488         return(d);
489 }
490
491 static inline struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
492 {
493         struct tc_class *c = tc_class_index_find(n, id, 0);
494
495         if(!c) {
496                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
497
498                 c = calloc(1, sizeof(struct tc_class));
499                 if(!c) {
500                         fatal("Cannot allocate memory for tc class");
501                         return NULL;
502                 }
503
504                 if(n->classes) n->classes->prev = c;
505                 c->next = n->classes;
506                 n->classes = c;
507
508                 c->id = strdup(id);
509                 if(!c->id) {
510                         free(c);
511                         return NULL;
512                 }
513                 c->hash = simple_hash(c->id);
514
515                 if(parentid && *parentid) {
516                         c->parentid = strdup(parentid);
517                         c->parent_hash = simple_hash(c->parentid);
518                 }
519
520                 if(leafid && *leafid) {
521                         c->leafid = strdup(leafid);
522                         c->leaf_hash = simple_hash(c->leafid);
523                 }
524
525                 tc_class_index_add(n, c);
526         }
527
528         c->seen = 1;
529
530         return(c);
531 }
532
533 static inline void tc_device_free(struct tc_device *n)
534 {
535         if(n->next) n->next->prev = n->prev;
536         if(n->prev) n->prev->next = n->next;
537         if(tc_device_root == n) {
538                 if(n->next) tc_device_root = n->next;
539                 else tc_device_root = n->prev;
540         }
541
542         tc_device_index_del(n);
543
544         while(n->classes) tc_class_free(n, n->classes);
545
546         if(n->id) free(n->id);
547         if(n->name) free(n->name);
548         if(n->family) free(n->family);
549
550         free(n);
551 }
552
553 static inline void tc_device_free_all()
554 {
555         while(tc_device_root)
556                 tc_device_free(tc_device_root);
557 }
558
559 #define MAX_WORDS 20
560
561 static inline int tc_space(char c) {
562         switch(c) {
563         case ' ':
564         case '\t':
565         case '\r':
566         case '\n':
567                 return 1;
568
569         default:
570                 return 0;
571         }
572 }
573
574 static inline void tc_split_words(char *str, char **words, int max_words) {
575         char *s = str;
576         int i = 0;
577
578         // skip all white space
579         while(tc_space(*s)) s++;
580
581         // store the first word
582         words[i++] = s;
583
584         // while we have something
585         while(*s) {
586                 // if it is a space
587                 if(unlikely(tc_space(*s))) {
588
589                         // terminate the word
590                         *s++ = '\0';
591
592                         // skip all white space
593                         while(tc_space(*s)) s++;
594
595                         // if we reached the end, stop
596                         if(!*s) break;
597
598                         // store the next word
599                         if(i < max_words) words[i++] = s;
600                         else break;
601                 }
602                 else s++;
603         }
604
605         // terminate the words
606         while(i < max_words) words[i++] = NULL;
607 }
608
609 pid_t tc_child_pid = 0;
610 void *tc_main(void *ptr)
611 {
612         if(ptr) { ; }
613
614         info("TC thread created with task id %d", gettid());
615
616         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
617                 error("Cannot set pthread cancel type to DEFERRED.");
618
619         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
620                 error("Cannot set pthread cancel state to ENABLE.");
621
622         struct rusage thread;
623         RRDSET *stcpu = NULL, *sttime = NULL;
624
625         char buffer[TC_LINE_MAX+1] = "";
626         char *words[MAX_WORDS] = { NULL };
627
628         uint32_t BEGIN_HASH = simple_hash("BEGIN");
629         uint32_t END_HASH = simple_hash("END");
630         uint32_t CLASS_HASH = simple_hash("class");
631         uint32_t SENT_HASH = simple_hash("Sent");
632         uint32_t LENDED_HASH = simple_hash("lended:");
633         uint32_t TOKENS_HASH = simple_hash("tokens:");
634         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
635         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
636         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
637         uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
638 #ifdef DETACH_PLUGINS_FROM_NETDATA
639         uint32_t MYPID_HASH = simple_hash("MYPID");
640 #endif
641         uint32_t first_hash;
642
643         for(;1;) {
644                 if(unlikely(netdata_exit)) break;
645
646                 FILE *fp;
647                 struct tc_device *device = NULL;
648                 struct tc_class *class = NULL;
649
650                 snprintfz(buffer, TC_LINE_MAX, "exec %s %d", config_get("plugin:tc", "script to run to get tc values", PLUGINS_DIR "/tc-qos-helper.sh"), rrd_update_every);
651                 debug(D_TC_LOOP, "executing '%s'", buffer);
652                 // fp = popen(buffer, "r");
653                 fp = mypopen(buffer, &tc_child_pid);
654                 if(!fp) {
655                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
656                         pthread_exit(NULL);
657                         return NULL;
658                 }
659
660                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
661                         if(unlikely(netdata_exit)) break;
662
663                         buffer[TC_LINE_MAX] = '\0';
664                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
665
666                         tc_split_words(buffer, words, MAX_WORDS);
667                         if(unlikely(!words[0] || !*words[0])) {
668                                 // debug(D_TC_LOOP, "empty line");
669                                 continue;
670                         }
671                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
672
673                         first_hash = simple_hash(words[0]);
674
675                         if(unlikely(device && first_hash == CLASS_HASH && strcmp(words[0], "class") == 0)) {
676                                 // debug(D_TC_LOOP, "CLASS line on class id='%s', parent='%s', parentid='%s', leaf='%s', leafid='%s'", words[2], words[3], words[4], words[5], words[6]);
677
678                                 // clear the last class
679                                 class = NULL;
680
681                                 // words[1] : class type
682                                 // words[2] : N:XX
683                                 // words[3] : parent or root
684                                 if(words[1] && words[2] && words[3] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
685                                         //char *type     = words[1];  // the class: htb, fq_codel, etc
686
687                                         // we are only interested for HTB classes
688                                         //if(strcmp(type, "htb") != 0) continue;
689
690                                         char *id       = words[2];      // the class major:minor
691                                         char *parent   = words[3];      // 'parent' or 'root'
692                                         char *parentid = words[4];      // the parent's id
693                                         char *leaf     = words[5];      // 'leaf'
694                                         char *leafid   = words[6];      // leafid
695
696                                         if(strcmp(parent, "root") == 0) {
697                                                 parentid = NULL;
698                                                 leafid = NULL;
699                                         }
700                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
701                                                 leafid = NULL;
702
703                                         char leafbuf[20 + 1] = "";
704                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
705                                                 strncpyz(leafbuf, leafid, 20 - 1);
706                                                 strcat(leafbuf, "1");
707                                                 leafid = leafbuf;
708                                         }
709
710                                         class = tc_class_add(device, id, parentid, leafid);
711                                 }
712                         }
713                         else if(unlikely(first_hash == END_HASH && strcmp(words[0], "END") == 0)) {
714                                 // debug(D_TC_LOOP, "END line");
715
716                                 if(device) {
717                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
718                                                 error("Cannot set pthread cancel state to DISABLE.");
719
720                                         tc_device_commit(device);
721                                         // tc_device_free(device);
722                                         device = NULL;
723                                         class = NULL;
724
725                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
726                                                 error("Cannot set pthread cancel state to ENABLE.");
727                                 }
728                         }
729                         else if(unlikely(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0)) {
730                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
731
732                                 if(device) {
733                                         // tc_device_free(device);
734                                         device = NULL;
735                                         class = NULL;
736                                 }
737
738                                 if(words[1] && *words[1]) {
739                                         device = tc_device_create(words[1]);
740                                         class = NULL;
741                                 }
742                         }
743                         else if(unlikely(device && class && first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0)) {
744                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
745                                 if(words[1] && *words[1]) {
746                                         class->bytes = strtoull(words[1], NULL, 10);
747                                         class->updated = 1;
748                                 }
749
750                                 if(words[3] && *words[3])
751                                         class->packets = strtoull(words[3], NULL, 10);
752
753                                 if(words[6] && *words[6])
754                                         class->dropped = strtoull(words[6], NULL, 10);
755
756                                 if(words[8] && *words[8])
757                                         class->overlimits = strtoull(words[8], NULL, 10);
758
759                                 if(words[10] && *words[10])
760                                         class->requeues = strtoull(words[8], NULL, 10);
761                         }
762                         else if(unlikely(device && class && class->updated && first_hash == LENDED_HASH && strcmp(words[0], "lended:") == 0)) {
763                                 // debug(D_TC_LOOP, "LENDED line '%s'", words[1]);
764                                 if(words[1] && *words[1])
765                                         class->lended = strtoull(words[1], NULL, 10);
766
767                                 if(words[3] && *words[3])
768                                         class->borrowed = strtoull(words[3], NULL, 10);
769
770                                 if(words[5] && *words[5])
771                                         class->giants = strtoull(words[5], NULL, 10);
772                         }
773                         else if(unlikely(device && class && class->updated && first_hash == TOKENS_HASH && strcmp(words[0], "tokens:") == 0)) {
774                                 // debug(D_TC_LOOP, "TOKENS line '%s'", words[1]);
775                                 if(words[1] && *words[1])
776                                         class->tokens = strtoull(words[1], NULL, 10);
777
778                                 if(words[3] && *words[3])
779                                         class->ctokens = strtoull(words[3], NULL, 10);
780                         }
781                         else if(unlikely(device && first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0)) {
782                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
783                                 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
784                         }
785                         else if(unlikely(device && first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0)) {
786                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
787                                 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
788                         }
789                         else if(unlikely(device && first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0)) {
790                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
791                                 char *id    = words[1];
792                                 char *path  = words[2];
793                                 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
794                         }
795                         else if(unlikely(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0)) {
796                                 // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
797                                 getrusage(RUSAGE_THREAD, &thread);
798
799                                 if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
800                                 if(!stcpu) {
801                                         stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "tc.helper", NULL, "NetData TC CPU usage", "milliseconds/s", 135000, rrd_update_every, RRDSET_TYPE_STACKED);
802                                         rrddim_add(stcpu, "user",  NULL,  1, 1000, RRDDIM_INCREMENTAL);
803                                         rrddim_add(stcpu, "system", NULL, 1, 1000, RRDDIM_INCREMENTAL);
804                                 }
805                                 else rrdset_next(stcpu);
806
807                                 rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
808                                 rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
809                                 rrdset_done(stcpu);
810
811                                 if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
812                                 if(!sttime) {
813                                         sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "tc.helper", NULL, "NetData TC script execution", "milliseconds/run", 135001, rrd_update_every, RRDSET_TYPE_AREA);
814                                         rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
815                                 }
816                                 else rrdset_next(sttime);
817
818                                 rrddim_set(sttime, "run_time", atoll(words[1]));
819                                 rrdset_done(sttime);
820
821                         }
822 #ifdef DETACH_PLUGINS_FROM_NETDATA
823                         else if(unlikely(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0))) {
824                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
825                                 char *id = words[1];
826                                 pid_t pid = atol(id);
827
828                                 if(pid) tc_child_pid = pid;
829
830                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
831                         }
832 #endif
833                         //else {
834                         //      debug(D_TC_LOOP, "IGNORED line");
835                         //}
836                 }
837                 // fgets() failed or loop broke
838                 int code = mypclose(fp, tc_child_pid);
839                 tc_child_pid = 0;
840
841                 if(device) {
842                         // tc_device_free(device);
843                         device = NULL;
844                         class = NULL;
845                 }
846
847                 if(netdata_exit) {
848                         tc_device_free_all();
849                         pthread_exit(NULL);
850                         return NULL;
851                 }
852
853                 if(code == 1 || code == 127) {
854                         // 1 = DISABLE
855                         // 127 = cannot even run it
856                         error("TC: tc-qos-helper.sh exited with code %d. Disabling it.", code);
857
858                         tc_device_free_all();
859                         pthread_exit(NULL);
860                         return NULL;
861                 }
862
863                 sleep((unsigned int) rrd_update_every);
864         }
865
866         pthread_exit(NULL);
867         return NULL;
868 }