This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Taking an input from a shell command

Old posts that have not been replied to for several years.
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Taking an input from a shell command

Post by TurboChicken »

hey... it's me again...

basically i'm making a script that monitors how much Bandwidth has been used for the current month on a particular server.

this information is then to be output on a particualr command in a channel

so what i need is to know how to readin the output from a shell command
i.e. this is what an output in shell would look like:

Code: Select all

$ vnstat -m

        inet (eth0)

           month        rx      |       tx      |    total
        ------------------------+---------------+---------------
          Mar '03    15,959 MB  |    18,823 MB  |    34,782 MB
          Apr '03    24,645 MB  |    20,440 MB  |    45,085 MB
          May '03    31,314 MB  |    39,103 MB  |    70,417 MB
          Jun '03    28,116 MB  |    42,988 MB  |    71,104 MB
          Jul '03    27,552 MB  |    33,924 MB  |    61,476 MB
          Aug '03    37,498 MB  |    38,189 MB  |    75,687 MB
          Sep '03    19,750 MB  |    17,488 MB  |    37,238 MB
          Oct '03     3,999 MB  |     4,492 MB  |     8,491 MB
        ------------------------+---------------+---------------
        estimated    18,479 MB  |    20,758 MB  |    39,237 MB

so i would want to take in the first line

Code: Select all

         Mar '03    15,959 MB  |    18,823 MB  |    34,782 MB
so i could then seperate the figures put them into variables and then output in irc.

so what i want to know is how i would go about reading that input in...
i.e how does tcl do it.... line by line etc., can i just take out line 6 easily??

any help on this would be great.

thanx in advance
Turbo
User avatar
awyeah
Revered One
Posts: 1580
Joined: Mon Apr 26, 2004 2:37 am
Location: Switzerland
Contact:

Post by awyeah »

Take a look at this:

Code: Select all

Bandwidth monitor
0.2 dev
4/3/2005
Ofloo

Publishes bandwidth from an interface of choice into the channel. Features multiple interfaces (for BSD and Linux), flexible msg templates, and support for Windows, Linux, BSD, etc.
http://www.egghelp.org/cgi-bin/tcl_arch ... ad&id=1134
·­awyeah·

==================================
Facebook: jawad@idsia.ch (Jay Dee)
PS: Guys, I don't accept script helps or requests personally anymore.
==================================
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

that one only shows the current usage of the interface.

what i'm wanting is one that shows the traffic that has gone in and out of the interface.

i.e. my webservers only have 1000GB of transfer allowed. and i need to know at the touch of a button how much of that has been used.
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Maybe something like this

Code: Select all

bind pub -|- !vnstat vnstat
proc vnstat {n u h c t} {
 set d [eval exec vnstat -m]
 foreach l [split $d \n] {
  if [string match -noc *[strftime %b]* $l] {
    putserv "PRIVMSG $c :[concat $l]"
  }
 }
}
*Edit* Fixed a typo.
Last edited by greenbear on Sat Mar 12, 2005 4:07 pm, edited 1 time in total.
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

so what exactly would that output?

Code: Select all

 foreach l [split $d \n] { 
  if [string match -noc *[strftime %b]* $l] { 
    putserv "PRIVMSG $c :[concat $l]" 
  } 
 } 
i don't understand what *[strftime %b]* is looking for?

i understand now that set <blah> [eval exec vnstat -m] will run the command and i can read it in using the foreach and the /n makes it split it everyline and run command in the {} if that is right?

so what i need to do is make it find the 6th line.... so possibly and inc var and then an if that triggers when it hits 6.... then i need to take out the stuff i need which i can do myself as i'm fimilar with the string command in that respect. and coz it always conforms to the same amount of characters that should be easy.
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

TurboChicken wrote:i don't understand what *[strftime %b]* is looking for?
It returns the current month, in a 3 char format. ie. Mar

So the code just look for the line that have 'Mar' in it and send it to irc.
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

well that should work ok as a temp solution until i can get out the stuff and parse it abit.

but there seems to be a problem with your code :/

Code: Select all

bind pub -|- !bwusage vnstat
proc vnstat [n u h c t] {
 set d [eval exec vnstat -m]
 foreach l [split $d \n] {
  if [string match -noc *[strftime %b]* $l] {
    putserv "PRIVMSG $c :[concat $l]"
  }
 }
}
i'm getting an error on rehash.

scripts/bwtest.tcl: invalid command name "n"

any ideas?
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

ah yea, I made a typo, it should be

Code: Select all

bind pub -|- !bwusage vnstat
proc vnstat {n u h c t} {
 set d [eval exec vnstat -m]
 foreach l [split $d \n] {
  if [string match -noc *[strftime %b]* $l] {
    putserv "PRIVMSG $c :[concat $l]"
  }
 }
 
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

yeah spotted that too


one thing i wanted to ask tho.... what exactly does the

Code: Select all

putserv "PRIVMSG $c :[concat $l]"
do? i understand privmsg to channel ($c) but i don't get y there is [concat in there

i.e when i'm doing the string range part... would just putting the $l bit work?
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

I just use concat to remove leading whitespace's, so that it looks prettier 8)
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

ahhh... it was confusing the hell outta me...

but i got it sorted... it works ok....

<Turbochicken> !bwusage
<Turbo1> IN BW: 126.71 MB OUT BW: 1,740 MB TOTAL: 1,866 MB

now i have something to add to it... coz that way is only giving in MB and really want it in GB..

so the script also output's something else.... dumpdb

which for one output's a load of useless crap... but it does output the RAW figure in MB...

so i can do a calc on it and get GB...

problem being i'm no good with calculating things and the output is kinda hard to parse

m;0;1110636195;133;2131;183;111;1

<indcates it's month>;<howmany months ago>;<no idea>;<in bw (MB)>;<out>;<no idea on the rest>

so i can use the same as the other one to find it by finding the first one that has an "m" in it. that bit's fine.... but i need to get that in bw number out and out bw out....

do a calc... set inbwGB $inbw/1024 ( which should be a number value to 2 decimal places) and same for out....
and the set totbwGB ($inbw+$outbw)/1024

then i can output that...

i know it's a lot to ask and it seems like you've already done most of the script for me.... i am usually good with scripting... be this thing is kinda beyond me.... i've never had to do calc's before

if you could even tell me how to split the line up at ";" and then take out the sections i need... i could do the rest...
and as for calc... just gimme an example calc line... and i will work it out from there.
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

Code: Select all

set line "m;0;1110636195;133;2131;183;111;1"

# split up the list on ; and find the info.
# 4th and 5th word on the list, counting from 0.
set inMB [lindex [split $line \;] 3]
set outMB [lindex [split $line \;] 4]

# do the math, making sure it uses decimals
set inGB [expr $inMB/1024.0]
set outGB [expr $outMB/1024.0]
set totGB [expr $outMB+$inMB]

# make it look pretty, with only 2 decimals
set in [format %.2f $inGB]
set out [format %.2f $outGB]
set total [format %.2f $totGB]

# or if you only want the total
set totalGB [expr ($inMB+$outMB)/1024.0]
set total [format %.2f $totalGB]
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

will you marry me?
g
greenbear
Owner
Posts: 733
Joined: Mon Sep 24, 2001 8:00 pm
Location: Norway

Post by greenbear »

If you take me somewhere sunny for the honeymoon, then yea, why not. :oops:

Oh, and I forgot to divide totGB in the code above, but I'm sure you'll figure it out.
T
TurboChicken
Halfop
Posts: 61
Joined: Wed Sep 29, 2004 3:18 pm

Post by TurboChicken »

i've made an evolution of the script....

so that one bot can be in a channel and someone type !bwusage <2char identifier>

as all the bots that are running this script are named Turbo-DG Turbo-GC etc.

i am using the botnet to communicate with the other bots

so in one channel someone types it and it sends a msg over the botnet to that bot.... and then that bot sends a msg back through the botnet to give the line of text.

now i'm almost there but i've only made it work once... and i'm not sure what i have changed to make it not work.

so anyway... here's the code

Turbo-BW is the master... that is in the channel and responding to commands.

Code: Select all

bind pubm B "#turbo-test !bwusage*" adminbwusage
proc adminbwusage {n u h c a} {
  set bwbot [lindex $a 1]
  putbot Turbo-$bwbot "bwusage"
}
that's the listena and send part... i've checked that part using console +htrv and i can see the message being sent over.

no on Turbo-DG which is the one that i want to know the bw of.

Code: Select all

bind bot W bwusage botnetusage
proc botnetusage {b k a} {
 set d [eval exec vnstat --dumpdb]
 set i 1
 foreach l [split $d \n] {
  if $i==44 {
   set line $l
   ...
   lots of code that i know works
   ...
   if {$est < 800.00} {
     putbot Turbo-BW "bwmsg \002IN BW:\002  $in GB >>>>>> \002OUT BW:\002  $out GB >>>>>> \002TOTAL:\002  $tot GB >>>>>> \002ESTIMATED:\002 \0039 $est\003 GB"
   }
   if {$est > 800.00} {
     putbot Turbo-BW "bwmsg \002IN BW:\002  $in GB >>>>>> \002OUT BW:\002  $out GB >>>>>> \002TOTAL:\002  $tot GB >>>>>> \002ESTIMATED:\002 \0034 $est\003 GB"
   }
  }
 incr i 1
 }
}
*note i know this part is working because i tried to replace the putbot part in there with just "putserv "PRIVMSG turbochic :working" and that responded but when i tried "putbot Turbo-BW "bwmsg working" " it didn't work

and now back on Turbo-BW (the master)

Code: Select all

bind bot W bwmsg adminmsg
proc adminmsg {b k a} {puthelp "PRIVMSG #turbo-test :$a"}
and nothing is responding...

both bots have the +W flag on them
Locked