-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes-apm-sys-sim.js
executable file
·393 lines (337 loc) · 9.8 KB
/
es-apm-sys-sim.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/usr/bin/env node
'use strict'
const meow = require('meow')
const hJSON = require('hjson')
const es = require('@elastic/elasticsearch')
const { createSineMetric } = require('./lib/sine-metric')
const { createRandomMetric } = require('./lib/random-metric')
const { createStepMetric } = require('./lib/step-metric')
const { createFlapMetric } = require('./lib/flap-metric')
const { createKeyboard } = require('./lib/keyboard')
const DEBUG = process.env.DEBUG != null
const MAX_MEM = 1000 * 1000 // ONE WHOLE MEGABYTE OF MEMORY!!!
const DEFAULT_INDEX_NAME = 'es-apm-sys-sim'
const DEFAULT_CLUSTER_URL = 'http://elastic:changeme@localhost:9200'
const DEFAULT_CLUSTER_URL_ENV = 'ES_URL'
const DEFAULT_HOSTS = 2
const MAX_DISPLAYED_HOSTS = 4
const hostKeys = [
{ inc: '1', dec: 'q' },
{ inc: '2', dec: 'w' },
{ inc: '3', dec: 'e' },
{ inc: '4', dec: 'r' },
]
const cliOptions = meow(getHelp(), {
flags: {
help: {
type: 'boolean',
alias: 'h',
default: false
},
keys: {
type: 'boolean',
alias: 'k',
default: false
},
random: {
type: 'boolean',
alias: 'r',
default: false
},
flap: {
type: 'boolean',
alias: 'f',
default: false
}
}
})
if (cliOptions.flags.help || cliOptions.input.length === 0) {
console.log(getHelp())
process.exit(1)
}
const isRandom = !!cliOptions.flags.random
const isKeys = !!cliOptions.flags.keys
const isFlap = !!cliOptions.flags.flap
const used = (isRandom ? 1 : 0) + (isKeys ? 1 : 0) + (isFlap ? 1 : 0)
if (used > 1) {
logError('--random, --keys, and --flap can not be used together')
}
const mode =
isRandom ? 'random walks' :
isKeys ? 'keys pressed' :
isFlap ? 'flapping' :
'sine waves'
console.log(`generating data based on ${mode}`)
const maxHosts = isKeys ? MAX_DISPLAYED_HOSTS : 1000
const [
intervalS,
hostsS = `${DEFAULT_HOSTS}`,
indexName = DEFAULT_INDEX_NAME,
clusterURL = process.env[DEFAULT_CLUSTER_URL_ENV] || DEFAULT_CLUSTER_URL
] = cliOptions.input
if (intervalS == null) logError('interval parameter missing')
const interval = parseInt(intervalS, 10)
if (isNaN(interval)) logError(`invalid interval parameter: ${intervalS}`)
const hosts = Math.min(maxHosts, parseInt(hostsS, 10))
if (isNaN(hosts)) logError(`invalid hosts parameter: ${intervalS}`)
console.log([
`running with`,
`interval: ${interval} sec;`,
`hosts: ${hosts};`,
`indexName: ${indexName};`,
`clusterURL: ${clusterURL}`
].join(' '))
let DocsWritten = 0
/** @type { Host[] } */
const Hosts = []
setImmediate(main)
/** @type { () =>void } */
function main() {
let esClient
try {
esClient = new es.Client({
node: clusterURL,
ssl: {
rejectUnauthorized: false
}
})
} catch (err) {
logError(`error creating ES client: ${err.message}`)
}
for (let i = 0; i < hosts; i++) {
Hosts.push(new Host(i, 16 * (i + 1), isRandom, isKeys))
}
const kbd = createKeyboard()
kbd.on(null, printRuntimeHelp)
kbd.on('x', () => process.exit())
kbd.on('ctrl-c', () => process.exit())
if (isKeys) {
for (const host of Hosts) {
const key = String.fromCharCode(49 + host.instance)
kbd.on(hostKeys[host.instance].inc, () => host.inc())
kbd.on(hostKeys[host.instance].dec, () => host.dec())
}
}
setImmediate(async () => { await create(); update() })
setInterval(update, 1000 * interval)
async function create() {
await createIndex(esClient)
}
let wroteSampleDoc = false
function update() {
for (const host of Hosts) {
const doc = host.nextDocument()
writeDoc(esClient, doc)
if (!wroteSampleDoc) {
wroteSampleDoc = true
const printable = hJSON.stringify(doc, {
//@ts-ignore doesn't like condense option
condense: 10000,
bracesSameLine: true,
})
//@ts-ignore doesn't like condense option
console.log(`sample doc:`, printable)
console.log('')
}
printCurrentStatus()
}
}
}
function printRuntimeHelp () {
if (isKeys) {
console.log(`\n\nhelp: press "1" ... "${hosts}" to increase, "q", "w", ... to decrease, "ctrl-c" or "x" to exit`)
} else {
console.log(`\n\nhelp: press "ctrl-c" to exit`)
}
printCurrentStatus()
}
function printCurrentStatus() {
const statuses = Hosts.slice(0, MAX_DISPLAYED_HOSTS).map(host => host.statusString())
const missing = Hosts.length <= MAX_DISPLAYED_HOSTS ? '' : ` (${Hosts.length - MAX_DISPLAYED_HOSTS} not shown)`
statuses.push(missing)
statuses.push(`docs: ${DocsWritten}`)
process.stdout.write(`\r${statuses.join(' ')}`)
}
class Host {
constructor(instance, period, isRandom, isKeys) {
this.instance = instance
this.hostName = `host-${instance + 1}`
this.isKeys = isKeys
this.cpuMetric = createMetric({
isKeys,
isRandom,
isFlap,
min: 0,
max: 1,
period,
})
this.memMetric = createMetric({
isKeys,
isRandom,
isFlap,
min: 0,
max: MAX_MEM * 4 / 10,
period,
})
}
statusString() {
const cpu = this.cpuMetric.current.toFixed(2)
const mem = Math.round(this.memMetric.current / 1000)
const memS = `${mem}`.padStart(3)
return `${this.hostName}: c:${cpu} m:${memS}K`
}
/** @type { () => any } */
nextDocument() {
const cpu = this.cpuMetric.next()
const mem = this.memMetric.next()
return getDocument(this.hostName, cpu, mem)
}
inc() {
if (!this.isKeys) return
this.cpuMetric.inc()
this.memMetric.inc()
printCurrentStatus()
}
dec() {
if (!this.isKeys) return
this.cpuMetric.dec()
this.memMetric.dec()
printCurrentStatus()
}
}
function createMetric ({isRandom, isKeys, isFlap, min, max, period}) {
if (isRandom) return createRandomMetric({ min, max });
if (isKeys) return createStepMetric({ min, max });
if (isFlap) return createFlapMetric({ min, max });
return createSineMetric({ min, max, period });
}
/** @type { (esClient: any) => Promise<void> } */
async function createIndex (esClient) {
if (DEBUG) console.log(`creating index ${indexName}`)
let response
try {
response = await esClient.indices.create({
index: indexName,
body: {
mappings: getIndexMappings(),
}
})
} catch (err) {
if (err.meta?.body?.error?.type === 'resource_already_exists_exception') {
console.log(`index ${indexName} already exists`)
return
}
logError(`error creating index: ${JSON.stringify(err, null, 4)}`)
}
if (response.body?.error?.type === 'resource_already_exists_exception') {
console.log(`index ${indexName} already exists`)
return
}
if (response.statusCode !== 200) {
logError(`unexpected error creating index: ${JSON.stringify(response, null, 4)}`)
}
}
/** @type { (esClient: any, doc: any) => Promise<void> } */
async function writeDoc (esClient, doc) {
if (DEBUG) console.log(`writing doc ${indexName}: ${JSON.stringify(doc)}`)
let response
try {
response = await esClient.index({
index: indexName,
body: doc
})
} catch (err) {
logError(`error indexing document: ${JSON.stringify(err, null, 4)}`)
}
if (response.statusCode !== 201) {
logError(`unexpected error indexing document: ${JSON.stringify(response, null, 4)}`)
}
DocsWritten++
}
/** @type { (hostName: string, cpu: number, mem: number) => any } */
function getDocument(hostName, cpu, mem) {
return {
'@timestamp': new Date().toISOString(),
host: {
name: hostName
},
system: {
cpu: {
total: {
norm: {
pct: cpu
}
}
},
memory: {
actual: {
free: mem
},
total: MAX_MEM
}
}
}
}
/** @type { () => string } */
function getHelp () {
return `
es-apm-sys-sim [options] <intervalSeconds> [<hosts> [<indexName> [<clusterURL>]]]
Writes apm system metrics documents containing cpu usage and free memory
metrics, for the specified number of hosts, on the specified interval, to
the specified index at the specified elasticsearch cluster.
<hosts> defaults to ${DEFAULT_HOSTS} if not supplied
<indexName> defaults to es-apm-sys-sim if not supplied
<clusterURL> defaults to the environment variable ${DEFAULT_CLUSTER_URL_ENV} or the value
${DEFAULT_CLUSTER_URL}
options:
-r, --random
-k, --keys
-f, --flap
If the --random or -r flag is used, the data generated is based on random walks.
If the --keys or -k flag is used, the data generated based on keys pressed. The
maximum number of hosts will be ${MAX_DISPLAYED_HOSTS}.
If the --flap or -f flag is used, the data generated will flap between the
minimum and maximum values.
Otherwise, the data generated is based on sine waves.
Fields in documents written:
@timestamp current time
host.name host name
host.name.keyword host name (keyword field for aggregations)
system.cpu.total.norm.pct cpu usage, 0 -> 1
memory.actual.free free memory, 0 -> 400KB
memory.total total memory, 1MB heh
home: https://github.com/pmuellr/es-apm-sys-sim
`.trim()
}
/** @type { (string) => void } */
function logError (message) {
console.log(message)
process.exit(1)
}
function getIndexMappings() {
const date = 'date'
const keyword = 'keyword'
const float = 'float'
const long = 'long'
return {
properties: {
"@timestamp": {
type: date
},
host: {
properties: { name: { type: keyword } }
},
system: {
properties:
{ cpu: { properties: { total: { properties: { norm: { properties: { pct: { type: float } } } } } } },
memory: {
properties: {
actual: { properties: { free: { type: long } } },
total: { type: long }
}
}
}
}
}
}
}