This bash script is used for Freemius' servers monitoring. It monitors a Linux server CPU(s), RAM, and Disk(s) usage. When a resource reaches a specified threshold, it will automatically send an incident alert to the desired email address. Once the resource consumption is reduced back under the threshold, the incident will be closed, and an additional alert will be sent to the same address, letting the sys-admin knowing that the incident has ended.
The script supports unlimited levels of severity limits. For example, you can have a warning severity limit as well as critical alerts.
This bash script was created for CentOS 6.X. It should also be compatible with RHEL and Amazon Linux. And thanks to @gnanet, it should work with Debian based systems and Ubuntu.
We've been using NewRelic for years and love the product. Recently, they made changes to their plans and the cheapest plan that supports email alerts, which was one of the main features we've been using, starts at $300 per month. Paying $3,600 per year for email alerts doesn't make sense for us, so we decided to build this script for our internal use. Later, we decided to contribute it to the open-source community - saving others these several days of development. Enjoy!
bash server-monitoring.sh [options]
| Argument | Description | |
|---|---|---|
--debug, -d |
If set to true will echo the progress. |
Optional |
--info, -i |
If set to true will output the server's CPU, RAM, and disks consumption. |
Optional |
--hostname,-h |
The hostname that will be used in the email alerts. | Optional |
--from,-f |
The email address incident alerts will be sent from. | Required |
--to, -t |
The email address incident alerts will be sent to. | Required |
--cpu, -c |
The avg. CPU consumption incident severity limits. | Required |
--memory, -m |
The RAM consumption incident severity limits. | Required |
--disk, -d |
The disk(s) consumption incident severity limits. | Required |
bash server-monitoring.sh --debug=true --hostname=myAwesomeServer [email protected] [email protected] --cpu=warning=20:critical=50 --memory=warning=30:critical=60 --disk=warning=40:critical=60:fatal=70
-
Select an email address that will be the source for the server incident alerts. Something like
[email protected]. -
The script is using
sendmail's most basic functionality. 99% of those emails will go directly to your spam. Thus, whitelist all messages from the selected address. -
Copy the script to your server. Since the script is going to generate files for open incidents, we recommend to create a new folder named
server-monitoringand to copy the script into that folder. -
To choose your resource consumption limits, first, execute the script in an
infomode to learn your current server's resource consumption:bash server-monitoring.sh --info=trueThe output of the script will look like this:
CPU: 14 MEMORY: 26 DISK: /dev/xvda 34 /dev/xvda1 12The numbers represent the current resource consumption in percentages. For example, 34% of the disk
/dev/xvdais used.If you're getting a
Command Not Founderror(s), try executingdos2unix server-monitoring.shto convert the line endings to a Unix format. -
After you know your resources normal state usage, assign the limits that you'd like to be alerted about.
bash server-monitoring.sh --debug=true [email protected] [email protected] --cpu=warning=20:critical=50 --memory=warning=30:critical=60 --disk=warning=40:critical=60:fatal=70
- When the server's avg. CPU(s) consumption will increase above 20%; a
warningCPU incident will be registered. If the CPU consumption increases beyond 50%, acriticalCPU incident will be registered. - When the server's memory/RAM consumption increases above 30%, a
warningmemory incident will be registered. If the RAM consumption increases beyond 60%, acriticalmemory incident will be registered. - When any mounted disk usage increases above 40%, a
warningdisk incident will be registered. If disk usage increases above the 60% threshold, acriticaldisk incident is registered. Finally, when more than 70% of a disk is utilized, it will trigger afatalincident.
To set an ongoing monitoring:
-
Open the crontab file:
crontab -e -
Add the following line to the end of the file:
*/2 * * * * bash /server-monitoring/server-monitoring.sh [email protected] [email protected] --cpu=warning=20:critical=50 --memory=warning=30:critical=60 --disk=warning=40:critical=60:fatal=70 -
Type
:xto save and exit.