-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add sweeper resources scan * update file common function * add log * update log print * update file generate path * update sweeper resource scan dir * generate csv file add Classification * delete cloud_common file * optimization add scanning resources * add scanning some resources * fix DescribeTargetGroups request Filters param * optimize Filters logic * update add scanning resources logic * csv file add CreationDuration * added handling of non-keep resources * add e2e scanning resources * update csv file title * update NonKeepResourceScanHeader * add err log print
- Loading branch information
1 parent
126bedc
commit 30f9713
Showing
32 changed files
with
732 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/csv" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
const ( | ||
SweeperResourceScanDir = "../../../tmp/resource_scan/" | ||
SweeperNonKeepResourceScanDir = "../../../tmp/non_keep_resource_scan/" | ||
) | ||
|
||
var ResourceScanHeader = []string{"资源类型", "资源名称", "实例ID", "实例名称", "分类", "创建时长(天)"} | ||
var NonKeepResourceScanHeader = []string{"ResourceType", "ResourceName", "InstanceId", "InstanceName"} | ||
|
||
// WriteCsvFileData write data to csv file | ||
func WriteCsvFileData(dirPath string, header []string, data [][]string) error { | ||
log.Printf("[INFO] write csv file data[%v] to path[%v] start", len(data), dirPath) | ||
|
||
count := 0 | ||
defer func() { | ||
log.Printf("[INFO] write csv file data to path[%v] success count[%v]", dirPath, count) | ||
}() | ||
|
||
if len(data) == 0 { | ||
return nil | ||
} | ||
|
||
err := os.MkdirAll(dirPath, 0755) | ||
if err != nil { | ||
log.Printf("[CRITAL] create directory %s error: %v", dirPath, err.Error()) | ||
return err | ||
} | ||
|
||
currentDate := time.Now().Format("20060102") | ||
filePath := filepath.Join(dirPath, currentDate+".csv") | ||
|
||
_, err = os.Stat(filePath) | ||
if os.IsNotExist(err) { | ||
err = GenerateCsvFile(filePath, header) | ||
if err != nil { | ||
log.Printf("[CRITAL] generate csv file error: %v", err.Error()) | ||
return err | ||
} | ||
} | ||
|
||
file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0644) | ||
if err != nil { | ||
log.Printf("[CRITAL] open csv file error: %v", err.Error()) | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
writer := csv.NewWriter(file) | ||
|
||
for _, row := range data { | ||
err = writer.Write(row) | ||
if err != nil { | ||
log.Printf("[CRITAL] write data[%v] to csv file error: %v", row, err.Error()) | ||
return err | ||
} | ||
count++ | ||
} | ||
writer.Flush() | ||
|
||
return nil | ||
} | ||
|
||
// GenerateCsvFile generate when csv file does not exist | ||
func GenerateCsvFile(filePath string, header []string) error { | ||
file, err := os.Create(filePath) | ||
if err != nil { | ||
log.Printf("[CRITAL] create csv file error: %v", err.Error()) | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
writer := csv.NewWriter(file) | ||
err = writer.Write(header) | ||
if err != nil { | ||
log.Printf("[CRITAL] write header to csv file error: %v", err.Error()) | ||
return err | ||
} | ||
writer.Flush() | ||
|
||
return nil | ||
} |
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,148 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
KeepResource = "keep" | ||
NonKeepResource = "non-keep" | ||
) | ||
|
||
// TimeFormats add all possible time formats | ||
var TimeFormats = []string{ | ||
time.RFC3339, //ISO8601 UTC time | ||
"2006-01-02 15:04:05", | ||
// add other time formats here | ||
} | ||
|
||
type ResourceInstance struct { | ||
Id string | ||
Name string | ||
CreatTime string | ||
DefaultKeep bool | ||
} | ||
|
||
func ProcessScanCloudResources(resources, nonKeepResources []*ResourceInstance, resourceType, resourceName string) { | ||
ProcessResources(resources, resourceType, resourceName) | ||
|
||
ProcessNonKeepResources(nonKeepResources, resourceType, resourceName) | ||
} | ||
|
||
// ProcessResources Process all scanned cloud resources | ||
func ProcessResources(resources []*ResourceInstance, resourceType, resourceName string) { | ||
data := make([][]string, len(resources)) | ||
for i, r := range resources { | ||
isResourceKeep := CheckResourceNameKeep(r.Name) | ||
// some resources default to keep | ||
if r.DefaultKeep { | ||
isResourceKeep = KeepResource | ||
} | ||
|
||
creationDuration, err := DaysSinceCreation(r.CreatTime) | ||
if err != nil { | ||
log.Printf("[CRITAL] compute resource creation duration error: %v", err.Error()) | ||
} | ||
|
||
data[i] = []string{ | ||
resourceType, | ||
resourceName, | ||
r.Id, | ||
r.Name, | ||
isResourceKeep, | ||
creationDuration, | ||
} | ||
} | ||
err := WriteCsvFileData(SweeperResourceScanDir, ResourceScanHeader, data) | ||
if err != nil { | ||
log.Printf("[CRITAL] write csv file data error: %v", err.Error()) | ||
} | ||
} | ||
|
||
// ProcessNonKeepResources Processing scanned non-keep cloud resources | ||
func ProcessNonKeepResources(nonKeepResources []*ResourceInstance, resourceType, resourceName string) { | ||
data := make([][]string, len(nonKeepResources)) | ||
for i, r := range nonKeepResources { | ||
data[i] = []string{ | ||
resourceType, | ||
resourceName, | ||
r.Id, | ||
r.Name, | ||
} | ||
} | ||
err := WriteCsvFileData(SweeperNonKeepResourceScanDir, NonKeepResourceScanHeader, data) | ||
if err != nil { | ||
log.Printf("[CRITAL] write csv file data error: %v", err.Error()) | ||
} | ||
} | ||
|
||
// CheckResourceNameKeep check whether to keep resource name | ||
func CheckResourceNameKeep(name string) string { | ||
flag := CheckResourcePersist(name, "") | ||
if flag { | ||
return KeepResource | ||
} | ||
return NonKeepResource | ||
} | ||
|
||
// CheckResourcePersist check whether to persist resource | ||
func CheckResourcePersist(name, createTime string) bool { | ||
if name == "" && createTime == "" { | ||
return false | ||
} | ||
parsedTime, _ := ParsedTime(createTime) | ||
|
||
createdWithin30Minutes := false | ||
if parsedTime != nil { | ||
createdWithin30Minutes = parsedTime.Add(time.Minute * 30).After(time.Now()) | ||
} | ||
|
||
flag := regexp.MustCompile("^(keep|Default)").MatchString(name) | ||
return flag || createdWithin30Minutes | ||
} | ||
|
||
// DaysSinceCreation compute resource creation duration | ||
func DaysSinceCreation(createTime string) (string, error) { | ||
parsedTime, err := ParsedTime(createTime) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
duration := time.Since(*parsedTime) | ||
days := duration.Hours() / 24 | ||
|
||
return fmt.Sprintf("%.2f", days), nil | ||
} | ||
|
||
// ParsedTime parse time | ||
func ParsedTime(createTime string) (*time.Time, error) { | ||
if createTime == "" { | ||
return nil, nil | ||
} | ||
|
||
var parsedTime time.Time | ||
var err error | ||
|
||
timestamp, err := strconv.ParseInt(createTime, 10, 64) | ||
if err == nil { | ||
parsedTime = time.Unix(timestamp, 0) | ||
} else { | ||
// try parsing input strings using different time formats | ||
for _, format := range TimeFormats { | ||
parsedTime, err = time.Parse(format, createTime) | ||
if err == nil { | ||
break | ||
} | ||
} | ||
} | ||
|
||
if err != nil { | ||
log.Printf("[CRITAL] unable to parse create time[%s]", createTime) | ||
return nil, fmt.Errorf("unable to parse create time: %v", err.Error()) | ||
} | ||
return &parsedTime, nil | ||
} |
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
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
Oops, something went wrong.