Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加whois插件,用于探测域名到期时间 #584

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions agent/metrics_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ import (
_ "flashcat.cloud/categraf/inputs/systemd"
_ "flashcat.cloud/categraf/inputs/tomcat"
_ "flashcat.cloud/categraf/inputs/vsphere"
_ "flashcat.cloud/categraf/inputs/whois"
_ "flashcat.cloud/categraf/inputs/xskyapi"
_ "flashcat.cloud/categraf/inputs/zookeeper"
)
Expand Down
24 changes: 24 additions & 0 deletions conf/input.whois/whois.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# # collect interval
#interval = 3600

#[[instances]]
## Used to collect domain name information.
#domain = "baidu.com"

## append some labels for series
#labels = { region="n9e", product="test1" }

## interval = global.interval * interval_times
#interval_times = 1


#[[instances]]
## Used to collect domain name information.
#domain = "google.com"

## append some labels for series
#labels = { region="n9e", product="test2" }

## interval = global.interval * interval_times
#interval_times = 1

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ require (
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/likexian/gokit v0.25.13 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/resourcetotelemetry v0.54.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/prometheusremotewrite v0.54.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
Expand All @@ -129,6 +130,8 @@ require (
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.5.0
github.com/aws/aws-sdk-go-v2/service/sts v1.18.3
github.com/coreos/go-systemd/v22 v22.3.2
github.com/likexian/whois v1.15.0
github.com/likexian/whois-parser v1.24.8
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/prometheusremotewriteexporter v0.54.0
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/prometheusreceiver v0.54.0
github.com/prometheus-community/pro-bing v0.1.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,12 @@ github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/likexian/gokit v0.25.13 h1:p2Uw3+6fGG53CwdU2Dz0T6bOycdb2+bAFAa3ymwWVkM=
github.com/likexian/gokit v0.25.13/go.mod h1:qQhEWFBEfqLCO3/vOEo2EDKd+EycekVtUK4tex+l2H4=
github.com/likexian/whois v1.15.0 h1:AYYJ5bNUo8Qy2T1Z5GgMp1oIcIlCcTDfg1buYz6TdAE=
github.com/likexian/whois v1.15.0/go.mod h1:456fUTkh+O8F8v09bGdVl7XxBjRaQ4LvYHyVWX5Bxyg=
github.com/likexian/whois-parser v1.24.8 h1:41RCpnRXSvlo9U7vPuLORi187UkTURH+8HXUcGdnhZA=
github.com/likexian/whois-parser v1.24.8/go.mod h1:b6STMHHDaSKbd4PzGrP50wWE5NzeBUETa/hT9gI0G9I=
github.com/linode/linodego v1.8.0 h1:7B2UaWu6C48tZZZrtINWRElAcwzk4TLnL9USjKf3xm0=
github.com/linode/linodego v1.8.0/go.mod h1:heqhl91D8QTPVm2k9qZHP78zzbOdTFLXE9NJc3bcc50=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
Expand Down
6 changes: 3 additions & 3 deletions inputs/nsq/nsq.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ func (ins *Instance) getQueuesInfo(topicName string) (int, error) {
func (ins *Instance) Gather(slist *types.SampleList) {
topics, err := ins.GetTopicInfo()
if err != nil {
log.Println("Failed to obtain the topic list error:", err)
log.Println("E! Failed to obtain the topic list error:", err)
} else {
for _, topic := range topics {
v, err := ins.getQueuesInfo(topic)
if err != nil {
v = 0
log.Println("Failed to obtain topic depth value error:", err)
log.Println("E! Failed to obtain topic depth value error:", err)
}
fields := map[string]interface{}{
"channel_depth": v,
Expand All @@ -166,7 +166,7 @@ func (ins *Instance) Gather(slist *types.SampleList) {
"topic_name": topic,
}

slist.PushSamples("nsq", fields, tags)
slist.PushSamples(inputName, fields, tags)
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions inputs/whois/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# whois

域名探测插件,用于探测域名的注册时间和到期时间,值为UTC0时间戳


## Configuration

最核心的配置就是 domain 配置,配置目标地址,比如想要监控一个地址:
默认保持注释状态,注释状态下,插件默认不启用

```toml
# [[instances]]
## Used to collect domain name information.
# domain = "baidu.com"
```
请注意这里配置的是域名不是URL

## 指标解释

whois_domain_createddate 域名创建时间戳
whois_domain_updateddate 域名更新时间戳
whois_domain_expirationdate 域名到期时间戳

## 注意事项
请不要将interval设置过短,会导致频繁请求timeout,没太大必要性,请尽量放长请求周期
127 changes: 127 additions & 0 deletions inputs/whois/whois.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package whois

import (
"log"
"time"

"github.com/likexian/whois"
whoisparser "github.com/likexian/whois-parser"

"flashcat.cloud/categraf/config"
kongfei605 marked this conversation as resolved.
Show resolved Hide resolved
"flashcat.cloud/categraf/inputs"
"flashcat.cloud/categraf/types"
)

const inputName = "whois"

type Whois struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
}

func init() {
inputs.Add(inputName, func() inputs.Input {
return &Whois{}
})
}

func (wh *Whois) Clone() inputs.Input {
return &Whois{}
}

func (wh *Whois) Name() string {
return inputName
}

kongfei605 marked this conversation as resolved.
Show resolved Hide resolved
func (wh *Whois) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(wh.Instances))
for i := 0; i < len(wh.Instances); i++ {
ret[i] = wh.Instances[i]
}
return ret
}

type Instance struct {
config.InstanceConfig
Domain string `toml:"domain"`
}

func (ins *Instance) Empty() bool {
if len(ins.Domain) > 0 {
return false
}

return true
}
func (ins *Instance) Init() error {
if ins.Empty() {
return types.ErrInstancesEmpty
}

return nil
}

func (ins *Instance) Gather(slist *types.SampleList) {
// 执行 Whois 查询
result, err := whois.Whois(ins.Domain)
if err != nil {
log.Println("E! query", ins.Domain, "domain information failed:", err)
return
}

// 使用 whois-parser 解析结果
parsedResult, err := whoisparser.Parse(result)
if err != nil {
log.Println("E! parse", ins.Domain, "domain whois result failure:", err)
return
}

// 输出解析后的信息
if parsedResult.Domain.CreatedDate != "" && parsedResult.Domain.ExpirationDate != "" && parsedResult.Domain.ExpirationDate != "" {
CreatedDate, err := ParseTimeToUTCTimestamp(parsedResult.Domain.CreatedDate)
if err != nil {
log.Println("E! parsing creation time:", parsedResult.Domain.CreatedDate, "time string failure:", err)
return
}
UpdatedDate, err := ParseTimeToUTCTimestamp(parsedResult.Domain.UpdatedDate)
if err != nil {
log.Println("E! parsing update time:", parsedResult.Domain.UpdatedDate, "time string failure:", err)
return
}
ExpirationDate, err := ParseTimeToUTCTimestamp(parsedResult.Domain.ExpirationDate)
if err != nil {
log.Println("E! parsing expiration time:", parsedResult.Domain.ExpirationDate, "time string failure:", err)
return
}

fields := map[string]interface{}{
"domain_createddate": CreatedDate,
"domain_updateddate": UpdatedDate,
"domain_expirationdate": ExpirationDate,
}
tags := map[string]string{
"domain": ins.Domain,
}

slist.PushSamples(inputName, fields, tags)

} else {
log.Println("E! creation or expiration time is null")
return
}

}

// ParseTimeToUTCTimestamp 将时间字符串解析为 UTC 时间戳
func ParseTimeToUTCTimestamp(timeStr string) (int64, error) {
// 解析时间字符串
parsedTime, err := time.Parse(time.RFC3339, timeStr)
if err != nil {
return 0, err
}

// 将时间转换为 UTC 时间戳
utcTimestamp := parsedTime.UTC().Unix()

return utcTimestamp, nil
}