Created
August 29, 2017 18:46
-
-
Save tsusanto/5a0a710791fb22cbfd0a8f4a4b8f2576 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
//Tenny Susanto | |
//restart hue that are in bad health | |
//send alerts | |
var http = require('http'); | |
var email = require("emailjs"); | |
var querystring = require('querystring') | |
var server = email.server.connect({ | |
host: "relay01.sc5.coupons.lan" | |
}); | |
//var host, port | |
host = 'CM_host'; | |
port = '7180'; | |
pw = 'CM_password' | |
// prepare the header | |
var getheaders = { | |
'Accept' : 'application/json', | |
'Content-Type' : 'application/json', | |
'Authorization': 'Basic ' + new Buffer('admin' + ':' + pw).toString('base64') | |
}; | |
// the get options | |
var optionsget = { | |
host : host, | |
port : port, | |
path : '/api/v13/clusters/cluster/services/hue/roles', | |
method : 'GET', | |
headers : getheaders | |
}; | |
// do the GET call | |
var reqGet = http.request(optionsget, function(res) { | |
var body = ''; | |
res.on('data', function(d) { | |
body += d; | |
}); | |
res.on('end', function() { | |
var obj = JSON.parse(body); | |
var items = obj.items; | |
for (var i = 0; i < items.length; i++) { | |
type=items[i].type; | |
roleHealth=items[i].entityStatus; | |
roleURL=items[i].roleUrl; | |
if (type == "HUE_SERVER" && roleHealth != "GOOD_HEALTH") { | |
var bad_count=0; | |
var hostname = items[i].hostRef.hostId; | |
// the get options | |
var optionsget2 = { | |
host : host, | |
port : port, | |
path : '/api/v13/hosts/' + hostname, | |
method : 'GET', | |
headers : getheaders | |
}; | |
var get_hostname_req = http.request(optionsget2, function(res) { | |
var hostbody = ''; | |
res.on('data', function(d) { | |
hostbody += d; | |
}); | |
res.on('end', function(){ | |
var o = JSON.parse(hostbody); | |
var hue_server=o.hostname; | |
console.log(hue_server); | |
}) | |
}); | |
get_hostname_req.end(); | |
for (var x=0; x < items[i].healthChecks.length; x++) { | |
if ( (items[i].healthChecks[x].name == "HUE_SERVER_WEB_METRIC_COLLECTION" || items[i].healthChecks[x].name == "HUE_SERVER_AUDIT_HEALTH") && items[i].healthChecks[x].summary != "GOOD") { | |
var bad_count=1; | |
} | |
} | |
if (bad_count > 0) { | |
var post_data = {} | |
post_data.items = [] | |
post_data.items.push(items[i].name) | |
post_data_string = JSON.stringify(post_data) | |
var headers = { | |
'Accept' : 'application/json', | |
'Content-Type' : 'application/json', | |
'Authorization': 'Basic ' + new Buffer('admin' + ':' + pw).toString('base64') | |
}; | |
var optionspost = { | |
host : host, | |
port : port, | |
path : '/api/v13/clusters/cluster/services/hue/roleCommands/restart', | |
method : 'POST', | |
headers : getheaders | |
}; | |
console.log(post_data_string); | |
var post_req = http.request(optionspost, function(res) { | |
res.setEncoding('utf8'); | |
res.on('data', function (chunk) { | |
console.log('Response: ' + chunk); | |
}); | |
}); | |
post_req.write(post_data_string); | |
post_req.end(); | |
server.send({ | |
text: roleURL, | |
from: "impala-admin@email.com", | |
to: "some@email.com", | |
subject: "Hue service auto-restarted" | |
}); | |
}//if | |
} | |
} | |
}); | |
}); | |
reqGet.end(); | |
reqGet.on('error', function(e) { | |
console.error(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment