Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory #3

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
163 changes: 129 additions & 34 deletions app/routes/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ db_name = "wstats"
db = nano.use(db_name)
cronJob = require('cron').CronJob


#Declare the different variables grabbed from Heroku environment for SSH
dotCloudpKey = process.env['dotcloudPRIVATEKEY']
ec2pKey = process.env['ec2PRIVATEKEY']
Expand Down Expand Up @@ -43,36 +42,94 @@ esPort = 22
esUser = process.env['EC2_ES_SSH_USER']

stageDBLoadAvg = ""
stageDBMemTotal = ""
stageDBMemFree = ""
stagePyLoadAvg = ""
stagePyMemTotal = ""
stagePyMemFree = ""
stageQLoadAvg = ""
stageQMemTotal = ""
stageQMemFree = ""
stageWorkersLoadAvg = ""
stageWorkersMemTotal = ""
stageWorkersMemFree = ""
prodDBLoadAvg = ""
prodDBMemTotal = ""
prodDBMemFree = ""
prodPyLoadAvg = ""
prodPyMemTotal = ""
prodPyMemFree = ""
prodQLoadAvg = ""
prodQMemTotal = ""
prodQMemFree = ""
prodWorkersLoadAvg = ""
prodWorkersMemTotal= ""
prodWorkersMemFree = ""
esLoadAvg = ""
esMemTotal = ""
esMemFree = ""

cpuCommand = "w | head -1 | awk '{print $12}'"
memoryTotalCommand = "egrep 'Mem' /proc/meminfo | awk '{print $2}' | head -1"
memoryFreeCommand = "egrep 'Mem' /proc/meminfo | awk '{print $2}' | tail -1"

#Mem Parser, parses the memory data. It could be more efficient to just SSH into the system again
#but that increases cost on our paid network side.....
memPercentage = (memoryTotal, memoryFree) ->
return 0 if memoryTotal < memoryFree
return 0 if memoryTotal == memoryFree
if memoryTotal > memoryFree
temp = memoryTotal - memoryFree
temp = temp/memoryTotal
temp = 1 - temp
return temp
#SSH Method
sshLogin = (sshHost, sshPort, sshUser, sshPrivateKey) ->
sshLogin = (sshHost, sshPort, sshUser, sshPrivateKey, value) ->
sshConnection = new ConnectServ()

sshConnection.on "connect", ->

#The exec code will give the load average of the past 15 minutes, and then will return the number
sshConnection.on "ready", ->
sshConnection.exec "w | head -1 | awk '{print $12}'", (err, stream) ->
sshConnection.on "ready", ->
if value is "CPU" then connection = cpuCommand
if value is "MemoryTotal" then connection = memoryTotalCommand
if value is "MemoryFree" then connection = memoryFreeCommand
console.log connection
console.log value
sshConnection.exec connection, (err, stream) ->
throw err if err
stream.on "data", (data, extended) ->
console.log ((if extended is "stderr" then "STDERR: " else "STDOUT ")) + data
stageDBLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageDBPort
stagePyLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stagePyPort
stageQLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageQPort
stageWorkersLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageWorkersPort
prodDBLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodDBPort
prodPyLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodPyPort
prodQLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodQPort
prodWorkersLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodWorkersPort
esLoadAvg = parseFloat(data.toString('ascii')) if sshPort is esPort
stageDBLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageDBPort and value is "CPU"
stagePyLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stagePyPort and value is "CPU"
stageQLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageQPort and value is "CPU"
stageWorkersLoadAvg = parseFloat(data.toString('ascii')) if sshPort is stageWorkersPort and value is "CPU"
prodDBLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodDBPort and value is "CPU"
prodPyLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodPyPort and value is "CPU"
prodQLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodQPort and value is "CPU"

prodWorkersLoadAvg = parseFloat(data.toString('ascii')) if sshPort is prodWorkersPort and value is "CPU"
esLoadAvg = parseFloat(data.toString('ascii')) if sshPort is esPort and value is "CPU"

stageDBMemTotal = parseFloat(data.toString('ascii')) if sshPort is stageDBPort and value is "MemoryTotal"
stagePyMemTotal = parseFloat(data.toString('ascii')) if sshPort is stagePyPort and value is "MemoryTotal"
stageQMemTotal = parseFloat(data.toString('ascii')) if sshPort is stageQPort and value is "MemoryTotal"
stageWorkersMemTotal = parseFloat(data.toString('ascii')) if sshPort is stageWorkersPort and value is "MemoryTotal"
prodDBMemTotal = parseFloat(data.toString('ascii')) if sshPort is prodDBPort and value is "MemoryTotal"
prodPyMemTotal = parseFloat(data.toString('ascii')) if sshPort is prodPyPort and value is "MemoryTotal"
prodQMemTotal = parseFloat(data.toString('ascii')) if sshPort is prodQPort and value is "MemoryTotal"
prodWorkersMemTotal = parseFloat(data.toString('ascii')) if sshPort is prodWorkersPort and value is "MemoryTotal"
esMemTotal = parseFloat(data.toString('ascii')) if sshPort is esPort and value is "MemoryTotal"

stageDBMemFree = parseFloat(data.toString('ascii')) if sshPort is stageDBPort and value is "MemoryFree"
stagePyMemFree = parseFloat(data.toString('ascii')) if sshPort is stagePyPort and value is "MemoryFree"
stageQMemFree = parseFloat(data.toString('ascii')) if sshPort is stageQPort and value is "MemoryFree"
stageWorkersMemFree = parseFloat(data.toString('ascii')) if sshPort is stageWorkersPort and value is "MemoryFree"
prodDBMemFree = parseFloat(data.toString('ascii')) if sshPort is prodDBPort and value is "MemoryFree"
prodPyMemFree = parseFloat(data.toString('ascii')) if sshPort is prodPyPort and value is "MemoryFree"
prodQMemFree = parseFloat(data.toString('ascii')) if sshPort is prodQPort and value is "MemoryFree"
prodWorkersMemFree = parseFloat(data.toString('ascii')) if sshPort is prodWorkersPort and value is "MemoryFree"
esMemFree = parseFloat(data.toString('ascii')) if sshPort is esPort and value is "MemoryFree"
stream.on "exit", (data, extended) ->
sshConnection.end()

Expand All @@ -93,19 +150,49 @@ sshLogin = (sshHost, sshPort, sshUser, sshPrivateKey) ->


datalogjob = new cronJob(
#Every 15 minutes.
cronTime: "0 */15 * * * *"
#every minute
cronTime: "0 * * * * *"
onTick: ->
#Grabs the loadaverage from each server by SSH into them
sshLogin stageHost, stageDBPort, stageDBUser, dotCloudpKey
sshLogin stageHost, stagePyPort, stagePyUser, dotCloudpKey
sshLogin stageHost, stageQPort, stageQUser, dotCloudpKey
sshLogin stageHost, stageWorkersPort, stageWorkersUser, dotCloudpKey
sshLogin prodHost, prodDBPort, prodDBUser, dotCloudpKey
sshLogin prodHost, prodPyPort, prodPyUser, dotCloudpKey
sshLogin prodHost, prodQPort, prodQUser, dotCloudpKey
sshLogin prodHost, prodWorkersPort, prodWorkersUser, dotCloudpKey
sshLogin esHost, esPort, esUser, ec2pKey
sshLogin stageHost, stageDBPort, stageDBUser, dotCloudpKey, "CPU"
console.log "CPU stageDB"
sshLogin stageHost, stagePyPort, stagePyUser, dotCloudpKey, "CPU"
console.log "CPU stagePY"
sshLogin stageHost, stageQPort, stageQUser, dotCloudpKey, "CPU"
console.log "CPU StageQ"
sshLogin stageHost, stageWorkersPort, stageWorkersUser, dotCloudpKey, "CPU"
sshLogin prodHost, prodDBPort, prodDBUser, dotCloudpKey, "CPU"
sshLogin prodHost, prodPyPort, prodPyUser, dotCloudpKey, "CPU"
sshLogin prodHost, prodQPort, prodQUser, dotCloudpKey, "CPU"
sshLogin prodHost, prodWorkersPort, prodWorkersUser, dotCloudpKey, "CPU"
sshLogin esHost, esPort, esUser, ec2pKey, "CPU"
sshLogin stageHost, stageDBPort, stageDBUser, dotCloudpKey, "MemoryTotal"
sshLogin stageHost, stagePyPort, stagePyUser, dotCloudpKey, "MemoryTotal"
sshLogin stageHost, stageQPort, stageQUser, dotCloudpKey, "MemoryTotal"
sshLogin stageHost, stageWorkersPort, stageWorkersUser, dotCloudpKey, "MemoryTotal"
sshLogin prodHost, prodDBPort, prodDBUser, dotCloudpKey, "MemoryTotal"
sshLogin prodHost, prodPyPort, prodPyUser, dotCloudpKey, "MemoryTotal"
sshLogin prodHost, prodQPort, prodQUser, dotCloudpKey, "MemoryTotal"
sshLogin prodHost, prodWorkersPort, prodWorkersUser, dotCloudpKey, "MemoryTotal"
sshLogin esHost, esPort, esUser, ec2pKey, "MemoryTotal"
sshLogin stageHost, stageDBPort, stageDBUser, dotCloudpKey, "MemoryFree"
sshLogin stageHost, stagePyPort, stagePyUser, dotCloudpKey, "MemoryFree"
sshLogin stageHost, stageQPort, stageQUser, dotCloudpKey, "MemoryFree"
sshLogin stageHost, stageWorkersPort, stageWorkersUser, dotCloudpKey, "MemoryFree"
sshLogin prodHost, prodDBPort, prodDBUser, dotCloudpKey, "MemoryFree"
sshLogin prodHost, prodPyPort, prodPyUser, dotCloudpKey, "MemoryFree"
sshLogin prodHost, prodQPort, prodQUser, dotCloudpKey, "MemoryFree"
sshLogin esHost, esPort, esUser, ec2pKey, "MemoryFree"

stageDBMem = memPercentage stageDBMemTotal, stageDBMemFree
stagePyMem = memPercentage stagePyMemTotal, stagePyMemFree
stageQMem = memPercentage stageQMemTotal, stageQMemFree
stageWorkersMem = memPercentage stageWorkersMemTotal, stageWorkersMemFree
prodDBMem = memPercentage prodDBMemTotal, prodDBMemFree
prodPyMem = memPercentage prodPyMemTotal, prodPyMemFree
prodQMem = memPercentage prodQMemTotal, prodQMemFree
prodWorkersMem = memPercentage prodWorkersMemTotal, prodWorkersMemFree
esMem = memPercentage esMemTotal, esMemFree

currentTime = Date.now().toString()
#Insert into database
Expand All @@ -114,16 +201,24 @@ datalogjob = new cronJob(
#Don't return as the database has most likely been created previously...
console.log "error occured due to status code ", error.message
db.insert
stagedb: stageDBLoadAvg
stagepy: stagePyLoadAvg
stageq: stageQLoadAvg
stageworkers: stageWorkersLoadAvg
proddb: prodDBLoadAvg
prodpy: prodPyLoadAvg
prodq: prodQLoadAvg
prodworkers: prodWorkersLoadAvg
es: esLoadAvg
date: currentTime
stagedbloadavg: stageDBLoadAvg
stagepyloadavg: stagePyLoadAvg
stageqloadavg: stageQLoadAvg
stageworkersloadavg: stageWorkersLoadAvg
proddbloadavg: prodDBLoadAvg
prodpyloadavg: prodPyLoadAvg
prodqloadavg: prodQLoadAvg
prodworkersloadavg: prodWorkersLoadAvg
esloadavg: esLoadAvg
stagedbmem: stageDBMem
stagepymem: stagePyMem
stageqmem: stageQMem
stageworkersmem: stageWorkersMem
proddbmem: prodDBMem
prodpymem: prodPyMem
prodqmem: prodQMem
prodworkersmem: prodWorkersMem
esmem: esMem
, currentTime, (error2, body, header) ->
if error2
console.log "error occured due to status code ", error2.message
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"nano": "3.x",
"miso.dataset" : "*",
"cron": "*",
"time": "*"

"time": "*",
"async": "*",
"underscore": "*"
},

"devDependencies" : {
Expand Down