forked from tailwarden/komiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.go
779 lines (680 loc) · 22.7 KB
/
internal.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
package internal
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"net/http"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"github.com/hashicorp/go-version"
"github.com/sirupsen/logrus"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/uptrace/bun/dialect"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/driver/sqliteshim"
"github.com/spf13/cobra"
v1 "github.com/tailwarden/komiser/internal/api/v1"
"github.com/tailwarden/komiser/internal/config"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
"github.com/tailwarden/komiser/providers/linode"
"github.com/tailwarden/komiser/utils"
"github.com/uptrace/bun"
)
var Version = "Unknown"
var GoVersion = runtime.Version()
var Buildtime = "Unknown"
var Commit = "Unknown"
var Os = runtime.GOOS
var Arch = runtime.GOARCH
var db *bun.DB
var analytics utils.Analytics
func Exec(address string, port int, configPath string, telemetry bool, a utils.Analytics, regions []string, cmd *cobra.Command) error {
analytics = a
ctx := context.Background()
cfg, clients, accounts, err := config.Load(configPath, telemetry, analytics, db)
if err != nil {
return err
}
err = setupDBConnection(cfg)
if err != nil {
return err
}
if db != nil {
err = utils.SetupSchema(db, cfg, accounts)
if err != nil {
return err
}
cron := gocron.NewScheduler(time.UTC)
_, err = cron.Every(1).Hours().Do(func() {
log.Info("Fetching resources workflow has started")
err = fetchResources(ctx, clients, regions, telemetry)
if err != nil {
log.Fatal(err)
}
})
if err != nil {
log.WithError(err).Error("setting up cron job failed")
}
_, err = cron.Every(1).Hours().Do(func() {
alertsExist, alerts := checkIfAlertsExist(ctx)
if alertsExist {
log.Info("Checking Alerts")
checkingAlerts(ctx, *cfg, telemetry, port, alerts)
}
})
if err != nil {
log.WithError(err).Error("setting up cron job failed")
}
_, err = cron.Every(1).Friday().At("09:00").Do(func() {
if len(cfg.Slack.Webhook) > 0 && cfg.Slack.Reporting {
log.Info("Sending weekly reporting")
sendTagsCoverageReport(ctx, *cfg)
sendCostBreakdownReport(ctx, *cfg)
}
})
if err != nil {
log.WithError(err).Error("setting up cron job failed")
}
cron.StartAsync()
}
go checkUpgrade()
err = runServer(address, port, telemetry, *cfg, accounts)
if err != nil {
return err
}
return nil
}
func checkIfAlertsExist(ctx context.Context) (bool, []models.Alert) {
alerts := make([]models.Alert, 0)
err := db.NewRaw("SELECT * FROM alerts").Scan(ctx, &alerts)
if err != nil {
log.WithError(err).Error("scan failed")
}
if len(alerts) > 0 {
return true, alerts
}
return false, alerts
}
func loggingMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
startTime := time.Now()
ctx.Next()
endTime := time.Now()
latencyTime := endTime.Sub(startTime)
reqMethod := ctx.Request.Method
reqUri := ctx.Request.RequestURI
statusCode := ctx.Writer.Status()
clientIP := ctx.ClientIP()
log.WithFields(log.Fields{
"method": reqMethod,
"uri": reqUri,
"status": statusCode,
"latency": latencyTime,
"ip": clientIP,
}).Info("HTTP request")
ctx.Next()
}
}
func runServer(address string, port int, telemetry bool, cfg models.Config, accounts []models.Account) error {
log.Infof("Komiser version: %s, commit: %s, buildt: %s", Version, Commit, Buildtime)
r := v1.Endpoints(context.Background(), telemetry, analytics, db, cfg, accounts)
r.Use(loggingMiddleware())
if err := r.Run(fmt.Sprintf("%s:%d", address, port)); err != nil {
return err
}
log.Infof("Server started on %s:%d", address, port)
return nil
}
func setupDBConnection(c *models.Config) error {
var sqldb *sql.DB
var err error
if len(c.SQLite.File) == 0 && len(c.Postgres.URI) == 0 {
log.Println("Database wasn't configured yet")
return nil
}
if len(c.SQLite.File) > 0 {
sqldb, err = sql.Open(sqliteshim.ShimName, fmt.Sprintf("file:%s?cache=shared", c.SQLite.File))
if err != nil {
return err
}
sqldb.SetMaxIdleConns(1000)
sqldb.SetConnMaxLifetime(0)
db = bun.NewDB(sqldb, sqlitedialect.New())
log.Println("Data will be stored in SQLite")
} else {
sqldb = sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(c.Postgres.URI)))
db = bun.NewDB(sqldb, pgdialect.New())
log.Println("Data will be stored in PostgreSQL")
}
return nil
}
func fetchResources(ctx context.Context, clients []providers.ProviderClient, regions []string, telemetry bool) error {
localHub := sentry.CurrentHub().Clone()
providerResourceCount := make(map[string]int)
resCountMu := &sync.Mutex{}
wp := providers.NewWorkerPool(64)
wp.Start()
for _, client := range clients {
var providerName string
var listOfSupportedServices func() []providers.FetchDataFunction
if client.AWSClient != nil {
providerName = "AWS"
} else if client.DigitalOceanClient != nil {
providerName = "DigitalOcean"
} else if client.OciClient != nil {
providerName = "OCI"
} else if client.CivoClient != nil {
providerName = "Civo"
} else if client.K8sClient != nil {
providerName = "Kubernetes"
} else if client.LinodeClient != nil {
listOfSupportedServices = linode.ListOfSupportedServices
providerName = "Linode"
} else if client.TencentClient != nil {
providerName = "Tencent"
} else if client.AzureClient != nil {
providerName = "Azure"
} else if client.ScalewayClient != nil {
providerName = "Scaleway"
} else if client.MongoDBAtlasClient != nil {
providerName = "MongoDBAtlas"
} else if client.GCPClient != nil {
providerName = "GCP"
}
localHub.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("provider", providerName)
})
if telemetry {
analytics.TrackEvent("fetching_resources", map[string]interface{}{
"provider": providerName,
})
}
for _, fetchDataFn := range listOfSupportedServices() {
wp.SubmitTask(func() {
resources, err := fetchDataFn(ctx, client)
if err != nil {
log.Printf("[%s][%s] %s", client.Name, providerName, err)
localHub.CaptureException(err)
localHub.Flush(2 * time.Second)
} else {
for _, resource := range resources {
_, err := db.NewInsert().Model(&resource).On("CONFLICT (resource_id) DO UPDATE").Set("cost = EXCLUDED.cost").Exec(context.Background())
if err != nil {
logrus.WithError(err).Errorf("db trigger failed")
}
}
if telemetry {
resCountMu.Lock()
providerResourceCount[providerName] += len(resources)
resCountMu.Unlock()
}
}
})
}
}
wp.Wait()
if telemetry {
for provider, resCount := range providerResourceCount {
analytics.TrackEvent("discovered_resources", map[string]interface{}{
"provider": provider,
"resources": resCount,
})
}
}
return nil
}
func checkUpgrade() {
url := "https://api.github.com/repos/tailwarden/komiser/releases/latest"
type GHRelease struct {
Version string `json:"tag_name"`
}
var myClient = &http.Client{Timeout: 5 * time.Second}
r, err := myClient.Get(url)
if err != nil {
log.Warnf("Failed to check for new version: %s", err)
return
}
defer r.Body.Close()
target := new(GHRelease)
err = json.NewDecoder(r.Body).Decode(target)
if err != nil {
log.Warnf("Failed to decode new release version: %s", err)
return
}
v1, err := version.NewVersion(Version)
if err != nil {
log.Warnf("Failed to parse version: %s", err)
} else {
v2, err := version.NewVersion(target.Version)
if err != nil {
log.Warnf("Failed to parse version: %s", err)
} else {
if v1.LessThan(v2) {
log.Warnf("Newer Komiser version is available: %s", target.Version)
log.Warnf("Upgrade instructions: /~https://github.com/tailwarden/komiser")
}
}
}
}
func hitCustomWebhook(endpoint string, secret string, viewName string, resources int, cost float64, alertType string) {
var payloadJSON []byte
var err error
payload := models.CustomWebhookPayload{
Komiser: Version,
View: viewName,
Timestamp: time.Now().Unix(),
}
switch alertType {
case "BUDGET":
payload.Message = "Cost alert"
payload.Data = cost
case "USAGE":
payload.Message = "Usage alert"
payload.Data = float64(resources)
default:
log.Error("Invalid Alert Type")
return
}
payloadJSON, err = json.Marshal(payload)
if err != nil {
log.Error("Couldn't encode JSON payload:", err)
return
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(payloadJSON))
if err != nil {
log.Error("Couldn't create HTTP request for custom webhook endpoint:", err)
return
}
req.Header.Set("Content-Type", "application/json")
if len(secret) > 0 {
req.Header.Set("Authorization", secret)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error("Couldn't make HTTP request for custom webhook endpoint:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Error("Custom Webhook with endpoint " + endpoint + " returned back a status code of " + string(rune(resp.StatusCode)) + " . Expected Status Code: 200")
return
}
}
func hitSlackWebhook(viewName string, port int, viewId int, resources int, cost float64, webhookUrl string, alertType string) {
attachment := slack.Attachment{
Color: "danger",
AuthorName: "Komiser",
AuthorSubname: "by Tailwarden",
AuthorLink: "https://tailwarden.com",
AuthorIcon: "https://cdn.komiser.io/images/komiser-logo.jpeg",
Footer: "Komiser",
Actions: []slack.AttachmentAction{
{
Name: "open",
Text: "Open view",
Type: "button",
URL: fmt.Sprintf("http://localhost:%d/inventory?view=%d", port, viewId),
},
},
Fields: []slack.AttachmentField{
{
Title: "View",
Value: viewName,
},
},
FooterIcon: "/~https://github.com/tailwarden/komiser",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
if alertType == "BUDGET" {
attachment.Text = "Cost alert :warning:"
attachment.Fields = append(attachment.Fields, slack.AttachmentField{
Title: "Cost",
Value: fmt.Sprintf("%.2f$", cost),
})
} else if alertType == "USAGE" {
attachment.Text = "Usage alert :warning:"
attachment.Fields = append(attachment.Fields, slack.AttachmentField{
Title: "Resources",
Value: fmt.Sprintf("%d", resources),
})
}
msg := slack.WebhookMessage{
Attachments: []slack.Attachment{attachment},
}
err := slack.PostWebhook(webhookUrl, &msg)
if err != nil {
log.Warn(err)
}
}
func checkingAlerts(ctx context.Context, cfg models.Config, telemetry bool, port int, alerts []models.Alert) {
for _, alert := range alerts {
var view models.View
err := db.NewRaw(fmt.Sprintf("SELECT * FROM views WHERE id = %s", alert.ViewId)).Scan(ctx, &view)
if err != nil {
log.WithError(err).Error("scan failed")
}
stats, err := getViewStats(ctx, view.Filters)
if err != nil {
log.Error("Couldn't get stats for view:", view.Name)
} else {
if alert.Type == "BUDGET" && alert.Budget <= stats.Costs {
if telemetry {
analytics.TrackEvent("sending_alerts", map[string]interface{}{
"type": "budget",
})
}
if alert.IsSlack {
log.Info("Sending Slack budget alert for view:", view.Name)
hitSlackWebhook(view.Name, port, int(view.Id), 0, stats.Costs, cfg.Slack.Webhook, alert.Type)
} else {
log.Info("Sending Custom Webhook budget alert for view:", view.Name)
hitCustomWebhook(alert.Endpoint, alert.Secret, view.Name, 0, stats.Costs, alert.Type)
}
}
if alert.Type == "USAGE" && alert.Usage <= stats.Resources {
if telemetry {
analytics.TrackEvent("sending_alerts", map[string]interface{}{
"type": "usage",
})
}
if alert.IsSlack {
log.Info("Sending Slack usage alert for view:", view.Name)
hitSlackWebhook(view.Name, port, int(view.Id), stats.Resources, 0, cfg.Slack.Webhook, alert.Type)
} else {
log.Info("Sending Custom Webhook usage alert for view:", view.Name)
hitCustomWebhook(alert.Endpoint, alert.Secret, view.Name, stats.Resources, 0, alert.Type)
}
}
}
}
}
func getViewStats(ctx context.Context, filters []models.Filter) (models.ViewStat, error) {
filterWithTags := false
whereQueries := make([]string, 0)
for _, filter := range filters {
if filter.Field == "name" || filter.Field == "region" || filter.Field == "service" || filter.Field == "provider" || filter.Field == "account" {
switch filter.Operator {
case "IS":
for i := 0; i < len(filter.Values); i++ {
filter.Values[i] = fmt.Sprintf("'%s'", filter.Values[i])
}
query := fmt.Sprintf("(%s IN (%s))", filter.Field, strings.Join(filter.Values, ","))
whereQueries = append(whereQueries, query)
case "IS_NOT":
for i := 0; i < len(filter.Values); i++ {
filter.Values[i] = fmt.Sprintf("'%s'", filter.Values[i])
}
query := fmt.Sprintf("(%s NOT IN (%s))", filter.Field, strings.Join(filter.Values, ","))
whereQueries = append(whereQueries, query)
case "CONTAINS":
queries := make([]string, 0)
specialChar := "%"
for i := 0; i < len(filter.Values); i++ {
queries = append(queries, fmt.Sprintf("(%s LIKE '%s%s%s')", filter.Field, specialChar, filter.Values[i], specialChar))
}
whereQueries = append(whereQueries, fmt.Sprintf("(%s)", strings.Join(queries, " OR ")))
case "NOT_CONTAINS":
queries := make([]string, 0)
specialChar := "%"
for i := 0; i < len(filter.Values); i++ {
queries = append(queries, fmt.Sprintf("(%s NOT LIKE '%s%s%s')", filter.Field, specialChar, filter.Values[i], specialChar))
}
whereQueries = append(whereQueries, fmt.Sprintf("(%s)", strings.Join(queries, " AND ")))
case "IS_EMPTY":
whereQueries = append(whereQueries, fmt.Sprintf("((coalesce(%s, '') = ''))", filter.Field))
case "IS_NOT_EMPTY":
whereQueries = append(whereQueries, fmt.Sprintf("((coalesce(%s, '') != ''))", filter.Field))
default:
return models.ViewStat{}, errors.New("Operation is invalid or not supported")
}
} else if strings.HasPrefix(filter.Field, "tag:") {
filterWithTags = true
key := strings.ReplaceAll(filter.Field, "tag:", "")
switch filter.Operator {
case "CONTAINS":
case "IS":
for i := 0; i < len(filter.Values); i++ {
filter.Values[i] = fmt.Sprintf("'%s'", filter.Values[i])
}
query := fmt.Sprintf("((res->>'key' = '%s') AND (res->>'value' IN (%s)))", key, strings.Join(filter.Values, ","))
if db.Dialect().Name() == dialect.SQLite {
query = fmt.Sprintf("((json_extract(value, '$.key') = '%s') AND (json_extract(value, '$.value') IN (%s)))", key, strings.Join(filter.Values, ","))
}
whereQueries = append(whereQueries, query)
case "NOT_CONTAINS":
case "IS_NOT":
for i := 0; i < len(filter.Values); i++ {
filter.Values[i] = fmt.Sprintf("'%s'", filter.Values[i])
}
query := fmt.Sprintf("((res->>'key' = '%s') AND (res->>'value' NOT IN (%s)))", key, strings.Join(filter.Values, ","))
if db.Dialect().Name() == dialect.SQLite {
query = fmt.Sprintf("((json_extract(value, '$.key') = '%s') AND (json_extract(value, '$.value') NOT IN (%s)))", key, strings.Join(filter.Values, ","))
}
whereQueries = append(whereQueries, query)
case "IS_EMPTY":
if db.Dialect().Name() == dialect.SQLite {
whereQueries = append(whereQueries, fmt.Sprintf("((json_extract(value, '$.key') = '%s') AND (json_extract(value, '$.value') = ''))", key))
} else {
whereQueries = append(whereQueries, fmt.Sprintf("((res->>'key' = '%s') AND (res->>'value' = ''))", key))
}
case "IS_NOT_EMPTY":
if db.Dialect().Name() == dialect.SQLite {
whereQueries = append(whereQueries, fmt.Sprintf("((json_extract(value, '$.key') = '%s') AND (json_extract(value, '$.value') != ''))", key))
} else {
whereQueries = append(whereQueries, fmt.Sprintf("((res->>'key' = '%s') AND (res->>'value' != ''))", key))
}
default:
return models.ViewStat{}, errors.New("Operation is invalid or not supported")
}
} else if filter.Field == "tags" {
switch filter.Operator {
case "IS_EMPTY":
if db.Dialect().Name() == dialect.SQLite {
whereQueries = append(whereQueries, "json_array_length(tags) = 0")
} else {
whereQueries = append(whereQueries, "jsonb_array_length(tags) = 0")
}
case "IS_NOT_EMPTY":
if db.Dialect().Name() == dialect.SQLite {
whereQueries = append(whereQueries, "json_array_length(tags) != 0")
} else {
whereQueries = append(whereQueries, "jsonb_array_length(tags) != 0")
}
default:
return models.ViewStat{}, errors.New("Operation is invalid or not supported")
}
} else if filter.Field == "cost" {
switch filter.Operator {
case "EQUAL":
cost, err := strconv.ParseFloat(filter.Values[0], 64)
if err != nil {
return models.ViewStat{}, errors.New("The value should be a number")
}
whereQueries = append(whereQueries, fmt.Sprintf("(cost = %f)", cost))
case "BETWEEN":
min, err := strconv.ParseFloat(filter.Values[0], 64)
if err != nil {
return models.ViewStat{}, errors.New("The value should be a number")
}
max, err := strconv.ParseFloat(filter.Values[1], 64)
if err != nil {
return models.ViewStat{}, errors.New("The value should be a number")
}
whereQueries = append(whereQueries, fmt.Sprintf("(cost >= %f AND cost <= %f)", min, max))
case "GREATER_THAN":
cost, err := strconv.ParseFloat(filter.Values[0], 64)
if err != nil {
return models.ViewStat{}, errors.New("The value should be a number")
}
whereQueries = append(whereQueries, fmt.Sprintf("(cost > %f)", cost))
case "LESS_THAN":
cost, err := strconv.ParseFloat(filter.Values[0], 64)
if err != nil {
return models.ViewStat{}, errors.New("The value should be a number")
}
whereQueries = append(whereQueries, fmt.Sprintf("(cost < %f)", cost))
default:
return models.ViewStat{}, errors.New("Operation is invalid or not supported")
}
} else {
return models.ViewStat{}, errors.New("Field is invalid or not supported")
}
}
whereClause := strings.Join(whereQueries, " AND ")
if filterWithTags {
query := fmt.Sprintf("FROM resources CROSS JOIN jsonb_array_elements(tags) AS res WHERE %s", whereClause)
if db.Dialect().Name() == dialect.SQLite {
query = fmt.Sprintf("FROM resources CROSS JOIN json_each(tags) WHERE type='object' AND %s", whereClause)
}
resources := struct {
Count int `bun:"count" json:"total"`
}{}
err := db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(ctx, &resources)
if err != nil {
log.WithError(err).Error("scan failed")
}
cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}
err = db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(ctx, &cost)
if err != nil {
log.WithError(err).Error("scan failed")
}
output := models.ViewStat{
Resources: resources.Count,
Costs: cost.Sum,
}
return output, nil
} else {
query := fmt.Sprintf("FROM resources WHERE %s", whereClause)
resources := struct {
Count int `bun:"count" json:"total"`
}{}
err := db.NewRaw(fmt.Sprintf("SELECT COUNT(*) as count %s", query)).Scan(ctx, &resources)
if err != nil {
log.WithError(err).Error("scan failed")
}
cost := struct {
Sum float64 `bun:"sum" json:"total"`
}{}
err = db.NewRaw(fmt.Sprintf("SELECT SUM(cost) as sum %s", query)).Scan(ctx, &cost)
if err != nil {
log.WithError(err).Error("scan failed")
}
output := models.ViewStat{
Resources: resources.Count,
Costs: cost.Sum,
}
return output, nil
}
}
func sendTagsCoverageReport(ctx context.Context, cfg models.Config) {
tags := make([]struct {
Total int `bun:"total"`
Label models.Tag `bun:"label"`
}, 0)
err := db.NewRaw("SELECT count(*) as total, value as label FROM resources CROSS JOIN json_each(tags) GROUP BY value ORDER BY total DESC").Scan(ctx, &tags)
if err != nil {
log.WithError(err).Error("scan failed")
}
fields := make([]slack.AttachmentField, 0)
for _, tag := range tags {
fields = append(fields, slack.AttachmentField{
Title: fmt.Sprintf("%s:%s", tag.Label.Key, tag.Label.Value),
Value: fmt.Sprintf("%d", tag.Total),
Short: true,
})
}
output := struct {
Total int `bun:"total"`
}{}
err = db.NewRaw("SELECT COUNT(*) as total FROM resources where json_array_length(tags) = 0;").Scan(ctx, &output)
if err != nil {
log.WithError(err).Error("scan failed")
}
currentTime := time.Now()
attachment := slack.Attachment{
Color: "good",
AuthorName: "Komiser",
AuthorSubname: "by Tailwarden",
AuthorLink: "https://tailwarden.com",
AuthorIcon: "https://cdn.komiser.io/images/komiser-logo.jpeg",
Text: fmt.Sprintf("On %s %d: *%d* of your resources are untagged. Below list of most used key/value pairs:", currentTime.Month(), currentTime.Day(), output.Total),
Footer: "Komiser",
Fields: fields,
FooterIcon: "/~https://github.com/tailwarden/komiser",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
msg := slack.WebhookMessage{
Attachments: []slack.Attachment{attachment},
}
err = slack.PostWebhook(cfg.Slack.Webhook, &msg)
if err != nil {
log.Warn(err)
}
}
func sendCostBreakdownReport(ctx context.Context, cfg models.Config) {
groups := make([]models.OutputCostByField, 0)
currentTime := time.Now()
for _, field := range []string{"service", "provider", "account", "region"} {
err := db.NewRaw(fmt.Sprintf("SELECT %s as label, SUM(cost) as total FROM resources GROUP BY %s ORDER by total desc;", field, field)).Scan(ctx, &groups)
if err != nil {
log.WithError(err).Error("scan failed")
}
segments := groups
if len(groups) > 3 {
segments = groups[:4]
if len(groups) > 4 {
sum := 0.0
for i := 4; i < len(groups); i++ {
sum += groups[i].Total
}
segments = append(segments, models.OutputCostByField{
Label: "Others",
Total: sum,
})
}
}
fields := make([]slack.AttachmentField, 0)
for _, segment := range segments {
fields = append(fields, slack.AttachmentField{
Title: segment.Label,
Value: fmt.Sprintf("%.2f", segment.Total),
Short: true,
})
}
attachment := slack.Attachment{
Color: "good",
AuthorName: "Komiser",
AuthorSubname: "by Tailwarden",
AuthorLink: "https://tailwarden.com",
AuthorIcon: "https://cdn.komiser.io/images/komiser-logo.jpeg",
Text: fmt.Sprintf("On %s %d: cost breakdown by cloud %s", currentTime.Month(), currentTime.Day(), field),
Footer: "Komiser",
Fields: fields,
FooterIcon: "/~https://github.com/tailwarden/komiser",
Ts: json.Number(strconv.FormatInt(time.Now().Unix(), 10)),
}
msg := slack.WebhookMessage{
Attachments: []slack.Attachment{attachment},
}
err = slack.PostWebhook(cfg.Slack.Webhook, &msg)
if err != nil {
log.Warn(err)
}
}
}