Compare commits
5 Commits
9028d9a69e
...
d2d15701ab
Author | SHA1 | Date | |
---|---|---|---|
d2d15701ab | |||
656988e17f | |||
926593d59c | |||
c90a3c0543 | |||
f7a2922d7d |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
env.sh
|
||||
cfcleaner
|
||||
*.exe
|
||||
*.exe
|
||||
*.csv
|
46
cf/cf.go
46
cf/cf.go
@ -57,7 +57,8 @@ type Analytic struct {
|
||||
Result struct {
|
||||
Rows int `json:"rows"`
|
||||
Data []struct {
|
||||
Metrics []int `json:"metrics"`
|
||||
Dimensions []string `json:"dimensions"`
|
||||
Metrics []int `json:"metrics"`
|
||||
} `json:"data"`
|
||||
DataLag int `json:"data_lag"`
|
||||
Min struct {
|
||||
@ -154,8 +155,9 @@ func (c *Client) GetRecordAnalytic(record Record) (*Analytic, error) {
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Add("filters", fmt.Sprintf("queryName==%s", record.Name))
|
||||
|
||||
/*
|
||||
TBD: Why does this give HTTP 400?
|
||||
TBD: Why does this give HTTP 403?
|
||||
q.Add("filters", fmt.Sprintf("queryName==%s,queryType==%s", record.Name, record.Type))
|
||||
*/
|
||||
req.URL.RawQuery = q.Encode()
|
||||
@ -184,3 +186,43 @@ func (c *Client) GetRecordAnalytic(record Record) (*Analytic, error) {
|
||||
|
||||
return analytic, nil
|
||||
}
|
||||
|
||||
// getRecordAnalytic provides a pointer to an Analytic for the given
|
||||
func (c *Client) GetZoneAnalytic(zone Zone) (*Analytic, error) {
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s/zones/%s/dns_analytics/report", c.APIURL, zone.ID), nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create new http request: %v", err)
|
||||
}
|
||||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.APIKey))
|
||||
|
||||
q := req.URL.Query()
|
||||
q.Add("metrics", "queryCount")
|
||||
q.Add("dimensions", "queryName")
|
||||
|
||||
req.URL.RawQuery = q.Encode()
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to execute http request: %v", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("http response has status '%d' but expected '%d'", resp.StatusCode, http.StatusOK)
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read http response body: %v", err)
|
||||
}
|
||||
|
||||
analytic := &Analytic{}
|
||||
err = json.Unmarshal(b, analytic)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal response body to struct: %v", err)
|
||||
}
|
||||
|
||||
return analytic, nil
|
||||
|
||||
}
|
||||
|
57
main.go
57
main.go
@ -1,12 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
@ -21,6 +20,8 @@ func init() {
|
||||
func main() {
|
||||
|
||||
cfAPIKey := flag.String("cfapikey", "", "in cf, my profile -> API Tokens -> Create Token")
|
||||
onlyZone := flag.String("onlyZone", "", "if specified, only the zone with this name will be processed. If omitted, all zones will be processed")
|
||||
outFile := flag.String("outFile", "cfcleaner.csv", "the path of the output file")
|
||||
flag.Parse()
|
||||
|
||||
if *cfAPIKey == "" {
|
||||
@ -38,21 +39,30 @@ func main() {
|
||||
outRows := make([]cf.Record, 0)
|
||||
|
||||
for _, zone := range zones {
|
||||
if *onlyZone != "" && zone.Name != *onlyZone {
|
||||
continue
|
||||
}
|
||||
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
||||
|
||||
analytic, err := cfClient.GetZoneAnalytic(zone)
|
||||
if err != nil {
|
||||
log.Printf("failed to get zone analytic for zone '%s': %v", zone.Name, err)
|
||||
}
|
||||
|
||||
records, err := cfClient.GetRecords(zone)
|
||||
if err != nil {
|
||||
log.Printf("failed to get records for zone '%s' with ID '%s': %v", zone.Name, zone.ID, err)
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
analytic, err := cfClient.GetRecordAnalytic(record)
|
||||
if err != nil {
|
||||
log.Printf("failed to get record report for record '%s' in zone '%s' with ID '%s': %v", record.Name, zone.Name, zone.ID, err)
|
||||
continue
|
||||
for i := range records {
|
||||
for _, metric := range analytic.Result.Data {
|
||||
if records[i].Name != metric.Dimensions[0] {
|
||||
continue
|
||||
}
|
||||
records[i].NumberQueries = metric.Metrics[0]
|
||||
break
|
||||
}
|
||||
record.NumberQueries = analytic.Result.Totals.QueryCount
|
||||
outRows = append(outRows, record)
|
||||
outRows = append(outRows, records[i])
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,20 +70,25 @@ func main() {
|
||||
return outRows[i].NumberQueries < outRows[j].NumberQueries
|
||||
})
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
writer := csv.NewWriter(buf)
|
||||
writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"})
|
||||
f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to open output file '%s': %v", *outFile, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
writer := csv.NewWriter(f)
|
||||
defer writer.Flush()
|
||||
err = writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write to outFile: %v", err)
|
||||
}
|
||||
|
||||
for _, row := range outRows {
|
||||
writer.Write([]string{row.Name, row.Type, strconv.Itoa(row.NumberQueries), row.CreatedOn.Format(time.RFC3339), row.ModifiedOn.Format(time.RFC3339), row.Comment, row.Content})
|
||||
}
|
||||
writer.Flush()
|
||||
|
||||
b, err := io.ReadAll(buf)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to read bytes from output buffer: %v", err)
|
||||
err = writer.Write([]string{row.Name, row.Type, strconv.Itoa(row.NumberQueries), row.CreatedOn.Format(time.RFC3339), row.ModifiedOn.Format(time.RFC3339), row.Comment, row.Content})
|
||||
if err != nil {
|
||||
log.Fatalf("failed to write to outFile: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(string(b))
|
||||
|
||||
fmt.Printf("wrote to file: %s\n", *outFile)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user