events.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 events
  15. import (
  16. "yunion.io/x/jsonutils"
  17. "yunion.io/x/onecloud/pkg/mcclient"
  18. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  19. "yunion.io/x/onecloud/pkg/mcclient/modules"
  20. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules/k8s"
  22. )
  23. type BaseEventListOptions struct {
  24. Scope string `help:"scope" choices:"project|domain|system"`
  25. Since string `help:"Show logs since specific date" metavar:"DATETIME"`
  26. Until string `help:"Show logs until specific date" metavar:"DATETIME"`
  27. Limit int64 `help:"Limit number of logs" default:"20"`
  28. Offset int64 `help:"Offset"`
  29. Ascending bool `help:"Ascending order"`
  30. Descending bool `help:"Descending order"`
  31. OrderBy string `help:"order by specific field"`
  32. Action []string `help:"Log action"`
  33. User string `help:"filter by operator user"`
  34. Project string `help:"filter by operator user's project"`
  35. OwnerProjectIds []string `help:"filter by owner project ids"`
  36. OwnerDomainIds []string `help:"filter by owner domain ids"`
  37. PagingMarker string `help:"marker for pagination"`
  38. PagingOrder string `help:"pagination order" choices:"DESC|ASC"`
  39. Filter []string `help:"Filters"`
  40. }
  41. type EventListOptions struct {
  42. BaseEventListOptions
  43. Id []string `help:"object IDs" metavar:"OBJ_ID"`
  44. Name []string `help:"object names" metavar:"OBJ_NAME"`
  45. Type []string `help:"Type of relevant object" metavar:"OBJ_TYPE"`
  46. }
  47. type TypeEventListOptions struct {
  48. BaseEventListOptions
  49. ID []string `help:"object IDs" metavar:"OBJ_ID"`
  50. }
  51. func doComputeEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  52. return DoEventList(compute.Logs, s, args)
  53. }
  54. func doImageEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  55. return DoEventList(modules.ImageLogs, s, args)
  56. }
  57. func doIdentityEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  58. return DoEventList(modules.IdentityLogs, s, args)
  59. }
  60. func doMonitorEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  61. return DoEventList(modules.MonitorLogs, s, args)
  62. }
  63. func doNotifyEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  64. return DoEventList(modules.NotifyLogs, s, args)
  65. }
  66. func doK8sEventList(s *mcclient.ClientSession, args *EventListOptions) error {
  67. return DoEventList(*k8s.Logs.ResourceManager, s, args)
  68. }
  69. func doHostDmesgList(s *mcclient.ClientSession, args *EventListOptions) error {
  70. return DoEventList(modules.DmesgLogs, s, args)
  71. }
  72. func DoEventList(man modulebase.ResourceManager, s *mcclient.ClientSession, args *EventListOptions) error {
  73. params := jsonutils.NewDict()
  74. if len(args.Type) > 0 {
  75. params.Add(jsonutils.NewStringArray(args.Type), "obj_type")
  76. }
  77. if len(args.Id) > 0 {
  78. params.Add(jsonutils.NewStringArray(args.Id), "obj_id")
  79. }
  80. if len(args.Name) > 0 {
  81. params.Add(jsonutils.NewStringArray(args.Name), "obj_name")
  82. }
  83. if len(args.Since) > 0 {
  84. params.Add(jsonutils.NewString(args.Since), "since")
  85. }
  86. if len(args.Until) > 0 {
  87. params.Add(jsonutils.NewString(args.Until), "until")
  88. }
  89. if args.Limit > 0 {
  90. params.Add(jsonutils.NewInt(args.Limit), "limit")
  91. }
  92. if args.Offset > 0 {
  93. params.Add(jsonutils.NewInt(args.Offset), "offset")
  94. }
  95. if args.Ascending && !args.Descending {
  96. params.Add(jsonutils.NewString("asc"), "order")
  97. } else if !args.Ascending && args.Descending {
  98. params.Add(jsonutils.NewString("desc"), "order")
  99. }
  100. if len(args.OrderBy) > 0 {
  101. params.Add(jsonutils.NewString(args.OrderBy), "order_by")
  102. }
  103. if len(args.Action) > 0 {
  104. params.Add(jsonutils.NewStringArray(args.Action), "action")
  105. }
  106. if len(args.User) > 0 {
  107. params.Add(jsonutils.NewString(args.User), "user")
  108. }
  109. if len(args.Project) > 0 {
  110. params.Add(jsonutils.NewString(args.Project), "project")
  111. }
  112. if len(args.Scope) > 0 {
  113. params.Add(jsonutils.NewString(args.Scope), "scope")
  114. }
  115. if len(args.OwnerProjectIds) > 0 {
  116. params.Add(jsonutils.NewStringArray(args.OwnerProjectIds), "owner_project_ids")
  117. }
  118. if len(args.OwnerDomainIds) > 0 {
  119. params.Add(jsonutils.NewStringArray(args.OwnerDomainIds), "owner_domain_ids")
  120. }
  121. if len(args.PagingMarker) > 0 {
  122. params.Add(jsonutils.NewString(args.PagingMarker), "paging_marker")
  123. }
  124. if len(args.PagingOrder) > 0 {
  125. params.Add(jsonutils.NewString(args.PagingOrder), "paging_order")
  126. }
  127. if len(args.Filter) > 0 {
  128. params.Add(jsonutils.NewStringArray(args.Filter), "filter")
  129. }
  130. logs, err := man.List(s, params)
  131. if err != nil {
  132. return err
  133. }
  134. printList(logs, man.GetColumns(s))
  135. return nil
  136. }
  137. func init() {
  138. R(&EventListOptions{}, "event-show", "Show operation event logs", doComputeEventList)
  139. R(&EventListOptions{}, "region-event-show", "Show operation event logs", doComputeEventList)
  140. R(&EventListOptions{}, "glance-event-show", "Show operation event logs", doImageEventList)
  141. R(&EventListOptions{}, "keystone-event-show", "Show operation event logs", doIdentityEventList)
  142. R(&EventListOptions{}, "monitor-event-show", "Show operation event logs", doMonitorEventList)
  143. R(&EventListOptions{}, "notify-event-show", "Show operation event logs", doNotifyEventList)
  144. R(&EventListOptions{}, "kube-event-show", "Show operation event logs", doK8sEventList)
  145. R(&EventListOptions{}, "host-dmesg-show", "Show host dmesgs", doHostDmesgList)
  146. R(&TypeEventListOptions{}, "server-event", "Show operation event logs of server", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  147. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"server"}}
  148. return doComputeEventList(s, &nargs)
  149. })
  150. R(&TypeEventListOptions{}, "container-event", "Show operation event logs of container", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  151. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"container"}}
  152. return doComputeEventList(s, &nargs)
  153. })
  154. R(&TypeEventListOptions{}, "disk-event", "Show operation event logs of disk", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  155. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"disk"}}
  156. return doComputeEventList(s, &nargs)
  157. })
  158. R(&TypeEventListOptions{}, "dbinstance-event", "Show operation event logs of disk", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  159. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"dbinstance"}}
  160. return doComputeEventList(s, &nargs)
  161. })
  162. R(&TypeEventListOptions{}, "lb-event", "Show operation event logs of disk", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  163. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"loadbalancer"}}
  164. return doComputeEventList(s, &nargs)
  165. })
  166. R(&TypeEventListOptions{}, "eip-event", "Show operation event logs of elastic IP", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  167. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"eip"}}
  168. return doComputeEventList(s, &nargs)
  169. })
  170. R(&TypeEventListOptions{}, "host-event", "Show operation event logs of host", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  171. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"host"}}
  172. return doComputeEventList(s, &nargs)
  173. })
  174. R(&TypeEventListOptions{}, "vpc-event", "Show operation event logs of vpc", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  175. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"vpc"}}
  176. return doComputeEventList(s, &nargs)
  177. })
  178. R(&TypeEventListOptions{}, "zone-event", "Show operation event logs of zone", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  179. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"zone"}}
  180. return doComputeEventList(s, &nargs)
  181. })
  182. R(&TypeEventListOptions{}, "region-event", "Show operation event logs of region", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  183. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"cloudregion"}}
  184. return doComputeEventList(s, &nargs)
  185. })
  186. R(&TypeEventListOptions{}, "wire-event", "Show operation event logs of wire", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  187. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"wire"}}
  188. return doComputeEventList(s, &nargs)
  189. })
  190. R(&TypeEventListOptions{}, "network-event", "Show operation event logs of network", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  191. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"network"}}
  192. return doComputeEventList(s, &nargs)
  193. })
  194. R(&TypeEventListOptions{}, "cloud-provider-event", "Show operation event logs of cloudprovider", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  195. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"cloudprovider"}}
  196. return doComputeEventList(s, &nargs)
  197. })
  198. R(&TypeEventListOptions{}, "cloud-account-event", "Show operation event logs of cloudaccount", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  199. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"cloudaccount"}}
  200. return doComputeEventList(s, &nargs)
  201. })
  202. R(&TypeEventListOptions{}, "bucket-event", "Show operation event logs of bucket", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  203. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"bucket"}}
  204. return doComputeEventList(s, &nargs)
  205. })
  206. R(&TypeEventListOptions{}, "image-event", "Show operation event logs of glance images", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  207. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"image"}}
  208. return doImageEventList(s, &nargs)
  209. })
  210. R(&TypeEventListOptions{}, "user-event", "Show operation event logs of keystone users", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  211. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"user"}}
  212. return doIdentityEventList(s, &nargs)
  213. })
  214. R(&TypeEventListOptions{}, "group-event", "Show operation event logs of keystone groups", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  215. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"group"}}
  216. return doIdentityEventList(s, &nargs)
  217. })
  218. R(&TypeEventListOptions{}, "domain-event", "Show operation event logs of keystone domains", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  219. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"domain"}}
  220. return doIdentityEventList(s, &nargs)
  221. })
  222. R(&TypeEventListOptions{}, "idp-event", "Show operation event logs of keystone identity provider", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  223. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"identity_provider"}}
  224. return doIdentityEventList(s, &nargs)
  225. })
  226. R(&TypeEventListOptions{}, "project-event", "Show operation event logs of keystone projects", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  227. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"project"}}
  228. return doIdentityEventList(s, &nargs)
  229. })
  230. R(&TypeEventListOptions{}, "organization-event", "Show operation event logs of keystone organizations", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  231. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"organization"}}
  232. return doIdentityEventList(s, &nargs)
  233. })
  234. R(&TypeEventListOptions{}, "role-event", "Show operation event logs of keystone roles", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  235. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"role"}}
  236. return doIdentityEventList(s, &nargs)
  237. })
  238. R(&TypeEventListOptions{}, "policy-event", "Show operation event logs of keystone policies", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  239. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"policy"}}
  240. return doIdentityEventList(s, &nargs)
  241. })
  242. R(&TypeEventListOptions{}, "endpoint-event", "Show operation event logs of keystone endpoints", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  243. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"endpoint"}}
  244. return doIdentityEventList(s, &nargs)
  245. })
  246. R(&TypeEventListOptions{}, "service-event", "Show operation event logs of keystone services", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  247. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"service"}}
  248. return doIdentityEventList(s, &nargs)
  249. })
  250. R(&TypeEventListOptions{}, "credential-event", "Show operation event logs of keystone credentials", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  251. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"credential"}}
  252. return doIdentityEventList(s, &nargs)
  253. })
  254. R(&TypeEventListOptions{}, "monitor-migrationalert-event", "Show operation event logs of monitor auto migrations", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  255. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"migrationalert"}}
  256. return doMonitorEventList(s, &nargs)
  257. })
  258. R(&TypeEventListOptions{}, "notification-event", "Show operation event logs of a notification", func(s *mcclient.ClientSession, args *TypeEventListOptions) error {
  259. nargs := EventListOptions{BaseEventListOptions: args.BaseEventListOptions, Id: args.ID, Type: []string{"notification"}}
  260. return doNotifyEventList(s, &nargs)
  261. })
  262. }