Friday, 10 November 2017

Node.js process monitoring

In previous blog post, we have seen how we can write custom exporters for process monitoring and get metrics in prometheus.Suppose, I want to monitor node.js application with prometheus. we can write our own shell script which will monitor my node.js process and send metrics to prometheus.

Wait… Before directly jump to script I should consider options available for monitoring node.js process. PM2 is great process manager for node.js. Installing PM2 is very easy.

# npm install pm2 -g

With PM2 you can start/stop node.js process and  monitor CPU,Memory of process.You don’t need to worry about what if my node process is crashed due to any runtime issue. PM2 will restart it and maintain restart count of process.PM2 provide many metrics about process like uptime,restart count ,CPU,Memory , start time , log path etc.

So, what we can do is we can format pm2 metrics in the prometheus acceptable format .I will be easy for us to monitor node.js along with all other process and have one common dashboard for all process in your application. Also we can set alerts in prometheus alert manager when CPU / Memory utilization of node.js process exceeds peak value.

Here is sample script for scraping metrics data from nodejs process using pm2 .

pm2_exporter.sh
#!/bin/bash
#Author Priyanka Dive,Pravin Magdum
#Email priyanka.dive@crevise.com,pravin.magdum@crevise.com
echo "Script to send pm2 status to Prometheus"
while true
do
pm2 list | grep "app.keymetrics.io"
if [ "$?" == 0 ]
then
linecount=5;
else
linecount=4;
fi
echo $linecount
pm2 list|head -n-2|tail -n+$linecount|awk  '{ print $2"_restart_count" "\t" $12 "\n" $2"_CPU_Usage" "\t" substr($16,1,  length($16)-1) "\n" $ 2"_MEMORY_Usage" "\t"
}'|sed 's/-/_/g' > Metrics/pm2_Merics.prom
sleep 10s             
done


Running script:
Create Metrics directory where we will store .prom files.
#mkdir Metrics

Run script as follows
#<Path-to-script>/pm2_exporter.sh > /<Path-to-script>/pm2_exporter.log

Script will collect metrics in Metrics directory in the form of .prom files.We need to restart our node_exporter and pass path of Metrics directory to it as textfile collector.

Restart node Exporter:
./node_exporter --collector.textfile.directory=Metrics

Now, node exporter will scrape all metrics present in Metrics directory which includes our node.js process CPU,Memory etc. You can set alerts for this metrics and form dashboard in grafana which will display all information about your node.js process on screen.

Stay tuned will see Nginx monitoring with prometheus in next post..!!

3 comments:

Amazon EKS - Kubernetes on AWS

By Komal Devgaonkar Amazon Elastic Container Service for Kubernetes (Amazon EKS), which is highly available and scalable AWS service....