forked from RedHatInsights/policies-ui-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pushhost.ts
executable file
·209 lines (188 loc) · 6.55 KB
/
pushhost.ts
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
#!/usr/bin/env ts-node
import { exec } from 'child_process';
import * as cliProgress from 'cli-progress';
import { Command } from 'commander';
import { createHash } from 'crypto';
import { config as dotenvConfig } from 'dotenv';
import { lookpath } from 'lookpath';
import randomWords from 'random-words';
const getHost = (account) => {
const hostname = randomWords({ min: 1, max: 2, join: ' ', formatter: (word, index) => {
return index === 0 ? word.slice(0, 1).toUpperCase().concat(word.slice(1)) : word;
} });
const inventoryId = createHash('md5').update(hostname).digest('hex');
const insightsId = createHash('md5').update(inventoryId).digest('hex');
return {
timestamp: new Date().toISOString(),
host: {
account,
created: new Date().toISOString(),
updated: new Date().toISOString(), //'2011-12-03T10:15:30+01:00',
display_name: hostname,
tags: [],
reporter: 'puptoo',
id: inventoryId,
insights_id: insightsId,
system_profile: {
arch: 'x86_64',
bios_release_date: 'string',
bios_vendor: 'string',
bios_version: 'string',
cloud_provider: 'string',
cores_per_socket: 2,
cpu_flags: [
'string'
],
disk_devices: [
{
device: '/dev/fdd0',
label: 'string',
mount_point: '/mnt/remote_nfs_shares',
options: {
ro: true,
uid: '0'
},
type: 'ext3'
}
],
enabled_services: [
'string'
],
infrastructure_type: 'string',
infrastructure_vendor: 'string',
insights_client_version: 'string',
insights_egg_version: 'string',
installed_packages: [
'0:krb5-libs-1.16.1-23.fc29.i686'
],
installed_products: [
{
id: '71',
name: 'string',
status: 'Subscribed'
}
],
installed_services: [
'string'
],
katello_agent_running: true,
kernel_modules: [
'string'
],
last_boot_time: '2019-11-20T14:42:38.905Z',
network_interfaces: [
{
ipv4_addresses: [
'198.51.100.42'
],
ipv6_addresses: [
'2001:0db8:5b96:0000:0000:426f:8e17:642a'
],
mac_address: '00:00:00:00:00:00',
mtu: 0,
name: 'eth0',
state: 'UP',
type: 'ether'
}
],
number_of_cpus: 2,
number_of_sockets: 2,
os_kernel_version: 'string',
os_release: 'string',
running_processes: [
'string'
],
satellite_managed: true,
subscription_auto_attach: 'string',
subscription_status: 'string',
system_memory_bytes: 0,
yum_repos: [
{
baseurl: 'string',
enabled: true,
gpgcheck: true,
name: 'string'
}
]
}
}
};
};
const run = async () => {
const kafkacat = await lookpath('kafkacat');
if (!kafkacat) {
console.error('kafkacat is required to run this command, please install it and have it available on your path');
process.exit(1);
}
const program = new Command();
program
.description('Pushes random hosts to local kafka for testing purposes')
.option(
'-a, --account <account-number>',
'Account number to use, also loaded from env.INSIGHTS_ACCOUNT or from file .push-host.env'
)
.option(
'-k, --kafka <kafka-server:kafka-port>',
'Kafka server to pass the data',
'localhost:9092'
)
.option(
'-t, --topic <topic>',
'Topic to send the data',
'platform.inventory.events'
)
.option<number>(
'-ac, --alert-count <alert-count>',
'Number of alerts to generate',
(value: string) => parseInt(value) || 20,
20
)
.option<number>(
'-s, --sleep <sleep-time-between-each-ms>',
'Sleep time (ms) between each data to avoid having them all at the same time',
(value: string) => parseInt(value) || 100,
100
);
program.parse();
interface Params {
account?: string;
kafka: string;
topic: string;
alertCount: number;
sleep: number;
}
const params = program as unknown as Params;
dotenvConfig({
path: './.push-host.env'
});
if (!params.account) {
if (!process.env.INSIGHTS_ACCOUNT) {
console.error('-account was not used and INSIGHTS_ACCOUNT env was not found, exiting');
process.exit(1);
}
params.account = process.env.INSIGHTS_ACCOUNT;
}
console.info('Using arguments:', {
account: params.account,
kafka: params.kafka,
topic: params.topic,
alertCount: params.alertCount,
sleep: params.sleep
});
const kafkacatProcess = exec(`${kafkacat} -P -t ${params.topic} -b ${params.kafka} -H "event_type=updated"`);
if (kafkacatProcess.stdin) {
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
progressBar.start(params.alertCount, 0);
for (let i = 0; i < params.alertCount; ++i) {
kafkacatProcess.stdin.write(JSON.stringify(getHost(params.account)));
kafkacatProcess.stdin.write('\n');
progressBar.increment();
await new Promise(resolve => setTimeout(resolve, params.sleep));
}
progressBar.stop();
kafkacatProcess.stdin.end();
}
kafkacatProcess.stdout?.pipe(process.stdout);
kafkacatProcess.stderr?.pipe(process.stderr);
};
run();