-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (90 loc) · 2.51 KB
/
main.go
File metadata and controls
104 lines (90 loc) · 2.51 KB
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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"telkom/netclient"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
var (
username = flag.String("username", "", "Telkom username")
password = flag.String("password", "", "Telkom password")
localDebug = flag.Bool("localdebug", false,
"Run locally instead of lambda handler")
)
func main() {
flag.Parse()
if *localDebug {
services, err := netclient.GetServiceBundles(*username, *password)
if err != nil {
log.Fatalf(err.Error())
}
for _, s := range services {
log.Println(s)
}
return
}
lambda.Start(handler)
}
func handler() error {
telkomUsernames := os.Getenv("TELKOM_USERNAMES")
telkomPasswords := os.Getenv("TELKOM_PASSWORDS")
if telkomUsernames == "" || telkomPasswords == "" {
return fmt.Errorf("Must set env variables TELKOM_USERNAMES and TELKOM_PASSWORDS")
}
// Retrieve service and bundle information:
users := strings.Split(telkomUsernames, ";")
passwords := strings.Split(telkomPasswords, ";")
services := []netclient.Service{}
for i, user := range users {
s, err := netclient.GetServiceBundles(user, passwords[i])
if err != nil {
return err
}
services = append(services, s...)
}
// Publish to aws cloudwatch metrics
sess := session.Must(session.NewSession())
return putTelkomMetricsToCloudwatch(sess, services)
}
func putTelkomMetricsToCloudwatch(sess *session.Session, services []netclient.Service) error {
svc := cloudwatch.New(sess)
for _, service := range services {
_, err := svc.PutMetricData(&cloudwatch.PutMetricDataInput{
Namespace: aws.String("TelkomBytes"),
MetricData: []*cloudwatch.MetricDatum{
&cloudwatch.MetricDatum{
MetricName: aws.String("Standard Remaining"),
Unit: aws.String("Bytes"),
Value: aws.Float64(float64(service.NonFreeBytesRemaining())),
Dimensions: []*cloudwatch.Dimension{
&cloudwatch.Dimension{
Name: aws.String("MSISDN"),
Value: aws.String(service.Msisdn),
},
},
},
&cloudwatch.MetricDatum{
MetricName: aws.String("NighSurfer Remaining"),
Unit: aws.String("Bytes"),
Value: aws.Float64(float64(service.NightSurferRemaining())),
Dimensions: []*cloudwatch.Dimension{
&cloudwatch.Dimension{
Name: aws.String("MSISDN"),
Value: aws.String(service.Msisdn),
},
},
},
},
})
if err != nil {
return err
}
}
return nil
}