1
0
Fork 0

Adding upstream version 1.34.4.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 07:26:29 +02:00
parent e393c3af3f
commit 4978089aab
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
4963 changed files with 677545 additions and 0 deletions

View file

@ -0,0 +1,93 @@
# Nginx Upstream Check Input Plugin
This plugin gathers metrics from the [Nginx web server][nginx] using the
[upstream check module][upstream_check_module]. This module periodically sends
the configured requests to servers in the Nginx's upstream determining their
availability.
⭐ Telegraf v1.10.0
🏷️ server, web
💻 all
[nginx]: https://www.nginx.com
[upstream_check_module]: https://github.com/yaoweibin/nginx_upstream_check_module
## Global configuration options <!-- @/docs/includes/plugin_config.md -->
In addition to the plugin-specific configuration settings, plugins support
additional global and plugin configuration settings. These settings are used to
modify metrics, tags, and field or create aliases and configure ordering, etc.
See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
[CONFIGURATION.md]: ../../../docs/CONFIGURATION.md#plugins
## Configuration
```toml @sample.conf
# Read nginx_upstream_check module status information (https://github.com/yaoweibin/nginx_upstream_check_module)
[[inputs.nginx_upstream_check]]
## An URL where Nginx Upstream check module is enabled
## It should be set to return a JSON formatted response
url = "http://127.0.0.1/status?format=json"
## HTTP method
# method = "GET"
## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}
## Override HTTP "Host" header
# host_header = "check.example.com"
## Timeout for HTTP requests
timeout = "5s"
## Optional HTTP Basic Auth credentials
# username = "username"
# password = "pa$$word"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false
```
## Metrics
- Measurement
- fall (The number of failed server check attempts, counter)
- rise (The number of successful server check attempts, counter)
- status (The reporter server status as a string)
- status_code (The server status code. 1 - up, 2 - down, 0 - other)
The "status_code" field most likely will be the most useful one because it
allows you to determine the current state of every server and, possible, add
some monitoring to watch over it. InfluxDB can use string values and the
"status" field can be used instead, but for most other monitoring solutions the
integer code will be appropriate.
### Tags
- All measurements have the following tags:
- name (The hostname or IP of the upstream server)
- port (The alternative check port, 0 if the default one is used)
- type (The check type, http/tcp)
- upstream (The name of the upstream block in the Nginx configuration)
- url (The status url used by telegraf)
## Example Output
When run with:
```sh
./telegraf --config telegraf.conf --input-filter nginx_upstream_check --test
```
It produces:
```text
nginx_upstream_check,host=node1,name=192.168.0.1:8080,port=0,type=http,upstream=my_backends,url=http://127.0.0.1:80/status?format\=json fall=0i,rise=100i,status="up",status_code=1i 1529088524000000000
nginx_upstream_check,host=node2,name=192.168.0.2:8080,port=0,type=http,upstream=my_backends,url=http://127.0.0.1:80/status?format\=json fall=100i,rise=0i,status="down",status_code=2i 1529088524000000000
```

View file

@ -0,0 +1,199 @@
//go:generate ../../../tools/readme_config_includer/generator
package nginx_upstream_check
import (
_ "embed"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
type NginxUpstreamCheck struct {
URL string `toml:"url"`
Username string `toml:"username"`
Password string `toml:"password"`
Method string `toml:"method"`
Headers map[string]string `toml:"headers"`
HostHeader string `toml:"host_header"`
Timeout config.Duration `toml:"timeout"`
tls.ClientConfig
client *http.Client
}
type nginxUpstreamCheckData struct {
Servers struct {
Total uint64 `json:"total"`
Generation uint64 `json:"generation"`
Server []nginxUpstreamCheckServer `json:"server"`
} `json:"servers"`
}
type nginxUpstreamCheckServer struct {
Index uint64 `json:"index"`
Upstream string `json:"upstream"`
Name string `json:"name"`
Status string `json:"status"`
Rise uint64 `json:"rise"`
Fall uint64 `json:"fall"`
Type string `json:"type"`
Port uint16 `json:"port"`
}
func (*NginxUpstreamCheck) SampleConfig() string {
return sampleConfig
}
func (check *NginxUpstreamCheck) Gather(accumulator telegraf.Accumulator) error {
if check.client == nil {
client, err := check.createHTTPClient()
if err != nil {
return err
}
check.client = client
}
statusURL, err := url.Parse(check.URL)
if err != nil {
return err
}
err = check.gatherStatusData(statusURL.String(), accumulator)
if err != nil {
return err
}
return nil
}
// createHTTPClient create a clients to access API
func (check *NginxUpstreamCheck) createHTTPClient() (*http.Client, error) {
tlsConfig, err := check.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
Timeout: time.Duration(check.Timeout),
}
return client, nil
}
// gatherJSONData query the data source and parse the response JSON
func (check *NginxUpstreamCheck) gatherJSONData(address string, value interface{}) error {
var method string
if check.Method != "" {
method = check.Method
} else {
method = "GET"
}
request, err := http.NewRequest(method, address, nil)
if err != nil {
return err
}
if (check.Username != "") || (check.Password != "") {
request.SetBasicAuth(check.Username, check.Password)
}
for header, value := range check.Headers {
request.Header.Add(header, value)
}
if check.HostHeader != "" {
request.Host = check.HostHeader
}
response, err := check.client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
//nolint:errcheck // LimitReader returns io.EOF and we're not interested in read errors.
body, _ := io.ReadAll(io.LimitReader(response.Body, 200))
return fmt.Errorf("%s returned HTTP status %s: %q", address, response.Status, body)
}
err = json.NewDecoder(response.Body).Decode(value)
if err != nil {
return err
}
return nil
}
func (check *NginxUpstreamCheck) gatherStatusData(address string, accumulator telegraf.Accumulator) error {
checkData := &nginxUpstreamCheckData{}
err := check.gatherJSONData(address, checkData)
if err != nil {
return err
}
for _, server := range checkData.Servers.Server {
tags := map[string]string{
"upstream": server.Upstream,
"type": server.Type,
"name": server.Name,
"port": strconv.Itoa(int(server.Port)),
"url": address,
}
fields := map[string]interface{}{
"status": server.Status,
"status_code": getStatusCode(server.Status),
"rise": server.Rise,
"fall": server.Fall,
}
accumulator.AddFields("nginx_upstream_check", fields, tags)
}
return nil
}
func getStatusCode(status string) uint8 {
switch status {
case "up":
return 1
case "down":
return 2
default:
return 0
}
}
func newNginxUpstreamCheck() *NginxUpstreamCheck {
return &NginxUpstreamCheck{
URL: "http://127.0.0.1/status?format=json",
Method: "GET",
Headers: make(map[string]string),
HostHeader: "",
Timeout: config.Duration(time.Second * 5),
}
}
func init() {
inputs.Add("nginx_upstream_check", func() telegraf.Input {
return newNginxUpstreamCheck()
})
}

View file

@ -0,0 +1,154 @@
package nginx_upstream_check
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/testutil"
)
const sampleStatusResponse = `
{
"servers": {
"total": 2,
"generation": 1,
"server": [
{
"index": 0,
"upstream": "upstream-1",
"name": "127.0.0.1:8081",
"status": "up",
"rise": 1000,
"fall": 0,
"type": "http",
"port": 0
},
{
"index": 1,
"upstream": "upstream-2",
"name": "127.0.0.1:8082",
"status": "down",
"rise": 0,
"fall": 2000,
"type": "tcp",
"port": 8080
}
]
}
}`
func TestNginxUpstreamCheckData(test *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/status" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Cannot handle request, expected: %q, actual: %q", "/status", request.URL.Path)
return
}
responseWriter.Header()["Content-Type"] = []string{"application/json"}
if _, err := fmt.Fprintln(responseWriter, sampleStatusResponse); err != nil {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Error(err)
return
}
}))
defer testServer.Close()
check := newNginxUpstreamCheck()
check.URL = testServer.URL + "/status"
var accumulator testutil.Accumulator
checkError := check.Gather(&accumulator)
require.NoError(test, checkError)
accumulator.AssertContainsTaggedFields(
test,
"nginx_upstream_check",
map[string]interface{}{
"status": "up",
"status_code": uint8(1),
"rise": uint64(1000),
"fall": uint64(0),
},
map[string]string{
"upstream": "upstream-1",
"type": "http",
"name": "127.0.0.1:8081",
"port": "0",
"url": testServer.URL + "/status",
})
accumulator.AssertContainsTaggedFields(
test,
"nginx_upstream_check",
map[string]interface{}{
"status": "down",
"status_code": uint8(2),
"rise": uint64(0),
"fall": uint64(2000),
},
map[string]string{
"upstream": "upstream-2",
"type": "tcp",
"name": "127.0.0.1:8082",
"port": "8080",
"url": testServer.URL + "/status",
})
}
func TestNginxUpstreamCheckRequest(test *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(responseWriter http.ResponseWriter, request *http.Request) {
if request.URL.Path != "/status" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Cannot handle request, expected: %q, actual: %q", "/status", request.URL.Path)
return
}
responseWriter.Header()["Content-Type"] = []string{"application/json"}
if _, err := fmt.Fprintln(responseWriter, sampleStatusResponse); err != nil {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Error(err)
return
}
if request.Method != "POST" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Not equal, expected: %q, actual: %q", "POST", request.Method)
return
}
if request.Header.Get("X-Test") != "test-value" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Not equal, expected: %q, actual: %q", "test-value", request.Header.Get("X-Test"))
return
}
if request.Header.Get("Authorization") != "Basic dXNlcjpwYXNzd29yZA==" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Not equal, expected: %q, actual: %q", "Basic dXNlcjpwYXNzd29yZA==", request.Header.Get("Authorization"))
return
}
if request.Host != "status.local" {
responseWriter.WriteHeader(http.StatusInternalServerError)
test.Errorf("Not equal, expected: %q, actual: %q", "status.local", request.Host)
return
}
}))
defer testServer.Close()
check := newNginxUpstreamCheck()
check.URL = testServer.URL + "/status"
check.Headers["X-test"] = "test-value"
check.HostHeader = "status.local"
check.Username = "user"
check.Password = "password"
check.Method = "POST"
var accumulator testutil.Accumulator
checkError := check.Gather(&accumulator)
require.NoError(test, checkError)
}

View file

@ -0,0 +1,28 @@
# Read nginx_upstream_check module status information (https://github.com/yaoweibin/nginx_upstream_check_module)
[[inputs.nginx_upstream_check]]
## An URL where Nginx Upstream check module is enabled
## It should be set to return a JSON formatted response
url = "http://127.0.0.1/status?format=json"
## HTTP method
# method = "GET"
## Optional HTTP headers
# headers = {"X-Special-Header" = "Special-Value"}
## Override HTTP "Host" header
# host_header = "check.example.com"
## Timeout for HTTP requests
timeout = "5s"
## Optional HTTP Basic Auth credentials
# username = "username"
# password = "pa$$word"
## Optional TLS Config
# tls_ca = "/etc/telegraf/ca.pem"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false