My intention for this script was that I needed to do something
called "the 95th percentile rule". This is a billing rule for
ISP's. Clients at the data center I am working are billed by
this rule. Each client is occupying a switch port in the network
core.I now poll with this script for each port the incoming and
outgoing octets/bytes. I scheduled this script to run automatically
with a tool called firedaemon, a free tool that lets you create
services that execute a script or whatever you want.
I am going to put a script out here in the next few weeks that
gives you an easy form to calculate things like average bandwidth
and the 95th percentile rule and other stuff. Check back.
You need a working environment of:
PHP and MySQL and SNMP support in PHP
I use Windows2000, Apache, MySQL, PHP, ActiveStatePerl, net snmp
(UCD snmp). Your environment might look different, but it should
still work. The description here works on this environment, but
if you have time and can write something how you made this script
work on yours, let me know. Also, this could be the beginning of
a nice monitor/network/billing package. I think I will also
create alittle admin tool, so that you can snmpwalk a machine and
select your interfaces that you want to poll thru a nice web gui.
But that is in the future...
You need to download the UCD SNMP package, also called net snmp.
You can find it on SourceForge here. Get the Win32 distribution and install
it. Use your root folder as the installation point, so that you
have the "usr" folder under c:\ .
Now you have to enable snmp. Got to your php.ini file in your windows
system folder. Find the section with the extension and take away the
semicolon from php_snmp.dll and make sure that you extension
directory is set to the directory "extensions" under your php
directory. (i.e. extension_dir = "c:\php\extensions\"). Now, I
copied the php_snmp.dll into my windows system folder....your
choice, don't know if it works without it.
Now comes the part that makes it all work. Copy from your php install
directory the files in the folder "MIBS" to the c:\usr\mibs directory
of the net snmp installation! You have to overwrite some files, that
is okay !
Restart your webserver. If you get an error message, troubleshoot it.
More likely your extension directory is not right or it can't find the
dll. When it starts clean, you can test your installation by making
alitte php script like this:
That should now display the name of your system, assuming you serve snmp
on you box (or replace the ip by your router/switch/whatever). If you get
Fatal error: Call to undefined function: snmpget()
in c:\wwwroot\yourwebfolder\snmp_test.php on line 2
then your SNMP in PHP is not working. PHP doesn't recognize snmpget as
a command statement and treats it as it is a function, and of course then
can't find it.
If you get this:
Warning: No Response from 127.0.0.1 in
c:\wwwroot\theworldsend.net\snmp_small.php on line 2
then SNMP is working in PHP, but your host is not serving SNMP. You can
install SNMP support or just change the IP to something that does SNMP.
If you still have problems after that, recheck your steps, repeat them and if
you can't fix it, search the web for help.
So you got snmp work. Great. Let's create the database and the tables.
You can create the database from the command line, but I prefer a
package called PHPMyAdmin. Simply create a database called collector,
then make the following tables:
CREATE TABLE collector (ip varchar(15) NOT NULL,
port varchar(10) NOT NULL,
snmpread varchar(30) NOT NULL );
CREATE TABLE ifdata (id int(11) NOT NULL auto_increment,
timestamp bigint(20) NOT NULL,
ip varchar(15) NOT NULL,
port varchar(10) NOT NULL,
ifinoctets bigint(20) NOT NULL,
ifoutoctets bigint(20) NOT NULL,
indiff bigint(20) NOT NULL ,
outdiff bigint(20) NOT NULL ,
PRIMARY KEY (`id`) );
Before you are running the script, you need to setup at least 1 entry in the
collector database. You need the IP, the snmp read community string and the
port. Now, the port is alittle bit "difficult". Imagine you want to monitor
a router interface, and you have 6 different interfaces there. Right now,
do the following, get a snmpwalk program (like GETIF) and walk the router by
hand, look at the ifindex table and find the index number for the interface
you want to monitor, that is the port you need to put into the database. As
I said, I will try to make a nice Admin interface...so you can do all that
via a webgui.
Open the script and edit your database information. You can start the script
a few times by hand. Simply put the script into your webfolder, and hit
it with your webbrowser. Do it a few times, and look how the database
is filling up. You need something to call this script every 5 minutes.
I use firedaemon. Checkout their website. Download and install it.
To configure the script to run via firedaemon, do it this way:
C:\Program Files\FireDaemon v0.09c>firedaemon -p
FireDaemon v0.09c (c) 1999-2000 Sublime Solutions
This process will install a new FireDaemon service for you.
Please answer the following questions:
1. Service name (no spaces):
2. Application working directory (eg. d:\path):
3. Application executable (eg. d:\path\filename.exe): (<--your php folder)
4. Application options (eg. /opt1 /opt2):
---> c:\apache\htdocs\working\snmpcollector.php (<--your webfolder)
5. Auto restart application (y/n):
6. Processor affinity mask (0 to 15):
7. Process priority (0 to 3):
8. Interact with the desktop (y/n):
9. Auto start service (y/n):
This creates the service for you. If you have problems, check out
firedaemons website for help. Don't start the service yet.
First, we need to alter a value in the registry, otherwise this
service starts the script every 5 seconds, which is not really
what you want. Open regedit, find the key
The value needs to be changed to 300000 (decimal), meaning 5
minutes. Now start the services (net start collector) and check
in your database that the service is running every 5 minutes. You
should see every 5 minutes new data in the ifdata table.
If you want to query more interfaces you can just add more entries into the collector
table....and you are done.
PLEASE, PLEASE leave me some feedback... perhaps the stuff only works on my system
© 2000, 2001 TheWorldsEnd.NET (You may cite this site at will. Just give credit where it's due.)
$SystemName = snmpget("127.0.0.1", "public", "system.sysName.0");