// 2017 Rackspace, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"): you may // not use this file except in compliance with the License. You may obtain // a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the // License for the specific language governing permissions and limitations // under the License. // Package journal houses universal types used to journal alerts and audit information to newrelic. package journal import ( "fmt" "os" "strconv" "strings" "time" "github.rackspace.com/gorax/clients/raxNewrelic" "github.rackspace.com/gorax/tools/util" ) var appName = "NSIMigrationAPI" type ( // Changes type is an alert representation of an event where the values // have changed and the change information is passed up within the alert. // // swagger:model Changes struct { EventType string `json:"eventType" bson:"eventType"` ID string `json:"_id" bson:"_id"` AccountID int `json:"account_number" bson:"account_number"` TeamName string `json:"team_name" bson:"team_name"` Current string `json:"current_value" bson:"current_value"` Destination string `json:"destination_value" bson:"destination_value"` Reason string `json:"reason" bson:"reason"` Timestamp int64 `json:"timestamp" bson:"timestamp"` Ignore bool `json:"ignore,omitempty" bson:"ignore,omitempty"` IgnoreReason string `json:"ignore_reason,omitempty" bson:"ignore_reason,omitempty"` IgnoreNote string `json:"ignore_note,omitempty" bson:"ignore_note,omitempty"` } // Notification type is an alert representation of an event that only needs summary information // about the data processed. Transaction logs are a good example. // // swagger:model Notification struct { EventType string `json:"eventType" bson:"eventType"` StartTime time.Time `json:"start_time" bson:"start_time"` Duration time.Duration `json:"duration" bson:"duration"` Updated int `json:"items_updated" bson:"items_updated"` Missed int `json:"items_missed" bson:"items_missed"` Errors string `json:"error" bson:"error"` } // Info type is an alert representation of an event that needs a notification, however // more information about the notification is required. CleanMigrationDB is an example. // // swagger:model Info struct { EventType string `json:"eventType" bson:"eventType"` ID string `json:"_id" bson:"_id"` AccountID string `json:"account_number" bson:"account_number"` TeamName string `json:"team_name" bson:"team_name"` Status string `json:"device_last_status" bson:"device_last_status"` MoveGroup string `json:"move_group" bson:"move_group"` Errors string `json:"error" bson:"error"` } ) // Notifications method is used for log messages for individual transaction details, NOT the results // of an audit. Ie. "This audit ran for x amount of time. Audit results should be logged under journalInformation // or journalAlerts func Notifications(a []Notification, client *raxNewrelic.Client) error { if util.IsStaging(os.Getenv("ENVIRONMENT")) { appName = "NSIMigrationAPI_STAGING" } m := make([]map[string]string, 0) for _, alert := range a { m = append(m, map[string]string{ "auditType": alert.EventType, "StartTime": alert.StartTime.Format(time.RFC822), "Duration": alert.Duration.String(), "ItemsUpdated": strconv.Itoa(alert.Updated), "ItemsMissed": strconv.Itoa(alert.Missed), }) } resp, err := client.Insights.JournalEvents(appName, "DCMigrations::AuditLogs", m) if err != nil { return err } if !resp.Success { return fmt.Errorf("received success false: %v", resp.Success) } return nil } // Information method is used to log audit results to NewRelic that require notification, however, // there is no need for remediation func Information(a []Info, client *raxNewrelic.Client) error { if util.IsStaging(os.Getenv("ENVIRONMENT")) { appName = "NSIMigrationAPI_STAGING" } m := make([]map[string]string, 0) for _, alert := range a { m = append(m, map[string]string{ "auditType": alert.EventType, "TeamName": alert.TeamName, "AccountID": alert.AccountID, "DeviceStatus": alert.Status, "DeviceID": alert.ID, "MoveGroup": alert.MoveGroup, }) } resp, err := client.Insights.JournalEvents(appName, "DCMigrations::InformationalLogs", m) if err != nil { return err } if !resp.Success { return fmt.Errorf("received success false: %v", resp.Success) } return nil } // Audits method is used to log audit results to NewRelic that require remediation. func Audits(a []Changes, collection string, client *raxNewrelic.Client) error { if util.IsStaging(os.Getenv("ENVIRONMENT")) { appName = "NSIMigrationAPI_STAGING" } m := make([]map[string]string, 0) for _, alert := range a { m = append(m, map[string]string{ "auditType": alert.EventType, "current": alert.Current, "destination": alert.Destination, "DeviceID": alert.ID, "TeamName": alert.TeamName, "AccountID": strconv.Itoa(alert.AccountID), "Reason": alert.Reason, "Migration": strings.ToUpper(collection), }) } resp, err := client.Insights.JournalEvents(appName, "DCMigrations::AuditChecks", m) if err != nil { return err } if !resp.Success { return fmt.Errorf("received success false: %v", resp.Success) } return nil }