This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
/
components_check.go
72 lines (59 loc) · 1.92 KB
/
components_check.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package cmd
import (
"strings"
"time"
"github.com/TheThingsNetwork/api"
"github.com/TheThingsNetwork/api/discovery"
"github.com/TheThingsNetwork/ttn/api/health"
"github.com/TheThingsNetwork/ttn/ttnctl/util"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/spf13/cobra"
)
var checkCmd = &cobra.Command{
Use: "check [ServiceType] [ServiceID]",
Short: "Check routing services",
Long: `ttnctl components check is used to check the status of routing services`,
Run: func(cmd *cobra.Command, args []string) {
assertArgsLength(cmd, args, 2, 2)
serviceType := strings.ToLower(args[0])
switch serviceType {
case "router", "broker", "handler":
default:
ctx.Fatalf("Service type %s unknown", serviceType)
}
serviceID := strings.ToLower(args[1])
if err := api.NotEmptyAndValidID(serviceID, "Service ID"); err != nil {
ctx.Fatal(err.Error())
}
dscConn, client := util.GetDiscovery(ctx)
defer dscConn.Close()
res, err := client.Get(util.GetContext(ctx), &discovery.GetRequest{
ServiceName: serviceType,
ID: serviceID,
})
if err != nil {
ctx.WithError(errors.FromGRPCError(err)).Fatalf("Could not get %s %s", serviceType, serviceID)
}
conn, err := res.Dial(nil)
if err != nil {
ctx.WithError(errors.FromGRPCError(err)).Fatalf("Could not dial %s %s", serviceType, serviceID)
}
defer conn.Close()
start := time.Now()
ok, err := health.Check(conn)
if err != nil {
ctx.WithError(errors.FromGRPCError(err)).Fatalf("Could not check %s %s", serviceType, serviceID)
}
ctx = ctx.WithField("Duration", time.Now().Sub(start))
if ok {
ctx.Infof("%s %s is up and running", serviceType, serviceID)
} else {
ctx.Warnf("%s %s is not feeling well", serviceType, serviceID)
}
},
}
func init() {
componentsCmd.AddCommand(checkCmd)
}