All pastes #1422327 Raw Edit

eggdrop bw.tcl script

public text v1 · immutable
#1422327 ·published 2009-05-14 03:55 UTC
rendered paste body
################################################################################# #  TCL scripts by Ofloo all rights reserved.##  HomePage: http://ofloo.net/#  CVS: http://cvs.ofloo.net/#  Email: support[at]ofloo.net# #  This program is free software; you can redistribute it and/or#  modify it under the terms of the GNU General Public License#  as published by the Free Software Foundation; either version 2#  of the License, or (at your option) any later version.#   #  This program is distributed in the hope that it will be useful,#  but WITHOUT ANY WARRANTY; without even the implied warranty of#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the#  GNU General Public License for more details.#   #  You should have received a copy of the GNU General Public License#  along with this program; if not, write to the Free Software#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#   ################################################################################################################################################################# Test release 0.2 dev## CHANGELOG:#  - 4/03/2005 complete rewrite fixed bsd diff added multiple interfaces#    support and flexible template.#################################################################################namespace eval bandwidth {   variable api "/proc/net/dev"  ##########################################  # <interface>:<alias> seperated by a ','  # or a hex representation of 2e, for  # windows use only win:<whatever>  ##########################################  variable interface "eth0:LAN,eth1:LAN,ppp0:WAN" ;# for windows win:<something> without the others..  ##########################################  # %IFN% : Interface name for example eth0  # %IFA% : Interface alias for example WAN  # %INC% : Incomming traffic  # %OUT% : Outgoing traffic  ##########################################  variable template "\[\002SeedBox\002\]: Up:3 %OUT% kb/s Down:4 %INC% kb/s ."   ##########################################  # 0: privmsg to channel  # 1: privmsg to nick  # 2: notice to nick  ##########################################  variable msgtype "0"  ##########################################  # Public trigger flag  ##########################################  variable pubflag "n"  ##########################################  # Public channel trigger  ##########################################  variable bandwidth "!bw"  ##########################################  # !!!!!!!!!! DO NOT EDIT BELOW !!!!!!!!!!  ##########################################  ##########################################  # setting script version not to be edit !  ##########################################  variable version 0.2  ##########################################  # Platform switch  ##########################################  switch -regexp [string tolower $::tcl_platform(platform)] {    "win" {      variable switch 1    }    "unix" {      variable switch 2    }    ".*" {      variable switch 0    }  }  ##########################################  # Msg type switch  ##########################################  switch  -exact $msgtype {    "0" {      variable msg "PRIVMSG %CHAN%"    }    "1" {      variable msg "PRIVMSG %NICK%"    }    "2" {      variable msg "NOTICE %NICK%"    }  }  ##########################################  # Main bandwidth proc  ##########################################  proc bandwidth_pub {nick host hand chan arg} {    variable switch; variable template; variable interface; variable msg; variable api    if {[string equal 0 $switch]} {      putlog "Error unknown operating system."    } elseif {[string equal "1" $switch]} {      if {[regexp -nocase {^(win):([a-zA-Z0-9\-]{1,100})$} $interface -> ifn ifa]} {        set s_inc 0; set s_out 0; set e_inc 0; set e_out 0        foreach {x} [split [exec netstat -e] \n] {          if {[string equal -nocase -length 4 byte $x]} {            set s_inc [lindex $x 1]            set s_out [lindex $x 2]          }        }        after 500        foreach {x} [split [exec netstat -e] \n] {          if {[string equal -nocase -length 4 byte $x]} {            set e_inc [lindex $x 1]            set e_out [lindex $x 2]          }        }        putserv "[string map [list %NICK% $nick %CHAN% $chan] $msg] :[string map [list %IFN% $ifn %IFA% $ifa %INC% [format %.2f [expr ($e_inc - $s_inc) / 512.0]] %OUT% [format %.2f [expr ($e_out - $s_out) / 512.0]]] $template]"      } else {         putlog "Script misconfiguration, check your settings."      }    } elseif {[string equal "2" $switch]} {      if {[file exists $api]} {        if {[file readable $api]} {          if {[regexp {win:} $interface]} {            putlog "Script misconfiguration, check your settings."          } else {            if {[catch {open $api} rf]} {              putlog "Error couldn't open $api for an unknown reason."            } else {               while {![eof $rf]} {                 gets $rf x                foreach {ifn ifa} [split $interface \x3a\x2c] {                  regexp "$ifn:\[\x20\t\]{0,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}" $x -> s_inc($ifn) s_out($ifn)                }              }              close $rf            }            after 500            if {[catch {open $api} rf]} {              putlog "Error couldn't open $api for an unknown reason."            } else {               while {![eof $rf]} {                 gets $rf x                foreach {ifn ifa} [split $interface \x3a\x2c] {                  if {[regexp "$ifn:\[\x20\t\]{0,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}(\[0-9\]{1,100})\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}\[\x20\t\]{1,100}\[0-9\]{1,100}" $x -> e_inc($ifn) e_out($ifn)]} {                    putserv "[string map [list %NICK% $nick %CHAN% $chan] $msg] :[string map [list %IFN% $ifn %IFA% $ifa %INC% [format %.2f [expr ($e_inc($ifn) - $s_inc($ifn)) / 512.0]] %OUT% [format %.2f [expr ($e_out($ifn) - $s_out($ifn)) / 512.0]]] $template]"                  }                }              }              close $rf            }          }        } else {           putlog "Error $api is not readable."        }      } else {         putlog "Error $api does not exist."      }    } else {       putlog "Error unknown operating system."    }  }  ##########################################  # Binds  ##########################################  bind pub $pubflag $bandwidth [namespace current]::bandwidth_pub  ##########################################  putlog "Bandwidth script version \(\002$version\002\) by \(\002Ofloo\002\) loaded."}