How to set up the custom memory cache monitor on Nagios and clear the memory cache with cronjob?
Introduction:
Monitoring memory cache utilization is crucial for maintaining optimal system performance and resource utilization in your IT infrastructure. Nagios, a powerful monitoring system, allows you to track various metrics, including memory cache usage.
Prerequisite:
Server root login credentials.
Step 1:
Log in to the Ubuntu or Centos server as a user with sudo privileges. we can use SSH or directly access the server.
ssh root@Ip |
Step 2:
Edit the /usr/local/nagios/etc/nrpe.cfg configuration file and search for command definition sections. Here you can define or update check commands.
$ vi /usr/local/nagios/etc/nrpe.cfg |
Please update the below command for the memory cache check.
$ command[check_memory_cache]=/usr/local/nagios/libexec/check_memory_cache |
Step 3:
Go to the libexec path and create a new script file in the specified path, such as check_memory_cache.
vi /usr/local/nagios/libexec/check_memory_cache |
Please copy the below commands, and paste the check_memory_cache file, and save the file.
!/bin/bash #Get the memory cache size in kilobytes memory_cache=$(free -k | awk ‘/^Mem:/ {print $6}’) #Convert the threshold to kilobytes (2GB = 2097152 KB) threshold_kb=2097152 if [ -z “$memory_cache” ]; then echo “UNKNOWN – Unable to retrieve memory cache information” exit 3 # Nagios UNKNOWN status elif [ “$memory_cache” -gt “$threshold_kb” ]; then # Clear memory cache by syncing file system buffers to disk sync; echo 1 > /proc/sys/vm/drop_caches echo "CRITICAL - Memory cache is above 2GB. Cached: $memory_cache KB and Cleared the Memory cache" else echo “OK – Memory cache is within limits. Cached: $memory_cache KB” exit 0 # Nagios OK status fi |
Step 4:
Give permission to the check_memory_cache file as below
$ chmod 755 check_memory_cache |
Step 5:
Next, restart the NRPE service. Now it is ready to listen to requests from the Nagios Server.
$ systemctl restart nagios-nrpe-server |
Step 6:
Login to Nagios server
Step 7:
Open the below for the configuration path.
$ vi /usr/local/nagios/etc/servers/server.cfg |
Add the below lines for the Memory cache checking the configuration and save it.
define service { use generic-service ; Name of service template to use host_name testserver2 service_description check_memory_cache check_command check_nrpe!check_memory_cache contact_groups admins notifications_enabled 1 } |
Step 8:
Save the configuration file.
And finally, restart Nagios to apply the recent configuration changes:
$ systemctl restart nagios |
Step 9:
To clear the memory cache with cronjob.
- Log in to the target server and set up a cron job to clear the memory cache.
- Edit the cron timing with the below command.
crontab -e |
3. Add the cron time every five minutes
*/5 * * * * /usr/local/nagios/libexec/check_memory_cache |
Conclusion:
Setting up a custom memory cache monitor on Nagios is essential for effectively monitoring and managing memory resources in your IT infrastructure. By proactively monitoring cache memory usage, administrators can identify potential performance bottlenecks or resource constraints before they impact system performance or availability