From fd31e834c1a5f3db7f905a89fd71efca1463225e Mon Sep 17 00:00:00 2001 From: Xavier Beaudouin Date: Mon, 24 Jun 2024 16:38:07 +0200 Subject: [PATCH 1/3] Add sysctl_mem option for FreeBSD --- agents/check_mk_agent.freebsd | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/agents/check_mk_agent.freebsd b/agents/check_mk_agent.freebsd index 6e2f6506070..c382b084ce5 100755 --- a/agents/check_mk_agent.freebsd +++ b/agents/check_mk_agent.freebsd @@ -597,6 +597,21 @@ run_purely_synchronous_sections() { rm -f "${tmpfile}" fi + + echo '<<>>' + _page_size=$( sysctl -n vm.stats.vm.v_page_size ) + echo "mem.cache $(( _page_size * $( sysctl -n vm.stats.vm.v_cache_count ) ))" + echo "mem.free $(( _page_size * $( sysctl -n vm.stats.vm.v_free_count ) ))" + echo "mem.total $( sysctl -n hw.physmem )" + echo "mem.used $(( _page_size * ( $( sysctl -n vm.stats.vm.v_active_count ) + $( sysctl -n vm.stats.vm.v_wire_count ) ) ))" + _swap_total=$( sysctl -n vm.swap_total ) + _swap_used=$(( $( swapinfo | tail -1 | awk '{print $3}' ) * 1024 )) + echo "swap.free $(( _swap_total - _swap_used ))" + echo "swap.total ${_swap_total}" + echo "swap.used ${_swap_used}" + unset -v _page_size + unset -v _swap_total + unset -v _swap_used } # From 9d93dfa9a5ba4bf3b6a77e6bd882bf592d8ea3f5 Mon Sep 17 00:00:00 2001 From: Xavier Beaudouin Date: Mon, 30 Sep 2024 12:13:57 +0200 Subject: [PATCH 2/3] Moved <> as a function as requested. --- agents/check_mk_agent.freebsd | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/agents/check_mk_agent.freebsd b/agents/check_mk_agent.freebsd index c382b084ce5..20743608390 100755 --- a/agents/check_mk_agent.freebsd +++ b/agents/check_mk_agent.freebsd @@ -364,6 +364,24 @@ section_fileinfo() { ' -- "${MK_CONFDIR}" } +section_sysctlmem() { + # Output FreeBSD memory info from sysctl + echo '<<>>' + _page_size=$( sysctl -n vm.stats.vm.v_page_size ) + echo "mem.cache $(( _page_size * $( sysctl -n vm.stats.vm.v_cache_count ) ))" + echo "mem.free $(( _page_size * $( sysctl -n vm.stats.vm.v_free_count ) ))" + echo "mem.total $( sysctl -n vm.kmem_size )" + echo "mem.used $(( _page_size * ( $( sysctl -n vm.stats.vm.v_active_count ) + $( sysctl -n vm.stats.vm.v_wire_count ) ) ))" + _swap_total=$( sysctl -n vm.swap_total ) + _swap_used=$(( $( swapinfo | tail -1 | awk '{print $3}' ) * 1024 )) + echo "swap.free $(( _swap_total - _swap_used ))" + echo "swap.total ${_swap_total}" + echo "swap.used ${_swap_used}" + unset -v _page_size + unset -v _swap_total + unset -v _swap_used +} + # # END COMMON AGENT CODE # @@ -376,6 +394,8 @@ run_purely_synchronous_sections() { section_checkmk_agent_plugins + section_sysctlmem + osver="$(uname -r)" is_jailed="$(sysctl -n security.jail.jailed)" @@ -597,21 +617,6 @@ run_purely_synchronous_sections() { rm -f "${tmpfile}" fi - - echo '<<>>' - _page_size=$( sysctl -n vm.stats.vm.v_page_size ) - echo "mem.cache $(( _page_size * $( sysctl -n vm.stats.vm.v_cache_count ) ))" - echo "mem.free $(( _page_size * $( sysctl -n vm.stats.vm.v_free_count ) ))" - echo "mem.total $( sysctl -n hw.physmem )" - echo "mem.used $(( _page_size * ( $( sysctl -n vm.stats.vm.v_active_count ) + $( sysctl -n vm.stats.vm.v_wire_count ) ) ))" - _swap_total=$( sysctl -n vm.swap_total ) - _swap_used=$(( $( swapinfo | tail -1 | awk '{print $3}' ) * 1024 )) - echo "swap.free $(( _swap_total - _swap_used ))" - echo "swap.total ${_swap_total}" - echo "swap.used ${_swap_used}" - unset -v _page_size - unset -v _swap_total - unset -v _swap_used } # From 0377116c083cb1800c9f6d5844a689e79c8d28f8 Mon Sep 17 00:00:00 2001 From: Xavier Beaudouin Date: Mon, 30 Sep 2024 12:28:17 +0200 Subject: [PATCH 3/3] Added parse function for sysctl_mem on FreeBSD --- .../agent_based/mem_used_sections.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/cmk/plugins/collection/agent_based/mem_used_sections.py b/cmk/plugins/collection/agent_based/mem_used_sections.py index d6d1be32209..233cd820150 100644 --- a/cmk/plugins/collection/agent_based/mem_used_sections.py +++ b/cmk/plugins/collection/agent_based/mem_used_sections.py @@ -308,3 +308,61 @@ def parse_openbsd_mem(string_table: StringTable) -> SectionMemUsed | None: parsed_section_name="mem_used", parse_function=parse_openbsd_mem, ) + + +def parse_freebsd_mem(string_table: StringTable) -> SectionMemUsed | None: + """ + >>> import pprint + >>> pprint.pprint(parse_statgrab_mem([ + ... ['mem.cache', '0'], + ... ['mem.free', '677666816'], + ... ['mem.total', '4294967296'], + ... ['mem.used', '3617300480'], + ... ['swap.free', '4976402432'], + ... ['swap.total', '8589934592'], + ... ['swap.used', '3613532160'] + ... ])) + {'Cached': 0, + 'MemFree': 677666816, + 'MemUsed': 3617300480, + 'MemTotal': 4294967296, + 'SwapFree': 4976402432, + 'SwapUsed': 3613532160, + 'SwapTotal': 8589934592} + + """ + parsed: dict[str, int] = {} + for var, value in string_table: + try: + parsed.setdefault(var, int(value)) + except ValueError: + pass + + try: + totalmem = parsed["mem.total"] + memused = parsed["mem.used"] + memfree = parsed["mem.free"] + totalswap = parsed["swap.total"] + swapused = parsed["swap.used"] + swapfree = parsed["swap.free"] + except KeyError: + return None + + section: SectionMemUsed = { + "MemTotal": totalmem, + "MemUsed": memused, + "MemFree": memfree, + "SwapTotal": totalswap, + "SwapUsed": swapused + "SwapFree": swapfree, + } + if "mem.cache" in parsed: + section["Cached"] = parsed["mem.cache"] + + +agent_section_freebsd_mem = AgentSection( + name="sysctl_mem", + parsed_section_name="mem_used", + parse_function=parse_freebsd_mem, + supersedes=["statgrab_mem","ucd_mem"], +)