Node.js receive notification for high memory usage

I have a Node.js application running on Ubuntu 14 on Amazon EC2.

I want to send an email in case the memory usage reaches a certain size.

I know that PM2 provides an API that allows, among other things, to restart the application when a certain amount of memory is reached. Now I don’t want to restart the application at the moment, but just to get a notification about it and do whatever I want with it (in my case, by sending an email).

How can I do this with either PM2 or any other free tool?

+5
source share
4 answers

In order to have a specific implementation mentioned by mother.

You can easily do this in node by periodically checking your memory usage: https://nodejs.org/api/process.html#process_process_memoryusage . If it is a long-term server, check it every hour or so. Then simply release the email with a package like nodemailer if the memory reaches a certain threshold.

I like PM2, but I was wondering if this could be done without it.

As indicated here , RSS (Resident Set Size) is the full use of memory in this process, including the use of memory in shared memory. Thus, RSS can be considered inappropriate for use in the memory of one process, since shared memory is also used by other processes. Therefore, there is PSS that divides shared memory into all other used processes.

I assume that we want to know the value of PSS if we want to show the most accurate memory usage by the node process. However, some people mention that PSS is more important for a huge number of fork processes such as the Apache server (and therefore a lot of shared memory).

var hour = 3600*1000; var checkMemory = function() { // Retrieves the memory usage var memory = process.memoryUsage(); var rss_memory_in_bytes = m.rss; var rss_memory_in_megabytes = m.rss / (1024**2); if (rss_memory_in_megabytes > 50) { // More than 50 megabytes RSS usage, do something doSomething(); } } var doSomething = function() { // For instance sending an email } // Check the memory usage every hour setInterval(checkMemory, hour); 

- UPDATED -

If more memory is needed for the heap, the node process will try to allocate that memory. This distribution is done automatically. When you can save the heap and rss storage. Therefore, heapUsage and heapTotal can be neglected in our case.

There are ways to set a memory limit, but we are interested in checking the limit. I think it's wise to check the remaining amount of free system memory. However, this has nothing to do with the actual memory usage of the node process itself and will require a different threat to check for free system memory using a node script.

+2
source

The answer is to use AWS CloudWatch alarms. They are free level compliant and have a nice toolbar. Detailed setup is described internally by this documentation guide , but I suggest you follow my steps to make sure this works for you.

The first thing you need to do is Launch a new instance of Ubuntu EC2, which can be written to CloudWatch . This is due to the new IAM role with permissions. (you cannot attach a new role to an existing instance - see second Note: here .). You no longer need to run an EC2 instance to change IAM roles. See announcement .

The next step you perform is: Install authoring perl AWS scripts that allow you to write to CloudWatch. Add a new cron to record to CloudWatch every five minutes.

Finally, create a new alarm in the CloudWatch console to send you an email when memory usage exceeds a certain threshold.


Below are the steps for each of the phases listed above:

Install authoring perl scripts for AWS

  • SSH into your new instance and run the following commands:

$ sudo apt-get update

$ sudo apt-get install unzip

$ sudo apt-get install libwww-perl libdatetime-perl

curl http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O

unzip CloudWatchMonitoringScripts-1.2.1.zip

rm CloudWatchMonitoringScripts-1.2.1.zip

cd aws-scripts-mon

  1. Verify that your perl scripts were installed correctly using the following command

./mon-put-instance-data.pl --mem-util --verify --verbose

  1. Add to cron, a perl script command that puts the amount of memory used by the ubuntu instance in CloudWatch

crontab -e

*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --from-cron

  1. Wait about 20 minutes for statistics to be added to CloudWatch.

Create a new alarm in the CloudWatch console

  • In the AWS console, select CoudWatch and click on the Blue, Performance Overview button.
  • On the right side of the screen, you should see the All indicators tab at the bottom of the screen and a link to the Linux system. Click "Linux System" and then "Instance" and you will see the MemoryUtilization label.
  • Click MemoryUtilization , and then go to the Grapted Metrics tab at the bottom.
  • On the right here you will see the Alarm icon. enter image description here
  • Click this icon to create an alarm. Set a threshold for emailing you if your memory usage is greater than 40.
  • Add stress to the instance and you will see that the email has arrived. I used the stress command found in this answer and it worked. Type stress and ubuntu will show you how to set stress. Below is a screenshot of the memory usage in CloudWatch that I created for this entry. I get an email every time memory usage crosses 40 percent.

enter image description here

Hope this helps.

+6
source

You can use a simple bash script that uses the memory of your instance and clicks that instance on CloudWatch using the "custom metrics" feature. You can then create alarms in Cloudwatch and send SNS via email. (You must create a cron job to run, for example, every 10 minutes).

aws cloudwatch put-metric-data - metric name memusage --namespace mem - value 20 --timestamp 2016-10-14T12: 00: 00.000Z

+2
source

You can easily do this in node by periodically checking your memory usage: https://nodejs.org/api/process.html#process_process_memoryusage . If it is a long-term server, check it every hour or so. Then simply release the email with a package like nodemailer if the memory reaches a certain threshold.

+1
source

Source: https://habr.com/ru/post/1260270/


All Articles