forked from heartbeatsjp/azmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (160 loc) · 4.31 KB
/
main.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
package main
import (
"errors"
"fmt"
"log"
"os"
"github.com/urfave/cli"
)
const (
// Name is the application name
Name = "azmon"
// Usage is the application usage
Usage = "A tool for Azure Monitor at Microsoft Azure"
// Version is the application version
Version = "1.0.0"
)
func buildFetchMetricDataInput(c *cli.Context) FetchMetricDataInput {
subCommand := c.Parent().Args().First()
var metricNames []string
if subCommand == "check" {
metricNames = []string{c.String("metric-name")}
} else if subCommand == "metric" {
metricNames = c.StringSlice("metric-names")
}
return FetchMetricDataInput{
subscriptionID: c.GlobalString("subscription-id"),
resourceGroup: c.GlobalString("resource-group"),
namespace: c.GlobalString("namespace"),
resource: c.GlobalString("resource"),
metricNames: metricNames,
aggregation: c.GlobalString("aggregation"),
}
}
func buildFetchMetricDefinitionsInput(c *cli.Context) FetchMetricDefinitionsInput {
return FetchMetricDefinitionsInput{
subscriptionID: c.GlobalString("subscription-id"),
resourceGroup: c.GlobalString("resource-group"),
namespace: c.GlobalString("namespace"),
resource: c.GlobalString("resource"),
}
}
func validationGlobalFlags(c *cli.Context) error {
if v := c.GlobalString("subscription-id"); v == "" {
return errors.New("missing subscription-id")
}
if v := c.GlobalString("resource-group"); v == "" {
return errors.New("missing resource-group")
}
if v := c.GlobalString("namespace"); v == "" {
return errors.New("missing namespace")
}
if v := c.GlobalString("resource"); v == "" {
return errors.New("missing resource")
}
if v := c.GlobalString("aggregation"); v == "" {
return errors.New("missing aggregation")
}
if v := c.GlobalString("aggregation"); v != "Total" && v != "Average" && v != "Maximum" && v != "Minimum" {
return errors.New("invalid aggregation: choose from \"Total\", \"Average\", \"Maximum\", \"Minimum\" (\"Count\" is not supported)")
}
return nil
}
func setAzureAuthLocation(c *cli.Context) error {
return os.Setenv("AZURE_AUTH_LOCATION", c.GlobalString("auth-file"))
}
func appBefore(c *cli.Context) error {
if err := validationGlobalFlags(c); err != nil {
return fmt.Errorf("validation global flags failed: %s", err.Error())
}
if err := setAzureAuthLocation(c); err != nil {
return fmt.Errorf("set AZURE_AUTH_LOCATION failed: %s", err.Error())
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = Name
app.Usage = Usage
app.Version = Version
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "subscription-id, s",
Usage: "Set the subscription id",
},
cli.StringFlag{
Name: "resource-group, g",
Usage: "Set the resource group name",
},
cli.StringFlag{
Name: "namespace, n",
Usage: "Set the metric namespace",
},
cli.StringFlag{
Name: "resource, r",
Usage: "Set the target resource name",
},
cli.StringFlag{
Name: "aggregation, a",
Usage: "Set the aggregation type. Choose from \"Total\", \"Average\", \"Maximum\", \"Minimum\" (\"Count\" is not supported)",
},
cli.StringFlag{
Name: "auth-file",
Usage: "Set the azure auth file path",
Value: "/etc/nagios/azure.auth",
},
}
app.Before = appBefore
app.Commands = []cli.Command{
{
Name: "check",
Usage: "check metric data as Nagios plugin",
Flags: []cli.Flag{
cli.StringFlag{
Name: "metric-name, m",
Usage: "Set the name of the metric",
},
cli.Float64Flag{
Name: "warning-over, w",
Usage: "Set the warning threshold",
Value: 0,
},
cli.Float64Flag{
Name: "warning-under, W",
Usage: "Set the warning threshold",
Value: 0,
},
cli.Float64Flag{
Name: "critical-over, c",
Usage: "Set the critical threshold",
Value: 0,
},
cli.Float64Flag{
Name: "critical-under, C",
Usage: "Set the critical threshold",
Value: 0,
},
},
Action: Check,
},
{
Name: "metric",
Usage: "print metric data in Sensu plugin format",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "metric-names, m",
Usage: "Set the names of the metric",
},
cli.StringFlag{
Name: "prefix, p",
Usage: "Set the metric key prefix",
Value: "azure",
},
},
Action: Metric,
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}