Netgate SG-1000 microFirewall

Author Topic: pfSense 2.3 Check_mk working with xinetd  (Read 4949 times)

0 Members and 1 Guest are viewing this topic.

Offline joeclifford

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
    • View Profile
pfSense 2.3 Check_mk working with xinetd
« on: May 09, 2016, 06:08:47 am »
As my company uses check_mk for monitoring  pfsense, it is frustrating that the package for ckeck_mk has been removed. To be honest, the old package didn't work without manual modification, so it is understandable that is was removed.

I've managed to get check_mk working with the pfsense 2.3 implementation of xinetd, which survives reboots, and allows config changed without rebooting the firewall..

As this is a manual installation, you are advised that following the information below at your own risk.

Before you install check_mk, you first need to understand how xinetd has been implemented in pfsense 2.3.

The config file stored in /var/etc/xinetd.conf is regenerated every time you perform a filter reload. This means everytime you change a NAT rule, a filewall rule, change the TFTP config, etc... the xinetd.conf file is deleted, regenerated and then xinetd is restarted. The consequences of this is any manual change to the xinetd.conf get wiped out.

My intention is to try and write a package for check_mk that makes the changes below  automatically, but it does depend on whether pfsense will change the way it has implemented xinetd in 2.3

So, to allow us to make make changes to the xinetd.conf file, we need to modify the filter reload process.

The way I have chosen to do this is to modify the function, in /etc/inc/filter.inc that generates the xinetd.conf file, to also include xinetd config files from another directory. This will allow config changes to individual xinetd services, without having to modify the /etc/inc/filter.inc everytime.

1) First we need to make sure the check_mk agent can run without modifying the agent executable each time; for this we need to install bash.
Code: [Select]
pkg install bash
2) Next we need to create a location to store the check_mk_agent executable, and a location to store the xinetd config. I've chosen to use /opt as it is less likely to be deleted during an upgrade.
Code: [Select]
mkdir -p /opt/bin
mkdir -p /opt/etc/xinetd.d

3) Download the latest version of the check_mk_agent.freebsd from the check_mk public git.

Place it is /opt/bin/check_mk_agent

http://git.mathias-kettner.de/git/?p=check_mk.git;a=blob_plain;f=agents/check_mk_agent.freebsd;hb=HEAD

4) Make it executable
Code: [Select]
chmod +x /opt/bin/check_mk_agent
5) At this point you should be able to run the agent and see the check_mk output
Code: [Select]
/opt/bin/check_mk_agent
6) To able to use xinetd we need to create a new config file with the service details.

Please edit this file to suit your needs.

Code: [Select]
vi /opt/etc/xinetd.d/check_mk
Code: [Select]
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

service check_mk
{
type           = UNLISTED
port           = 6556
socket_type    = stream
protocol       = tcp
wait           = no
user           = root
server         = /opt/bin/check_mk_agent

# If you use fully redundant monitoring and poll the client
# from more then one monitoring servers in parallel you might
# want to use the agent cache wrapper:<br />

#server         = /usr/bin/check_mk_caching_agent

# configure the IP address(es) of your Nagios server here:
#only_from      = 127.0.0.1 10.0.20.1 10.0.20.2

# Don't be too verbose. Don't log every check. This might be
# commented out for debugging. If this option is commented out
# the default options will be used for this service.
log_on_success =

disable        = no
}

7) Now we need to modify the filter reload function to include our config files.
Code: [Select]
vi /etc/inc/filter.inc
You need to find the line which begins fclose($xinetd_fd);
This is the last part of the xinetd.conf file generation function. We need to insert our changes just above this line, it will need to look like this:

Code: [Select]

        fwrite($xinetd_fd, "includedir /opt/etc/xinetd.d");
       
        fclose($xinetd_fd);             // Close file handle

8) That should be it to get xinetd to pick up the changes, you now need to manually initiate a filter reload.

Status / Filter Reload / Reload Filters

You can check the System Logs to see if xinetd has picked up the new service; you should see something like below.

Code: [Select]
May 9 11:41:57 check_reload_status Reloading filter
May 9 11:41:58 xinetd 8327 Starting reconfiguration
May 9 11:41:58 xinetd 8327 Swapping defaults
May 9 11:41:58 xinetd 8327 readjusting service 6969-udp
May 9 11:41:58 xinetd 8327 readjusting service check_mk
May 9 11:41:58 xinetd 8327 Reconfigured: new=0 old=2 dropped=0 (services)

9) By default your LAN interface will allow access to port 6556, if you need access on your WAN, you can create a firewall or NAT rule and pass 6556 to localhost.


Do let me know if you have any feedback about this, or if it doesn't work.

Joe

Offline couillard45682

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #1 on: May 09, 2016, 10:52:06 am »
It works very well!
« Last Edit: May 09, 2016, 01:27:53 pm by couillard45682 »

Offline joeclifford

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #2 on: May 10, 2016, 04:00:42 am »
Thanks, good to know.

Offline cordeiroef

  • Newbie
  • *
  • Posts: 8
  • Karma: +1/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #3 on: May 10, 2016, 12:00:58 pm »
Hi  joeclifford,
I had problem with step 5, I could not install libstatgrab.
The pfsense not find the package.
Would you help me?

Offline joeclifford

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #4 on: May 10, 2016, 12:25:57 pm »
The check_mk_agent will just output an empty statgrab section is libstatgrab isn't installed.

By default pfsense 2.3 doesn't pull packages from the FreeBSD repo.

You can install it manually if you want to; you can download the latest build from the FreeBSD Ports Monitoring System.

http://portsmon.freebsd.org/portoverview.py?category=devel&portname=libstatgrab

Just grab the package for your processor type. E.g, for pfsense 2.3 x64;

Code: [Select]
curl --remote-name "http://pkg.freebsd.org/freebsd:10:x86:64/latest/All/libstatgrab-0.91.txz"
pkg install libstatgrab-0.91.txz

You might have to restart the firewall once you've installed statgrab to be able to use it.

Offline cordeiroef

  • Newbie
  • *
  • Posts: 8
  • Karma: +1/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #5 on: May 10, 2016, 12:50:56 pm »
Worked perfectly.
Thanks for the help there, man.
I will continue the following steps ...

 :D :D :D

Offline cedlerouge

  • Newbie
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #6 on: May 18, 2016, 05:07:19 am »
Like previous post, it works so good
Thx

Offline z80

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #7 on: May 19, 2016, 07:57:13 am »
Tested on 2.3.1
works !! :)

Thanks

Offline MontanaIce

  • Newbie
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #8 on: May 23, 2016, 11:45:49 am »
All worked then I came back to work the next week and found the agent is unreachable. I went through the steps againt and found that the filter file changed back to default settings and is missing the line:
fwrite($xinetd_fd, "includedir /opt/etc/xinetd.d");

If I re-add the line again, the agent works again. How do I make that filter file save the changes persistently through a reboot?

Offline azekiel

  • Jr. Member
  • **
  • Posts: 44
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #9 on: May 24, 2016, 03:14:34 am »
Thank you, worked like a charm.

Offline z80

  • Newbie
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #10 on: May 26, 2016, 05:38:48 am »
After a update to 2.3.1_1 the filter.inc have to edit again.
and a filter reload.

Offline FJerusalem

  • Newbie
  • *
  • Posts: 1
  • Karma: +1/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #11 on: May 30, 2016, 06:18:27 am »
Make it update-persistent

Problem: After an update the filter.inc has to be modified again.
Solution: Modify the /etc/inc/filter.inc-file automatically via cron-Script.

A) Create the cron-script
# vi /opt/filter_check_mk_cron

#!/bin/sh

grep includedir /etc/inc/filter.inc
if [ $? -eq 0 ]
then
        exit 0
else
        awk '/Close file handle/{print "fwrite($xinetd_fd, \"includedir /opt/etc/xinetd.d\");"}1' /etc/inc/filter.inc > /etc/inc/filter.inc.temp
        mv /etc/inc/filter.inc.temp /etc/inc/filter.inc
fi
exit 0

B) Make it executable
# chmod +x /opt/filter_check_mk_cron

C) Create the cron-job

In the web-ui go to
System -> Package Manager
If cron is not installed -> install

Then open Services -> Cron and click on Add

*/15    *    *    *    *    root    /opt/filter_check_mk_cron

This will check the config-file every 15 Minutes

Somebody might want to create an official package by now? :)

Offline joeclifford

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #12 on: May 31, 2016, 10:26:20 am »
FJerusalem,

Good idea to cron job a script to add in the needed line to the filter.inc. It might be worth modifying your script so it finds the line based on the fclose statement, rather than the comment. It's unlikely, but if and update adds an identical comment to the filter.inc file it could mess it up. I've used the below script.

Code: [Select]
#!/bin/sh

grep includedir /etc/inc/filter.inc
if [ $? -eq 0 ]
then
        exit 0
else
        awk '/fclose\(\$xinetd_fd\)\;/{print "fwrite($xinetd_fd, \"includedir /opt/etc/xinetd.d\");"}1' /etc/inc/filter.inc > /etc/inc/filter.inc.temp
        mv /etc/inc/filter.inc.temp /etc/inc/filter.inc
fi
exit 0


Offline joeclifford

  • Newbie
  • *
  • Posts: 10
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #13 on: May 31, 2016, 10:31:11 am »
All worked then I came back to work the next week and found the agent is unreachable. I went through the steps againt and found that the filter file changed back to default settings and is missing the line:
fwrite($xinetd_fd, "includedir /opt/etc/xinetd.d");

If I re-add the line again, the agent works again. How do I make that filter file save the changes persistently through a reboot?

Hmm, I haven't yet had the filter.inc file revert on just a reboot, it normally only happens when there is an update that overrides the filter.inc file. Like this bug fix introduced in 2.3.1 . https://redmine.pfsense.org/issues/6307

Are you running the nano version of pfsense? Pre 2.3.1 the filesystem is set to read only by default.

Thankfully, FJerusalem's script will make the changes for you.

Offline azekiel

  • Jr. Member
  • **
  • Posts: 44
  • Karma: +3/-0
    • View Profile
Re: pfSense 2.3 Check_mk working with xinetd
« Reply #14 on: February 08, 2017, 02:46:46 am »
Thank you very much for the manual solution. Any intentions to continue to develop the package?