/
alerts_and_notifications_panel.go
103 lines (85 loc) · 2.66 KB
/
alerts_and_notifications_panel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package RDS
import (
"fmt"
"log"
"time"
"github.com/Appkube-awsx/awsx-getelementdetails/comman-function"
"github.com/Appkube-awsx/awsx-common/authenticate"
"github.com/Appkube-awsx/awsx-common/model"
// "github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
type AlarmNotification struct {
Timestamp time.Time
Alert string
Description string
}
var RdsAlarmandNotificationcmd = &cobra.Command{
Use: "rds_alerts_and_notifications_panel",
Short: "Retrieve recent alerts and notifications related to RDS instance availability",
Long: `Command to retrieve recent alerts and notifications related to RDS instance availability`,
Run: func(cmd *cobra.Command, args []string) {
authFlag, clientAuth, err := handleAuth(cmd)
if err != nil {
log.Println("Error during authentication:", err)
return
}
if authFlag {
responseType, _ := cmd.PersistentFlags().GetString("responseType")
notifications, err := GetAlertsAndNotificationsPanell(cmd, clientAuth)
if err != nil {
log.Println("Error getting alerts and notifications:", err)
return
}
if responseType == "frame" {
fmt.Println(notifications)
} else {
//printTable(notifications)
}
}
},
}
func handleAuth(cmd *cobra.Command) (bool, *model.Auth, error) {
authFlag, clientAuth, err := authenticate.AuthenticateCommand(cmd)
if err != nil {
return false, nil, err
}
return authFlag, clientAuth, nil
}
func GetAlertsAndNotificationsPanell(cmd *cobra.Command, clientAuth *model.Auth) ([]AlarmNotification, error) {
startTime, endTime, err := comman_function.ParseTimes(cmd)
if err != nil {
return nil, fmt.Errorf("error parsing time: %v", err)
}
// Retrieve CloudWatch alarms
alarms, err := comman_function.GetCloudWatchAlarms(clientAuth, startTime, endTime)
if err != nil {
log.Println("Error getting CloudWatch alarms:", err)
return nil, err
}
// Convert CloudWatch alarms to AlarmNotification struct
notifications := make([]AlarmNotification, len(alarms))
for i, alarm := range alarms {
notifications[i] = AlarmNotification{
Timestamp: *alarm.StateUpdatedTimestamp,
Alert: *alarm.StateReason,
Description: *alarm.AlarmDescription,
}
}
return notifications, nil
}
// func printTable(notifications []AlarmNotification) {
// table := tablewriter.NewWriter(os.Stdout)
// table.SetHeader([]string{"Timestamp", "Alert", "Description"})
// for _, notification := range notifications {
// table.Append([]string{
// notification.Timestamp.Format(time.RFC3339),
// notification.Alert,
// notification.Description,
// })
// }
// table.Render()
// }
func init() {
comman_function.InitAwsCmdFlags(RdsAlarmandNotificationcmd)
}