dont use external dependencies

This commit is contained in:
Ranjib Dey 2016-06-03 14:53:42 -07:00
parent b65a95cfb1
commit 7f3eccfc83
1 changed files with 34 additions and 3 deletions

View File

@ -1,15 +1,29 @@
package pagerduty package pagerduty
import ( import (
"bytes"
"encoding/json"
"fmt" "fmt"
pd "github.com/PagerDuty/go-pagerduty"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/outputs" "github.com/influxdata/telegraf/plugins/outputs"
"go/token" "go/token"
"go/types" "go/types"
"log" "log"
"net/http"
) )
const EventEndPoint = "https://events.pagerduty.com/generic/2010-04-15/create_event.json"
type Event struct {
Type string `json:"event_type"`
ServiceKey string `json:"service_key"`
Description string `json:"description,omitempty"`
Client string `json:"client,omitempty"`
ClientURL string `json:"client_url,omitempty"`
Details interface{} `json:"details,omitempty"`
Contexts []interface{} `json:"contexts,omitempty"`
}
type PD struct { type PD struct {
ServiceKey string `toml:"service_key"` ServiceKey string `toml:"service_key"`
Desc string `toml:"description"` Desc string `toml:"description"`
@ -32,6 +46,23 @@ field = "time_iowait"
expression = "> 50.0" expression = "> 50.0"
` `
func createEvent(e Event) (*http.Response, error) {
data, err := json.Marshal(e)
if err != nil {
return nil, err
}
req, _ := http.NewRequest("POST", EventEndPoint, bytes.NewBuffer(data))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return resp, fmt.Errorf("HTTP Status Code: %d", resp.StatusCode)
}
return resp, nil
}
func (p *PD) Connect() error { func (p *PD) Connect() error {
return nil return nil
} }
@ -83,7 +114,7 @@ func (p *PD) Write(metrics []telegraf.Metric) error {
if len(metrics) == 0 { if len(metrics) == 0 {
return nil return nil
} }
event := pd.Event{ event := Event{
Type: "trigger", Type: "trigger",
ServiceKey: p.ServiceKey, ServiceKey: p.ServiceKey,
Description: p.Desc, Description: p.Desc,
@ -100,7 +131,7 @@ func (p *PD) Write(metrics []telegraf.Metric) error {
m["name"] = metric.Name() m["name"] = metric.Name()
m["timestamp"] = metric.UnixNano() / 1000000000 m["timestamp"] = metric.UnixNano() / 1000000000
event.Details = m event.Details = m
_, err := pd.CreateEvent(event) _, err := createEvent(event)
if err != nil { if err != nil {
return err return err
} }