prtg

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 17, 2019 License: BSD-3-Clause Imports: 3 Imported by: 1

README

go-prtg-sensor-api

API for writing PRTG custom sensors in Go.

Build Status GoDoc Go Report Card

Example EXE Sensor

This simple example sensor sends an HTTP request to http://paessler.com and returns two channels:

  • Response time - time it takes to perform the request and read the body
  • Bytes read - number of bytes the body contained
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/PRTG/go-prtg-sensor-api"
)

func main() {
	// Initiate Sensor instance
	sensor := prtg.New()

	// Log start time
	start := time.Now()
	// Perform HTTP request
	resp, err := http.Get("http://paessler.com")
	if err != nil {
		sensor.SetError(true)
		sensor.SetSensorText(err.Error())
		json, err := sensor.MarshalToString()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(json)
		return
	}
	// Read the response
	buffer, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		sensor.SetError(true)
		sensor.SetSensorText(err.Error())
		json, err := sensor.MarshalToString()
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(json)
		return
	}
	// Evaluate results
	responseTime := time.Since(start)
	responseBytes := len(buffer)

	// Response time channel
	sensor.AddChannel("Response time").SetValue(responseTime.Seconds() * 1000).SetUnit(prtg.TimeResponse)
	// Bytes read channel
	sensor.AddChannel("Bytes read").SetValue(responseBytes).SetUnit(prtg.BytesFile)

	json, err := sensor.MarshalToString()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(json)
}

To test this example in your PRTG installation follow these steps:

  1. Build the example
  2. Copy the .exe file to [PRTG install folder]\Custom Sensors\EXEXML
  3. Go to PRTG web interface
  4. Add Sensor to a device of your choice
  5. Choose EXE/SCRIPT ADVANCED as sensor typee (filter for Custom Sensors)
  6. Under EXE/Script choose the .exe you copied in step 2
  7. Done

Example HTTP Data Advanced Sensor

This example sensor uses a http server to serve the sensor data, that can be pulled by a http data advanced sensor.

  • Some value - shows sample percentage value
  • Something - shows if the Something service is up and running

The Sensor returns an error if "Something" is not ok.

package main

import (
	"fmt"
	"log"
	"math/rand"
	"net/http"

	"github.com/PRTG/go-prtg-sensor-api"
)

func main() {
	// Create a webserver listening on "/"
	http.HandleFunc("/", reportStatus)
	http.ListenAndServe(":8080", nil)
}

func reportStatus(w http.ResponseWriter, r *http.Request) {
	// Initiate PRTG instance
	sensor := prtg.New()

	// Set sensor text
	sensor.SetSensorText("This is a test sensor")

	// Add a channel with a random float value in Percent
	sensor.AddChannel("Some value").SetValue(rand.Float64() * 100).
		SetUnit(prtg.Percent).SetMaxWarnLimit(80).SetMaxErrLimit(90)

	// Take a look if Something is working
	isUp, err := isSomethingUp()
	// Create a Sensor that shows the uptime of Something
	sensor.AddChannel("Something").SetValue(isUp).
		SetValueLookup("prtg.standardlookups.connectionstate.stateonlineok")
	// Create error message on sensor if the Something is down
	if err != nil {
		sensor.SetError(true)
		sensor.SetSensorText("Test sensor error: " + err.Error())
	}

	// Create json output
	json, err := sensor.MarshalToString()
	if err != nil {
		log.Fatal(err)
	}

	// Deliver to website
	_, err = fmt.Fprint(w, json)
	if err != nil {
		log.Fatal(err)
	}
}

func isSomethingUp() (bool, error) {
	// Generate a "Thing" to watch, that is working in 90% of the time
	if rand.Intn(10) > 1 {
		return true, nil
	}
	return false, fmt.Errorf("the Something is struggling")
}

To test this example in your PRTG installation follow these steps:

  1. Build the example
  2. Run the binary
  3. Go to PRTG web interface
  4. Add Sensor to a device of your choice
  5. Choose HTTP Data Advanced as sensor type
  6. Under URL choose the ip of your device with the port 8080
  7. Done

Documentation

Documentation

Overview

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.

Copyright (c) 2019, Paessler AG. All Rights Reserved

Copyright (c) 2019, Paessler AG. All Rights Reserved

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DecimalModeType added in v0.2.0

type DecimalModeType string
const (
	OptionAuto DecimalModeType = "Auto"
	OptionAll  DecimalModeType = "All"
)

Options for Decimalmode

type Sensor added in v0.2.0

type Sensor interface {
	AddChannel(string) *SensorChannel
	MarshalToString() (string, error)
	SetSensorText(string) *SensorResults
	SetError(bool) *SensorResults
}

func New added in v0.2.0

func New() Sensor

Creates the Sensor instance and returns the interface.

type SensorChannel

type SensorChannel struct {
	Channel string `json:"channel"`
	Value   string `json:"value"`

	// Options
	ValueMode       string `json:"mode,omitempty"`
	Unit            string `json:"unit,omitempty"`
	CustomUnit      string `json:"customunit,omitempty"`
	ValueLookup     string `json:"valuelookup,omitempty"`
	VolumeSize      string `json:"volumesize,omitempty"`
	SpeedSize       string `json:"speedsize,omitempty"`
	SpeedTime       string `json:"speedtime,omitempty"`
	Float           string `json:"float,omitempty"`
	DecimalMode     string `json:"decimalmode,omitempty"`
	ShowChart       string `json:"showchart,omitempty"`
	ShowTable       string `json:"showtable,omitempty"`
	LimitMinWarning string `json:"limitminwarning,omitempty"`
	LimitMaxWarning string `json:"limitmaxwarning,omitempty"`
	LimitWarningMsg string `json:"limitwarningmsg,omitempty"`
	LimitMinError   string `json:"limitminerror,omitempty"`
	LimitMaxError   string `json:"limitmaxerror,omitempty"`
	LimitErrorMsg   string `json:"limiterrormsg,omitempty"`
	LimitMode       string `json:"limitmode,omitempty"`
	Warning         string `json:"warning,omitempty"`
}

SensorChannel has the channel name and value and options

func (*SensorChannel) SetCustomUnit added in v0.2.0

func (r *SensorChannel) SetCustomUnit(unit string) *SensorChannel

Sets a custom unit text, that is displayed behind the value.

func (*SensorChannel) SetDecimalMode added in v0.2.0

func (r *SensorChannel) SetDecimalMode(mode DecimalModeType) *SensorChannel

If value is a float a custom number of decimal places can be set.

func (*SensorChannel) SetErrLimitMsg added in v0.2.0

func (r *SensorChannel) SetErrLimitMsg(msg string) *SensorChannel

Define an additional message. It will be added to the sensor's message when entering a Down status that is triggered by a limit.

func (*SensorChannel) SetMaxErrLimit added in v0.2.0

func (r *SensorChannel) SetMaxErrLimit(limit int) *SensorChannel

Define an upper error limit for the channel. If enabled, the sensor will be set to a Down status if this value is overrun.

func (*SensorChannel) SetMaxWarnLimit added in v0.2.0

func (r *SensorChannel) SetMaxWarnLimit(limit int) *SensorChannel

Define an upper warning limit for the channel. If enabled, the sensor will be set to a Warning status if this value is overrun.

func (*SensorChannel) SetMinErrLimit added in v0.2.0

func (r *SensorChannel) SetMinErrLimit(limit int) *SensorChannel

Define a lower error limit for the channel. If enabled, the sensor will be set to a Down status if this value is undercut.

func (*SensorChannel) SetMinWarnLimit added in v0.2.0

func (r *SensorChannel) SetMinWarnLimit(limit int) *SensorChannel

Define an upper error limit for the channel. If enabled, the sensor will be set to a Down status if this value is overrun.

func (*SensorChannel) SetShowChart added in v0.2.0

func (r *SensorChannel) SetShowChart(show bool) *SensorChannel

Init value for the Show in graphs option. Default is yes.

func (*SensorChannel) SetShowTable added in v0.2.0

func (r *SensorChannel) SetShowTable(show bool) *SensorChannel

Init value for the Show in tables option. Default is yes.

func (*SensorChannel) SetSpeedSize added in v0.2.0

func (r *SensorChannel) SetSpeedSize(size SpeedType) *SensorChannel

Speed size used for the display value. For example, if you have a value of 50000 and use KiloBit as size, the display is 50 kiloBits. Default is One (value used as returned).

func (*SensorChannel) SetSpeedTime added in v0.2.0

func (r *SensorChannel) SetSpeedTime(time TimeType) *SensorChannel

Time size used for the display value. For example, if you have a value of 780 and use Minute as size, the display is 13 Minutes. Default is Second (value used as returned).

func (*SensorChannel) SetToWarning added in v0.2.0

func (r *SensorChannel) SetToWarning() *SensorChannel

If enabled for at least one channel, the entire sensor is set to "Warning" status. Default is "not set".

func (*SensorChannel) SetUnit added in v0.2.0

func (r *SensorChannel) SetUnit(unit UnitType) *SensorChannel

The unit of the value. Default is Custom. This is useful for PRTG to be able to convert volumes and times.

func (*SensorChannel) SetValue added in v0.2.0

func (r *SensorChannel) SetValue(val interface{}) *SensorChannel

Sets a value. All types allowed except complex and uintptr.

func (*SensorChannel) SetValueLookup added in v0.2.0

func (r *SensorChannel) SetValueLookup(lookup string) *SensorChannel

Define if you want to use a lookup file (for example, to view integer values as status texts). Enter the ID of the lookup file you want to use, or omit this element to not use lookups.

func (*SensorChannel) SetVolumeSize added in v0.2.0

func (r *SensorChannel) SetVolumeSize(size SizeType) *SensorChannel

Volume size used for the display value. For example, if you have a value of 50000 and use Kilo as size, the display is 50 kilo. Default is One (value used as returned).

func (*SensorChannel) SetWarnLimitMsg added in v0.2.0

func (r *SensorChannel) SetWarnLimitMsg(msg string) *SensorChannel

Define an additional message. It will be added to the sensor's message when entering a Warning status that is triggered by a limit.

func (*SensorChannel) ValueIsDifference added in v0.2.0

func (r *SensorChannel) ValueIsDifference() *SensorChannel

Select if the value is an absolute value or counter. Default is "Absolute" set.

type SensorResponse

type SensorResponse struct {
	SensorResults SensorResults `json:"prtg"`
}

SensorStatus has the whole JSON object

type SensorResults added in v0.2.0

type SensorResults struct {
	SensorChannels []SensorChannel `json:"result"`
	Text           string          `json:"text,omitempty"`
	Error          string          `json:"error,omitempty"`
}

SensorResults has all the channels

func (*SensorResults) AddChannel added in v0.2.0

func (sr *SensorResults) AddChannel(channelName string) *SensorChannel

Name of the channel as displayed in user interfaces.

func (*SensorResults) MarshalToString added in v0.2.0

func (sr *SensorResults) MarshalToString() (string, error)

Create a JSON string from the PRTG object

func (*SensorResults) SetError added in v0.2.0

func (sr *SensorResults) SetError(err bool) *SensorResults

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 (*SensorResults) SetSensorText added in v0.2.0

func (sr *SensorResults) SetSensorText(text string) *SensorResults

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.

type SizeType added in v0.2.0

type SizeType string
const (
	One      SizeType = "One"
	Kilo     SizeType = "Kilo"
	Mega     SizeType = "Mega"
	Giga     SizeType = "Giga"
	Tera     SizeType = "Tera"
	Byte     SizeType = "Byte"
	KiloByte SizeType = "KiloByte"
	MegaByte SizeType = "MegaByte"
	GigaByte SizeType = "GigaByte"
	TeraByte SizeType = "TeraByte"
)

type SpeedType added in v0.2.0

type SpeedType string
const (
	Bit     SpeedType = "Bit"
	KiloBit SpeedType = "KiloBit"
	MegaBit SpeedType = "MegaBit"
	GigaBit SpeedType = "GigaBit"
	TeraBit SpeedType = "TeraBit"
)

type TimeType added in v0.2.0

type TimeType string
const (
	Second TimeType = "Second"
	Minute TimeType = "Minute"
	Hour   TimeType = "Hour"
	Day    TimeType = "Day"
)

type UnitType added in v0.2.0

type UnitType string
const (
	BytesBandwidth UnitType = "BytesBandwidth"
	BytesDisk      UnitType = "BytesDisk"
	Temperature    UnitType = "Temperature"
	Percent        UnitType = "Percent"
	TimeResponse   UnitType = "TimeResponse"
	TimeSeconds    UnitType = "TimeSeconds"
	Custom         UnitType = "Custom"
	Count          UnitType = "Count"
	CPU            UnitType = "CPU"
	BytesFile      UnitType = "BytesFile"
	SpeedDisk      UnitType = "SpeedDisk"
	SpeedNet       UnitType = "SpeedNet"
	TimeHours      UnitType = "TimeHours"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL