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