-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
117 lines (99 loc) · 2.53 KB
/
script.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
const { Client } = require('@elastic/elasticsearch')
const fs = require('fs')
//For password security.
require('dotenv').config();
//Create .env file and setup the following
//USER_NAME=netsage username to access elasticsearch datasource
//USER_PASSWORD=password to authenticate.
const userName = process.env.USER_NAME;
const password = process.env.USER_PASSWORD;
// SET UP CLIENT
//node : elasticsearch endpoint url
//auth : Can use username and password as well as API Key if available.
const client = new Client({
node: 'https://netsage-elk1.grnoc.iu.edu/esproxy2/',
auth: {
username: userName,
password: password
}
})
// ANOTHER METHOD TO SETUP CLIENT
// const client = new Client({
// node: 'https://username:password@url'
// })
//API KEY AUTHENTICATION FOR CLIENT
// const client = new Client({
// node: 'endpoint_URL_here,
// auth: {
// apiKey: 'apikey_here'
// }
// })
//USAGE
// promise API
// const result = await client.search({
// index: 'my-index',
// body: {
// query: {
// match: { hello: 'world' }
// }
// }
// })
// callback API
// client.search({
// index: 'my-index',
// body: {
// query: {
// match: { hello: 'world' }
// }
// }
// }, (err, result) => {
// if (err) console.log(err)
// })
//EXAMPLE SEARCH QUERY
client.search({
index: 'om-netsage-irnc-*',
body: {
query: {
match: {'meta.sensor_id' :'University of Hawaii Tstat' }
//match: { "meta.protocol" : "tcp" }
}
}
}, (err, result) => {
//Handle error
if (err) console.log(err);
if(result){
//do things with the result.
console.log(result.body);
}
})
// client.search({
// index: 'om-ns-netsage-*',
// body: {
// query: {
// match: {'meta.sensor_id.keyword' :'University of Hawaii Tstat' }
// }
// }
// }, (err, result) => {
// //Handle error
// if (err) console.log(err);
// if(result){
// UHresults = result.body.hits;
// console.log(UHresults);
// myJSON = JSON.stringify(UHresults);
// fs.writeFile('./testOutput.json', myJSON, err => {
// if (err) {
// console.log('Error writing file', err)
// } else {
// console.log('Successfully wrote file')
// }
// })
// }
// })
//The result is always in the following format.
// {
// body: object | boolean
// statusCode: number
// headers: object
// warnings: [string],
// meta: object
// }