summaryrefslogtreecommitdiff
path: root/modules/telegraf/telegraf_utils.py
blob: 00bc232ed09d4ccb0ac917874737bbafc333f0e4 (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
import toml

def modify_input(new_pipeline_path, key, value):
    """
    new_pipeline_path: Uue konveieri faili asukoht 
    key: plugginas muudetav võti
    value: muudetava võtme väärtus

    Muudab konfiguratsiooni faili 'inputs.http' plugginas etteantud väärtuse
    """
    data = toml.load(new_pipeline_path)
    pluggin = data["inputs"]["http"][0]

    if key in pluggin:

        #print(f"Before: {key} = {pluggin[key]}")
        pluggin[key] = value
        #print(f"After:  {key} = {pluggin[key]}")


    with open(new_pipeline_path, "w") as f:
        toml.dump(data, f)


##modify_input("templates/basic_ETL.toml", "test_pipers.toml, "urls", ["stillTesting"])

def modify_agent(new_pipeline_path, key, value):
    """
    new_pipeline_path: Uue konveieri faili asukoht 
    key: plugginas muudetav võti
    value: muudetava võtme väärtus

    Muudab konfiguratsiooni faili 'agent' plugginas etteantud väärtuse
    """
    data = toml.load(new_pipeline_path)
    pluggin = data["agent"]

    if key in pluggin:

        #print(f"Before: {key} = {pluggin[key]}")
        pluggin[key] = value
        #print(f"After:  {key} = {pluggin[key]}")


    with open(new_pipeline_path, "w") as f:
        toml.dump(data, f)


def modify_output(new_pipeline_path, key, value):
    """
    new_pipeline_path: Uue konveieri faili asukoht 
    key: plugginas muudetav võti
    value: muudetava võtme väärtus

    Muudab konfiguratsiooni faili 'outputs.influxdb' plugginas etteantud väärtuse
    """
    data = toml.load(new_pipeline_path)
    pluggin = data["outputs"]["influxdb"][0]

    if key in pluggin:

        #print(f"Before: {key} = {pluggin[key]}")
        pluggin[key] = value
        #print(f"After:  {key} = {pluggin[key]}")


    with open(new_pipeline_path, "w") as f:
        toml.dump(data, f)




### different_jsonPaths_ETL template funcs ###


#def modify_processorsConventer(new_pipeline_path, key, value):
#    data = toml.load(new_pipeline_path)
#    #print(data)
#    pluggin = data["processors"]["converter"][0]["fields"]
#    print(pluggin)
#
#    if key in pluggin:
#        pluggin[key] = value
#    with open(new_pipeline_path, "w") as f:
#        toml.dump(data, f)
#
#
#def modify_processorsRename(new_pipeline_path, key, value):
#    data = toml.load(new_pipeline_path)
#    pluggin = data["processors"]["rename"][0]["replace"][0]
#    print(pluggin)
#    pluggin = data["processors"]["rename"][0]["replace"][1]
#    print(pluggin)
#
#    if key in pluggin:
#        pluggin[key] = value
#    with open(new_pipeline_path, "w") as f:
#        toml.dump(data, f)
#








### ChatGPT was used in the procesess of creating this function
##   def add_new_replace_block(new_pipeline_name):
##   
##   new_block = """  [[processors.rename.replace]]
##       field = "placeholder"
##       dest = "placeholder"
##   """
##   
##   with open(new_pipeline_name, "r") as file:
##       lines = file.readlines()
##   
##   # Find the last occurrence of '[[processors.rename.replace]]'
##   insert_index = -1
##   for i, line in enumerate(lines):
##       if line.strip().startswith("[[processors.rename.replace]]"):
##           insert_index = i
##   
##   while insert_index + 1 < len(lines) and lines[insert_index + 1].startswith(" "):
##       insert_index += 1
##   
##   # Insert the new block
##   lines.insert(insert_index + 1, new_block + "\n")
##   
##   with open(new_pipeline_name, "w") as file:
##       file.writelines(lines)
##