-
-
Save chrisronline/d24ec862efdb0cfa331c7ef785241116 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const fs = require('fs-extra'); | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | |
async function esRequest(method, url, data, onFailure) { | |
try { | |
const opts = { | |
method, | |
url, | |
auth: { | |
username: 'elastic', | |
password: 'changeme', | |
}, | |
}; | |
if (data) { | |
opts.data = data; | |
} | |
const { data: result } = await axios(opts); | |
return result; | |
} catch (err) { | |
if (onFailure) { | |
onFailure(); | |
} else { | |
console.log(`Error executing request to ${method}:${url}`, err.message); | |
// console.log(err.response.data); | |
} | |
} | |
} | |
function findAliases(props, parentPath = [], aliases = []) { | |
const keys = Object.keys(props); | |
for (const key of keys) { | |
const value = props[key]; | |
if (value.type === 'alias') { | |
aliases.push({ key: [...parentPath, key].join('.'), path: value.path }); | |
} | |
if (value.properties) { | |
findAliases(value.properties, [...parentPath, key], aliases); | |
} | |
} | |
return aliases; | |
} | |
(async function() { | |
const result = await esRequest('GET', 'https://localhost:9200/metricbeat-*/_mapping'); | |
const result2 = await esRequest('GET', 'https://localhost:9200/metricbeat-*/_settings'); | |
const properties = Object.values(result)[0].mappings.properties; | |
const aliases = findAliases(properties); | |
const settings = Object.values(result2)[0].settings; | |
delete settings.index.creation_date; | |
delete settings.index.provided_name; | |
delete settings.index.uuid; | |
delete settings.index.version; | |
fs.writeJSONSync('/Users/chris/Desktop/mb_aliases.json', aliases) | |
fs.writeJSONSync('/Users/chris/Desktop/mb_settings.json', { | |
type: 'index', | |
value: { | |
index: 'metricbeat-8.0.0', | |
mappings: Object.values(result)[0].mappings, | |
settings, | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment