⬆️ ⬇️

Communication billing and Cisco Catalyst 2960 via SNMP. Change port speed, traffic count

As promised, after the greeting , we begin to slightly disclose technical details.



Task



Give users the ability to choose the connection speed and charging method for each of their servers. Give administrators the ability to switch the speed of the channel or even extinguish the port through the admin panel.



Iron



Each rack has at least one Cisco Catalyst 2960 switch. The specific model is WS-C2960G-48TC-L with LANBASE SPF. The input to it is 4x SFP, and 48 Gigabit Ethernet ports are facing the servers. With a live load of several gigabits, there were no problems. In the photo, various members of the 2960 family.

image



')

Port speed and status





From the user’s side, everything looks pretty simple:





In case of selecting 10 Mb connection, the lower options disappear, as the traffic in this case is free. The option of limiting traffic on the use of prepaid volume is due to concern for users, because according to 95th policy, if you downloaded at a gigabit speed of 36 hours and then did not use the channel at all, then the account will be just for full gigabit. Imagine, while you were away fishing for the weekend, you got the Habra effect and thousands of users continuously download the image of your virtual machine, which was laid out on your server and can help many developers, and on Monday you will find $ 2000 on your hosting account. This Monday will be especially difficult. In our case, after 24 hours, the port will switch to 10 megabits, or after 36 hours, traffic will be purchased in equal portions until the financial limit of a specific user is reached.



From the backend side, all of the work for us, in fact, has already been done by Cisco engineers, and to manage their devices, we will use the Simple Network Management Protocol (SNMP), which was created in 1988, and secured in 1990 ( RFC 1157 ) by the group hackers (in the original meaning of the word), headed by JD Case, for which a special thanks to him.



For this feature to work, you must enable SNMP support on your switch, and also remember to specify the communtity and allow for this community to perform operations not only on reading, but also on writing. The PHP function looks like this:



function set_switch_port_speed ($host, $community, $port_no, $speed, $model='2960'){

// use SNMP to set speed. $speed can be 10, 100, 1000 or 0 to shutDown port.

if(!in_array($speed, array(10, 100, 1000))){

//echo 'Wrong port speed! '.$speed."\n";

return false;

}



$oid = '';



if(stristr($model, '2960')){ // magic smtp strings, path to

$oid = '1.3.6.1.4.1.9.5.1.4.1.1.9.1.'.$port_no;

}else{

$oid = '1.3.6.1.4.1.9.9.87.1.4.1.1.33.0.'.$port_no; // c2900PortAdminSpeed

}



$speed *= 1000000;

echo "setting new speed...\n";

return snmpset($host, $community, $oid, 'i', $speed);



}





Accordingly, it is called when the server is configured, the limit is reached, the lease is terminated, and so on.



The port can be turned off in two ways - by setting the speed equal to port 0, or by using a separate function:



function set_switch_port_status($host, $community, $port_no, $status, $model=''){

// use SNMP to set status. $status can be 0 or 1

$status = (int) $status;

if(!in_array($status, array(0, 1))){

echo 'Wrong port status! '.$status."\n";

return false;

}



// decode status, 1 means UP 2 means down

if($status == 0){

$status = 2;

}



$oid = '';



// for 2960 set oid = 'IF-MIB::ifAdminStatus.101'.$port_no;

if(stristr($model, '2960')){

if($port_no < 10){

$port_no = '0'.$port_no;

}

$oid = 'IF-MIB::ifAdminStatus.101'.$port_no;

}else{

$oid = 'IF-MIB::ifAdminStatus.'.($port_no + 1); // first interface on 2900xl is VLAN1

}



return snmpset($host, $community, $oid, 'i', $status);



}





Traffic counting



What the user sees:



The server widget, on it is spark graphics, showing a traffic trend for the last 48 hours without a scale value (for that reason it is sparkling - there is little space in the widgets). The number indicates the current usage of the channel.



A general schedule for all servers is displayed under the server list (it has no relation to billing, the traffic is shared individually for each server)





Well, the schedule of a particular server is already a full-fledged display in a larger format, with the ability to select a period of time. According to these indicators, a bandwidth bill is already being formed.





The red line measures 95%. Anything higher is not charged. As you can see, this user experienced Habraeffect for free and without a single break :). Well, who has not seen - our panel entirely.



To collect statistics from ports, we use the cacti package. After cacti has removed and processed the information, it writes data to the rrd file for each port. This file contains statistics for the entire port operation period. In the future, we can extract this information and carry out any operations, whether it is traffic counting for a certain period or drawing a chart for a client. Below is a function to extract data from this file for further post-processing using the rrdtool utility:



function getRRDData($rrd_file, $start, $end = ''){



$str_end = '';

if($end)

$str_end = ' -e '.$end;



$res = exec('/usr/local/bin/rrdtool fetch '.$rrd_file.' AVERAGE -s '.$start.$str_end, $output);

if(!$res || (count($output) < 1)){

return array(); // smth is wrong

}

// we need only 3 ... N-1 elements

$rrd_data = array();

for($i = 2; $i < (count($output) ); $i++){



// replace NAN with 0

if(stristr($output[$i], 'nan'))

$output[$i] = str_ireplace('nan', '0', $output[$i]);



$values = explode(' ', str_replace(':', '', $output[$i]));

// check timestamp

if($end && ($values[0] > $end))

break;



$rrd_data[] = $output[$i];

}

return $rrd_data;



}





In order not to get the sheets from the code at all, we will not give here massive functions of traffic counting and drawing. When traffic is calculated, the beginning and the end of the time interval are determined, the data extraction function is called (the code is above), this data is calculated and at the end a figure of 95 percent or the number of gigabytes is output. 95% is considered averaged five-minute segments, 1 megabit equals 1000 * 1000 bits per second, and a gigabyte is 1024 * 1024 * 1024 bytes. An impressive percentage of ordinary people go astray when naming these numbers, and no wonder :).



In anticipation of constructive criticism,

ServerClub Dedicated Servers .

Source: https://habr.com/ru/post/115539/



All Articles