cloudwatch.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package cloudwatch
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "time"
  7. "github.com/aws/aws-sdk-go/aws"
  8. "github.com/aws/aws-sdk-go/service/cloudwatch"
  9. "github.com/aws/aws-sdk-go/service/ec2"
  10. "github.com/grafana/grafana/pkg/middleware"
  11. )
  12. type actionHandler func(*cwRequest, *middleware.Context)
  13. var actionHandlers map[string]actionHandler
  14. type cwRequest struct {
  15. Region string `json:"region"`
  16. Action string `json:"action"`
  17. Body []byte `json:"-"`
  18. }
  19. func init() {
  20. actionHandlers = map[string]actionHandler{
  21. "GetMetricStatistics": handleGetMetricStatistics,
  22. "ListMetrics": handleListMetrics,
  23. "DescribeInstances": handleDescribeInstances,
  24. "__GetRegions": handleGetRegions,
  25. "__GetNamespaces": handleGetNamespaces,
  26. "__GetMetrics": handleGetMetrics,
  27. "__GetDimensions": handleGetDimensions,
  28. }
  29. }
  30. func handleGetMetricStatistics(req *cwRequest, c *middleware.Context) {
  31. svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)})
  32. reqParam := &struct {
  33. Parameters struct {
  34. Namespace string `json:"namespace"`
  35. MetricName string `json:"metricName"`
  36. Dimensions []*cloudwatch.Dimension `json:"dimensions"`
  37. Statistics []*string `json:"statistics"`
  38. StartTime int64 `json:"startTime"`
  39. EndTime int64 `json:"endTime"`
  40. Period int64 `json:"period"`
  41. } `json:"parameters"`
  42. }{}
  43. json.Unmarshal(req.Body, reqParam)
  44. params := &cloudwatch.GetMetricStatisticsInput{
  45. Namespace: aws.String(reqParam.Parameters.Namespace),
  46. MetricName: aws.String(reqParam.Parameters.MetricName),
  47. Dimensions: reqParam.Parameters.Dimensions,
  48. Statistics: reqParam.Parameters.Statistics,
  49. StartTime: aws.Time(time.Unix(reqParam.Parameters.StartTime, 0)),
  50. EndTime: aws.Time(time.Unix(reqParam.Parameters.EndTime, 0)),
  51. Period: aws.Int64(reqParam.Parameters.Period),
  52. }
  53. resp, err := svc.GetMetricStatistics(params)
  54. if err != nil {
  55. c.JsonApiErr(500, "Unable to call AWS API", err)
  56. return
  57. }
  58. c.JSON(200, resp)
  59. }
  60. func handleListMetrics(req *cwRequest, c *middleware.Context) {
  61. svc := cloudwatch.New(&aws.Config{Region: aws.String(req.Region)})
  62. reqParam := &struct {
  63. Parameters struct {
  64. Namespace string `json:"namespace"`
  65. MetricName string `json:"metricName"`
  66. Dimensions []*cloudwatch.DimensionFilter `json:"dimensions"`
  67. } `json:"parameters"`
  68. }{}
  69. json.Unmarshal(req.Body, reqParam)
  70. params := &cloudwatch.ListMetricsInput{
  71. Namespace: aws.String(reqParam.Parameters.Namespace),
  72. MetricName: aws.String(reqParam.Parameters.MetricName),
  73. Dimensions: reqParam.Parameters.Dimensions,
  74. }
  75. resp, err := svc.ListMetrics(params)
  76. if err != nil {
  77. c.JsonApiErr(500, "Unable to call AWS API", err)
  78. return
  79. }
  80. c.JSON(200, resp)
  81. }
  82. func handleDescribeInstances(req *cwRequest, c *middleware.Context) {
  83. svc := ec2.New(&aws.Config{Region: aws.String(req.Region)})
  84. reqParam := &struct {
  85. Parameters struct {
  86. Filters []*ec2.Filter `json:"filters"`
  87. InstanceIds []*string `json:"instanceIds"`
  88. } `json:"parameters"`
  89. }{}
  90. json.Unmarshal(req.Body, reqParam)
  91. params := &ec2.DescribeInstancesInput{}
  92. if len(reqParam.Parameters.Filters) > 0 {
  93. params.Filters = reqParam.Parameters.Filters
  94. }
  95. if len(reqParam.Parameters.InstanceIds) > 0 {
  96. params.InstanceIds = reqParam.Parameters.InstanceIds
  97. }
  98. resp, err := svc.DescribeInstances(params)
  99. if err != nil {
  100. c.JsonApiErr(500, "Unable to call AWS API", err)
  101. return
  102. }
  103. c.JSON(200, resp)
  104. }
  105. func HandleRequest(c *middleware.Context) {
  106. var req cwRequest
  107. req.Body, _ = ioutil.ReadAll(c.Req.Request.Body)
  108. json.Unmarshal(req.Body, &req)
  109. if handler, found := actionHandlers[req.Action]; !found {
  110. c.JsonApiErr(500, "Unexpected AWS Action", errors.New(req.Action))
  111. return
  112. } else {
  113. handler(&req, c)
  114. }
  115. }