Tuesday, April 28, 2020

Cron job - Daemon - Shell script to send email alert to admins when RAM utilization hits critical levels

In this example I am creating a daemon to run a cron job. Cron jobs are scheduled tasks that can be setup to run in an automated fashion on particular, days, weeks or even yearly quarters. And daemons are silent processes that are running in the background constantly in an alert state ready to spring into action.

First, I checked to see how much ram is free on this server at any given time. The server is an Ubuntu LAMP stack on a cloud. In this particular instance 265MB of ram are free.


This command returns the exact value for comparisons and is what I will plug into my program later on:


So now that I know my parameters for where I want my RAM to be running and when I want to be emailed about any alarming spikes in usage. The alerts.sh program I created below sends emails to tester@slabj.com whenever the free ram is less than or equal to 267MB. I set the threshold really low to test my script.

Cool. It's working now and I got an email telling me that the RAM is low. That was expected since I set normal usage as low to check my script.


And here is the email in a little more detail. You can see that I am having "root" email me whenever the RAM free size is "LOW" which is an arbitrary parameter that can be set however I choose.


Here I'm having the script echo back what it is doing to the console. This is for illustrative purposes as daemons are not to be seen nor heard from unless they are taking action.


With the addition of a while loop, this now becomes infinite. The process will start running now and won't stop unless I tell it to.

To make it more clear here I am echoing responses for either state the machine is in.



As you can see, with the Daemon running constantly the alerts are constant. This is not necessarily ideal. I could easily just exclude the echos and have my daemon run all the time without the average user noticing. However, once there are many daemons I don't want them all running in the background eating up memory space or using up CPU power.


Fortunately Linux has a cool tool just for this particular situation. Cron jobs are scheduled tasks that run at assigned dates and times. In this instance I scheduled a Cron job to check the RAM usage every Tuesday at 9:20am. Ideally you'd want to check at different intervals but since I initially created this on Tuesday a little after 9am I decided it would be wise to set the first Cron job to run in the next few minutes to test it out. 

And that is it in a nutshell. I now have a program that is automated and chronologically scheduled to run at specific intervals to check system RAM utilization. Additionally, the system sends email alerts to the appropriate individuals if any administration intervention is needed to keep everything running smoothly.

No comments:

Post a Comment

Automated Exploitation of a Bluetooth vulnerability that leads to 0-click code execution

This blog post covers an interesting vulnerability that was just discovered earlier this year and an open source free tool that was created ...