summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/agent/netdataapi/api.go
blob: 4f2b7a9b580647733377415136fb0953eb838332 (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
// SPDX-License-Identifier: GPL-3.0-or-later

package netdataapi

import (
	"bytes"
	"fmt"
	"io"
	"strconv"
)

type (
	// API implements Netdata external plugins API.
	// https://learn.netdata.cloud/docs/agent/collectors/plugins.d#the-output-of-the-plugin
	API struct {
		io.Writer
	}
)

const quotes = "' '"

var (
	end          = []byte("END\n\n")
	clabelCommit = []byte("CLABEL_COMMIT\n")
	newLine      = []byte("\n")
)

func New(w io.Writer) *API { return &API{w} }

// CHART  creates or update a chart.
func (a *API) CHART(
	typeID string,
	ID string,
	name string,
	title string,
	units string,
	family string,
	context string,
	chartType string,
	priority int,
	updateEvery int,
	options string,
	plugin string,
	module string) error {
	_, err := a.Write([]byte("CHART " + "'" +
		typeID + "." + ID + quotes +
		name + quotes +
		title + quotes +
		units + quotes +
		family + quotes +
		context + quotes +
		chartType + quotes +
		strconv.Itoa(priority) + quotes +
		strconv.Itoa(updateEvery) + quotes +
		options + quotes +
		plugin + quotes +
		module + "'\n"))
	return err
}

// DIMENSION adds or update a dimension to the chart just created.
func (a *API) DIMENSION(
	ID string,
	name string,
	algorithm string,
	multiplier int,
	divisor int,
	options string) error {
	_, err := a.Write([]byte("DIMENSION '" +
		ID + quotes +
		name + quotes +
		algorithm + quotes +
		strconv.Itoa(multiplier) + quotes +
		strconv.Itoa(divisor) + quotes +
		options + "'\n"))
	return err
}

// CLABEL adds or update a label to the chart.
func (a *API) CLABEL(key, value string, source int) error {
	_, err := a.Write([]byte("CLABEL '" +
		key + quotes +
		value + quotes +
		strconv.Itoa(source) + "'\n"))
	return err
}

// CLABELCOMMIT adds labels to the chart. Should be called after one or more CLABEL.
func (a *API) CLABELCOMMIT() error {
	_, err := a.Write(clabelCommit)
	return err
}

// BEGIN initializes data collection for a chart.
func (a *API) BEGIN(typeID string, ID string, msSince int) (err error) {
	if msSince > 0 {
		_, err = a.Write([]byte("BEGIN " + "'" + typeID + "." + ID + "' " + strconv.Itoa(msSince) + "\n"))
	} else {
		_, err = a.Write([]byte("BEGIN " + "'" + typeID + "." + ID + "'\n"))
	}
	return err
}

// SET sets the value of a dimension for the initialized chart.
func (a *API) SET(ID string, value int64) error {
	_, err := a.Write([]byte("SET '" + ID + "' = " + strconv.FormatInt(value, 10) + "\n"))
	return err
}

// SETEMPTY sets the empty value of a dimension for the initialized chart.
func (a *API) SETEMPTY(ID string) error {
	_, err := a.Write([]byte("SET '" + ID + "' = \n"))
	return err
}

// VARIABLE sets the value of a CHART scope variable for the initialized chart.
func (a *API) VARIABLE(ID string, value int64) error {
	_, err := a.Write([]byte("VARIABLE CHART '" + ID + "' = " + strconv.FormatInt(value, 10) + "\n"))
	return err
}

// END completes data collection for the initialized chart.
func (a *API) END() error {
	_, err := a.Write(end)
	return err
}

// DISABLE disables this plugin. This will prevent Netdata from restarting the plugin.
func (a *API) DISABLE() error {
	_, err := a.Write([]byte("DISABLE\n"))
	return err
}

// EMPTYLINE writes an empty line.
func (a *API) EMPTYLINE() error {
	_, err := a.Write(newLine)
	return err
}

func (a *API) HOSTINFO(guid, hostname string, labels map[string]string) error {
	if err := a.HOSTDEFINE(guid, hostname); err != nil {
		return err
	}
	for k, v := range labels {
		if err := a.HOSTLABEL(k, v); err != nil {
			return err
		}
	}
	return a.HOSTDEFINEEND()
}

func (a *API) HOSTDEFINE(guid, hostname string) error {
	_, err := fmt.Fprintf(a, "HOST_DEFINE '%s' '%s'\n", guid, hostname)
	return err
}

func (a *API) HOSTLABEL(name, value string) error {
	_, err := fmt.Fprintf(a, "HOST_LABEL '%s' '%s'\n", name, value)
	return err
}

func (a *API) HOSTDEFINEEND() error {
	_, err := fmt.Fprintf(a, "HOST_DEFINE_END\n\n")
	return err
}

func (a *API) HOST(guid string) error {
	_, err := a.Write([]byte("HOST " + "'" +
		guid + "'\n\n"))
	return err
}

func (a *API) FUNCRESULT(uid, contentType, payload, code, expireTimestamp string) {
	var buf bytes.Buffer

	buf.WriteString("FUNCTION_RESULT_BEGIN " +
		uid + " " +
		code + " " +
		contentType + " " +
		expireTimestamp + "\n",
	)

	if payload != "" {
		buf.WriteString(payload + "\n")
	}

	buf.WriteString("FUNCTION_RESULT_END\n\n")

	_, _ = buf.WriteTo(a)
}

func (a *API) CONFIGCREATE(id, status, configType, path, sourceType, source, supportedCommands string) {
	// https://learn.netdata.cloud/docs/contributing/external-plugins/#config

	_, _ = a.Write([]byte("CONFIG " +
		id + " " +
		"create" + " " +
		status + " " +
		configType + " " +
		path + " " +
		sourceType + " '" +
		source + "' '" +
		supportedCommands + "' 0x0000 0x0000\n\n",
	))
}

func (a *API) CONFIGDELETE(id string) {
	_, _ = a.Write([]byte("CONFIG " + id + " delete\n\n"))
}

func (a *API) CONFIGSTATUS(id, status string) {
	_, _ = a.Write([]byte("CONFIG " + id + " status " + status + "\n\n"))
}