Number of port users logged in - 2.1-RELEASE



  • Hello,
    I recently did an update to 2.1-RELEASE - I cant remember what version it was before, but I used to have a little script that polled the pfsense firewall that showed the number of users connected -

    #!/usr/local/bin/perl

    $line = expect -c 'spawn ssh **HIDDEN** -l root wc -l /var/db/captiveportal.db ; expect assword ; send "PASSWORD\n" ; interact' | tail -n 1 ;

    chomp($line);

    $line =~ s/^\s*(.?)\s$/$1/;
    @line = split(/ /,$line);

    print “Online |users=@line[0]\n”;

    This script was run by Zenoss on our central monitoring server to poll the number of users on the pfsense portal connected at that time, so we can graph centrally the connected users.

    However since upgrading the captiveportal.db file doesn’t exist any longer. so the script obviously doesn’t work any longer.

    Does anyone know how we can get the number of users logged in to the portal via the command line or even SNMP ?

    many thanks…


  • Rebel Alliance Developer Netgate

    check /var/db/captiveportal*.db - It is undoubtedly there, but it would have the zone name in it since each zone has its own separate database file now.



  • HI,

    I think its the  /var/db/captiveportalcpzone.db file - but its laid out differently and so the cheeky hack I was using before doesn’t work any longer.

    It looks as though I’ve actually got to query the database to get the result.

    Is there a simpler way or has anyone already solved this one ?

    cheers,



  • Hello.

    
    #!/usr/local/bin/php -q
    	require_once("/etc/inc/util.inc");
    	require_once("/etc/inc/functions.inc");
    	require_once("/etc/inc/captiveportal.inc");
    
    	/* read in captive portal db */
    	/* determine number of logged in users */
    	$count_cpusers = 0;
    	/* Is portal activated ? */
    	if (is_array($config['captiveportal']))
    		/* For every zone, do */
    		foreach ($config['captiveportal'] as $cpkey => $cp)
    			/* Sanity check */
    			if (is_array($config['captiveportal'][$cpkey])) 
    				/* Is zone enabled ? */
    				if (array_key_exists('enable', $config['captiveportal'][$cpkey])) {
    					$cpzone = $cpkey;
    					/* Zone selected -> count users and add */
    					$count_cpusers += count(captiveportal_read_db());
    				}
    
    	echo $count_cpusers;
    ?>
    

    This code will count all logged in users on all captive portal zones.

    2016-05-05 : skip disabled zones : only opens the data base when it is needed.

    This code works for me : https://www.test-domaine.fr/munin/brit-hotel-fumel.net/pfsense.brit-hotel-fumel.net/portalusers.html



  • Thanks for the script;

    With a bit of tweaking I’ve managed to get the data I wanted in the format that will import into Zennos.

    Thanks for your help.



  • @Gertjan,
    Thanks for your script!

    I tried it in 2.3 release, I have 2 zones, one named “test1”, the other named “test2”, I changed the (hard code) “cpzone” to “test1” or “test2”:

    $cpzone ="test1"; /* hard code */
    

    but it always get “0”, actually we have about 20 users online for each zone.

    May I know if it still support 2.3?



  • @yanqian:

    ……
    May I know if it still support 2.3?

    I’m still using the script as of today.

    I updated the script (look above), it counts now all users on all zones. No need to change anything.
    Btw : I repaired the link so you can check it out on my “Munin” page.

    2016-05-04 : 13h45m : I updated the script ones more.



  • Hello everyone,

    The scripts works like a charm directly in my pfsense box but when i try with my zabbix server i have this error message :

    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class ‘SQLite3’ not found

    the script is excecuted with root previlige.

    If someone hear my help 🙂

    Thanks.

    Best Regards.
    Myke.



  • @myke:

    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class ‘SQLite3’ not found

    This errors shows up when the date base with logged in users doesn’t exists.
    This could be normal when the captive portal isn’t running.
    Maybe the script should check if a given portal instance is actually enabled ( <enabled>) before trying to open the database.
    If it runs (= enabled) , the data base is created. The error comes from the portal code that handles the database.

    @myke:

    The scripts works like a charm directly in my pfsense box but when i try with my zabbix server i have this error message :

    What is a zabbix box ???
    The script was written for pfsense - it didn’t work me when I put it in my Nespresso machine neither.

    edit : I will update the script to include the check.</enabled>



  • Hi, thanks for your answers.

    Quote from: myke on Yesterday at 10:43:00 am
    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class ‘SQLite3’ not found
    This errors shows up when the date base with logged in users doesn’t exists.
    This could be normal when the captive portal isn’t running.
    Maybe the script should check if a given portal instance is actually enabled ( <enabled>) before trying to open the database.
    If it runs (= enabled) , the data base is created. The error comes from the portal code that handles the database.</enabled>

    My captive portal is running and I don’t have any issue when I execute the script directly in my pfsense box.

    The scripts works like a charm directly in my pfsense box but when i try with my zabbix server i have this error message :
    What is a zabbix box ???
    The script was written for pfsense - it didn’t work me when I put it in my Nespresso machine neither.

    That a shame that your Nespresso can launch a script and I hope you don’t buy your nespresso’s machine for doing that  ;D

    I use zabbix agent on pfsense and  zabbix server give a parameter to launch the script like this :
    https://forum.pfsense.org/index.php?topic=111257.0

    And for test the connection with my zabbix, I write this command :

    zabbix_get -s "Wan" -p 10050 -k CP_users
    

    And return this error message :

    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class 'SQLite3' not found
    

    Thanks for your time Mr Gertjan

    Best Regards.
    Myke.



  • @myke:

    zabbix_get -s "Wan" -p 10050 -k CP_users
    

    Ah, ok, didn’t know what “zabbix” was. I figured it out https://fr.wikipedia.org/wiki/Zabbix

    Btw :
    SSH into your box.
    ‘cd’ to the pace where you put your script ( mine is called captiveportal_count_online_users.php )
    Then:

     php -q captiveportal_count_online_users.php
    

    Does it return a number like 0 or more, or the same error.

    While you are in there, check if the database really doesn’t exist.
    It should be here :

    /var/db/captiveportal[zone-name].db

    where [zone-name] is the name of your captive portal zone.
    Other .db might exist.

    I have a file database file called : captiveportalcpzone1.db
    because my portal is running
    and
    the name is “cpzone1”.



  • Code: [Select]
    php -q captiveportal_count_online_users.php
    Does it return a number like 0 or more, or the same error.

    php -q cptotaluser
    

    It gives me the right number of the connection of my captive portal.

    And in the /var/db, my DB exists and it’s call captiveportal2017.db

    Update :
    it’s really weird cause i just test the script in 2 pfsense in 2.3.2-release . one of two works. it’s the same script with same name and the zabbix agent is the same of course.



  • @myke:

    Code: [Select]
    php -q captiveportal_count_online_users.php
    Does it return a number like 0 or more, or the same error.

    php -q cptotaluser
    

    It gives me the right number of the connection of my captive portal.

    Thus, the script works.

    Btw :

    				if (array_key_exists('enable', $config['captiveportal'][$cpkey])) {
    

    is already checking IF the captive portal instance is running.

    Double check if your are running the script as super user  (= ‘root’)



  • Btw :

    				if (array_key_exists('enable', $config['captiveportal'][$cpkey])) {
    

    is already checking IF the captive portal instance is running.

    Double check if your are running the script as super user  (= ‘root’)

    i add “echo $USER” on the script  :

    zabbix_get -s "Wan" -p 10050 -k CP_users
    root
    Fatal error: Class 'SQLite3' not found in /etc/inc/captiveportal.inc on line 1454
    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class 'SQLite3' not found
    root
    
    

    so you can see the script is running with the user root.



  • @myke:

    PHP ERROR: Type: 1, File: /etc/inc/captiveportal.inc, Line: 1454, Message: Class ‘SQLite3’ not found

    Was reading this message again - and this time in the afternoon, so this time the caffeine worked.
    The errors is “Class ‘SQLite3’ not found” which is (should be) part of the PHP install on pfSense.

    Visit this page : https://your-pfsense-box/phpinfo.php
    Does it have a section that says “sqlite3” is included ?

    Also : when using a navigator, php is using this php.ini file : /usr/local/etc/php.ini
    When using the CLI, it ‘could’ be another one - check that.
    Run :

    php -q /usr/local/www/phpinfo.php | grep 'sqlite'
    

    Does it say:

    PDO drivers => sqlite
    pdo_sqlite
    sqlite3
    sqlite3.extension_dir => no value => no value

    It looks like you haven’t the same version of pfSEnse as I do … (which is, of course, the latest ‘stable’ version)

    edit : or whatever what could explain why the sqlite3 php extension isn’t avaible.


  • Banned

    That extension hasn’t been there for a while. Use /usr/local/bin/sqlite3 instead.



  • @doktornotor:

    That extension hasn’t been there for a while. Use /usr/local/bin/sqlite3 instead.

    According to : /usr/local/etc/php/extensions.ini :

    [2.3.2-RELEASE][admin@pfsense.brit-hotel-fumel.net]/root: cat /usr/local/etc/php/extensions.ini
    ....
    extension=pdo_sqlite.so
    .....
    extension=sqlite3.so
    
    

    I have a clean

    2.3.2-RELEASE-p1 (amd64)
    built on Tue Sep 27 12:13:07 CDT 2016
    FreeBSD 10.3-RELEASE-p9
    

    install.
    I do have

    /usr/local/bin/sqlite3
    

    But : does /etc/inc/captiveportal.inc use the ‘extension’ or the external binary ? If the external binary was used, I should have found a proof in the code.



  • Hello Gentleman,

    Visit this page : https://your-pfsense-box/phpinfo.php
    Does it have a section that says “sqlite3” is included ?

    I have an error : 404 Not Found so i can’t tell you.

    And

    php -q /usr/local/www/phpinfo.php

    I don’t have any phpinfo.php on my whole pfsense…

    Why the library missed when i used my zabbix server to launch the script on pfsense. and it’s perfectly working on pfsense 😞



  • @myke:

    Hello Gentleman,

    Visit this page : https://your-pfsense-box/phpinfo.php
    Does it have a section that says “sqlite3” is included ?

    I have an error : 404 Not Found so i can’t tell you.

    And

    php -q /usr/local/www/phpinfo.php

    I don’t have any phpinfo.php on my whole pfsense…

    Stupid me.
    Create a file called “phpinfo.php” in /usr/local/www/
    an put this into it :

    phpinfo();
    ?>
    

    @myke:

    Why the library missed when i used my zabbix server to launch the script on pfsense. and it’s perfectly working on pfsense 😞

    Dono.
    I’m using a “munin plugin” like this :

    
    #!/bin/sh
    #
    # Plugin to monitor the number of captive portal users. 
    #
    # Parameters:
    #
    # 	config   (required)
    # 	autoconf (optional - used by munin-config)
    #
    # Magick markers (optional - used by munin-config and som installation
    # scripts):
    #%# family=auto
    #%# capabilities=autoconf
    
    if [ "$1" = "autoconf" ]; then
    	echo yes 
    	exit 0
    fi
    
    if [ "$1" = "config" ]; then
    
    	echo 'graph_title Captive Portal Users'
    	echo 'graph_args --base 1000 -l 0 '
    	echo 'graph_vlabel Number of users'
    	echo 'graph_category portalusers'
    	echo 'graph_info This graph shows the number of portal users on the system.'
    	echo 'portalusers.label users'
    	echo 'portalusers.info The current number of users.'
    	echo 'portalusers.draw LINE2'
    	exit 0
    fi
    
    echo -n "portalusers.value "
    /usr/local/bin/php -q /root/captiveportal_count_online_users.php
    

    and it works well.



  • I just test with the new release of pfSense 2.3.3 and the script worked.
    hallelujah 🙂

    Thanks for the help by the way  🙂

    Best regards
    Myke



  • Thanks, this still works (2.3.4)

    I implemented it differently, but using your script. I uploaded the script in the captive portal and my cacti-install gets the value via https from https://pfsensehost:8003/captiveportal-cpcount.php and graphs the output… works great! Finally CP count monitored.

    An OID for SNMP would be great though.



 

© Copyright 2002 - 2018 Rubicon Communications, LLC | Privacy Policy