Well, I won’t claim it’s pretty or the most efficiently written script ever made….
However, I had a problem. OpenVPN, when it is run in daemon mode, do not give you any easy was to check who is currently logged into the VPN or what the internal routes are, etc. I learned from reading the man page, that you can cause the daemon to output status if you send a SIGUSR2 to the process. Now, if OpenVPN is not running as a daemon, it will send the status to stdout; if it IS running as a daemon, it is sent to syslog.
So, you can tail your /var/log/messages and read the stats. But that’s kinda clunky and certainly not easy for people with weak shell skills. Some people who help admin my OpenVPN server are not shell savvy, so I wanted to make a utility you could use to easy display the current stats.
Essentially, this utility sends the SIGUSR2 signal to the OpenVPN process. When it does that, it notes what time it is. Then it uses some clever regular expression magic to parse the status information out of the messages file and attempt to display it nicely. I really couldn’t find a easy way to have it auto pad the columns, etc. I was thinking of using ncurses, but thought that was over kill. If you have any ideas how to do this in Python, then please let me know.
Note in the code, that I have parsed all the fields out. So, it would be easy to extend this code to a web page/interface, or anything else that need might arise for. Yes, I know I should have added comments, but I was in a hurry when I wrote this, so save your tisking. One final note: this script does need to be run as root or suid, because it needs to send a signal and because it needs to access /var/log/messages for reading.
If you find this script useful, let me know. Enjoy…
Update: I was not able to get WordPress to display Python code correctly. Therefore, I have published the script to my web site. Please go here to download it.