[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

script excerpt



This is an excerpt from a tcl script I wrote.
I would remember you all that I'm absolutely NOT a programmer or
developer :((
So I know that the structure of this script probably is a mere offence
for you all but don't blame me, please.

The purpose of these few lines are to write in a file the total bytes
that we use on the external interface. The fact is that I don't have
problems to read the simple log file that iptraf makes, but there are a
lot of customers out there that don't understand the meaning of "cd ..".
And also it is not their work (for example they work for fashion or in a
marble factory) so I have to prepare for them simple and stupid txt
files just to read a number.

I use grep to prepare a logfile containing only the bytes I want to
summarize. A typical form as you know is (file name is xxx.yyyy):

Thu May 16 15:06:42 2002; ******** TCP/UDP service monitor started
********

*** TCP/UDP traffic log, generated Thu May 16 16:06:42 2002

TCP/22: 155 packets, 12040 bytes total, 0.03 kbits/s; 85 packets, 4400
bytes inc
oming, 0.01 kbits/s; 70 packets, 7640 bytes outgoing, 0.02 kbits/s

UDP/138: 198 packets, 46882 bytes total, 0.11 kbits/s; 99 packets, 23441
bytes i
ncoming, 0.05 kbits/s; 99 packets, 23441 bytes outgoing, 0.05 kbits/s

UDP/137: 470 packets, 36660 bytes total, 0.08 kbits/s; 235 packets,
18330 bytes
incoming, 0.04 kbits/s; 235 packets, 18330 bytes outgoing, 0.04 kbits/s

TCP/80: 11 packets, 1893 bytes total, 0.00 kbits/s; 7 packets, 917 bytes
incomin
g, 0.00 kbits/s; 4 packets, 976 bytes outgoing, 0.00 kbits/s

UDP/67: 36 packets, 10912 bytes total, 0.03 kbits/s; 19 packets, 5336
bytes inco
ming, 0.01 kbits/s; 17 packets, 5576 bytes outgoing, 0.01 kbits/s

UDP/68: 36 packets, 10912 bytes total, 0.03 kbits/s; 17 packets, 5576
bytes inco
ming, 0.01 kbits/s; 19 packets, 5336 bytes outgoing, 0.01 kbits/s


Running time: 3600 seconds

*** TCP/UDP traffic log, generated Thu May 16 17:06:42 2002

I use grep -F "total" xxx* >> temp.txt to isolate the lines I need. The
result is:

TCP/22: 155 packets, 12040 bytes total, 0.03 kbits/s; 85 packets, 4400
bytes incoming, 0.01 kbits/s; 70 packets, 7640 bytes outgoing, 0.02
kbits/s

UDP/138: 198 packets, 46882 bytes total, 0.11 kbits/s; 99 packets, 23441
bytes incoming, 0.05 kbits/s; 99 packets, 23441 bytes outgoing, 0.05
kbits/s

UDP/137: 470 packets, 36660 bytes total, 0.08 kbits/s; 235 packets,
18330 bytes incoming, 0.04 kbits/s; 235 packets, 18330 bytes outgoing,
0.04 kbits/s

TCP/80: 11 packets, 1893 bytes total, 0.00 kbits/s; 7 packets, 917 bytes
incoming, 0.00 kbits/s; 4 packets, 976 bytes outgoing, 0.00 kbits/s

UDP/67: 36 packets, 10912 bytes total, 0.03 kbits/s; 19 packets, 5336
bytes incoming, 0.01 kbits/s; 17 packets, 5576 bytes outgoing, 0.01
kbits/s

UDP/68: 36 packets, 10912 bytes total, 0.03 kbits/s; 17 packets, 5576
bytes incoming, 0.01 kbits/s; 19 packets, 5336 bytes outgoing, 0.01
kbits/s

TCP/22: 155 packets, 12040 bytes total, 0.01 kbits/s; 85 packets, 4400
bytes incoming, 0.00 kbits/s; 70 packets, 7640 bytes outgoing, 0.01
kbits/s

UDP/138: 494 packets, 117174 bytes total, 0.13 kbits/s; 247 packets,
58587 bytes incoming, 0.07 kbits/s; 247 packets, 58587 bytes outgoing,
0.07 kbits/s

UDP/137: 1060 packets, 82680 bytes total, 0.09 kbits/s; 530 packets,
41340 bytes incoming, 0.05 kbits/s; 530 packets, 41340 bytes outgoing,
0.05 kbits/s

TCP/80: 199 packets, 58391 bytes total, 0.07 kbits/s; 106 packets, 20315
bytes incoming, 0.02 kbits/s; 93 packets, 38076 bytes outgoing, 0.04
kbits/s

UDP/67: 76 packets, 23158 bytes total, 0.03 kbits/s; 39 packets, 11022
bytes incoming, 0.01 kbits/s; 37 packets, 12136 bytes outgoing, 0.01
kbits/s

UDP/68: 76 packets, 23158 bytes total, 0.03 kbits/s; 37 packets, 12136
bytes incoming, 0.01 kbits/s; 39 packets, 11022 bytes outgoing, 0.01
kbits/s

TCP/22: 324 packets, 25944 bytes total, 0.02 kbits/s; 181 packets, 9280
bytes incoming, 0.01 kbits/s; 143 packets, 16664 bytes outgoing, 0.01
kbits/s

UDP/138: 624 packets, 147724 bytes total, 0.12 kbits/s; 312 packets,
73862 bytes incoming, 0.06 kbits/s; 312 packets, 73862 bytes outgoing,
0.06 kbits/s

UDP/137: 1410 packets, 109980 bytes total, 0.09 kbits/s; 705 packets,
54990 bytes incoming, 0.05 kbits/s; 705 packets, 54990 bytes outgoing,
0.05 kbits/s

TCP/80: 199 packets, 58391 bytes total, 0.05 kbits/s; 106 packets, 20315
bytes incoming, 0.02 kbits/s; 93 packets, 38076 bytes outgoing, 0.03
kbits/s

UDP/67: 92 packets, 27958 bytes total, 0.02 kbits/s; 47 packets, 13198
bytes incoming, 0.01 kbits/s; 45 packets, 14760 bytes outgoing, 0.01
kbits/s

UDP/68: 92 packets, 27958 bytes total, 0.02 kbits/s; 45 packets, 14760
bytes 
incoming, 0.01 kbits/s; 47 packets, 13198 bytes outgoing, 0.01 kbits/s

Here I can isolate the lines I need: so I can use for my purposes only
the values for port 80 and 22 for example so I use grep with

grep -F "TCP/80" temp.txt >> logfile
grep -F "TCP/22" temp.txt >> logfile

Then I use this tcl script to parse the file and simply write the values
for total bytes one over the other

set a [open "logfile" "r"]
set x [open "bcount" "w"]
if {[catch {while {[set b [gets $a]] != ""} {
                set c [split $b {,}]
                set d [lindex $c 1]
                set e [string trim $d]
                set f [string trimright $e "bytes total"]
                puts $x $f
             }
      } status]} {puts $status
                 puts "End of program: errors somewhere"
                 exit 1}

(Don't kill me for this shit please)

The output is something like this:

1893
58391
58391
60058
60058
60058
60058
60058
168520
(not related with the above files)

Then if I want to summarize the counts I have something like this (in c
language)

#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

int main(){
    //////////variables definition/////////
    char f[30];
    float fl;
    float sum = 0;
    ///////////////////////////////////////
    //////////file opening////////////////
    ifstream in("bcount");
    ///////////////////////////////////////
    //////////start of cycle/////////////////
    while(in.getline(f, 30))
        {
        fl = atof(f);
        sum = sum + fl;
        }
    ///////////////////////////////////////
    //////////file closing////////////////
    in.close();
    ///////////////////////////////////////
    cout << sum << "\n";
    return 0;
}

Usually I redirect the output to a file that I send by mail once a day.
Other times I use these scripts to make monthly reports.

To achieve this I have 2 or 3 sh scripts in my crontab file that start
and stop iptraf, run the tcl and c scripts,... blab bla bla.

Best Regards
----------------
Pier Luigi Conte
Product Analyst
CPSoftware NE
00390458921948