项目作者: PRTG

项目描述 :
API for writing PRTG custom sensors in Go.
高级语言: Go
项目地址: git://github.com/PRTG/go-prtg-sensor-api.git
创建时间: 2016-10-14T11:26:17Z
项目社区:https://github.com/PRTG/go-prtg-sensor-api

开源协议:BSD 3-Clause "New" or "Revised" License

下载


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
  1. package main
  2. import (
  3. "fmt"
  4. "io"
  5. "log"
  6. "net/http"
  7. "time"
  8. "github.com/PRTG/go-prtg-sensor-api"
  9. )
  10. func main() {
  11. // Initiate Sensor instance
  12. sensor := prtg.New()
  13. // Log start time
  14. start := time.Now()
  15. // Perform HTTP request
  16. resp, err := http.Get("http://paessler.com")
  17. if err != nil {
  18. sensor.SetError(true)
  19. sensor.SetSensorText(err.Error())
  20. json, err := sensor.MarshalToString()
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. fmt.Println(json)
  25. return
  26. }
  27. // Read the response
  28. buffer, err := io.ReadAll(resp.Body)
  29. if err != nil {
  30. sensor.SetError(true)
  31. sensor.SetSensorText(err.Error())
  32. json, err := sensor.MarshalToString()
  33. if err != nil {
  34. log.Fatal(err)
  35. }
  36. fmt.Println(json)
  37. return
  38. }
  39. // Evaluate results
  40. responseTime := time.Since(start)
  41. responseBytes := len(buffer)
  42. // Response time channel
  43. sensor.AddChannel("Response time").SetValue(responseTime.Seconds() * 1000).SetUnit(prtg.TimeResponse)
  44. // Bytes read channel
  45. sensor.AddChannel("Bytes read").SetValue(responseBytes).SetUnit(prtg.BytesFile)
  46. json, err := sensor.MarshalToString()
  47. if err != nil {
  48. log.Fatal(err)
  49. }
  50. fmt.Println(json)
  51. }

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.

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. "github.com/PRTG/go-prtg-sensor-api"
  8. )
  9. func main() {
  10. // Create a webserver listening on "/"
  11. http.HandleFunc("/", reportStatus)
  12. http.ListenAndServe(":8080", nil)
  13. }
  14. func reportStatus(w http.ResponseWriter, r *http.Request) {
  15. // Initiate PRTG instance
  16. sensor := prtg.New()
  17. // Set sensor text
  18. sensor.SetSensorText("This is a test sensor")
  19. // Add a channel with a random float value in Percent
  20. sensor.AddChannel("Some value").SetValue(rand.Float64() * 100).
  21. SetUnit(prtg.Percent).SetMaxWarnLimit(80).SetMaxErrLimit(90)
  22. // Take a look if Something is working
  23. isUp, err := isSomethingUp()
  24. // Create a Sensor that shows the uptime of Something
  25. sensor.AddChannel("Something").SetValue(isUp).
  26. SetValueLookup("prtg.standardlookups.connectionstate.stateonlineok")
  27. // Create error message on sensor if the Something is down
  28. if err != nil {
  29. sensor.SetError(true)
  30. sensor.SetSensorText("Test sensor error: " + err.Error())
  31. }
  32. // Create json output
  33. json, err := sensor.MarshalToString()
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. // Deliver to website
  38. _, err = fmt.Fprint(w, json)
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. }
  43. func isSomethingUp() (bool, error) {
  44. // Generate a "Thing" to watch, that is working in 90% of the time
  45. if rand.Intn(10) > 1 {
  46. return true, nil
  47. }
  48. return false, fmt.Errorf("the Something is struggling")
  49. }

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