The pfSense Store

Author Topic: Number of port users logged in - 2.1-RELEASE  (Read 1734 times)

0 Members and 1 Guest are viewing this topic.

Offline skipzoid

  • Newbie
  • *
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Number of port users logged in - 2.1-RELEASE
« on: December 24, 2013, 05:36:12 am »
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..

Offline jimp

  • Administrator
  • Hero Member
  • *****
  • Posts: 19811
  • Karma: +1121/-8
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #1 on: December 24, 2013, 08:24:00 am »
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.
Need help fast? Commercial Support!

Co-Author of pfSense: The Definitive Guide. - Check the Doc Wiki for FAQs.

Do not PM for help!

Offline skipzoid

  • Newbie
  • *
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #2 on: December 24, 2013, 04:04:37 pm »
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,

Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #3 on: December 26, 2013, 02:39:43 am »
Hello.
Code: [Select]
#!/usr/local/bin/php -q
<?php
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
« Last Edit: May 05, 2016, 06:42:27 am by Gertjan »

Offline skipzoid

  • Newbie
  • *
  • Posts: 21
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE - Solved
« Reply #4 on: January 02, 2014, 11:23:00 am »
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.

Offline yanqian

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #5 on: May 04, 2016, 05:29:46 am »
@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":

Code: [Select]
$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?

Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #6 on: May 05, 2016, 02:58:20 am »
.....
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.
« Last Edit: May 05, 2016, 06:43:49 am by Gertjan »

Offline myke

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #7 on: February 15, 2017, 10:43:00 am »
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.


Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #8 on: February 16, 2017, 03:46:37 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.

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.

Offline myke

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #9 on: February 16, 2017, 04:27:56 am »
Hi, thanks for your answers.

Quote
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.

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

Quote
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 :
Code: [Select]
zabbix_get -s "Wan" -p 10050 -k CP_users
And return this error message :
Code: [Select]
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.

« Last Edit: February 16, 2017, 04:47:21 am by myke »

Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #10 on: February 16, 2017, 05:12:57 am »
Code: [Select]
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:
Code: [Select]
php -q captiveportal_count_online_users.phpDoes 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 :
Quote
/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".

Offline myke

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #11 on: February 16, 2017, 07:32:18 am »
Quote
Code: [Select]
 php -q captiveportal_count_online_users.php
Does it return a number like 0 or more, or the same error.

Code: [Select]
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.
« Last Edit: February 16, 2017, 10:37:15 am by myke »

Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #12 on: February 17, 2017, 04:09:40 am »
Quote
Code: [Select]
 php -q captiveportal_count_online_users.php
Does it return a number like 0 or more, or the same error.

Code: [Select]
php -q cptotaluser
It gives me the right number of the connection of my captive portal.

Thus, the script works.

Btw :
Code: [Select]
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')

Offline myke

  • Jr. Member
  • **
  • Posts: 50
  • Karma: +0/-0
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #13 on: February 17, 2017, 05:37:12 am »
Btw :
Code: [Select]
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')
[/quote]

i add "echo $USER" on the script  :
Code: [Select]
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.


Online Gertjan

  • Hero Member
  • *****
  • Posts: 1642
  • Karma: +131/-5
    • View Profile
Re: Number of port users logged in - 2.1-RELEASE
« Reply #14 on: February 17, 2017, 09:15:49 am »
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 :
Code: [Select]
php -q /usr/local/www/phpinfo.php | grep 'sqlite'Does it say:
Quote
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.
« Last Edit: February 17, 2017, 12:07:39 pm by Gertjan »