-
Notifications
You must be signed in to change notification settings - Fork 236
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
增加whois插件,用于探测域名到期时间 #584
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# 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,没太大必要性,请尽量放长请求周期 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package whois | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"flashcat.cloud/categraf/config" | ||
kongfei605 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"flashcat.cloud/categraf/inputs" | ||
"flashcat.cloud/categraf/types" | ||
"github.com/likexian/whois" | ||
whoisparser "github.com/likexian/whois-parser" | ||
) | ||
|
||
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) 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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里默认应该保持注释状态,注释状态下,插件默认不启用
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是介绍文档,注不注释不太重要吧