Skip to content

Commit

Permalink
Merge pull request #269 from chaspy/configurable-alert-warn-hours
Browse files Browse the repository at this point in the history
Make ALERT_HOURS and WARNING_HOURS configurable
  • Loading branch information
chaspy authored Jan 25, 2022
2 parents 838b986 + 93dcdc0 commit c8fee82
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ The following policy must be attached to the AWS role to be executed.
}
```

## Environment Variable

|name |required|default|description |
|----------------|--------|-------|--------------------------------------------------|
|ALERT_HOURS |no | 2160 | Time to determine "alert" status for EOL dates |
|WARNING_HOURS |no | 4320 | Time to determine "warning" status for EOL dates |
|AWS_API_INTERVAL|no | 300 | Interval between calls to the AWS API |

## Datadog Autodiscovery

If you use Datadog, you can use [Kubernetes Integration Autodiscovery](https://docs.datadoghq.com/agent/kubernetes/integrations/?tab=kubernetes) feature.
Expand Down
46 changes: 42 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,36 @@ func getInterval() (int, error) {
return integerGithubAPIInterval, nil
}

func getAlertHours() (int, error) {
const defaultAlertHours = 2160 // 90 days * 24 hour
alertHours := os.Getenv("ALERT_HOURS")
if len(alertHours) == 0 {
return defaultAlertHours, nil
}

integerAlertHours, err := strconv.Atoi(alertHours)
if err != nil {
return 0, fmt.Errorf("failed to read Alert Hours: %w", err)
}

return integerAlertHours, nil
}

func getWarningHours() (int, error) {
const defaultWarningHours = 4320 // 180 days * 24 hour
warningHours := os.Getenv("WARNING_HOURS")
if len(warningHours) == 0 {
return defaultWarningHours, nil
}

integerWarningHours, err := strconv.Atoi(warningHours)
if err != nil {
return 0, fmt.Errorf("failed to read Warning Hours: %w", err)
}

return integerWarningHours, nil
}

func getRDSClusters() ([]RDSInfo, error) {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
Expand Down Expand Up @@ -339,8 +369,16 @@ func validateEOLStatus(rdsInfo RDSInfo, minimumSupportedInfos []MinimumSupported
func validateEOLDate(validDate string, now time.Time) (string, error) {
var layout = "2006-01-02"
var eolStatus string
const alertHours = 30 * 24 // 30 Days
const warningHours = 90 * 24 // 90 Days

alertHours, err := getAlertHours()
if err != nil {
return "", fmt.Errorf("failed to get Alert Hour: %w", err)
}

warningHours, err := getWarningHours()
if err != nil {
return "", fmt.Errorf("failed to get Warning Hour: %w", err)
}

dueDate, err := time.Parse(layout, validDate)
if err != nil {
Expand All @@ -350,9 +388,9 @@ func validateEOLDate(validDate string, now time.Time) (string, error) {
switch {
case now.After(dueDate):
eolStatus = "expired"
case now.After(dueDate.Add(-1 * alertHours * time.Hour)):
case now.After(dueDate.Add(-1 * time.Duration(alertHours) * time.Hour)):
eolStatus = "alert"
case now.After(dueDate.Add(-1 * warningHours * time.Hour)):
case now.After(dueDate.Add(-1 * time.Duration(warningHours) * time.Hour)):
eolStatus = "warning"
default:
eolStatus = "ok"
Expand Down
8 changes: 4 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ func TestValidateEOLDate(t *testing.T) {
{name: "Expired", validDate: "2020-12-31", now: now, out: "expired"},
{name: "Alert", validDate: "2021-01-01", now: now, out: "alert"},
{name: "Alert", validDate: "2021-01-02", now: now, out: "alert"},
{name: "Warning", validDate: "2021-01-30", now: now, out: "alert"},
{name: "Warning", validDate: "2021-01-31", now: now, out: "warning"},
{name: "Warning", validDate: "2021-03-31", now: now, out: "warning"},
{name: "OK", validDate: "2021-04-01", now: now, out: "ok"},
{name: "Warning", validDate: "2021-03-30", now: now, out: "alert"},
{name: "Warning", validDate: "2021-04-01", now: now, out: "warning"},
{name: "Warning", validDate: "2021-06-29", now: now, out: "warning"},
{name: "OK", validDate: "2021-06-30", now: now, out: "ok"},
}

for _, tt := range tests {
Expand Down

0 comments on commit c8fee82

Please sign in to comment.