-
Notifications
You must be signed in to change notification settings - Fork 2k
/
uptime.js
331 lines (288 loc) · 10.2 KB
/
uptime.js
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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* This application demonstrates how to perform basic operations on uptime check
* configs with the Google Stackdriver Monitoring API.
*
* For more information, see the README.md under /monitoring and the
* documentation at https://cloud.google.com/monitoring/uptime-checks/.
*/
'use strict';
async function createUptimeCheckConfig(projectId, hostname) {
// [START monitoring_uptime_check_create]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const hostname = 'mydomain.com';
const request = {
// i.e. parent: 'projects/my-project-id'
parent: client.projectPath(projectId),
uptimeCheckConfig: {
displayName: 'My Uptime Check',
monitoredResource: {
// See the Uptime Check docs for supported MonitoredResource types
type: 'uptime_url',
labels: {
host: hostname,
},
},
httpCheck: {
path: '/',
port: 80,
},
timeout: {
seconds: 10,
},
period: {
seconds: 300,
},
},
};
// Creates an uptime check config for a GCE instance
const [uptimeCheckConfig] = await client.createUptimeCheckConfig(request);
console.log('Uptime check created:');
console.log(`ID: ${uptimeCheckConfig.name}`);
console.log(`Display Name: ${uptimeCheckConfig.displayName}`);
console.log('Resource: %j', uptimeCheckConfig.monitoredResource);
console.log('Period: %j', uptimeCheckConfig.period);
console.log('Timeout: %j', uptimeCheckConfig.timeout);
console.log(`Check type: ${uptimeCheckConfig.check_request_type}`);
console.log(
'Check: %j',
uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck
);
console.log(
`Content matchers: ${uptimeCheckConfig.contentMatchers
.map(matcher => matcher.content)
.join(', ')}`
);
console.log(`Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`);
// [END monitoring_uptime_check_create]
}
async function listUptimeCheckConfigs(projectId) {
// [START monitoring_uptime_check_list_configs]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
const request = {
parent: client.projectPath(projectId),
};
// Retrieves an uptime check config
const [uptimeCheckConfigs] = await client.listUptimeCheckConfigs(request);
uptimeCheckConfigs.forEach(uptimeCheckConfig => {
console.log(`ID: ${uptimeCheckConfig.name}`);
console.log(` Display Name: ${uptimeCheckConfig.displayName}`);
console.log(' Resource: %j', uptimeCheckConfig.monitoredResource);
console.log(' Period: %j', uptimeCheckConfig.period);
console.log(' Timeout: %j', uptimeCheckConfig.timeout);
console.log(` Check type: ${uptimeCheckConfig.check_request_type}`);
console.log(
' Check: %j',
uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck
);
console.log(
` Content matchers: ${uptimeCheckConfig.contentMatchers
.map(matcher => matcher.content)
.join(', ')}`
);
console.log(` Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`);
});
// [END monitoring_uptime_check_list_configs]
}
async function listUptimeCheckIps() {
// [START monitoring_uptime_check_list_ips]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
// List uptime check IPs
const [uptimeCheckIps] = await client.listUptimeCheckIps();
uptimeCheckIps.forEach(uptimeCheckIp => {
console.log(
uptimeCheckIp.region,
uptimeCheckIp.location,
uptimeCheckIp.ipAddress
);
});
// [END monitoring_uptime_check_list_ips]
}
async function getUptimeCheckConfig(projectId, uptimeCheckConfigId) {
// [START monitoring_uptime_check_get]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Retrieving ${request.name}`);
// Retrieves an uptime check config
const [uptimeCheckConfig] = await client.getUptimeCheckConfig(request);
console.log(`ID: ${uptimeCheckConfig.name}`);
console.log(`Display Name: ${uptimeCheckConfig.displayName}`);
console.log('Resource: %j', uptimeCheckConfig.monitoredResource);
console.log('Period: %j', uptimeCheckConfig.period);
console.log('Timeout: %j', uptimeCheckConfig.timeout);
console.log(`Check type: ${uptimeCheckConfig.check_request_type}`);
console.log(
'Check: %j',
uptimeCheckConfig.httpCheck || uptimeCheckConfig.tcpCheck
);
console.log(
`Content matchers: ${uptimeCheckConfig.contentMatchers
.map(matcher => matcher.content)
.join(', ')}`
);
console.log(`Regions: ${uptimeCheckConfig.selectedRegions.join(', ')}`);
// [END monitoring_uptime_check_get]
}
async function deleteUptimeCheckConfig(projectId, uptimeCheckConfigId) {
// [START monitoring_uptime_check_delete]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Deleting ${request.name}`);
// Delete an uptime check config
await client.deleteUptimeCheckConfig(request);
console.log(`${request.name} deleted.`);
// [END monitoring_uptime_check_delete]
}
async function updateUptimeCheckConfigDisplayName(
projectId,
uptimeCheckConfigId,
displayName,
path
) {
// [START monitoring_uptime_check_update]
// Imports the Google Cloud client library
const monitoring = require('@google-cloud/monitoring');
// Creates a client
const client = new monitoring.UptimeCheckServiceClient();
/**
* TODO(developer): Uncomment and edit the following lines of code.
*/
// const projectId = 'YOUR_PROJECT_ID';
// const uptimeCheckConfigId = 'YOUR_UPTIME_CHECK_CONFIG_ID';
// const displayName = 'A New Display Name';
// const path = '/some/path';
const request = {
// i.e. name: 'projects/my-project-id/uptimeCheckConfigs/My-Uptime-Check
name: client.projectUptimeCheckConfigPath(projectId, uptimeCheckConfigId),
};
console.log(`Updating ${request.name} to ${displayName}`);
// Updates the display name and path on an uptime check config
request.uptimeCheckConfig = {
name: request.name,
displayName: displayName,
httpCheck: {
path: path,
},
};
request.updateMask = {
paths: ['display_name', 'http_check.path'],
};
const [response] = await client.updateUptimeCheckConfig(request);
console.log(`${response.name} config updated.`);
// [END monitoring_uptime_check_update]
}
require('yargs')
.demand(1)
.command(
'create <hostname> [projectId]',
'Creates an uptime check config.',
{},
opts => createUptimeCheckConfig(opts.projectId, '' + opts.hostname)
)
.command('list [projectId]', 'Lists uptime check configs.', {}, opts =>
listUptimeCheckConfigs(opts.projectId)
)
.command('list-ips', 'Lists uptime check config IPs.', {}, () =>
listUptimeCheckIps()
)
.command(
'get <uptimeCheckConfigId> [projectId]',
'Gets an uptime check config.',
{},
opts => getUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId)
)
.command(
'delete <uptimeCheckConfigId> [projectId]',
'Deletes an uptime check config.',
{},
opts => deleteUptimeCheckConfig(opts.projectId, opts.uptimeCheckConfigId)
)
.command(
'update <uptimeCheckConfigId> <displayName> <path> [projectId]',
'Update the display name of an uptime check config.',
{},
opts =>
updateUptimeCheckConfigDisplayName(
opts.projectId,
opts.uptimeCheckConfigId,
opts.displayName,
opts.path
)
)
.options({
projectId: {
alias: 'p',
default: process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT,
global: true,
requiresArg: true,
type: 'string',
},
})
.example('node $0 create mydomain.com', 'Create an uptime check.')
.example('node $0 list', 'List all uptime check configs.')
.example(
'node $0 list "resource.type = gce_instance AND resource.label.instance_id = mongodb"',
'List all uptime check configs for a specific GCE instance.'
)
.example('node $0 list-ips')
.example('node $0 get My-Uptime-Check')
.example('node $0 delete My-Uptime-Check')
.example('node $0 update My-Uptime-Check "My New Uptime Check" /some/path')
.wrap(120)
.recommendCommands()
.epilogue(
'For more information, see https://cloud.google.com/monitoring/uptime-checks/'
)
.help()
.strict().argv;