-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
executable file
·208 lines (175 loc) · 5.85 KB
/
health.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
)
// NodeHealth represents a dataset of basic health information for a node
type NodeHealth struct {
EC2InstanceID string `json:"ec2_instance_id"`
UpTime uint64 `json:"uptime"`
CPUPercent float64 `json:"cpu_utilization_percent"`
DiskPercent float64 `json:"disk_utilization_percent"`
RAMTotalBytesUsed uint64 `json:"total_ram_bytes_used"`
RAMTotalBytesAvailable uint64 `json:"total_ram_bytes_available"`
}
// ClusterHealth represents a set of NodeHealth
type ClusterHealth struct {
NodeHealths []NodeHealth `json:"node_healths"`
}
// GetInstanceID retrieves the ec2 instance id
// If in DEBUG mode, "local" is returned
func GetInstanceID(writer http.ResponseWriter) string {
ec2InstanceID := "local"
if os.Getenv("DEBUG") != "true" {
idResp, err := http.Get("http://169.254.169.254/latest/meta-data/instance-id")
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
defer idResp.Body.Close()
ec2Id, err := ioutil.ReadAll(idResp.Body)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
ec2InstanceID = string(ec2Id[:])
}
return ec2InstanceID
}
// GetUptime retrieves the Host Uptime
func GetUptime(writer http.ResponseWriter) uint64 {
upTime, err := host.Uptime()
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
return upTime
}
// GetCPUUtilization retrieves the CPU Utilization percent
func GetCPUUtilization(writer http.ResponseWriter) float64 {
cpuPercent, err := cpu.Percent(0, false)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
return cpuPercent[0]
}
// GetDiskUtilization retrieves the Disk Utilization percent. This involves summing the utilization of the partitions
func GetDiskUtilization(writer http.ResponseWriter) float64 {
// Get disk partitions
diskPartitions, err := disk.Partitions(false)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
// Sum up utilization of the disk partitions
diskUtilization := 0.0
for _, partition := range diskPartitions {
u, err := disk.Usage(partition.Mountpoint)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
diskUtilization += u.UsedPercent
}
return diskUtilization
}
// GetClusterHealth retrieves the health of the nodes in the cluster
func GetClusterHealth(writer http.ResponseWriter) *ClusterHealth {
var ipAddresses []string
if os.Getenv("DEBUG") == "true" {
ipAddresses = append(ipAddresses, "localhost")
} else {
sess := getSession(writer)
ec2ids := getEC2IdsFromELB(writer, sess)
ipAddresses = getIPAddressesFromEC2Ids(writer, sess, ec2ids)
}
clusterHealth := queryNodeHealths(writer, ipAddresses)
return clusterHealth
}
// getSession instantiates a Session on AWS
// This method is only invoked on the deployed environment
func getSession(writer http.ResponseWriter) *session.Session {
// Create new session
sess := session.New(&aws.Config{Region: aws.String(os.Getenv("AWS_REGION"))})
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
return sess
}
// getEC2IdsFromELB retrieves EC2 Instance Ids from the ELB
// This method is only invoked on the deployed environment
func getEC2IdsFromELB(writer http.ResponseWriter, sess *session.Session) []*string {
// Retrieve list of EC2 Instance Id's of nodes currently on the load balancer
elbService := elb.New(sess)
elbParams := &elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{
aws.String(os.Getenv("ELB_NAME")),
},
}
elbResponse, err := elbService.DescribeLoadBalancers(elbParams)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
var ec2ids []*string
for _, elbInstance := range elbResponse.LoadBalancerDescriptions[0].Instances {
ec2ids = append(ec2ids, elbInstance.InstanceId)
}
return ec2ids
}
// getIPAddressesFromEC2Ids retrieves IP Addresses from EC2 Instance Ids
// This method is only invoked on the deployed environment
func getIPAddressesFromEC2Ids(writer http.ResponseWriter, sess *session.Session, ec2ids []*string) []string {
// Instantiate the EC2 Service, and use it to retrieve server IP Addresses from instance id's
ec2Service := ec2.New(sess)
describeInstancesParams := &ec2.DescribeInstancesInput{
InstanceIds: ec2ids,
}
ec2Instances, err := ec2Service.DescribeInstances(describeInstancesParams)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
var instanceIPAddresses []string
for _, ec2Reservation := range ec2Instances.Reservations {
for _, ec2Instance := range ec2Reservation.Instances {
instanceIPAddresses = append(instanceIPAddresses, *ec2Instance.PrivateIpAddress)
}
}
return instanceIPAddresses
}
// queryNodeHealths invokes the health endpoint on each node, and returns a ClusterHealth instance
func queryNodeHealths(writer http.ResponseWriter, instanceIPAddresses []string) *ClusterHealth {
// Invoke the health endpoint on each node, and store the results
clusterHealth := new(ClusterHealth)
for _, IPAddress := range instanceIPAddresses {
queryURL := "http://" + IPAddress + "/health"
nodeHealthResp, err := http.Get(queryURL)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
defer nodeHealthResp.Body.Close()
nodeHealth, err := ioutil.ReadAll(nodeHealthResp.Body)
if err != nil {
fmt.Fprintf(writer, err.Error())
panic(err.Error())
}
var nodeHealthData NodeHealth
json.Unmarshal(nodeHealth, &nodeHealthData)
clusterHealth.NodeHealths = append(clusterHealth.NodeHealths, nodeHealthData)
}
return clusterHealth
}