]> arthur.barton.de Git - netdata.git/blob - src/plugin_tc.c
build: migrate to autotools
[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
9 #include "avl.h"
10 #include "log.h"
11 #include "common.h"
12 #include "appconfig.h"
13 #include "rrd.h"
14 #include "popen.h"
15 #include "plugin_tc.h"
16
17 #define RRD_TYPE_TC                                     "tc"
18 #define RRD_TYPE_TC_LEN                         strlen(RRD_TYPE_TC)
19
20 // ----------------------------------------------------------------------------
21 // /sbin/tc processor
22 // this requires the script plugins.d/tc-qos-helper.sh
23
24 #define TC_LINE_MAX 1024
25
26 struct tc_class {
27         avl avl;
28
29         char *id;
30         uint32_t hash;
31
32         char *name;
33
34         char *leafid;
35         uint32_t leaf_hash;
36
37         char *parentid;
38         uint32_t parent_hash;
39
40         char hasparent;
41         char isleaf;
42         unsigned long long bytes;
43
44         char updated;   // updated bytes
45         char seen;              // seen in the tc list (even without bytes)
46
47         struct tc_class *next;
48         struct tc_class *prev;
49 };
50
51 struct tc_device {
52         avl avl;
53
54         char *id;
55         uint32_t hash;
56
57         char *name;
58         char *family;
59
60         avl_tree classes_index;
61
62         struct tc_class *classes;
63 };
64
65
66 // ----------------------------------------------------------------------------
67 // tc_device index
68
69 static int tc_device_iterator(avl *a) { if(a) {}; return 0; }
70
71 static int tc_device_compare(void* a, void* b) {
72         if(((struct tc_device *)a)->hash < ((struct tc_device *)b)->hash) return -1;
73         else if(((struct tc_device *)a)->hash > ((struct tc_device *)b)->hash) return 1;
74         else return strcmp(((struct tc_device *)a)->id, ((struct tc_device *)b)->id);
75 }
76
77 avl_tree tc_device_root_index = {
78                 NULL,
79                 tc_device_compare
80 };
81
82 #define tc_device_index_add(st) avl_insert(&tc_device_root_index, (avl *)(st))
83 #define tc_device_index_del(st) avl_remove(&tc_device_root_index, (avl *)(st))
84
85 static struct tc_device *tc_device_index_find(const char *id, uint32_t hash) {
86         struct tc_device *result = NULL, tmp;
87         tmp.id = (char *)id;
88         tmp.hash = (hash)?hash:simple_hash(tmp.id);
89
90         avl_search(&(tc_device_root_index), (avl *)&tmp, tc_device_iterator, (avl **)&result);
91         return result;
92 }
93
94
95 // ----------------------------------------------------------------------------
96 // tc_class index
97
98 static int tc_class_iterator(avl *a) { if(a) {}; return 0; }
99
100 static int tc_class_compare(void* a, void* b) {
101         if(((struct tc_class *)a)->hash < ((struct tc_class *)b)->hash) return -1;
102         else if(((struct tc_class *)a)->hash > ((struct tc_class *)b)->hash) return 1;
103         else return strcmp(((struct tc_class *)a)->id, ((struct tc_class *)b)->id);
104 }
105
106 #define tc_class_index_add(st, rd) avl_insert(&((st)->classes_index), (avl *)(rd))
107 #define tc_class_index_del(st, rd) avl_remove(&((st)->classes_index), (avl *)(rd))
108
109 static struct tc_class *tc_class_index_find(struct tc_device *st, const char *id, uint32_t hash) {
110         struct tc_class *result = NULL, tmp;
111         tmp.id = (char *)id;
112         tmp.hash = (hash)?hash:simple_hash(tmp.id);
113
114         avl_search(&(st->classes_index), (avl *)&tmp, tc_class_iterator, (avl **)&result);
115         return result;
116 }
117
118 // ----------------------------------------------------------------------------
119
120 static void tc_class_free(struct tc_device *n, struct tc_class *c) {
121         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);
122
123         if(c->next) c->next->prev = c->prev;
124         if(c->prev) c->prev->next = c->next;
125         if(n->classes == c) n->classes = c->next;
126
127         tc_class_index_del(n, c);
128
129         if(c->id) free(c->id);
130         if(c->name) free(c->name);
131         if(c->leafid) free(c->leafid);
132         if(c->parentid) free(c->parentid);
133
134         free(c);
135 }
136
137 static void tc_device_classes_cleanup(struct tc_device *d) {
138         static int cleanup_every = 999;
139
140         if(cleanup_every > 0) {
141                 cleanup_every = -config_get_number("plugin:tc", "cleanup unused classes every", 60);
142                 if(cleanup_every > 0) cleanup_every = -cleanup_every;
143                 if(cleanup_every == 0) cleanup_every = -1;
144         }
145
146         struct tc_class *c = d->classes;
147         while(c) {
148                 if(c->seen < cleanup_every) {
149                         struct tc_class *nc = c->next;
150                         tc_class_free(d, c);
151                         c = nc;
152                 }
153                 else c = c->next;
154
155                 if(c) {
156                         c->updated = 0;
157                         c->seen--;
158                 }
159         }
160 }
161
162 static void tc_device_commit(struct tc_device *d)
163 {
164         static int enable_new_interfaces = -1;
165
166         if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean("plugin:tc", "enable new interfaces detected at runtime", 1);
167         
168         // we only need to add leaf classes
169         struct tc_class *c, *x;
170
171         // set all classes
172         for(c = d->classes ; c ; c = c->next) {
173                 c->isleaf = 1;
174                 c->hasparent = 0;
175         }
176
177         // mark the classes as leafs and parents
178         for(c = d->classes ; c ; c = c->next) {
179                 if(!c->updated) continue;
180
181                 for(x = d->classes ; x ; x = x->next) {
182                         if(!x->updated) continue;
183
184                         if(c == x) continue;
185
186                         if(x->parentid && (
187                                 (               c->hash      == x->parent_hash && strcmp(c->id,     x->parentid) == 0) ||
188                                 (c->leafid   && c->leaf_hash == x->parent_hash && strcmp(c->leafid, x->parentid) == 0))) {
189                                 // 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);
190                                 c->isleaf = 0;
191                                 x->hasparent = 1;
192                         }
193                 }
194         }
195         
196         // debugging:
197         /*
198         for ( c = d->classes ; c ; c = c->next) {
199                 if(c->isleaf && c->hasparent) debug(D_TC_LOOP, "TC: Device %s, class %s, OK", d->name, c->id);
200                 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);
201         }
202         */
203
204         // we need at least a class
205         for(c = d->classes ; c ; c = c->next) {
206                 // 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);
207                 if(!c->updated) continue;
208                 if(c->isleaf && c->hasparent) break;
209         }
210         if(!c) {
211                 debug(D_TC_LOOP, "TC: Ignoring TC device '%s'. No leaf classes.", d->name?d->name:d->id);
212                 tc_device_classes_cleanup(d);
213                 return;
214         }
215
216         char var_name[CONFIG_MAX_NAME + 1];
217         snprintf(var_name, CONFIG_MAX_NAME, "qos for %s", d->id);
218         if(config_get_boolean("plugin:tc", var_name, enable_new_interfaces)) {
219                 RRDSET *st = rrdset_find_bytype(RRD_TYPE_TC, d->id);
220                 if(!st) {
221                         debug(D_TC_LOOP, "TC: Creating new chart for device '%s'", d->name?d->name:d->id);
222
223                         st = rrdset_create(RRD_TYPE_TC, d->id, d->name?d->name:d->id, d->family?d->family:d->id, "Class Usage", "kilobits/s", 1000, rrd_update_every, RRDSET_TYPE_STACKED);
224
225                         for(c = d->classes ; c ; c = c->next) {
226                                 if(!c->updated) continue;
227
228                                 if(c->isleaf && c->hasparent)
229                                         rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
230                         }
231                 }
232                 else {
233                         debug(D_TC_LOOP, "TC: Updating chart for device '%s'", d->name?d->name:d->id);
234                         rrdset_next_plugins(st);
235
236                         if(d->name && strcmp(d->id, d->name) != 0) rrdset_set_name(st, d->name);
237                 }
238
239                 for(c = d->classes ; c ; c = c->next) {
240                         if(!c->updated) continue;
241
242                         if(c->isleaf && c->hasparent) {
243                                 RRDDIM *rd = rrddim_find(st, c->id);
244
245                                 if(!rd) {
246                                         debug(D_TC_LOOP, "TC: Adding to chart '%s', dimension '%s'", st->id, c->id, c->name);
247                                         
248                                         // new class, we have to add it
249                                         rd = rrddim_add(st, c->id, c->name?c->name:c->id, 8, 1024 * rrd_update_every, RRDDIM_INCREMENTAL);
250                                 }
251                                 else debug(D_TC_LOOP, "TC: Updating chart '%s', dimension '%s'", st->id, c->id);
252
253                                 rrddim_set_by_pointer(st, rd, c->bytes);
254
255                                 // if it has a name, different to the id
256                                 if(c->name) {
257                                         // update the rrd dimension with the new name
258                                         debug(D_TC_LOOP, "TC: Setting chart '%s', dimension '%s' name to '%s'", st->id, rd->id, c->name);
259                                         rrddim_set_name(st, rd, c->name);
260
261                                         free(c->name);
262                                         c->name = NULL;
263                                 }
264                         }
265                 }
266                 rrdset_done(st);
267         }
268
269         tc_device_classes_cleanup(d);
270 }
271
272 static void tc_device_set_class_name(struct tc_device *d, char *id, char *name)
273 {
274         struct tc_class *c = tc_class_index_find(d, id, 0);
275         if(c) {
276                 if(c->name) free(c->name);
277                 c->name = NULL;
278
279                 if(name && *name && strcmp(c->id, name) != 0) {
280                         debug(D_TC_LOOP, "TC: Setting device '%s', class '%s' name to '%s'", d->id, id, name);
281                         c->name = strdup(name);
282                 }
283         }
284 }
285
286 static void tc_device_set_device_name(struct tc_device *d, char *name) {
287         if(d->name) free(d->name);
288         d->name = NULL;
289
290         if(name && *name && strcmp(d->id, name) != 0) {
291                 debug(D_TC_LOOP, "TC: Setting device '%s' name to '%s'", d->id, name);
292                 d->name = strdup(name);
293         }
294 }
295
296 static void tc_device_set_device_family(struct tc_device *d, char *family) {
297         if(d->family) free(d->family);
298         d->family = NULL;
299
300         if(family && *family && strcmp(d->id, family) != 0) {
301                 debug(D_TC_LOOP, "TC: Setting device '%s' family to '%s'", d->id, family);
302                 d->family = strdup(family);
303         }
304         // no need for null termination - it is already null
305 }
306
307 static struct tc_device *tc_device_create(char *id)
308 {
309         struct tc_device *d = tc_device_index_find(id, 0);
310
311         if(!d) {
312                 debug(D_TC_LOOP, "TC: Creating device '%s'", id);
313
314                 d = calloc(1, sizeof(struct tc_device));
315                 if(!d) {
316                         fatal("Cannot allocate memory for tc_device %s", id);
317                         return NULL;
318                 }
319
320                 d->id = strdup(id);
321                 d->hash = simple_hash(d->id);
322
323                 d->classes_index.root = NULL;
324                 d->classes_index.compar = tc_class_compare;
325
326                 tc_device_index_add(d);
327         }
328
329         return(d);
330 }
331
332 static struct tc_class *tc_class_add(struct tc_device *n, char *id, char *parentid, char *leafid)
333 {
334         struct tc_class *c = tc_class_index_find(n, id, 0);
335
336         if(!c) {
337                 debug(D_TC_LOOP, "TC: Creating in device '%s', class id '%s', parentid '%s', leafid '%s'", n->id, id, parentid?parentid:"", leafid?leafid:"");
338
339                 c = calloc(1, sizeof(struct tc_class));
340                 if(!c) {
341                         fatal("Cannot allocate memory for tc class");
342                         return NULL;
343                 }
344
345                 if(n->classes) n->classes->prev = c;
346                 c->next = n->classes;
347                 n->classes = c;
348
349                 c->id = strdup(id);
350                 if(!c->id) {
351                         free(c);
352                         return NULL;
353                 }
354                 c->hash = simple_hash(c->id);
355
356                 if(parentid && *parentid) {
357                         c->parentid = strdup(parentid);
358                         c->parent_hash = simple_hash(c->parentid);
359                 }
360
361                 if(leafid && *leafid) {
362                         c->leafid = strdup(leafid);
363                         c->leaf_hash = simple_hash(c->leafid);
364                 }
365
366                 tc_class_index_add(n, c);
367         }
368
369         c->seen = 1;
370
371         return(c);
372 }
373 /*
374 static void tc_device_free(struct tc_device *n)
375 {
376         tc_device_index_del(n);
377
378         while(n->classes) tc_class_free(n, n->classes);
379
380         if(n->id) free(n->id);
381         if(n->name) free(n->name);
382         if(n->family) free(n->family);
383
384         free(n);
385 }
386 */
387
388 #define MAX_WORDS 20
389
390 static inline int tc_space(char c) {
391         switch(c) {
392         case ' ':
393         case '\t':
394         case '\r':
395         case '\n':
396                 return 1;
397
398         default:
399                 return 0;
400         }
401 }
402
403 static void tc_split_words(char *str, char **words, int max_words) {
404         char *s = str;
405         int i = 0;
406
407         // skip all white space
408         while(tc_space(*s)) s++;
409
410         // store the first word
411         words[i++] = s;
412
413         // while we have something
414         while(*s) {
415                 // if it is a space
416                 if(tc_space(*s)) {
417
418                         // terminate the word
419                         *s++ = '\0';
420
421                         // skip all white space
422                         while(tc_space(*s)) s++;
423
424                         // if we reached the end, stop
425                         if(!*s) break;
426
427                         // store the next word
428                         if(i < max_words) words[i++] = s;
429                         else break;
430                 }
431                 else s++;
432         }
433
434         // terminate the words
435         while(i < max_words) words[i++] = NULL;
436 }
437
438 pid_t tc_child_pid = 0;
439 void *tc_main(void *ptr)
440 {
441         if(ptr) { ; }
442
443         info("TC thread created with task id %d", gettid());
444
445         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
446                 error("Cannot set pthread cancel type to DEFERRED.");
447
448         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
449                 error("Cannot set pthread cancel state to ENABLE.");
450
451         char buffer[TC_LINE_MAX+1] = "";
452         char *words[MAX_WORDS] = { NULL };
453
454         uint32_t BEGIN_HASH = simple_hash("BEGIN");
455         uint32_t END_HASH = simple_hash("END");
456         uint32_t CLASS_HASH = simple_hash("class");
457         uint32_t SENT_HASH = simple_hash("Sent");
458         uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
459         uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
460         uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
461 #ifdef DETACH_PLUGINS_FROM_NETDATA
462         uint32_t MYPID_HASH = simple_hash("MYPID");
463 #endif
464         uint32_t first_hash;
465
466         for(;1;) {
467                 FILE *fp;
468                 struct tc_device *device = NULL;
469                 struct tc_class *class = NULL;
470
471                 snprintf(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);
472                 debug(D_TC_LOOP, "executing '%s'", buffer);
473                 // fp = popen(buffer, "r");
474                 fp = mypopen(buffer, &tc_child_pid);
475                 if(!fp) {
476                         error("TC: Cannot popen(\"%s\", \"r\").", buffer);
477                         return NULL;
478                 }
479
480                 while(fgets(buffer, TC_LINE_MAX, fp) != NULL) {
481                         buffer[TC_LINE_MAX] = '\0';
482                         // debug(D_TC_LOOP, "TC: read '%s'", buffer);
483
484                         tc_split_words(buffer, words, MAX_WORDS);
485                         if(!words[0] || !*words[0]) {
486                                 // debug(D_TC_LOOP, "empty line");
487                                 continue;
488                         }
489                         // else debug(D_TC_LOOP, "First word is '%s'", words[0]);
490
491                         first_hash = simple_hash(words[0]);
492
493                         if(first_hash == CLASS_HASH && strcmp(words[0], "class") == 0 && device) {
494                                 // 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]);
495
496                                 if(words[1] && words[2] && words[3] && words[4] && (strcmp(words[3], "parent") == 0 || strcmp(words[3], "root") == 0)) {
497                                         // char *type     = words[1];  // the class: htb, fq_codel, etc
498                                         char *id       = words[2];      // the class major:minor
499                                         char *parent   = words[3];      // 'parent' or 'root'
500                                         char *parentid = words[4];      // the parent's id
501                                         char *leaf     = words[5];      // 'leaf'
502                                         char *leafid   = words[6];      // leafid
503
504                                         if(strcmp(parent, "root") == 0) {
505                                                 parentid = NULL;
506                                                 leafid = NULL;
507                                         }
508                                         else if(!leaf || strcmp(leaf, "leaf") != 0)
509                                                 leafid = NULL;
510
511                                         char leafbuf[20 + 1] = "";
512                                         if(leafid && leafid[strlen(leafid) - 1] == ':') {
513                                                 strncpy(leafbuf, leafid, 20 - 1);
514                                                 strcat(leafbuf, "1");
515                                                 leafid = leafbuf;
516                                         }
517
518                                         class = tc_class_add(device, id, parentid, leafid);
519                                 }
520                         }
521                         else if(first_hash == END_HASH && strcmp(words[0], "END") == 0) {
522                                 // debug(D_TC_LOOP, "END line");
523
524                                 if(device) {
525                                         if(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL) != 0)
526                                                 error("Cannot set pthread cancel state to DISABLE.");
527
528                                         tc_device_commit(device);
529                                         // tc_device_free(device);
530                                         device = NULL;
531                                         class = NULL;
532
533                                         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
534                                                 error("Cannot set pthread cancel state to ENABLE.");
535                                 }
536                         }
537                         else if(first_hash == BEGIN_HASH && strcmp(words[0], "BEGIN") == 0) {
538                                 // debug(D_TC_LOOP, "BEGIN line on device '%s'", words[1]);
539
540                                 if(device) {
541                                         // tc_device_free(device);
542                                         device = NULL;
543                                         class = NULL;
544                                 }
545
546                                 if(words[1] && *words[1]) {
547                                         device = tc_device_create(words[1]);
548                                         class = NULL;
549                                 }
550                         }
551                         else if(first_hash == SENT_HASH && strcmp(words[0], "Sent") == 0 && device && class) {
552                                 // debug(D_TC_LOOP, "SENT line '%s'", words[1]);
553                                 if(words[1] && *words[1]) {
554                                         class->bytes = atoll(words[1]);
555                                         class->updated = 1;
556                                 }
557                                 else class->bytes = 0;
558                         }
559                         else if(first_hash == SETDEVICENAME_HASH && strcmp(words[0], "SETDEVICENAME") == 0 && device) {
560                                 // debug(D_TC_LOOP, "SETDEVICENAME line '%s'", words[1]);
561                                 if(words[1] && *words[1]) tc_device_set_device_name(device, words[1]);
562                         }
563                         else if(first_hash == SETDEVICEGROUP_HASH && strcmp(words[0], "SETDEVICEGROUP") == 0 && device) {
564                                 // debug(D_TC_LOOP, "SETDEVICEGROUP line '%s'", words[1]);
565                                 if(words[1] && *words[1]) tc_device_set_device_family(device, words[1]);
566                         }
567                         else if(first_hash == SETCLASSNAME_HASH && strcmp(words[0], "SETCLASSNAME") == 0 && device) {
568                                 // debug(D_TC_LOOP, "SETCLASSNAME line '%s' '%s'", words[1], words[2]);
569                                 char *id    = words[1];
570                                 char *path  = words[2];
571                                 if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
572                         }
573 #ifdef DETACH_PLUGINS_FROM_NETDATA
574                         else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
575                                 // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);
576                                 char *id = words[1];
577                                 pid_t pid = atol(id);
578
579                                 if(pid) tc_child_pid = pid;
580
581                                 debug(D_TC_LOOP, "TC: Child PID is %d.", tc_child_pid);
582                         }
583 #endif
584                         //else {
585                         //      debug(D_TC_LOOP, "IGNORED line");
586                         //}
587                 }
588                 mypclose(fp, tc_child_pid);
589                 tc_child_pid = 0;
590
591                 if(device) {
592                         // tc_device_free(device);
593                         device = NULL;
594                         class = NULL;
595                 }
596
597                 sleep(rrd_update_every);
598         }
599
600         return NULL;
601 }
602