-
Notifications
You must be signed in to change notification settings - Fork 2
/
prtg.go
68 lines (56 loc) · 1.87 KB
/
prtg.go
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
// Copyright (c) 2019, Paessler AG.
// All Rights Reserved
// Package PRTG implements the API for PRTG custom sensors.
// It provides all structs and constants needed to implement your own advanced exe sensor in Go.
package prtg
import (
"encoding/json"
)
// SensorStatus has the whole JSON object
type SensorResponse struct {
SensorResults SensorResults `json:"prtg"`
}
// SensorResults has all the channels
type SensorResults struct {
SensorChannels []SensorChannel `json:"result"`
Text string `json:"text,omitempty"`
Error string `json:"error,omitempty"`
}
type Sensor interface {
AddChannel(string) *SensorChannel
MarshalToString() (string, error)
SetSensorText(string) *SensorResults
SetError(bool) *SensorResults
}
// Creates the Sensor instance and returns the interface.
func New() Sensor {
return &SensorResults{}
}
// Name of the channel as displayed in user interfaces.
func (sr *SensorResults) AddChannel(channelName string) *SensorChannel {
newChan := SensorChannel{Channel: channelName}
sr.SensorChannels = append(sr.SensorChannels, newChan)
return &sr.SensorChannels[len(sr.SensorChannels)-1]
}
// Create a JSON string from the PRTG object
func (sr *SensorResults) MarshalToString() (string, error) {
bytes, err := json.Marshal(&SensorResponse{*sr})
return string(bytes), err
}
// Text the sensor returns in the Message field with every scanning interval.
// There can be one message per sensor, regardless of the number of channels.
// Default is OK.
func (sr *SensorResults) SetSensorText(text string) *SensorResults {
sr.Text = text
return sr
}
// If enabled, the sensor will return an error status.
// This element can be combined with the SensorText element in order to show an error message.
// Default is 0.
func (sr *SensorResults) SetError(err bool) *SensorResults {
sr.Error = "0"
if err {
sr.Error = "1"
}
return sr
}