Adapting check_mem.pl to measure memory usage on FreeBSD

Written by - 0 comments

Published on - Listed in Nagios BSD Perl Monitoring


For a new big monitoring project I was looking for a Nagios script or plugin which measures the memory usage and is able to run on different systems. I came across the "new and improved check_mem.pl", which can be used on several different operating systems. As I started to test the script on FreeBSD systems I got very strange values though:

 ./check_mem.pl -f -w 10 -c 5
CRITICAL - 3.2% (211 kB) free!|TOTAL=6608KB;;;; USED=6397KB;;;; FREE=211KB;;;; CACHES=0KB;;;;

The script claims that only 3.2% of memory is left free on this system. There's only a mail server running on this machine... Besides that the total is shown as 6608KB but the server has 3GB RAM installed.

I took a closer look at the perl code and while there are checks for Linux and SunOS (Solaris) uname outputs, there is no further check for BSD systems. In this case the perl script uses vmstat to calculate its free space. Unfortunately that doesn't help a lot as the information is more or less useless:

vmstat
 procs      memory      page                    disks     faults         cpu
 r b w     avm    fre   flt  re  pi  po    fr  sr da0 da1   in   sy   cs us sy id
 0 2 0   6357M   218M    33   0   0   1    11   3   0   0   16   45   20 13 38 49

More helpful information comes from top:

top
last pid:  5446;  load averages:  0.00,  0.02,  0.00
103 processes: 1 running, 101 sleeping, 1 zombie
CPU:     % user,     % nice,     % system,     % interrupt,     % idle
Mem: 2070M Active, 488M Inact, 235M Wired, 20M Cache, 112M Buf, 191M Free
Swap: 4071M Total, 262M Used, 3809M Free, 6% Inuse

So instead of just cussing around that the plugin doesn't give correct information on FreeBSD systems, I added a FreeBSD section. The important information is how to correctly calculate the memory usage on BSD systems. Believe me, that is no easy task. As a Linux admin, I'm used to the free command but BSD doesn't give you such a tool. After some reasearch I came across this post (https://support.zabbix.com/browse/ZBXNEXT-1024).  According to this text, the memory is calculated through the following sysctl values:

"vm.stats.vm.v_inactive_count" * "vm.stats.vm.v_page_size" (lets call it INACT)
"vm.stats.vm.v_cache_count" * "vm.stats.vm.v_page_size" (CACHE)
"vm.stats.vm.v_free_count" * "vm.stats.vm.v_page_size" (FREE)

free_mem = INACT + CACHE + FREE

By using the formula seen above, I adapted the plugin and added a new FreeBSD section:

151     }
152     elsif ( $uname =~ /FreeBSD/ ) {
153       # The FreeBSD case. 2013-03-19 www.claudiokuenzler.com
154       # free mem = Inactive*Page Size + Cache*Page Size + Free*Page Size
155       my $pagesize = `sysctl vm.stats.vm.v_page_size`;
156       $pagesize =~ s/[^0-9]//g;
157       my $mem_inactive = 0;
158       my $mem_cache = 0;
159       my $mem_free = 0;
160       my $mem_total = 0;
161       my $free_memory = 0;
162         my @meminfo = `/sbin/sysctl vm.stats.vm`;
163         foreach (@meminfo) {
164             chomp;
165             if (/^vm.stats.vm.v_inactive_count:\s+(\d+)/) {
166             $mem_inactive = ($1 * $pagesize);
167             }
168             elsif (/^vm.stats.vm.v_cache_count:\s+(\d+)/) {
169             $mem_cache = ($1 * $pagesize);
170             }
171             elsif (/^vm.stats.vm.v_free_count:\s+(\d+)/) {
172             $mem_free = ($1 * $pagesize);
173             }
174             elsif (/^vm.stats.vm.v_page_count:\s+(\d+)/) {
175             $mem_total = ($1 * $pagesize);
176             }
177         }   
178         $free_memory = $mem_inactive + $mem_cache + $mem_free;
179         $free_memory_kb = ( $free_memory / 1024);
180         $total_memory_kb = ( $mem_total / 1024);
181         $used_memory_kb = $total_memory_kb - $free_memory_kb;
182         $caches_kb = ($mem_cache / 1024);
183     }
184     elsif ( $uname =~ /SunOS/ ) {

The output of the plugin now looks more correct:

./check_mem.new.pl -f -w 10 -c 5
OK - 23.6% (724924 kB) free.|TOTAL=3077200KB;;;; USED=2352276KB;;;; FREE=724924KB;;;; CACHES=20604KB;;;;

I'll contact the plugin developer or maintainer and hopefully my contribution will be added in the original script. Until then, the modified perl plugin can be downloaded here.

Update March 20th 2013: The official repository of check_mem.pl has merged my modifications: https://github.com/justintime/nagios-plugins/tree/master/check_mem


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder