Skip to content

Commit 880d604

Browse files
committed
first commit
0 parents  commit 880d604

File tree

3 files changed

+490
-0
lines changed

3 files changed

+490
-0
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# url_monitor (A Telegraf Input Plugin, Copy from http_response)
2+
3+
This input plugin will test HTTP/HTTPS connections.
4+
5+
### Configuration:
6+
7+
8+
```
9+
# HTTP/HTTPS request given an address a method and a timeout
10+
11+
[[inputs.url_monitor]]
12+
## Server address (default http://localhost)
13+
address = "http://github.com"
14+
## Set response_timeout (default 5 seconds)
15+
response_timeout = "5s"
16+
## HTTP Request Method
17+
method = "GET"
18+
## Whether to follow redirects from the server (defaults to false)
19+
follow_redirects = true
20+
## HTTP Request Headers (all values must be strings)
21+
# [inputs.url_monitor.headers]
22+
# Host = "github.com"
23+
## Optional HTTP Request Body
24+
# body = '''
25+
# {'fake':'data'}
26+
# '''
27+
28+
## Optional SSL Config
29+
# ssl_ca = "/etc/telegraf/ca.pem"
30+
# ssl_cert = "/etc/telegraf/cert.pem"
31+
# ssl_key = "/etc/telegraf/key.pem"
32+
## Use SSL but skip chain & host verification
33+
# insecure_skip_verify = false
34+
```
35+
36+
### Measurements & Fields:
37+
38+
39+
- url_monitor
40+
- response_time (float, seconds)
41+
- url_monitor_code (int) #The code received
42+
43+
### Tags:
44+
45+
- All measurements have the following tags:
46+
- server
47+
- method
48+
49+
### Example Output:
50+
51+
```
52+
$ ./telegraf -config telegraf.conf -input-filter url_monitor -test
53+
url_monitor,method=GET,server=http://www.github.com url_monitor_code=200i,response_time=6.223266528 1459419354977857955
54+
```

url_monitor.go

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package url_monitor
2+
3+
import (
4+
"errors"
5+
"io"
6+
"net/http"
7+
"net/url"
8+
"strings"
9+
"time"
10+
11+
"github.com/influxdata/telegraf"
12+
"github.com/influxdata/telegraf/internal"
13+
"github.com/influxdata/telegraf/plugins/inputs"
14+
)
15+
16+
// HTTPResponse struct
17+
type HTTPResponse struct {
18+
Address string
19+
Body string
20+
Method string
21+
ResponseTimeout internal.Duration
22+
Headers map[string]string
23+
FollowRedirects bool
24+
25+
// Path to CA file
26+
SSLCA string `toml:"ssl_ca"`
27+
// Path to host cert file
28+
SSLCert string `toml:"ssl_cert"`
29+
// Path to cert key file
30+
SSLKey string `toml:"ssl_key"`
31+
// Use SSL but skip chain & host verification
32+
InsecureSkipVerify bool
33+
}
34+
35+
// Description returns the plugin Description
36+
func (h *HTTPResponse) Description() string {
37+
return "HTTP/HTTPS request given an address a method and a timeout"
38+
}
39+
40+
var sampleConfig = `
41+
## Server address (default http://localhost)
42+
address = "http://github.com"
43+
## Set response_timeout (default 5 seconds)
44+
response_timeout = "5s"
45+
## HTTP Request Method
46+
method = "GET"
47+
## Whether to follow redirects from the server (defaults to false)
48+
follow_redirects = true
49+
## HTTP Request Headers (all values must be strings)
50+
# [inputs.url_monitor.headers]
51+
# Host = "github.com"
52+
## Optional HTTP Request Body
53+
# body = '''
54+
# {'fake':'data'}
55+
# '''
56+
57+
## Optional SSL Config
58+
# ssl_ca = "/etc/telegraf/ca.pem"
59+
# ssl_cert = "/etc/telegraf/cert.pem"
60+
# ssl_key = "/etc/telegraf/key.pem"
61+
## Use SSL but skip chain & host verification
62+
# insecure_skip_verify = false
63+
`
64+
65+
// SampleConfig returns the plugin SampleConfig
66+
func (h *HTTPResponse) SampleConfig() string {
67+
return sampleConfig
68+
}
69+
70+
// ErrRedirectAttempted indicates that a redirect occurred
71+
var ErrRedirectAttempted = errors.New("redirect")
72+
73+
// CreateHttpClient creates an http client which will timeout at the specified
74+
// timeout period and can follow redirects if specified
75+
func (h *HTTPResponse) createHttpClient() (*http.Client, error) {
76+
tlsCfg, err := internal.GetTLSConfig(
77+
h.SSLCert, h.SSLKey, h.SSLCA, h.InsecureSkipVerify)
78+
if err != nil {
79+
return nil, err
80+
}
81+
tr := &http.Transport{
82+
ResponseHeaderTimeout: h.ResponseTimeout.Duration,
83+
TLSClientConfig: tlsCfg,
84+
}
85+
client := &http.Client{
86+
Transport: tr,
87+
Timeout: h.ResponseTimeout.Duration,
88+
}
89+
90+
if h.FollowRedirects == false {
91+
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
92+
return ErrRedirectAttempted
93+
}
94+
}
95+
return client, nil
96+
}
97+
98+
// HTTPGather gathers all fields and returns any errors it encounters
99+
func (h *HTTPResponse) HTTPGather() (map[string]interface{}, error) {
100+
// Prepare fields
101+
fields := make(map[string]interface{})
102+
103+
client, err := h.createHttpClient()
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
var body io.Reader
109+
if h.Body != "" {
110+
body = strings.NewReader(h.Body)
111+
}
112+
request, err := http.NewRequest(h.Method, h.Address, body)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
for key, val := range h.Headers {
118+
request.Header.Add(key, val)
119+
if key == "Host" {
120+
request.Host = val
121+
}
122+
}
123+
124+
// Start Timer
125+
start := time.Now()
126+
resp, err := client.Do(request)
127+
if err != nil {
128+
if h.FollowRedirects {
129+
return nil, err
130+
}
131+
if urlError, ok := err.(*url.Error); ok &&
132+
urlError.Err == ErrRedirectAttempted {
133+
err = nil
134+
} else {
135+
return nil, err
136+
}
137+
}
138+
fields["response_time"] = time.Since(start).Seconds()
139+
fields["url_monitor_code"] = resp.StatusCode
140+
return fields, nil
141+
}
142+
143+
// Gather gets all metric fields and tags and returns any errors it encounters
144+
func (h *HTTPResponse) Gather(acc telegraf.Accumulator) error {
145+
// Set default values
146+
if h.ResponseTimeout.Duration < time.Second {
147+
h.ResponseTimeout.Duration = time.Second * 5
148+
}
149+
// Check send and expected string
150+
if h.Method == "" {
151+
h.Method = "GET"
152+
}
153+
if h.Address == "" {
154+
h.Address = "http://localhost"
155+
}
156+
addr, err := url.Parse(h.Address)
157+
if err != nil {
158+
return err
159+
}
160+
if addr.Scheme != "http" && addr.Scheme != "https" {
161+
return errors.New("Only http and https are supported")
162+
}
163+
// Prepare data
164+
tags := map[string]string{"server": h.Address, "method": h.Method}
165+
var fields map[string]interface{}
166+
// Gather data
167+
fields, err = h.HTTPGather()
168+
if err != nil {
169+
return err
170+
}
171+
// Add metrics
172+
acc.AddFields("url_monitor", fields, tags)
173+
return nil
174+
}
175+
176+
func init() {
177+
inputs.Add("url_monitor", func() telegraf.Input {
178+
return &HTTPResponse{}
179+
})
180+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy