event.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package azure
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strings"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. )
  23. type SAuthorization struct {
  24. Action string
  25. Scope string
  26. }
  27. type SClaims struct {
  28. Aud string
  29. Iss string
  30. Iat string
  31. Nbf string
  32. Exp string
  33. Aio string
  34. Appid string
  35. Appidacr string
  36. Uti string
  37. Ver string
  38. }
  39. type SLocalized struct {
  40. Value string
  41. LocalizedValue string
  42. }
  43. type SEvent struct {
  44. region *SRegion
  45. Authorization SAuthorization
  46. Channels string
  47. Claims SClaims
  48. CorrelationId string
  49. Description string
  50. EventDataId string
  51. EventName SLocalized
  52. Category SLocalized
  53. Level string
  54. ResourceGroupName string
  55. ResourceProviderName SLocalized
  56. ResourceId string
  57. ResourceType SLocalized
  58. OperationId string
  59. OperationName SLocalized
  60. Properties string
  61. Status SLocalized
  62. SubStatus SLocalized
  63. Caller string
  64. EventTimestamp time.Time
  65. SubmissionTimestamp time.Time
  66. SubscriptionId string
  67. TenantId string
  68. ID string
  69. Name string
  70. }
  71. func (event *SEvent) GetName() string {
  72. return event.ResourceId
  73. }
  74. func (event *SEvent) GetService() string {
  75. return event.ResourceProviderName.Value
  76. }
  77. func (event *SEvent) GetAction() string {
  78. return event.OperationName.Value
  79. }
  80. func (event *SEvent) GetResourceType() string {
  81. return event.ResourceType.Value
  82. }
  83. func (event *SEvent) GetRequestId() string {
  84. return event.CorrelationId
  85. }
  86. func (event *SEvent) GetRequest() jsonutils.JSONObject {
  87. return jsonutils.Marshal(event)
  88. }
  89. func (event *SEvent) GetAccount() string {
  90. return event.Claims.Appid
  91. }
  92. func (event *SEvent) IsSuccess() bool {
  93. return event.Status.Value != "Failed"
  94. }
  95. func (event *SEvent) GetCreatedAt() time.Time {
  96. return event.EventTimestamp
  97. }
  98. func (region *SRegion) GetICloudEvents(start time.Time, end time.Time, withReadEvent bool) ([]cloudprovider.ICloudEvent, error) {
  99. events, err := region.GetEvents(start, end)
  100. if err != nil {
  101. return nil, err
  102. }
  103. iEvents := []cloudprovider.ICloudEvent{}
  104. for i := range events {
  105. read := false
  106. for _, k := range []string{"read", "listKeys"} {
  107. if strings.Contains(events[i].Authorization.Action, k) {
  108. read = true
  109. break
  110. }
  111. }
  112. if withReadEvent || !read {
  113. iEvents = append(iEvents, &events[i])
  114. }
  115. }
  116. return iEvents, nil
  117. }
  118. func (region *SRegion) GetEvents(start time.Time, end time.Time) ([]SEvent, error) {
  119. events := []SEvent{}
  120. if start.IsZero() {
  121. start = time.Now().AddDate(0, 0, -7)
  122. }
  123. if end.IsZero() {
  124. end = time.Now()
  125. }
  126. params := url.Values{}
  127. params.Set("$filter", fmt.Sprintf("eventTimestamp ge '%s' and eventTimestamp le '%s' and eventChannels eq 'Admin, Operation' and levels eq 'Critical,Error,Warning,Informational'", start.Format("2006-01-02T15:04:05Z"), end.Format("2006-01-02T15:04:05Z")))
  128. resource := fmt.Sprintf("microsoft.insights/eventtypes/management/values")
  129. err := region.client.list(resource, params, &events)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return events, nil
  134. }