bashclub / checkmk-opnsense-agent

65 stars 14 forks source link

ignoring openvpn clients after upgrade to OPNsense 23.7.1 #41

Open sibelle-labs opened 10 months ago

sibelle-labs commented 10 months ago

Hi ! after upgrading to OPNsense 23.7.1, client connections aren't detected thanks a lot

katamadone commented 4 months ago

Are you using openvpn server or instances?

katamadone commented 4 months ago

if you are using instances this would work. I've put that above the section def checklocal_ipsec(self): But you would have to check with your instance ID.. - sorry absolutely beginner here.

    def checklocal_openvpninstances(self):

        def search_key_in_json(obj, key):
            if isinstance(obj, dict):
                if key in obj:
                    return True
                for val in obj.values():
                    if search_key_in_json(val, key):
                        return True
            elif isinstance(obj, list):
                for item in obj:
                    if search_key_in_json(item, key):
                        return True
            return False

        _ret =[]
        # Run the command and capture its output
        # /usr/local/opnsense/scripts/openvpn/ovpn_status.py
        output = subprocess.run(['python3', '/usr/local/opnsense/scripts/openvpn/ovpn_status.py'], capture_output=True, text=True)

        # Parse the output as JSON
        data = json.loads(output.stdout)
        result = search_key_in_json(data, "client_id")

        if result:
            print("client_id found!")

            # Extracting the client_id from the client list
            client_list = data["server"]["0f7731d9-0222-44f8-be6d-2ae16147650f"]["client_list"]
            client_ids = [entry["client_id"] for entry in client_list]

            # Count the number of entries
            num_entries = len(client_ids)
            # Construct string
            outvar = (f'0 "OVPN Instances Clients" total_connected_clients={num_entries} Total Clients over all Instances')
            #print(outvar)
            _ret.append(outvar)
            return _ret
        else:
            outvar = (f'0 "OVPN Instances Clients" total_connected_clients=0 Total Clients over all Instances')
            #print(outvar)
            _ret.append(outvar)
            return _ret
        #pass
katamadone commented 4 months ago

https://github.com/katamadone/checkmk-opnsense-agent/tree/main - ⚠️⚠️⚠️as told: don't take this as "the new" only to see it in the complete python file