2024-11-06 00:09:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/csv"
|
2024-11-06 04:34:16 +00:00
|
|
|
"flag"
|
2024-11-06 00:09:54 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-11-06 04:54:16 +00:00
|
|
|
"os"
|
2024-11-06 00:09:54 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
"code.stevenpolley.net/steven/cfcleaner/cf"
|
|
|
|
)
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
func init() {
|
2024-11-06 00:09:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
func main() {
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
cfAPIKey := flag.String("cfapikey", "", "in cf, my profile -> API Tokens -> Create Token")
|
2024-11-06 04:54:16 +00:00
|
|
|
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")
|
2024-11-06 04:34:16 +00:00
|
|
|
flag.Parse()
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
if *cfAPIKey == "" {
|
|
|
|
fmt.Println("you must specify an API key with the -cfapikey=<key> parameter")
|
|
|
|
return
|
|
|
|
}
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
cfClient := cf.NewClient(*cfAPIKey)
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
zones, err := cfClient.GetZones()
|
2024-11-06 00:09:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get zones: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
outRows := make([]cf.Record, 0)
|
2024-11-06 00:09:54 +00:00
|
|
|
|
|
|
|
for _, zone := range zones {
|
2024-11-06 04:54:16 +00:00
|
|
|
if *onlyZone != "" && zone.Name != *onlyZone {
|
|
|
|
continue
|
|
|
|
}
|
2024-11-06 00:09:54 +00:00
|
|
|
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
|
|
|
|
2024-11-06 04:34:16 +00:00
|
|
|
records, err := cfClient.GetRecords(zone)
|
2024-11-06 00:09:54 +00:00
|
|
|
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 {
|
2024-11-06 04:34:16 +00:00
|
|
|
analytic, err := cfClient.GetRecordAnalytic(record)
|
2024-11-06 00:09:54 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
record.NumberQueries = analytic.Result.Totals.QueryCount
|
|
|
|
outRows = append(outRows, record)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(outRows, func(i, j int) bool {
|
|
|
|
return outRows[i].NumberQueries < outRows[j].NumberQueries
|
|
|
|
})
|
|
|
|
|
2024-11-06 05:15:21 +00:00
|
|
|
f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
2024-11-06 04:54:16 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to open output file '%s': %v", *outFile, err)
|
2024-11-06 00:09:54 +00:00
|
|
|
}
|
2024-11-06 04:54:16 +00:00
|
|
|
defer f.Close()
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:54:16 +00:00
|
|
|
writer := csv.NewWriter(f)
|
|
|
|
defer writer.Flush()
|
|
|
|
err = writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"})
|
2024-11-06 00:09:54 +00:00
|
|
|
if err != nil {
|
2024-11-06 04:54:16 +00:00
|
|
|
log.Fatalf("failed to write to outFile: %v", err)
|
2024-11-06 00:09:54 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 04:54:16 +00:00
|
|
|
for _, row := range outRows {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-11-06 00:09:54 +00:00
|
|
|
|
2024-11-06 04:54:16 +00:00
|
|
|
fmt.Printf("wrote to file: %s\n", *outFile)
|
2024-11-06 00:09:54 +00:00
|
|
|
}
|