summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/plugins/in_node_exporter_metrics/ne.c
blob: d7781795788b9a31849bcb7b89b17a3f69b6c982 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*  Fluent Bit
 *  ==========
 *  Copyright (C) 2015-2022 The Fluent Bit Authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fluent-bit/flb_input_plugin.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_config_map.h>
#include <fluent-bit/flb_error.h>
#include <fluent-bit/flb_pack.h>

#include "ne.h"
#include "ne_config.h"
#include "ne_filefd_linux.h"

/* collectors */
#include "ne_cpu.h"
#include "ne_cpufreq.h"
#include "ne_meminfo.h"
#include "ne_diskstats.h"
#include "ne_filesystem.h"
#include "ne_uname.h"
#include "ne_stat_linux.h"
#include "ne_time.h"
#include "ne_loadavg.h"
#include "ne_vmstat_linux.h"
#include "ne_netdev.h"
#include "ne_textfile.h"
#include "ne_systemd.h"

static int ne_timer_cpu_metrics_cb(struct flb_input_instance *ins,
                                   struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_cpu_update(ctx);

    return 0;
}

static int ne_timer_cpufreq_metrics_cb(struct flb_input_instance *ins,
                                       struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_cpufreq_update(ctx);

    return 0;
}

static int ne_timer_meminfo_metrics_cb(struct flb_input_instance *ins,
                                       struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_meminfo_update(ctx);

    return 0;
}

static int ne_timer_diskstats_metrics_cb(struct flb_input_instance *ins,
                                         struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_diskstats_update(ctx);

    return 0;
}

static int ne_timer_filesystem_metrics_cb(struct flb_input_instance *ins,
                                          struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_filesystem_update(ctx);

    return 0;
}

static int ne_timer_uname_metrics_cb(struct flb_input_instance *ins,
                                     struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_uname_update(ctx);

    return 0;
}

static int ne_timer_stat_metrics_cb(struct flb_input_instance *ins,
                                    struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_stat_update(ctx);

    return 0;
}

static int ne_timer_time_metrics_cb(struct flb_input_instance *ins,
                                    struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_time_update(ctx);

    return 0;
}

static int ne_timer_loadavg_metrics_cb(struct flb_input_instance *ins,
                                       struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_loadavg_update(ctx);

    return 0;
}

static int ne_timer_vmstat_metrics_cb(struct flb_input_instance *ins,
                                      struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_vmstat_update(ctx);

    return 0;
}

static int ne_timer_netdev_metrics_cb(struct flb_input_instance *ins,
                                      struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_netdev_update(ctx);

    return 0;
}

static int ne_timer_filefd_metrics_cb(struct flb_input_instance *ins,
                                      struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_filefd_update(ctx);

    return 0;
}

static int ne_timer_textfile_metrics_cb(struct flb_input_instance *ins,
                                        struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_textfile_update(ctx);

    return 0;
}

static int ne_timer_systemd_metrics_cb(struct flb_input_instance *ins,
                                       struct flb_config *config, void *in_context)
{
    struct flb_ne *ctx = in_context;

    ne_systemd_update(ctx);

    return 0;
}

struct flb_ne_callback {
    char *name;
    void (*func)(char *, void *, void *);
};

static int ne_update_cb(struct flb_ne *ctx, char *name);

static void update_metrics(struct flb_input_instance *ins, struct flb_ne *ctx)
{
    int ret;
    struct mk_list *head;
    struct flb_slist_entry *entry;

    /* Update our metrics */
    if (ctx->metrics) {
        mk_list_foreach(head, ctx->metrics) {
            entry = mk_list_entry(head, struct flb_slist_entry, _head);
            ret = flb_callback_exists(ctx->callback, entry->str);
            if (ret == FLB_TRUE) {
                ne_update_cb(ctx, entry->str);
            }
            else {
                flb_plg_debug(ctx->ins, "Callback for metrics '%s' is not registered", entry->str);
            }
        }
    }
}

/*
 * Update the metrics, this function is invoked every time 'scrape_interval'
 * expires.
 */
static int cb_ne_collect(struct flb_input_instance *ins,
                         struct flb_config *config, void *in_context)
{
    int ret;
    struct flb_ne *ctx = in_context;

    update_metrics(ins, ctx);

    /* Append the updated metrics */
    ret = flb_input_metrics_append(ins, NULL, 0, ctx->cmt);
    if (ret != 0) {
        flb_plg_error(ins, "could not append metrics");
    }

    return 0;
}

static void ne_cpu_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_cpu_update(ctx);
}

static void ne_cpufreq_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_cpufreq_update(ctx);
}

static void ne_meminfo_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_meminfo_update(ctx);
}

static void ne_diskstats_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_diskstats_update(ctx);
}

static void ne_filesystem_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_filesystem_update(ctx);
}

static void ne_uname_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_uname_update(ctx);
}

static void ne_stat_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_stat_update(ctx);
}

static void ne_time_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_time_update(ctx);
}

static void ne_loadavg_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_loadavg_update(ctx);
}

static void ne_vmstat_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_vmstat_update(ctx);
}

static void ne_netdev_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_netdev_update(ctx);
}

static void ne_filefd_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_filefd_update(ctx);
}

static void ne_textfile_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_textfile_update(ctx);
}

static void ne_systemd_update_cb(char *name, void *p1, void *p2)
{
    struct flb_ne *ctx = p1;

    ne_systemd_update(ctx);
}

static int ne_update_cb(struct flb_ne *ctx, char *name)
{
    int ret;

    ret = flb_callback_do(ctx->callback, name, ctx, NULL);
    return ret;
}

/*
 * Callbacks Table
 */
struct flb_ne_callback ne_callbacks[] = {
    /* metrics */
    { "cpufreq", ne_cpufreq_update_cb },
    { "cpu", ne_cpu_update_cb },
    { "meminfo", ne_meminfo_update_cb },
    { "diskstats", ne_diskstats_update_cb },
    { "filesystem", ne_filesystem_update_cb },
    { "uname", ne_uname_update_cb },
    { "stat", ne_stat_update_cb },
    { "time", ne_time_update_cb },
    { "loadavg", ne_loadavg_update_cb },
    { "vmstat", ne_vmstat_update_cb },
    { "netdev", ne_netdev_update_cb },
    { "filefd", ne_filefd_update_cb },
    { "textfile", ne_textfile_update_cb },
    { "systemd", ne_systemd_update_cb },
    { 0 }
};

static int in_ne_init(struct flb_input_instance *in,
                      struct flb_config *config, void *data)
{
    int ret;
    int metric_idx = -1;
    struct flb_ne *ctx;
    struct mk_list *head;
    struct flb_slist_entry *entry;
    struct flb_ne_callback *cb;

    /* Create plugin context */
    ctx = flb_ne_config_create(in, config);
    if (!ctx) {
        flb_errno();
        return -1;
    }

    /* Initialize fds */
    ctx->coll_fd = -1;
    ctx->coll_cpu_fd = -1;
    ctx->coll_cpufreq_fd = -1;
    ctx->coll_meminfo_fd = -1;
    ctx->coll_diskstats_fd = -1;
    ctx->coll_filesystem_fd = -1;
    ctx->coll_uname_fd = -1;
    ctx->coll_stat_fd = -1;
    ctx->coll_time_fd = -1;
    ctx->coll_loadavg_fd = -1;
    ctx->coll_vmstat_fd = -1;
    ctx->coll_netdev_fd = -1;
    ctx->coll_filefd_fd = -1;
    ctx->coll_textfile_fd = -1;
    ctx->coll_systemd_fd = -1;

    ctx->callback = flb_callback_create(in->name);
    if (!ctx->callback) {
        flb_plg_error(ctx->ins, "Create callback failed");
        return -1;
    }

    /* Associate context with the instance */
    flb_input_set_context(in, ctx);

    /* Create the collector */
    ret = flb_input_set_collector_time(in,
                                       cb_ne_collect,
                                       ctx->scrape_interval, 0,
                                       config);
    if (ret == -1) {
        flb_plg_error(ctx->ins,
                      "could not set collector for Node Exporter Metrics plugin");
        return -1;
    }
    ctx->coll_fd = ret;

    /* Check and initialize enabled metrics */
    if (ctx->metrics) {
        mk_list_foreach(head, ctx->metrics) {
            entry = mk_list_entry(head, struct flb_slist_entry, _head);
            ret = flb_callback_exists(ctx->callback, entry->str);

            if (ret == FLB_FALSE) {
                if (strncmp(entry->str, "cpufreq", 7) == 0) {
                    if (ctx->cpu_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 0;
                    }
                    else if (ctx->cpufreq_scrape_interval > 0) {
                        /* Create the cpufreq collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_cpufreq_metrics_cb,
                                                           ctx->cpufreq_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set cpufreq collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_cpufreq_fd = ret;
                    }
                    ne_cpufreq_init(ctx);
                }
                else if (strncmp(entry->str, "cpu", 3) == 0) {
                    if (ctx->cpufreq_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 1;
                    }
                    else if (ctx->cpu_scrape_interval > 0) {
                        /* Create the cpu collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_cpu_metrics_cb,
                                                           ctx->cpu_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set cpu collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_cpu_fd = ret;
                    }
                    ne_cpu_init(ctx);
                }
                else if (strncmp(entry->str, "meminfo", 7) == 0) {
                    if (ctx->meminfo_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 2;
                    }
                    else if (ctx->meminfo_scrape_interval > 0) {
                        /* Create the meminfo collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_meminfo_metrics_cb,
                                                           ctx->meminfo_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set meminfo collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_meminfo_fd = ret;
                    }
                    ne_meminfo_init(ctx);
                }
                else if (strncmp(entry->str, "diskstats", 9) == 0) {
                    if (ctx->diskstats_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 3;
                    }
                    else if (ctx->diskstats_scrape_interval > 0) {
                        /* Create the diskstats collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_diskstats_metrics_cb,
                                                           ctx->diskstats_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set diskstats collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_diskstats_fd = ret;
                    }
                    ne_diskstats_init(ctx);
                }
                else if (strncmp(entry->str, "filesystem", 10) == 0) {
                    if (ctx->diskstats_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 4;
                    }
                    else if (ctx->filesystem_scrape_interval > 0) {
                        /* Create the diskstats collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_filesystem_metrics_cb,
                                                           ctx->filesystem_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set filesystem collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_filesystem_fd = ret;
                    }
                    ne_filesystem_init(ctx);
                }
                else if (strncmp(entry->str, "uname", 5) == 0) {
                    if (ctx->uname_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 5;
                    }
                    else if (ctx->uname_scrape_interval > 0) {
                        /* Create the uname collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_uname_metrics_cb,
                                                           ctx->uname_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set uname collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_uname_fd = ret;
                    }
                    ne_uname_init(ctx);
                }
                else if (strncmp(entry->str, "stat", 4) == 0) {
                    if (ctx->stat_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 6;
                    }
                    else if (ctx->stat_scrape_interval > 0) {
                        /* Create the meminfo collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_stat_metrics_cb,
                                                           ctx->stat_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set meminfo collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_stat_fd = ret;
                    }
                    ne_stat_init(ctx);
                }
                else if (strncmp(entry->str, "time", 4) == 0) {
                    if (ctx->time_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 7;
                    }
                    else if (ctx->time_scrape_interval > 0) {
                        /* Create the time collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_time_metrics_cb,
                                                           ctx->time_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set time collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_time_fd = ret;
                    }
                    ne_time_init(ctx);
                }
                else if (strncmp(entry->str, "loadavg", 7) == 0) {
                    if (ctx->loadavg_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 8;
                    }
                    else if (ctx->loadavg_scrape_interval > 0) {
                        /* Create the loadavg collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_loadavg_metrics_cb,
                                                           ctx->loadavg_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set loadavg collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_loadavg_fd = ret;
                    }
                    ne_loadavg_init(ctx);
                }
                else if (strncmp(entry->str, "vmstat", 6) == 0) {
                    if (ctx->vmstat_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 9;
                    }
                    else if (ctx->vmstat_scrape_interval > 0) {
                        /* Create the vmstat collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_vmstat_metrics_cb,
                                                           ctx->vmstat_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set vmstat collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_vmstat_fd = ret;
                    }
                    ne_vmstat_init(ctx);
                }
                else if (strncmp(entry->str, "netdev", 6) == 0) {
                    if (ctx->netdev_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 10;
                    }
                    else if (ctx->netdev_scrape_interval > 0) {
                        /* Create the netdev collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_netdev_metrics_cb,
                                                           ctx->netdev_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set netdev collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_netdev_fd = ret;
                    }
                    ne_netdev_init(ctx);
                }
                else if (strncmp(entry->str, "filefd", 6) == 0) {
                    if (ctx->filefd_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 11;
                    }
                    else if (ctx->filefd_scrape_interval > 0) {
                        /* Create the filefd collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_filefd_metrics_cb,
                                                           ctx->filefd_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set filefd collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_filefd_fd = ret;
                    }
                    ne_filefd_init(ctx);
                }
                else if (strncmp(entry->str, "textfile", 8) == 0) {
                    if (ctx->textfile_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 12;
                    }
                    else if (ctx->textfile_scrape_interval > 0) {
                        /* Create the filefd collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_textfile_metrics_cb,
                                                           ctx->textfile_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set textfile collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_textfile_fd = ret;
                    }
                    ne_textfile_init(ctx);
                }
                else if (strncmp(entry->str, "systemd", 7) == 0) {
                    if (ctx->systemd_scrape_interval == 0) {
                        flb_plg_debug(ctx->ins, "enabled metrics %s", entry->str);
                        metric_idx = 13;
                    }
                    else if (ctx->systemd_scrape_interval > 0) {
                        /* Create the filefd collector */
                        ret = flb_input_set_collector_time(in,
                                                           ne_timer_systemd_metrics_cb,
                                                           ctx->systemd_scrape_interval, 0,
                                                           config);
                        if (ret == -1) {
                            flb_plg_error(ctx->ins,
                                          "could not set systemd collector for Node Exporter Metrics plugin");
                            return -1;
                        }
                        ctx->coll_systemd_fd = ret;
                    }
                    ne_systemd_init(ctx);
                }
                else {
                    flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
                    metric_idx = -1;
                }

                if (metric_idx >= 0) {
                    cb = &ne_callbacks[metric_idx];
                    ret = flb_callback_set(ctx->callback, cb->name, cb->func);
                    if (ret == -1) {
                        flb_plg_error(ctx->ins, "error setting up default "
                                      "callback '%s'", cb->name);
                    }
                }
            }
        }
    }
    else {
        flb_plg_error(ctx->ins, "No metrics is specified");

        return -1;
    }

    return 0;
}

static int in_ne_exit(void *data, struct flb_config *config)
{
    int ret;
    struct flb_ne *ctx = data;
    struct mk_list *head;
    struct flb_slist_entry *entry;

    if (!ctx) {
        return 0;
    }

    /* Teardown for callback tied up resources */
    if (ctx->metrics) {
        mk_list_foreach(head, ctx->metrics) {
            entry = mk_list_entry(head, struct flb_slist_entry, _head);
            ret = flb_callback_exists(ctx->callback, entry->str);

            if (ret == FLB_TRUE) {
                if (strncmp(entry->str, "cpufreq", 7) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "cpu", 3) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "meminfo", 7) == 0) {
                    ne_meminfo_exit(ctx);
                }
                else if (strncmp(entry->str, "diskstats", 9) == 0) {
                    ne_diskstats_exit(ctx);
                }
                else if (strncmp(entry->str, "filesystem", 10) == 0) {
                    ne_filesystem_exit(ctx);
                }
                else if (strncmp(entry->str, "uname", 5) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "stat", 4) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "time", 4) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "loadavg", 7) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "vmstat", 6) == 0) {
                    ne_vmstat_exit(ctx);
                }
                else if (strncmp(entry->str, "netdev", 6) == 0) {
                    ne_netdev_exit(ctx);
                }
                else if (strncmp(entry->str, "filefd", 6) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "textfile", 8) == 0) {
                    /* nop */
                }
                else if (strncmp(entry->str, "systemd", 7) == 0) {
                    ne_systemd_exit(ctx);
                }
                else {
                    flb_plg_warn(ctx->ins, "Unknown metrics: %s", entry->str);
                }
            }
        }
    }

    /* destroy callback context */
    if (ctx->callback) {
        flb_callback_destroy(ctx->callback);
    }

    /* Teardown for timer tied up resources */
    if (ctx->coll_meminfo_fd != -1) {
        ne_meminfo_exit(ctx);
    }
    if (ctx->coll_diskstats_fd != -1) {
        ne_diskstats_exit(ctx);
    }
    if (ctx->coll_filesystem_fd != -1) {
        ne_filesystem_exit(ctx);
    }
    if (ctx->coll_vmstat_fd != -1) {
        ne_vmstat_exit(ctx);
    }
    if (ctx->coll_netdev_fd != -1) {
        ne_netdev_exit(ctx);
    }
    if (ctx->coll_systemd_fd != -1) {
        ne_systemd_exit(ctx);
    }

    flb_ne_config_destroy(ctx);

    return 0;
}

static void in_ne_pause(void *data, struct flb_config *config)
{
    struct flb_ne *ctx = data;

    flb_input_collector_pause(ctx->coll_fd, ctx->ins);
    if (ctx->coll_cpu_fd != -1) {
        flb_input_collector_pause(ctx->coll_cpu_fd, ctx->ins);
    }
    if (ctx->coll_cpufreq_fd != -1) {
        flb_input_collector_pause(ctx->coll_cpufreq_fd, ctx->ins);
    }
    if (ctx->coll_meminfo_fd != -1) {
        flb_input_collector_pause(ctx->coll_meminfo_fd, ctx->ins);
    }
    if (ctx->coll_diskstats_fd != -1) {
        flb_input_collector_pause(ctx->coll_diskstats_fd, ctx->ins);
    }
    if (ctx->coll_filesystem_fd != -1) {
        flb_input_collector_pause(ctx->coll_filesystem_fd, ctx->ins);
    }
    if (ctx->coll_uname_fd != -1) {
        flb_input_collector_pause(ctx->coll_uname_fd, ctx->ins);
    }
    if (ctx->coll_stat_fd != -1) {
        flb_input_collector_pause(ctx->coll_stat_fd, ctx->ins);
    }
    if (ctx->coll_time_fd != -1) {
        flb_input_collector_pause(ctx->coll_time_fd, ctx->ins);
    }
    if (ctx->coll_loadavg_fd != -1) {
        flb_input_collector_pause(ctx->coll_loadavg_fd, ctx->ins);
    }
    if (ctx->coll_vmstat_fd != -1) {
        flb_input_collector_pause(ctx->coll_vmstat_fd, ctx->ins);
    }
    if (ctx->coll_netdev_fd != -1) {
        flb_input_collector_pause(ctx->coll_netdev_fd, ctx->ins);
    }
    if (ctx->coll_filefd_fd != -1) {
        flb_input_collector_pause(ctx->coll_filefd_fd, ctx->ins);
    }
    if (ctx->coll_textfile_fd != -1) {
        flb_input_collector_pause(ctx->coll_textfile_fd, ctx->ins);
    }
    if (ctx->coll_systemd_fd != -1) {
        flb_input_collector_pause(ctx->coll_systemd_fd, ctx->ins);
    }
}

static void in_ne_resume(void *data, struct flb_config *config)
{
    struct flb_ne *ctx = data;

    flb_input_collector_resume(ctx->coll_fd, ctx->ins);
    if (ctx->coll_cpu_fd != -1) {
        flb_input_collector_resume(ctx->coll_cpu_fd, ctx->ins);
    }
    if (ctx->coll_cpufreq_fd != -1) {
        flb_input_collector_resume(ctx->coll_cpufreq_fd, ctx->ins);
    }
    if (ctx->coll_meminfo_fd != -1) {
        flb_input_collector_resume(ctx->coll_meminfo_fd, ctx->ins);
    }
    if (ctx->coll_diskstats_fd != -1) {
        flb_input_collector_resume(ctx->coll_diskstats_fd, ctx->ins);
    }
    if (ctx->coll_filesystem_fd != -1) {
        flb_input_collector_resume(ctx->coll_filesystem_fd, ctx->ins);
    }
    if (ctx->coll_uname_fd != -1) {
        flb_input_collector_resume(ctx->coll_uname_fd, ctx->ins);
    }
    if (ctx->coll_stat_fd != -1) {
        flb_input_collector_resume(ctx->coll_stat_fd, ctx->ins);
    }
    if (ctx->coll_time_fd != -1) {
        flb_input_collector_resume(ctx->coll_time_fd, ctx->ins);
    }
    if (ctx->coll_loadavg_fd != -1) {
        flb_input_collector_resume(ctx->coll_loadavg_fd, ctx->ins);
    }
    if (ctx->coll_vmstat_fd != -1) {
        flb_input_collector_resume(ctx->coll_vmstat_fd, ctx->ins);
    }
    if (ctx->coll_netdev_fd != -1) {
        flb_input_collector_resume(ctx->coll_netdev_fd, ctx->ins);
    }
    if (ctx->coll_filefd_fd != -1) {
        flb_input_collector_resume(ctx->coll_filefd_fd, ctx->ins);
    }
    if (ctx->coll_textfile_fd != -1) {
        flb_input_collector_resume(ctx->coll_textfile_fd, ctx->ins);
    }
    if (ctx->coll_systemd_fd != -1) {
        flb_input_collector_resume(ctx->coll_systemd_fd, ctx->ins);
    }
}

/* Configuration properties map */
static struct flb_config_map config_map[] = {
    {
     FLB_CONFIG_MAP_TIME, "scrape_interval", "5",
     0, FLB_TRUE, offsetof(struct flb_ne, scrape_interval),
     "scrape interval to collect metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.cpu.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, cpu_scrape_interval),
     "scrape interval to collect cpu metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.cpufreq.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, cpufreq_scrape_interval),
     "scrape interval to collect cpufreq metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.meminfo.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, meminfo_scrape_interval),
     "scrape interval to collect meminfo metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.diskstats.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, diskstats_scrape_interval),
     "scrape interval to collect diskstats metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.filesystem.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, filesystem_scrape_interval),
     "scrape interval to collect filesystem metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.uname.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, uname_scrape_interval),
     "scrape interval to collect uname metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.stat.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, stat_scrape_interval),
     "scrape interval to collect stat metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.time.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, time_scrape_interval),
     "scrape interval to collect time metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.loadavg.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, loadavg_scrape_interval),
     "scrape interval to collect loadavg metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.vmstat.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, vmstat_scrape_interval),
     "scrape interval to collect vmstat metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.netdev.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, netdev_scrape_interval),
     "scrape interval to collect netdev metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.filefd.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, filefd_scrape_interval),
     "scrape interval to collect filefd metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.textfile.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, textfile_scrape_interval),
     "scrape interval to collect textfile metrics from the node."
    },

    {
     FLB_CONFIG_MAP_TIME, "collector.systemd.scrape_interval", "0",
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_scrape_interval),
     "scrape interval to collect systemd metrics from the node."
    },

    {
     FLB_CONFIG_MAP_CLIST, "metrics",
     "cpu,cpufreq,meminfo,diskstats,filesystem,uname,stat,time,loadavg,vmstat,netdev,filefd,systemd",
     0, FLB_TRUE, offsetof(struct flb_ne, metrics),
     "Comma separated list of keys to enable metrics."
    },

    {
     FLB_CONFIG_MAP_STR, "collector.textfile.path", NULL,
     0, FLB_TRUE, offsetof(struct flb_ne, path_textfile),
     "Specify file path or directory to collect textfile metrics from the node."
    },

    {
     FLB_CONFIG_MAP_STR, "path.procfs", "/proc",
     0, FLB_TRUE, offsetof(struct flb_ne, path_procfs),
     "procfs mount point"
    },

    {
     FLB_CONFIG_MAP_STR, "path.sysfs", "/sys",
     0, FLB_TRUE, offsetof(struct flb_ne, path_sysfs),
     "sysfs mount point"
    },

    /* Systemd specific settings */
    {
     FLB_CONFIG_MAP_BOOL, "systemd_service_restart_metrics", "false",
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_include_service_restarts),
     "include systemd service restart metrics"
    },

    {
     FLB_CONFIG_MAP_BOOL, "systemd_unit_start_time_metrics", "false",
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_include_unit_start_times),
     "include systemd unit start time metrics"
    },

    {
     FLB_CONFIG_MAP_BOOL, "systemd_include_service_task_metrics", "false",
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_include_service_task_metrics),
     "include systemd service task metrics"
    },

    {
     FLB_CONFIG_MAP_STR, "systemd_include_pattern", NULL,
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_regex_include_list_text),
     "include list regular expression"
    },

    {
     FLB_CONFIG_MAP_STR, "systemd_exclude_pattern", ".+\\.(automount|device|mount|scope|slice)",
     0, FLB_TRUE, offsetof(struct flb_ne, systemd_regex_exclude_list_text),
     "exclude list regular expression"
    },

    /* filesystem specific settings */
    {
     FLB_CONFIG_MAP_STR, "filesystem.ignore_mount_point_regex", IGNORED_MOUNT_POINTS,
     0, FLB_TRUE, offsetof(struct flb_ne, fs_regex_ingore_mount_point_text),
     "ignore regular expression for mount points"
    },

    {
     FLB_CONFIG_MAP_STR, "filesystem.ignore_filesystem_type_regex", IGNORED_FS_TYPES,
     0, FLB_TRUE, offsetof(struct flb_ne, fs_regex_ingore_filesystem_type_text),
     "ignore regular expression for filesystem types"
    },

    /* diskstats specific settings */
    {
     FLB_CONFIG_MAP_STR, "diskstats.ignore_device_regex", IGNORED_DEVICES,
     0, FLB_TRUE, offsetof(struct flb_ne, dt_regex_skip_devices_text),
     "ignore regular expression for disk devices"
    },
    /* EOF */
    {0}
};

struct flb_input_plugin in_node_exporter_metrics_plugin = {
    .name         = "node_exporter_metrics",
    .description  = "Node Exporter Metrics (Prometheus Compatible)",
    .cb_init      = in_ne_init,
    .cb_pre_run   = NULL,
    .cb_collect   = cb_ne_collect,
    .cb_flush_buf = NULL,
    .config_map   = config_map,
    .cb_pause     = in_ne_pause,
    .cb_resume    = in_ne_resume,
    .cb_exit      = in_ne_exit,
    .flags        = FLB_INPUT_THREADED
};