-
Notifications
You must be signed in to change notification settings - Fork 5
/
openvpn-cron.php
47 lines (41 loc) · 1.67 KB
/
openvpn-cron.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
//////////////////////////////////////////////////////////////
// PHP Script that will log the information from the OpenVPN
// status log and store it into the database
//
// Retrieved from on 29-12-2016:
// http://jeroen.pro/2011/09/openvpn-stats-page-using-php/
//////////////////////////////////////////////////////////////
require 'config/mysql.php';
require 'config/openvpn.php';
require 'functions/functions.php';
//////////////////////////////////////////////////////////////
function cron()
{
$conn = mysql_connect(get_mysql_host(), get_mysql_user(), get_mysql_pass());
if(!$conn)
{
logMsg(mysql_error());
return;
}
$selectDb = mysql_select_db(get_mysql_db());
if(!$selectDb)
{
logMsg(mysql_error());
return;
}
$stats = parseLog(get_openvpn_status());
foreach($stats['users'] as $user)
{
if($user['CommonName'] != "UNDEF")
{
$result = mysql_query("UPDATE stats SET updated = '".time()."', VirtualAddress = '".$user['VirtualAddress']."', BytesSent='".$user['BytesSent']."', BytesReceived='".$user['BytesReceived']."', LastRef='".$user['LastRef']."' WHERE CommonName='".$user['CommonName']."' AND Since='".$user['Since']."'") or die(mysql_error());
if (mysql_affected_rows() == 0)
{
$result = mysql_query("insert into stats (CommonName, RealAddress, BytesReceived, BytesSent, Since, VirtualAddress, LastRef) values ('".$user['CommonName']."', '".$user['RealAddress']."', '".$user['BytesReceived']."', '".$user['BytesSent']."', '".$user['Since']."', '".$user['VirtualAddress']."', '".$user['LastRef']."')");
}
}
}
}
cron();
?>