1#!@@GOODSH@@
2#
3# Plugin to monitor memory usage on FreeBSD.
4#
5# Parameters:
6#
7#       config   (required)
8#       autoconf (optional - only used by munin-config)
9#
10# Magic markers (optional - only used by munin-config and some
11# installation scripts):
12#%# family=auto
13#%# capabilities=autoconf
14
15if [ "$1" = "autoconf" ]; then
16    if [ -x /sbin/sysctl ]; then
17        if /sbin/sysctl vm.stats.vm.v_page_size > /dev/null; then
18            echo yes
19            exit 0
20        else
21            echo no
22            exit 0
23        fi
24    else
25        echo no
26        exit 0
27    fi
28fi
29
30PAGESIZE=$(/sbin/sysctl -n vm.stats.vm.v_page_size)
31MEMSIZE=$(/sbin/sysctl -n vm.stats.vm.v_page_count)
32MEMMAX=$(($PAGESIZE*$MEMSIZE))
33CACHE_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_cache_count)
34LAUNDRY_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_laundry_count)
35
36if [ "$1" = "config" ]; then
37    echo 'graph_args --base 1024 -l 0 --vertical-label Bytes --upper-limit' $MEMMAX
38    echo 'graph_title Memory usage'
39    echo 'graph_category system'
40    echo 'graph_info This graph shows what the machine uses its memory for.'
41    # assemble the graph order (including the optional items)
42    printf 'graph_order active inactive wired buffers'
43    if [ -n "$CACHE_COUNT" ]; then printf ' cached'; fi
44    if [ -n "$LAUNDRY_COUNT" ]; then printf ' laundry'; fi
45    echo ' free swap'
46    echo 'active.label active'
47    echo 'active.info pages recently statistically used'
48    echo 'active.draw AREA'
49    echo 'inactive.label inactive'
50    echo 'inactive.info pages recently statistically unused'
51    echo 'inactive.draw STACK'
52    echo 'wired.label wired'
53    echo 'wired.info pages that are fixed into memory, usually for kernel purposes, but also sometimes for special use in processes'
54    echo 'wired.draw STACK'
55    echo 'buffers.label buffers'
56    echo 'buffers.info pages used for filesystem buffers'
57    echo 'buffers.draw STACK'
58    # the "cache" counter was removed in 2015
59    if [ -n "$CACHE_COUNT" ]; then
60        echo 'cached.label cache'
61        echo 'cached.info pages that have percolated from inactive to a status where they maintain their data, but can often be immediately reused'
62        echo 'cached.draw STACK'
63    fi
64    # the "laundry" counter was introduced in 2015
65    if [ -n "$LAUNDRY_COUNT" ]; then
66        echo 'laundry.label laundry'
67        echo 'laundry.info number of dirty bytes inactive'
68        echo 'laundry.draw STACK'
69    fi
70    echo 'free.label free'
71    echo 'free.info pages without data content'
72    echo 'free.draw STACK'
73    echo 'swap.label swap'
74    echo 'swap.info Swap space used'
75    echo 'swap.draw STACK'
76    exit 0
77fi
78
79ACTIVE_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_active_count)
80INACTIVE_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_inactive_count)
81WIRED_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_wire_count)
82BUFFERS_COUNT=$(/sbin/sysctl -n vfs.bufspace)
83FREE_COUNT=$(/sbin/sysctl -n vm.stats.vm.v_free_count)
84
85echo active.value $(($ACTIVE_COUNT*$PAGESIZE))
86echo inactive.value $(($INACTIVE_COUNT*$PAGESIZE))
87echo wired.value $(($WIRED_COUNT*$PAGESIZE))
88echo buffers.value $(($BUFFERS_COUNT))
89if [ -n "$CACHE_COUNT" ]; then
90    echo cached.value $(($CACHE_COUNT*$PAGESIZE))
91fi
92if [ -n "$LAUNDRY_COUNT" ]; then
93    echo "laundry.value $(( LAUNDRY_COUNT * PAGESIZE ))"
94fi
95echo free.value $(($FREE_COUNT*$PAGESIZE))
96echo swap.value $(swapinfo -k | awk '!/^Total/ && !/^Device/ {sum += $3}; END {print sum * 1024}')
97