prtg

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2017 License: BSD-3-Clause Imports: 1 Imported by: 0

README

go-prtg-api

API for writing PRTG custom sensors in Go.

Example

This simple exampe 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"
	"net/http"
	"time"

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

func main() {
	// Create empty response and log start time
	r := &prtg.SensorResponse{}
	start := time.Now()
	// Perform HTTP request
	resp, err := http.Get("http://paessler.com")
	if err != nil {
		r.PRTG.Error = "1"
		r.PRTG.Text = err.Error()
		fmt.Println(r.String())
		return
	}
	// Read the response
	buffer, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		r.PRTG.Error = "1"
		r.PRTG.Text = err.Error()
		fmt.Println(r.String())
		return
	}
	// Evaluate results
	responseTime := time.Since(start)
	responseBytes := len(buffer)
	// Response time channel
	r.AddChannel(prtg.SensorChannel{
		Channel:   "Response time",
		Value:     fmt.Sprintf("%f", responseTime.Seconds()*1000),
		Float:     1,
		ShowChart: 1,
		ShowTable: 1,
		Unit:      prtg.UnitTimeResponse,
	})
	// Bytes read channel
	r.AddChannel(prtg.SensorChannel{
		Channel:   "Bytes read",
		Value:     fmt.Sprintf("%d", responseBytes),
		ShowChart: 1,
		ShowTable: 1,
		Unit:      prtg.UnitBytesFile,
	})

	fmt.Println(r.String())
}

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

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.

API reference can be found here: https://prtg.paessler.com/api.htm?username=demo&password=demodemo&tabid=7

Index

Constants

View Source
const (
	UnitBytesBandwidth = "BytesBandwidth"
	UnitBytesMemory    = "BytesMemory"
	UnitBytesDisk      = "BytesDisk"
	UnitBytesFile      = "BytesFile"
	UnitTemperature    = "Temperature"
	UnitPercent        = "Percent"
	UnitTimeResponse   = "TimeResponse"
	UnitTimeSeconds    = "TimeSeconds"
	UnitCustom         = "Custom"
	UnitCount          = "Count"
	UnitCPU            = "CPU"
	UnitSpeedDisk      = "SpeedDisk"
	UnitSpeedNet       = "SpeedNet"
	UnitTimeHours      = "TimeHours"
)

Units of channel values

View Source
const (
	UnitOne      = "One"
	UnitKilo     = "Kilo"
	UnitMega     = "Mega"
	UnitGiga     = "Giga"
	UnitTera     = "Tera"
	UnitByte     = "Byte"
	UnitKiloByte = "KiloByte"
	UnitMegaByte = "MegaByte"
	UnitGigaByte = "Gigabyte"
	UnitTeraByte = "TeraByte"
	UnitBit      = "Bit"
	UnitKiloBit  = "KiloBit"
	UnitMegaBit  = "MegaBit"
	UnitGigaBit  = "GigaBit"
	UnitTeraBit  = "TeraBit"
)

Unit of VolumeSize or SpeedSize

View Source
const (
	UnitSecond = "Second"
	UnitMinute = "Minute"
	UnitHour   = "Hour"
	UnitDay    = "Day"
)

Unit of speedTime

View Source
const (
	OptionAbsolute   = "Absolute"
	OptionDifference = "Difference"
	OptionAuto       = "Auto"
	OptionAll        = "All"
)

Options for DecimalMode and Mode

Variables

This section is empty.

Functions

This section is empty.

Types

type SensorChannel

type SensorChannel struct {
	Name      string  `json:"channel,omitempty"`
	ChannelID *int    `json:"channelid,omitempty"`
	Value     float64 ``

	// Options
	Unit            string   `json:",omitempty"`
	CustomUnit      string   `json:",omitempty"`
	SpeedSize       string   `json:",omitempty"`
	VolumeSize      string   `json:",omitempty"`
	SpeedTime       string   `json:",omitempty"`
	Mode            string   `json:",omitempty"`
	Float           int      `json:",omitempty"`
	DecimalMode     string   `json:",omitempty"`
	Warning         int      `json:",omitempty"`
	ShowChart       *int     `json:",omitempty"`
	ShowTable       *int     `json:",omitempty"`
	LimitMaxError   *float64 `json:",omitempty"`
	LimitMaxWarning *float64 `json:",omitempty"`
	LimitMinWarning *float64 `json:",omitempty"`
	LimitMinError   *float64 `json:",omitempty"`
	LimitErrorMsg   string   `json:",omitempty"`
	LimitWarningMsg string   `json:",omitempty"`
	LimitMode       int      `json:",omitempty"`
	ValueLookup     string   `json:",omitempty"`
	NotifyChanged   bool     `json:",omitempty"`
}

SensorChannel represents a PRTG sensor channel

type SensorResponse

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

SensorResponse is the struct returned by the sensor

func (*SensorResponse) AddChannel

func (s *SensorResponse) AddChannel(channel SensorChannel)

AddChannel adds a new channel to a SensorResponse

func (SensorResponse) String

func (s SensorResponse) String() string

String converts the SensorResponse to a JSON formatted string

type SensorResult

type SensorResult struct {
	Channels []SensorChannel `json:"result"`
	Error    int             `json:"error,omitempty"`
	Text     string          `json:"text,omitempty"`
}

SensorResult contains the sensors channels and optional error/text

Jump to

Keyboard shortcuts

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