add nginx plugin (#156)

* add nginx plugin

* update nginx.toml for nginx plugin

* Update nginx.go

Co-authored-by: ulricqin <ulricqin@qq.com>
This commit is contained in:
lesteryou 2022-08-15 19:47:27 +08:00 committed by GitHub
parent 9dd6ca9372
commit a4702a5c60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 326 additions and 0 deletions

View File

@ -31,6 +31,7 @@ import (
_ "flashcat.cloud/categraf/inputs/net_response"
_ "flashcat.cloud/categraf/inputs/netstat"
_ "flashcat.cloud/categraf/inputs/nfsclient"
_ "flashcat.cloud/categraf/inputs/nginx"
_ "flashcat.cloud/categraf/inputs/nginx_upstream_check"
_ "flashcat.cloud/categraf/inputs/ntp"
_ "flashcat.cloud/categraf/inputs/nvidia_smi"

View File

@ -0,0 +1,36 @@
# # collect interval
# interval = 15
[[instances]]
## An array of Nginx stub_status URI to gather stats.
urls = [
# "http://192.168.0.216:8000/nginx_status",
# "https://www.baidu.com/ngx_status"
]
## append some labels for series
# labels = { region="cloud", product="n9e" }
## interval = global.interval * interval_times
# interval_times = 1
## Set response_timeout (default 5 seconds)
response_timeout = "5s"
## Whether to follow redirects from the server (defaults to false)
# follow_redirects = false
## Optional HTTP Basic Auth Credentials
#username = "admin"
#password = "admin"
## Optional headers
# headers = ["X-From", "categraf", "X-Xyz", "abc"]
## Optional TLS Config
# use_tls = false
# tls_ca = "/etc/categraf/ca.pem"
# tls_cert = "/etc/categraf/cert.pem"
# tls_key = "/etc/categraf/key.pem"
## Use TLS but skip chain & host verification
# insecure_skip_verify = false

25
inputs/nginx/README.md Normal file
View File

@ -0,0 +1,25 @@
# nginx
nginx 监控采集插件由telegraf改造而来。
该插件依赖**nginx**的 **http_stub_status_module**
在**nginx**中添加如下配置:
```nginx
server{
listen 8000;
server_name _;
location /nginx_status {
stub_status on;
access_log off;
}
}
```
## Configuration
请参考配置[示例](../../conf/input.nginx/nginx.toml)
## 监控大盘和告警规则
待更新...

264
inputs/nginx/nginx.go Normal file
View File

@ -0,0 +1,264 @@
package nginx
import (
"bufio"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/pkg/tls"
"flashcat.cloud/categraf/types"
)
const inputName = "nginx"
type Nginx struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
}
func init() {
inputs.Add(inputName, func() inputs.Input {
return &Nginx{}
})
}
func (ngx *Nginx) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(ngx.Instances))
for i := 0; i < len(ngx.Instances); i++ {
ret[i] = ngx.Instances[i]
}
return ret
}
type Instance struct {
config.InstanceConfig
Urls []string `toml:"urls"`
ResponseTimeout config.Duration `toml:"response_timeout"`
FollowRedirects bool `toml:"follow_redirects"`
Username string `toml:"username"`
Password string `toml:"password"`
Headers []string `toml:"headers"`
tls.ClientConfig
client *http.Client
}
func (ins *Instance) Init() error {
if len(ins.Urls) == 0 {
return types.ErrInstancesEmpty
}
if ins.ResponseTimeout < config.Duration(time.Second) {
ins.ResponseTimeout = config.Duration(time.Second * 5)
}
client, err := ins.createHTTPClient()
if err != nil {
return fmt.Errorf("failed to create http client: %v", err)
}
ins.client = client
for _, u := range ins.Urls {
addr, err := url.Parse(u)
if err != nil {
return fmt.Errorf("failed to parse the url: %s, error: %v", u, err)
}
if addr.Scheme != "http" && addr.Scheme != "https" {
return fmt.Errorf("only http and https are supported, url: %s", u)
}
}
return nil
}
func (ins *Instance) Gather(slist *types.SampleList) {
var wg sync.WaitGroup
if len(ins.Urls) == 0 {
return
}
for _, u := range ins.Urls {
addr, err := url.Parse(u)
if err != nil {
log.Println("E! failed to parse the url:", u, "error:", err)
continue
}
wg.Add(1)
go func(addr *url.URL) {
defer wg.Done()
if err := ins.gather(addr, slist); err != nil {
log.Println("E!", err)
}
}(addr)
}
wg.Wait()
}
func (ins *Instance) createHTTPClient() (*http.Client, error) {
tlsCfg, err := ins.ClientConfig.TLSConfig()
if err != nil {
return nil, err
}
trans := &http.Transport{
DisableKeepAlives: true,
}
if ins.UseTLS {
trans.TLSClientConfig = tlsCfg
}
client := &http.Client{
Transport: trans,
Timeout: time.Duration(ins.ResponseTimeout),
}
if !ins.FollowRedirects {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
return client, nil
}
func (ins *Instance) gather(addr *url.URL, slist *types.SampleList) error {
if config.Config.DebugMode {
log.Println("D! nginx... url:", addr)
}
var body io.Reader
request, err := http.NewRequest("GET", addr.String(), body)
if err != nil {
return fmt.Errorf("failed to create an HTTP request, url: %s, error: %s", addr.String(), err)
}
for i := 0; i < len(ins.Headers); i += 2 {
request.Header.Add(ins.Headers[i], ins.Headers[i+1])
if ins.Headers[i] == "Host" {
request.Host = ins.Headers[i+1]
}
}
if ins.Username != "" || ins.Password != "" {
request.SetBasicAuth(ins.Username, ins.Password)
}
resp, err := ins.client.Do(request)
if err != nil {
return fmt.Errorf("failed to request the url: %s, error: %s", addr.String(), err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Println("E! failed to close the body of client:", err)
}
}(resp.Body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("the HTTP response status exception, url: %s, status: %s", addr.String(), resp.Status)
}
r := bufio.NewReader(resp.Body)
// Active connections
_, err = r.ReadString(':')
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
line, err := r.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
active, err := strconv.ParseUint(strings.TrimSpace(line), 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
// Server accepts handled requests
_, err = r.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
line, err = r.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
data := strings.Fields(line)
accepts, err := strconv.ParseUint(data[0], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
handled, err := strconv.ParseUint(data[1], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
requests, err := strconv.ParseUint(data[2], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
// Reading/Writing/Waiting
line, err = r.ReadString('\n')
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
data = strings.Fields(line)
reading, err := strconv.ParseUint(data[1], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
writing, err := strconv.ParseUint(data[3], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
waiting, err := strconv.ParseUint(data[5], 10, 64)
if err != nil {
return fmt.Errorf("failed to parse the response, error: %s", err)
}
host, port, err := net.SplitHostPort(addr.Host)
if err != nil {
host = addr.Host
if addr.Scheme == "http" {
port = "80"
} else if addr.Scheme == "https" {
port = "443"
} else {
port = ""
}
}
fields := map[string]interface{}{
"active": active,
"accepts": accepts,
"handled": handled,
"requests": requests,
"reading": reading,
"writing": writing,
"waiting": waiting,
}
tags := map[string]string{
"server": host,
"port": port,
}
slist.PushSamples("nginx", fields, tags)
return nil
}