Yesterday I was facing a problem that I don’t face many times: Something isn’t widely documented on the internet.
In this case it was about OpenVPN, the status of the server, per client, more specifically. After doing some searching on the internet I figured that OpenVPN isn’t used much, or that only pro’s use it and they write their own code anyway.
The only thing I found was a very basic(and non-working) PHP page from 2005, and a simple PHP script(also from 2005) that parses the logfile and shows the current status. I decided to base my script on that.
Before we start coding, make sure your OpenVPN server configuration contains: “status openvpn-status.log”. Now let’s start coding. First I made a table in the database.
DB Scheme:
MySQL |copy code |?
01 02 03 04 05 06 07 08 09 10 
Sorry for my dirty type choosing, this script was made in a hurry.
Then I made a cron that saves the stats:
Cron.php
PHP |copy code |?
01 <?02 03 04 05 06 07 08 09 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 48 }49 50 51 {52 53 {54 55 56 57 58 }59 }60 }61 ?>
You have to edit the mysql details and the log location.
Here we insert the stats in the db, if the session start time and the client name is the same, it updates. If not, it inserts. We filter out UNDEF, UNDEF sometimes appears in the log, my guess is when the client isn’t fully connected yet.
Then I made a cron that saves the stats every minute:
Cron:
PHP |copy code |?
1 
Replace [location] with the absolute folder to the cron, like /home/openvpn
I run the script every minute to prevent stats loss, but you could also do this less.
Stats.php:
PHP |copy code |?
001 <?php002 003 004 005 006 007 008 009 010 }011 012 013 014 015 016 017 018 019 020 021 }022 }023 024 025 026 027 028 029 }030 }031 }032 033 034 }035 036 037 038 039 040 041 }042 043 044 045 }046 047 048 049 <table>050 <tr style="font-weight: bold;" bgcolor="#888888">051 <td>Common Name</td>052 <td>Real Address</td>053 <td>Virtual Address</td>054 <td>Logged In At</td>055 <td>Bytes Sent</td>056 <td>Bytes Received</td>057 <td>Last Activity</td>058 </tr>059 060 061 {062 063 064 065 066 067 068 069 070 071 }072 073 </table>074 <br>075 076 </b>077 078 079 080 081 <table>082 <tr style="font-weight: bold;" bgcolor="#888888">083 <td>Common Name</td>084 <td>Bytes Sent</td>085 <td>Bytes Received</td>086 </tr>087 088 089 090 091 092 <td>Total</td>093 094 095 096 }097 098 099 100 101 102 103 104 105 }106 107 </table>108 109 110 111 112 113 <table>114 <tr style="font-weight: bold;" bgcolor="#888888">115 <td>Common Name</td>116 <td>Bytes Sent</td>117 <td>Bytes Received</td>118 </tr>119 120 121 122 123 124 <td>Total</td>125 126 127 128 }129 130 131 132 133 134 135 136 137 }138 139 </table>140 141 142 143 144 145 <table>146 <tr style="font-weight: bold;" bgcolor="#888888">147 <td>Common Name</td>148 <td>Bytes Sent</td>149 <td>Bytes Received</td>150 </tr>151 152 153 154 155 {156 157 }158 159 160 161 <td>Total</td>162 163 164 165 }166 167 168 169 170 171 172 173 174 }175 176 </table>177 178 179 180 181 <table>182 <tr style="font-weight: bold;" bgcolor="#888888">183 <td>Common Name</td>184 <td>Bytes Sent</td>185 <td>Bytes Received</td>186 </tr>187 188 189 190 191 192 <td>Total</td>193 194 195 196 }197 198 199 200 201 202 203 204 205 }206 207 </table>208 209 210 211 212 <table>213 <tr style="font-weight: bold;" bgcolor="#888888">214 <td>Common Name</td>215 <td>Bytes Sent</td>216 <td>Bytes Received</td>217 </tr>218 219 220 221 222 223 <td>Total</td>224 225 226 227 }228 229 230 231 232 233 234 235 236 }237 238 </table>239 240 241 ?>
Edit the database details and log location here too, and you’re done. I advise to protect the folder with a .htaccess+.htpasswd or some simple php login.
Sorry for my dirty code, but hey, it works.
 
			
Hai,
Would you be willing to rewrite this to the current versions of php/mysql ?
I tried it but keep on failing. I replaced mysql_ to mysqli_ ereg to preg_match but now ending up with fgets and feof errors 🙂
Cheers Paul
Hi Paul,
Sure, but I can imagine the openvpn logs format have been changed since 2011, maybe you can send me a sample?